3 class LabelListMode < LineCursorMode
5 k.add :select_label, "Select label", :enter
6 k.add :reload, "Discard label list and reload", '@'
7 k.add :toggle_show_unread_only, "Toggle between all labels and those with unread mail", :tab
23 def lines; @text.length end
24 def [] i; @text[i] end
27 def toggle_show_unread_only
28 @unread_only = !@unread_only
34 buffer.mark_dirty if buffer
39 labels = LabelManager.listable_labels
41 counts = labels.map do |label|
42 string = LabelManager.string_for label
43 total = Index.num_results_for :label => label
44 unread = Index.num_results_for :labels => [label, :unread]
45 [label, string, total, unread]
46 end.sort_by { |l, s, t, u| s.downcase }
48 width = counts.max_of { |l, s, t, u| s.length }
51 counts.delete_if { | l, s, t, u | u == 0 }
55 counts.map do |label, string, total, unread|
56 if total == 0 && !LabelManager::RESERVED_LABELS.include?(label)
57 Redwood::log "no hits for label #{label}, deleting"
58 LabelManager.delete label
62 @text << [[(unread == 0 ? :labellist_old_color : :labellist_new_color),
63 sprintf("%#{width + 1}s %5d %s, %5d unread", string, total, total == 1 ? " message" : "messages", unread)]]
65 yield i if block_given?
70 @value, string = @labels[curpos]
71 @done = true if @value