3 class LabelListMode < LineCursorMode
5 k.add :select_label, "Search by label", :enter
6 k.add :reload, "Discard label list and reload", '@'
7 k.add :jump_to_next_new, "Jump to next new thread", :tab
8 k.add :toggle_show_unread_only, "Toggle between showing all labels and those with unread mail", 'u'
19 def lines; @text.length end
20 def [] i; @text[i] end
23 n = ((curpos + 1) ... lines).find { |i| @labels[i][1] > 0 } || (0 ... curpos).find { |i| @labels[i][1] > 0 }
25 ## jump there if necessary
26 jump_to_line n unless n >= topline && n < botline
29 BufferManager.flash "No labels messages with unread messages."
34 def toggle_show_unread_only
35 @unread_only = !@unread_only
41 buffer.mark_dirty if buffer
46 labels = LabelManager.listable_labels
48 counts = labels.map do |label|
49 string = LabelManager.string_for label
50 total = Index.num_results_for :label => label
51 unread = Index.num_results_for :labels => [label, :unread]
52 [label, string, total, unread]
53 end.sort_by { |l, s, t, u| s.downcase }
55 width = counts.max_of { |l, s, t, u| s.length }
58 counts.delete_if { | l, s, t, u | u == 0 }
62 counts.map do |label, string, total, unread|
63 if total == 0 && !LabelManager::RESERVED_LABELS.include?(label)
64 Redwood::log "no hits for label #{label}, deleting"
65 LabelManager.delete label
69 @text << [[(unread == 0 ? :labellist_old_color : :labellist_new_color),
70 sprintf("%#{width + 1}s %5d %s, %5d unread", string, total, total == 1 ? " message" : "messages", unread)]]
71 @labels << [label, unread]
72 yield i if block_given?
75 BufferManager.flash "No labels with unread messages!" if counts.empty? && @unread_only
79 label, num_unread = @labels[curpos]
81 LabelSearchResultsMode.spawn_nicely label