]> git.cworth.org Git - sup/blob - lib/sup/tagger.rb
Merge branch 'preemptive-loading' into next
[sup] / lib / sup / tagger.rb
1 module Redwood
2
3 class Tagger
4   def initialize mode, noun="thread", plural_noun=nil
5     @mode = mode
6     @tagged = {}
7     @noun = noun
8     @plural_noun = plural_noun || (@noun + "s")
9   end
10
11   def tagged? o; @tagged[o]; end
12   def toggle_tag_for o; @tagged[o] = !@tagged[o]; end
13   def tag o; @tagged[o] = true; end
14   def untag o; @tagged[o] = false; end
15   def drop_all_tags; @tagged.clear; end
16   def drop_tag_for o; @tagged.delete o; end
17
18   def apply_to_tagged action=nil
19     targets = @tagged.select_by_value
20     num_tagged = targets.size
21     if num_tagged == 0
22       BufferManager.flash "No tagged threads!"
23       return
24     end
25
26     noun = num_tagged == 1 ? @noun : @plural_noun
27
28     unless action
29       c = BufferManager.ask_getch "apply to #{num_tagged} tagged #{noun}:"
30       return if c.nil? # user cancelled
31       action = @mode.resolve_input c
32     end
33
34     if action
35       tagged_sym = "multi_#{action}".intern
36       if @mode.respond_to? tagged_sym
37         @mode.send tagged_sym, targets
38       else
39         BufferManager.flash "That command cannot be applied to multiple threads."
40       end
41     else
42       BufferManager.flash "Unknown command #{c.to_character}."
43     end
44   end
45
46 end
47
48 end