Class: Command::Tag

Inherits:
CommandBase show all
Defined in:
lib/command/tag.rb

Constant Summary collapse

COLORS =
%w(green yellow blue magenta cyan red white)
BAN_CHAR =

禁止文字

/[:;"'><$@&^\\\|%'\/`]/

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CommandBase

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

Constructor Details

#initializeTag

Returns a new instance of Tag.



20
21
22
23
24
25
26
27
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
# File 'lib/command/tag.rb', line 20

def initialize
  super("<option> <tagname> <target> [<target2> ...]\n" \
        "<tagname> [<tagname2> ...]")
  @opt.separator "\n  \u30FB\u5C0F\u8AAC\u306B\u30BF\u30B0\u3092\u8A2D\u5B9A\u3057\u307E\u3059\u3002\u8A2D\u5B9A\u500B\u6570\u306E\u4E0A\u9650\u306F\u3042\u308A\u307E\u305B\u3093\n  \u30FB\u30BF\u30B0\u540D\u306B\u306F\u30B9\u30DA\u30FC\u30B9\u4EE5\u5916\u306E\u6587\u5B57\u304C\u4F7F\u3048\u307E\u3059(\u5927\u6587\u5B57\u5C0F\u6587\u5B57\u533A\u5225)\n  \u30FB\u30BF\u30B0\u306B\u306F\u81EA\u52D5\u3067\u8272\u304C\u3064\u304D\u307E\u3059\u3002\u81EA\u5206\u3067\u6307\u5B9A\u3059\u308B\u5834\u5408\u306F--color\u3092\u6307\u5B9A\u3057\u3066\u4E0B\u3055\u3044\n  \u30FB\u4E00\u90E8\u7279\u6B8A\u306A\u30BF\u30B0\u304C\u3042\u308A\u307E\u3059\u3002\u8A2D\u5B9A\u3059\u308B\u3053\u3068\u3067list\u30B3\u30DE\u30F3\u30C9\u306B\u53CD\u6620\u3055\u308C\u307E\u3059\n  - end: \u5C0F\u8AAC\u304C\u5B8C\u7D50\u72B6\u614B\n  - 404: \u63B2\u8F09\u30B5\u30A4\u30C8\u304B\u3089\u524A\u9664\u3055\u308C\u305F\u72B6\u614B\n  \u30FB\u8A2D\u5B9A\u3057\u305F\u30BF\u30B0\u306F\u4ED6\u306E\u30B3\u30DE\u30F3\u30C9\u3067\u6307\u5B9A\u3059\u308B\u3053\u3068\u3067\u3001\u5C0F\u8AACID\u6307\u5B9A\u306E\u4EE3\u308F\u308A\u306B\u3059\u308B\u3053\u3068\u304C\u3067\u304D\u307E\u3059(\u4E00\u90E8\u30BF\u30B0\u3092\u4F7F\u3048\u306A\u3044\u30B3\u30DE\u30F3\u30C9\u3082\u5B58\u5728\u3057\u307E\u3059\u3002\u307E\u305Falias\u3067\u3064\u3051\u305F\u5225\u540D\u3088\u308A\u3082\u30BF\u30B0\u306E\u307B\u3046\u304C\u512A\u5148\u3055\u308C\u307E\u3059)\n\n  Examples:\nnarou tag --add fav 0 2     # ID:0\u30681\u306E\u5C0F\u8AAC\u306Bfav\u30BF\u30B0\u3092\u8A2D\u5B9A(\u8FFD\u52A0)\nnarou t -a fav 0 2          # \u3082\u3057\u304F\u306F\u3053\u306E\u69D8\u306B\u66F8\u3051\u307E\u3059\nnarou t -a \"fav later\" 0 2  # \u4E00\u5EA6\u306B\u8907\u6570\u306E\u30BF\u30B0\u3092\u6307\u5B9A\u51FA\u6765\u307E\u3059\nnarou t -a fav -c red 0     # fav\u3068\u3044\u3046\u30BF\u30B0\u3092\u8D64\u8272\u3067\u8A2D\u5B9A\u3059\u308B\nnarou tag --delete fav 2    # ID:2\u306E\u5C0F\u8AAC\u306Efav\u30BF\u30B0\u3092\u5916\u3059\nnarou t -d fav 2\n\nnarou tag end               # end\u30BF\u30B0(\u5B8C\u7D50)\u306E\u4ED8\u3044\u3066\u3044\u308B\u5C0F\u8AAC\u306E\u4E00\u89A7\u3092\u8868\u793A\nnarou tag fav later         # fav,later\u30BF\u30B0\u4E21\u65B9\u4ED8\u3044\u3066\u3044\u308B\u5C0F\u8AAC\u3092\u8868\u793A\nnarou list -t \"fav later\" -rl   # list\u30B3\u30DE\u30F3\u30C9\u3067\u3082\u30BF\u30B0\u3067\u691C\u7D22\u51FA\u6765\u307E\u3059\nnarou tag                   # \u4F55\u3082\u6307\u5B9A\u3057\u306A\u3044\u5834\u5408\u3001\u5B58\u5728\u3059\u308B\u30BF\u30B0\u4E00\u89A7\u3092\u8868\u793A\n\n# \u4ED6\u306E\u30B3\u30DE\u30F3\u30C9\u3067\u30BF\u30B0\u3092\u4F7F\u3046\nnarou freeze --on end       # end(\u5B8C\u7D50\u30BF\u30B0)\u304C\u4ED8\u3044\u305F\u5C0F\u8AAC\u3092\u4E00\u62EC\u3067\u51CD\u7D50\nnarou update tag:404        # 404\u30BF\u30B0\u3092\u660E\u793A\u3059\u308B\n                            # ID:404 \u304C\u5B58\u5728\u3057\u306A\u3051\u308C\u3070\u660E\u793A\u3059\u308B\u5FC5\u8981\u306F\u306A\u3044\n\n  Options:\n  EOS\n  @opt.on(\"-a\", \"--add TAGS\", String, \"\u30BF\u30B0\u3092\u8FFD\u52A0\u3059\u308B\") { |tags|\n    @options[\"mode\"] = :add\n    @options[\"tags\"] = tags.split.tap { |array|\n      array.each do |tag|\n        if tag =~ BAN_CHAR\n          error \"\#{tag} \u306B\u4F7F\u7528\u7981\u6B62\u8A18\u53F7\u304C\u542B\u307E\u308C\u3066\u3044\u307E\u3059\"\n          exit Narou::EXIT_ERROR_CODE\n        end\n      end\n    }\n  }\n  @opt.on(\"-d\", \"--delete TAGS\", String, \"\u30BF\u30B0\u3092\u5916\u3059\") { |tags|\n    @options[\"mode\"] = :delete\n    @options[\"tags\"] = tags.split\n  }\n  @opt.on(\"-c\", \"--color COL\", String,\n          \"\u30BF\u30B0\u306E\u8272\u3092\u81EA\u5206\u3067\u6307\u5B9A\u3059\u308B\\n\" \\\n          \"\#{' '*25}COL=\#{get_color_list}\") { |color|\n    color.downcase!\n    unless COLORS.include?(color)\n      error \"\#{color}\u3068\u3044\u3046\u8272\u306F\u5B58\u5728\u3057\u307E\u305B\u3093\u3002\u8272\u6307\u5B9A\u306F\u7121\u8996\u3055\u308C\u307E\u3059\"\n      color = nil\n    end\n    @options[\"color\"] = color\n  }\n  @opt.on(\"--clear\", \"\u6307\u5B9A\u3057\u305F\u5C0F\u8AAC\u306E\u30BF\u30B0\u3092\u3059\u3079\u3066\u5916\u3059\") {\n    @options[\"mode\"] = :clear\n  }\nend\n"

Class Method Details

.get_color(tagname) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
# File 'lib/command/tag.rb', line 184

def self.get_color(tagname)
  tag_colors = Inventory.load("tag_colors", :local)
  color = tag_colors[tagname]
  return color if color
  last_color = tag_colors.values.last || COLORS.last
  index = (COLORS.index(last_color) + 1) % COLORS.size
  color = COLORS[index]
  tag_colors[tagname] = color
  tag_colors.save
  color
end

.get_tag_list(ids = nil) ⇒ Object

タグ一覧取得

ids: 特定の小説のタグだけ取得するためのIDの配列を指定@return: { “タグの名前” => タグが付けられた小説の数, … }



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/command/tag.rb', line 89

def self.get_tag_list(ids = nil)
  database = Database.instance
  tag_list = Hash.new(0)
  database.each do |_, data|
    if ids.kind_of?(Array)
      next unless ids.include?(data["id"])
    end
    tags = data["tags"] || []
    tags.each do |tag|
      tag_list[tag] += 1
    end
  end
  tag_list.default = nil
  tag_list
end

.oneline_helpObject



16
17
18
# File 'lib/command/tag.rb', line 16

def self.oneline_help
  "各小説にタグを設定及び閲覧が出来ます"
end

Instance Method Details

#display_taglistObject



130
131
132
133
134
135
136
137
138
# File 'lib/command/tag.rb', line 130

def display_taglist
  database = Database.instance
  tag_list = Tag.get_tag_list
  puts "タグ一覧"
  puts tag_list.map { |tag, count|
    color = Tag.get_color(tag)
    "<bold><#{color}>#{TermColorLight.escape(tag)}(#{count})</#{color}></bold>"
  }.join(" ").termcolor
end

#edit_tags(argv) ⇒ Object



144
145
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
# File 'lib/command/tag.rb', line 144

def edit_tags(argv)
  database = Database.instance
  tagname_to_ids(argv)
  argv.each do |target|
    data = Downloader.get_data_by_target(target)
    unless data
      error "#{target} は存在しません"
      next
    end
    tags = data["tags"] || []
    title = data["title"]
    case @options["mode"]
    when :add
      tags |= @options["tags"]
      puts "#{title} にタグを設定しました"
    when :delete
      tags -= @options["tags"]
      puts "#{title} からタグを外しました"
    when :clear
      tags.clear
      puts "#{title} のタグをすべて外しました"
    end
    if @options["color"] && @options["tags"]
      @options["tags"].each do |tag|
        set_color(tag, @options["color"])
      end
    end
    if tags.size > 0
      print "現在のタグは "
      print tags.map { |tagname|
        color = Tag.get_color(tagname)
        "<bold><#{color}>#{TermColorLight.escape(tagname)}</#{color}></bold>"
      }.join(" ").termcolor
      puts " です"
    end
    database[data["id"]]["tags"] = tags
  end
  database.save_database
end

#execute(argv) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/command/tag.rb', line 111

def execute(argv)
  super
  @options["mode"] ||= :list
  if argv.empty?
    if @options["mode"] == :list
      display_taglist
      return
    end
    error "対象の小説を指定して下さい"
    exit Narou::EXIT_ERROR_CODE
  else
    if @options["mode"] == :list
      search_novel_by_tag(argv)
    else
      edit_tags(argv)
    end
  end
end

#get_color_listObject



105
106
107
108
109
# File 'lib/command/tag.rb', line 105

def get_color_list
  COLORS.map { |color|
    "<bold><#{color}>#{color}</#{color}></bold>"
  }.join(",").termcolor
end

#search_novel_by_tag(argv) ⇒ Object



140
141
142
# File 'lib/command/tag.rb', line 140

def search_novel_by_tag(argv)
  List.execute!(["--tag", argv.join(" ")])
end

#set_color(tagname, color) ⇒ Object



196
197
198
199
200
# File 'lib/command/tag.rb', line 196

def set_color(tagname, color)
  tag_colors = Inventory.load("tag_colors", :local)
  tag_colors[tagname] = color
  tag_colors.save
end