Class: Command::List

Inherits:
CommandBase show all
Defined in:
lib/command/list.rb,
lib/command/list/novel_decorator.rb

Defined Under Namespace

Classes: NovelDecorator

Constant Summary collapse

ANNOTATION_COLOR_TIME_LIMIT =

更新してから何秒まで色を変更するか

6 * 60 * 60
NOVEL_TYPE_LABEL =

MEMO: 0 は昔の小説を凍結したままな場合、novel_type が設定されていないので、

nil.to_i 
%w(連載 連載 短編)
FILTERS_TYPE =
Hash[*%w(series 連載 ss 短編 frozen 凍結 nonfrozen 非凍結)]

Instance Attribute Summary collapse

Attributes inherited from CommandBase

#stream_io

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CommandBase

#disable_logging, #display_help!, execute!, #execute!, #force_change_settings_function, help, #hook_call, #load_local_settings, #tagname_to_ids

Constructor Details

#initializeList

rubocop:disable Metrics/MethodLength



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/command/list.rb', line 28

def initialize
  super("[<limit>] [options]")
  @opt.separator "\n  \u30FB\u73FE\u5728\u7BA1\u7406\u3057\u3066\u3044\u308B\u5C0F\u8AAC\u306E\u4E00\u89A7\u3092\u8868\u793A\u3057\u307E\u3059\n  \u30FB\u8868\u793A\u3055\u308C\u308BID\u306F\u5404\u30B3\u30DE\u30F3\u30C9\u3067\u6307\u5B9A\u3059\u308B\u3053\u3068\u3067\u5C0F\u8AAC\u540D\u7B49\u3092\u5165\u529B\u3059\u308B\u624B\u9593\u3092\u7701\u3051\u307E\u3059\n  \u30FB\u500B\u6570\u3092\u4E0E\u3048\u308B\u3053\u3068\u3067\u3001\u6700\u5927\u8868\u793A\u6570\u3092\u5236\u9650\u3067\u304D\u307E\u3059(\u30C7\u30D5\u30A9\u30EB\u30C8\u306F\u5168\u3066\u8868\u793A)\n  \u30FBnarou list\u306E\u30C7\u30D5\u30A9\u30EB\u30C8\u52D5\u4F5C\u3092 narou s default_arg.list= \u3067\u8A2D\u5B9A\u3059\u308B\u3068\u4FBF\u5229\u3067\u3059\n  \u30FB\u30D1\u30A4\u30D7\u3067\u4ED6\u306Enarou\u30B3\u30DE\u30F3\u30C9\u306B\u7E4B\u3050\u3068ID\u5165\u529B\u306E\u4EE3\u308F\u308A\u306B\u3067\u304D\u307E\u3059\n\n  Examples:\nnarou list             # ID\u306E\u5C0F\u3055\u3044\u9806\u306B\u5168\u3066\u8868\u793A\nnarou list 10 -r       # ID\u306E\u5927\u304D\u3044\u9806\u306B10\u4EF6\u8868\u793A\nnarou list 5 -l        # \u6700\u8FD1\u66F4\u65B0\u306E\u3042\u3063\u305F5\u4EF6\u8868\u793A\nnarou list 10 -rl      # \u53E4\u3044\u9806\u306B10\u4EF6\u8868\u793A\nnarou list -f ss       # \u77ED\u7DE8\u5C0F\u8AAC\u3060\u3051\u8868\u793A\nnarou list -f \"ss frozen\"   # \u51CD\u7D50\u3057\u3066\u3044\u308B\u77ED\u7DE8\u3060\u3051\u8868\u793A\n\n# \u5C0F\u8AAC\u5BB6\u306B\u306A\u308D\u3046\u306E\u5C0F\u8AAC\u306E\u307F\u3092\u8868\u793A\nnarou list --site --grep \u5C0F\u8AAC\u5BB6\u306B\u306A\u308D\u3046\nnarou l -sg \u5C0F\u8AAC\u5BB6\u306B\u306A\u308D\u3046    # \u4E0A\u8A18\u3068\u540C\u3058\u610F\u5473\n# \u4F5C\u8005\u201C\u7D2B\u708E\u201D\u3092\u542B\u3080\u5C0F\u8AAC\u3092\u8868\u793A\nnarou list --author --grep \u7D2B\u708E\nnarou l -ag \u7D2B\u708E              # \u4E0A\u8A18\u3068\u540C\u3058\u610F\u5473\n# \u201C\u7D2B\u708E\u201D\u3068\u201C\u306A\u308D\u3046\u201D\u3092\u542B\u3080\u5C0F\u8AAC\u3092\u8868\u793A(AND\u691C\u7D22)\nnarou l -asg \"\u7D2B\u708E \u306A\u308D\u3046\"\n# \u201C\u306A\u308D\u3046\u201D\u3092\u542B\u307E\u306A\u3044\u5C0F\u8AAC\u3092\u8868\u793A(NOT\u691C\u7D22)\nnarou l -sg \"-\u306A\u308D\u3046\"\n\n# \u30CF\u30FC\u30E1\u30EB\u30F3\u3092\u542B\u3080\u5C0F\u8AAC\u306Bhameln\u30BF\u30B0\u3092\u4ED8\u3051\u308B\nnarou l -sg \u30CF\u30FC\u30E1\u30EB\u30F3 | narou t -a hameln\n# \u77ED\u7DE8\u3092\u5168\u3066\u51CD\u7D50\u3059\u308B\nnarou l -f ss | narou freeze --on\n\n# \u30EA\u30B9\u30C8\u3092\u305D\u306E\u307E\u307E\u4FDD\u5B58\u3057\u305F\u3044\u6642(echo\u30AA\u30D7\u30B7\u30E7\u30F3)\nnarou l -e > list.txt\n\n  Options:\n  EOS\n  @opt.on(\"-l\", \"--latest\", \"\u6700\u8FD1\u66F4\u65B0\u306E\u3042\u3063\u305F\u9806\u306B\u5C0F\u8AAC\u3092\u8868\u793A\u3059\u308B\") {\n    @options[\"latest\"] = true\n  }\n  @opt.on(\"--gl\", \"\u66F4\u65B0\u65E5\u3067\u306F\u306A\u304F\u6700\u65B0\u8A71\u63B2\u8F09\u65E5\u3092\u4F7F\u7528\u3059\u308B\") {\n    @options[\"general-lastup\"] = true\n  }\n  @opt.on(\"-r\", \"--reverse\", \"\u9006\u9806\u306B\u8868\u793A\u3059\u308B\") {\n    @options[\"reverse\"] = true\n  }\n  @opt.on(\"-u\", \"--url\", \"\u5C0F\u8AAC\u306E\u63B2\u8F09\u30DA\u30FC\u30B8\u3082\u8868\u793A\u3059\u308B\") {\n    @options[\"url\"] = true\n  }\n  @opt.on(\"-k\", \"--kind\", \"\u5C0F\u8AAC\u306E\u7A2E\u5225\uFF08\u77ED\u7DE8\uFF0F\u9023\u8F09\uFF09\u3082\u8868\u793A\u3059\u308B\") {\n    @options[\"kind\"] = true\n  }\n  @opt.on(\"-s\", \"--site\", \"\u63B2\u8F09\u5C0F\u8AAC\u30B5\u30A4\u30C8\u540D\u3082\u8868\u793A\u3059\u308B\") {\n    @options[\"site\"] = true\n  }\n  @opt.on(\"-a\", \"--author\", \"\u4F5C\u8005\u540D\u3082\u8868\u793A\u3059\u308B\") {\n    @options[\"author\"] = true\n  }\n  @opt.on(\"-f\", \"--filter VAL\", String,\n          \"\u8868\u793A\u3092\u7D5E\u308B\u305F\u3081\u306E\u30D5\u30A3\u30EB\u30BF\u30FC\u3002\u30B9\u30DA\u30FC\u30B9\u533A\u5207\u308A\u3067\u8907\u6570\u53EF\\n\" \\\n          \"\#{' ' * 25}\#{filter_type_help}\") { |filter|\n    @options[\"filters\"] = filter.downcase.split\n  }\n  @opt.on(\"-g\", \"--grep VAL\", String,\n          \"\u6307\u5B9A\u3055\u308C\u305F\u6587\u5B57\u5217\u3067\u30EA\u30B9\u30C8\u3092\u691C\u7D22\u3059\u308B\") { |search|\n    @options[\"grep\"] = search.split\n  }\n  @opt.on(\"-t\", \"--tag [TAGS]\", String,\n          \"\u30BF\u30B0\u3082\u8868\u793A\u3002\u5F15\u6570\u3092\u6307\u5B9A\u3057\u305F\u5834\u5408\u305D\u306E\u30BF\u30B0\u3092\u542B\u3080\u5C0F\u8AAC\u3092\u8868\u793A\") { |tags|\n    if tags\n      @options[\"tags\"] = tags.split\n    else\n      @options[\"all-tags\"] = true\n    end\n  }\n  @opt.on(\"-e\", \"--echo\", \"\u30D1\u30A4\u30D7\u3084\u30EA\u30C0\u30A4\u30EC\u30AF\u30C8\u3067\u3082\u305D\u306E\u307E\u307E\u51FA\u529B\u3059\u308B\") {\n    @options[\"echo\"] = true\n  }\nend\n"

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



