]> git.cworth.org Git - sup/blob - lib/sup/modes/label-list-mode.rb
moved evertying to devel
[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, "Reload", "R"
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; regen_text; end
19
20   def load_in_background
21     ::Thread.new do
22       regen_text do |i|
23         if i % 10 == 0
24           buffer.mark_dirty
25           BufferManager.draw_screen
26           sleep 0.1 # ok, dirty trick.
27         end
28       end
29       buffer.mark_dirty
30       BufferManager.draw_screen
31     end
32   end
33
34 protected
35
36   def reload
37     buffer.mark_dirty
38     BufferManager.draw_screen
39     load_in_background
40   end
41   
42   def regen_text
43     @text = []
44     @labels = LabelManager::LISTABLE_LABELS.sort_by { |t| t.to_s } +
45                 LabelManager.user_labels.sort_by { |t| t.to_s }
46
47     counts = @labels.map do |t|
48       total = Index.num_results_for :label => t
49       unread = Index.num_results_for :labels => [t, :unread]
50       [t, total, unread]
51     end      
52
53     width = @labels.map { |t| t.to_s.length }.max
54
55     counts.map_with_index do |(t, total, unread), i|
56       if total == 0 && !LabelManager::LISTABLE_LABELS.include?(t)
57         Redwood::log "no hits for label #{t}, deleting"
58         LabelManager.delete t
59         @labels.delete t
60         next
61       end
62
63       label =
64         case t
65         when *LabelManager::LISTABLE_LABELS
66           t.to_s.ucfirst
67         else
68           t.to_s
69         end
70       @text << [[(unread == 0 ? :labellist_old_color : :labellist_new_color),
71           sprintf("%#{width + 1}s %5d %s, %5d unread", label, total, total == 1 ? " message" : "messages", unread)]]
72       yield i if block_given?
73     end.compact
74   end
75
76   def view_results
77     label = @labels[curpos]
78     if label == :inbox
79       BufferManager.raise_to_front BufferManager["inbox"]
80     else
81       b = BufferManager.spawn_unless_exists(label) do
82         mode = LabelSearchResultsMode.new [label]
83       end
84       b.mode.load_more_threads b.content_height
85     end
86   end
87 end
88
89 end