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 reload # make sure unread message counts are up-to-date
39 def toggle_show_unread_only
40 @unread_only = !@unread_only
46 buffer.mark_dirty if buffer
51 labels = LabelManager.all_labels
53 counts = labels.map do |label|
54 string = LabelManager.string_for label
55 total = Index.num_results_for :label => label
56 unread = (label == :unread)? total : Index.num_results_for(:labels => [label, :unread])
57 [label, string, total, unread]
58 end.sort_by { |l, s, t, u| s.downcase }
60 width = counts.max_of { |l, s, t, u| s.length }
63 counts.delete_if { | l, s, t, u | u == 0 }
67 counts.map do |label, string, total, unread|
68 ## if we've done a search and there are no messages for this label, we can delete it from the
69 ## list. BUT if it's a brand-new label, the user may not have sync'ed it to the index yet, so
70 ## don't delete it in this case.
72 ## this is all a hack. what should happen is:
73 ## TODO make the labelmanager responsible for label counts
74 ## and then it can listen to labeled and unlabeled events, etc.
75 if total == 0 && !LabelManager::RESERVED_LABELS.include?(label) && !LabelManager.new_label?(label)
76 debug "no hits for label #{label}, deleting"
77 LabelManager.delete label
81 @text << [[(unread == 0 ? :labellist_old_color : :labellist_new_color),
82 sprintf("%#{width + 1}s %5d %s, %5d unread", string, total, total == 1 ? " message" : "messages", unread)]]
83 @labels << [label, unread]
84 yield i if block_given?
87 BufferManager.flash "No labels with unread messages!" if counts.empty? && @unread_only
91 label, num_unread = @labels[curpos]
93 LabelSearchResultsMode.spawn_nicely label