21
22
23
# File 'lib/command/list.rb', line 21

def options
  @options
end

Class Method Details

.oneline_helpObject



23
24
25
# File 'lib/command/list.rb', line 23

def self.oneline_help
  "現在管理している小説の一覧を表示します"
end

Instance Method Details

#decorate_lines(novels) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/command/list.rb', line 169

def decorate_lines(novels)
  filters = @options["filters"] || []
  selected_lines = {}
  novels.each do |novel|
    novel_decorator = NovelDecorator.new(novel, self)
    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
    selected_lines[id] = novel_decorator.decorate_line
  end
  grep(selected_lines)
end

#execute(argv) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/command/list.rb', line 219

def execute(argv)
  disable_logging
  super
  database_values = Database.instance.get_object.values
  limit =
    if !argv.empty? && argv.first =~ /^\d+$/
      argv.first.to_i
    else
      database_values.size
    end
  if @options["latest"]
    database_values = Database.instance.sort_by(view_date_type)
  end
  database_values.reverse! if @options["reverse"]
  output_list(database_values, limit)
end

#filter_type_helpObject



118
119
120
121
122
# File 'lib/command/list.rb', line 118

def filter_type_help
  FILTERS_TYPE.map { |filter, info|
    "#{filter}(#{info})"
  }.join(",")
