Method: Command::List#output_list

Defined in:
lib/command/list.rb

#output_list(novels) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/command/list.rb', line 146

def output_list(novels)
  now = Time.now
  today = now.strftime("%y/%m/%d")
  filters = @options["filters"] || []
  selected_lines = {}
  novels.each do |novel|
    novel_type = novel["novel_type"].to_i
    id = novel["id"]
    frozen = Narou.novel_frozen?(id)

    unless filters.empty?
      next unless test_filter(filters, novel_type, frozen)
    end

    if @options["tags"]
      next unless valid_tags?(novel, @options["tags"])
    end
    disp_id = ((frozen ? "*" : "") + id.to_s).rjust(4)
    disp_id = disp_id.sub("*", "<bold><cyan>*</cyan></bold>") if frozen
    tags = novel["tags"] || []
    selected_lines[id] = [
      disp_id,
      novel["last_update"].strftime("%y/%m/%d").tap { |s|
        new_arrivals_date = novel["new_arrivals_date"]
        last_update = novel["last_update"]
        if new_arrivals_date && new_arrivals_date >= last_update \
           && new_arrivals_date + ANNOTATION_COLOR_TIME_LIMIT >= now
          # 新着表示色
          s.replace "<bold><magenta>#{s}</magenta></bold>"
        elsif last_update + ANNOTATION_COLOR_TIME_LIMIT >= now
          # 更新だけあった色
          s.replace "<bold><green>#{s}</green></bold>"
        end
      },
      @options["kind"] ? NOVEL_TYPE_LABEL[novel_type] : nil,
      @options["author"] ? novel["author"].escape : nil,
      @options["site"] ? novel["sitename"].escape : nil,
      novel["title"].escape + (!@options["kind"] && novel_type == 2 ?
                       "  <bold><black>(#{NOVEL_TYPE_LABEL[novel_type]})</black></bold>" :
                       "") +
                       (tags.include?("end") ? " <bold><black>(完結)</black></bold>" : "") +
                       (tags.include?("404") ? " <bold><black>(削除)</black></bold>" : ""),
      @options["url"] ? novel["toc_url"].escape : nil,
      @options["tags"] || @options["all-tags"] ?
          tags.empty? ? nil : tags.map{ |tag|
            color = Tag.get_color(tag)
            "<bold><#{color}>#{tag.escape}</#{color}></bold>"
          }.join(",") : nil,
    ].compact.join(" | ")
  end
  if @options["grep"]
    @options["grep"].each do |search_word|
      selected_lines.keep_if { |_, line|
        if search_word =~ /^-(.+)/
          # NOT検索
          !line.include?($1)
        else
          line.include?(search_word)
        end
      }
    end
  end
  if STDOUT.tty?
    puts header
    puts selected_lines.values.join("\n").termcolor
  else
    if @options["echo"]
      # pipeにそのまま出力するときはansicolorコードが邪魔なので削除
      puts header
      puts TermColorLight.strip_tag(selected_lines.values.join("\n"))
    else
      # pipeに接続するときはIDを渡す
      puts selected_lines.keys.join(" ")
    end
  end
end