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