end

#grep(selected_lines) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/command/list.rb', line 190

def grep(selected_lines)
  return selected_lines unless @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
  selected_lines
end

#headerObject



145
146
147
148
149
150
151
152
153
154
# File 'lib/command/list.rb', line 145

def header
  [
    " ID ",
    @options["general-lastup"] ? " 掲載日 " : " 更新日 ",
    @options["kind"] ? "種別" : nil,
    @options["author"] ? "作者名" : nil,
    @options["site"] ? "サイト名" : nil,
    "     タイトル"
  ].compact.join(" | ")
end

#nowObject



160
161
162
# File 'lib/command/list.rb', line 160

def now
  @now ||= Time.now
end

#output_lines(took_lines) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/command/list.rb', line 205

def output_lines(took_lines)
  if STDOUT.tty?
    stream_io.puts header
    stream_io.puts took_lines.values.join("\n").termcolor
  elsif @options["echo"]
    # pipeにそのまま出力するときはansicolorコードが邪魔なので削除
    stream_io.puts header
    stream_io.puts TermColorLight.strip_tag(took_lines.values.join("\n"))
  else
    # pipeに接続するときはIDを渡す
    stream_io.puts took_lines.keys.join(" ")
  end
end

#output_list(novels, limit) ⇒ Object



164
165
166
167
# File 'lib/command/list.rb', line 164

def output_list(novels, limit)
  took_lines = Hash[decorate_lines(novels).take(limit)]
  output_lines(took_lines)
end

#test_filter(filters, novel_type, frozen) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/command/list.rb', line 124

def test_filter(filters, novel_type, frozen)
  apply_sum = filters.inject(0) do |sum, item|
    apply = case item
            when "series"
              novel_type == 0 || novel_type == 1
            when "ss"
              novel_type == 2
            when "frozen"
              frozen
            when "nonfrozen"
              !frozen
            else
              stream_io.error "不明なフィルターです(#{item})"
              stream_io.puts "filters = #{filter_type_help}"
              exit Narou::EXIT_ERROR_CODE
            end
    sum + (apply ? 1 : 0)
  end
  apply_sum == filters.count
end

#valid_tags?(novel, tags) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
116
# File 'lib/command/list.rb', line 110

def valid_tags?(novel, tags)
  novel_tags = novel["tags"] or return false
  tags.each do |tag|
    return false unless novel_tags.include?(tag)
  end
  true
end

#view_date_typeObject



156
157
158
# File 'lib/command/list.rb', line 156

def view_date_type
  @_view_data_type ||= @options["general-lastup"] ? "general_lastup" : "last_update"
end