]> git.cworth.org Git - sup/blob - lib/sup/modes/label-list-mode.rb
fixed label and contact-list modes to be a little better
[sup] / lib / sup / modes / label-list-mode.rb
1 module Redwood
2
3 class LabelListMode < LineCursorMode
4   register_keymap do |k|
5     k.add :view_results, "View messages with the selected label", :enter
6     k.add :reload, "Discard results and reload", 'D'
7   end
8
9   def initialize
10     @labels = []
11     @text = []
12     super()
13   end
14
15   def lines; @text.length; end
16   def [] i; @text[i]; end
17
18   def load_in_background
19     Redwood::reporting_thread do
20       BufferManager.say("Counting labels...") { regen_text }
21       BufferManager.draw_screen
22     end
23   end
24
25 protected
26
27   def reload
28     buffer.mark_dirty
29     BufferManager.draw_screen
30     load_in_background
31   end
32   
33   def regen_text
34     @text = []
35     @labels = (LabelManager::LISTABLE_LABELS + LabelManager.user_labels).sort_by { |t| t.to_s }
36
37     counts = @labels.map do |t|
38       total = Index.num_results_for :label => t
39       unread = Index.num_results_for :labels => [t, :unread]
40       [t, total, unread]
41     end      
42
43     width = @labels.map { |t| t.to_s.length }.max
44
45     counts.map_with_index do |(t, total, unread), i|
46       if total == 0 && !LabelManager::LISTABLE_LABELS.include?(t)
47         Redwood::log "no hits for label #{t}, deleting"
48         LabelManager.delete t
49         @labels.delete t
50         next
51       end
52
53       label =
54         case t
55         when *LabelManager::LISTABLE_LABELS
56           t.to_s.ucfirst
57         else
58           t.to_s
59         end
60       @text << [[(unread == 0 ? :labellist_old_color : :labellist_new_color),
61           sprintf("%#{width + 1}s %5d %s, %5d unread", label, total, total == 1 ? " message" : "messages", unread)]]
62       yield i if block_given?
63     end.compact
64
65     buffer.mark_dirty
66   end
67
68   def view_results
69     label = @labels[curpos]
70     if label == :inbox
71       BufferManager.raise_to_front BufferManager["inbox"]
72     else
73       b = BufferManager.spawn_unless_exists(label) do
74         mode = LabelSearchResultsMode.new [label]
75       end
76       b.mode.load_threads :num => b.content_height
77     end
78   end
79 end
80
81 end