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)

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



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

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\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\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}\".termcolor) { |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



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/command/tag.rb', line 161

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.count
  color = COLORS[index]
  @@tag_colors[tagname] = color
  @@tag_colors.save
  color
end

.oneline_helpObject



14
15
16
# File 'lib/command/tag.rb', line 14

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

Instance Method Details

#display_taglistObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/command/tag.rb', line 97

def display_taglist
  database = Database.instance
  tags_list = {}
  database.each do |_, data|
    tags = data["tags"] || []
    tags.each do |tag|
      if tags_list[tag]
        tags_list[tag] += 1
      else
        tags_list[tag] = 1
      end
    end
  end
  puts "タグ一覧"
  puts tags_list.map { |tag, count|
    color = Tag.get_color(tag)
    "<bold><#{color}>#{TermColor.escape(tag)}(#{count})</#{color}></bold>"
  }.join(" ").termcolor
end

#edit_tags(argv) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/command/tag.rb', line 121

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.count > 0
      print "現在のタグは "
      print tags.map { |tagname|
        color = Tag.get_color(tagname)
        "<bold><#{color}>#{TermColor.escape(tagname)}</#{color}></bold>"
      }.join(" ").termcolor
      puts " です"
    end
    database[data["id"]]["tags"] = tags
  end
  database.save_database
end

#execute(argv) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/command/tag.rb', line 78

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

#get_color_listObject



72
73
74
75
76
# File 'lib/command/tag.rb', line 72

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

#search_novel_by_tag(argv) ⇒ Object



117
118
119
# File 'lib/command/tag.rb', line 117

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

#set_color(tagname, color) ⇒ Object



173
174
175
176
177
# File 'lib/command/tag.rb', line 173

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