William Erik Baxter
2009-Sep-18 19:54 UTC
[sup-talk] [PATCH] Add hooks to sort and format label-list-mode display.
---
lib/sup/modes/label-list-mode.rb | 37 ++++++++++++++++++++++++++++++++++---
1 files changed, 34 insertions(+), 3 deletions(-)
diff --git a/lib/sup/modes/label-list-mode.rb b/lib/sup/modes/label-list-mode.rb
index f65ec2e..d94f56f 100644
--- a/lib/sup/modes/label-list-mode.rb
+++ b/lib/sup/modes/label-list-mode.rb
@@ -8,6 +8,24 @@ class LabelListMode < LineCursorMode
k.add :toggle_show_unread_only, "Toggle between showing all labels and
those with unread mail", ''u''
end
+ HookManager.register "label-list-filter", <<EOS
+Filter the label list, typically to sort.
+Variables:
+ counted: an array of counted labels.
+Return value:
+ An array of counted labels with sort_by output structure.
+EOS
+
+ HookManager.register "label-list-format", <<EOS
+Create the sprintf format string for label-list-mode.
+Variables:
+ width: the maximum label width
+ tmax: the maximum total message count
+ umax: the maximum unread message count
+Return value:
+ A format string for sprintf
+EOS
+
def initialize
@labels = []
@text = []
@@ -50,14 +68,22 @@ protected
@text = []
labels = LabelManager.all_labels
- counts = labels.map do |label|
+ counted = labels.map do |label|
string = LabelManager.string_for label
total = Index.num_results_for :label => label
unread = (label == :unread)? total : Index.num_results_for(:labels =>
[label, :unread])
[label, string, total, unread]
- end.sort_by { |l, s, t, u| s.downcase }
+ end
+
+ if HookManager.enabled? "label-list-filter"
+ counts = HookManager.run "label-list-filter", :counted =>
counted
+ else
+ counts = counted.sort_by { |l, s, t, u| s.downcase }
+ end
width = counts.max_of { |l, s, t, u| s.length }
+ tmax = counts.max_of { |l, s, t, u| t }
+ umax = counts.max_of { |l, s, t, u| u }
if @unread_only
counts.delete_if { | l, s, t, u | u == 0 }
@@ -78,8 +104,13 @@ protected
next
end
+ fmt = HookManager.run "label-list-format", :width => width,
:tmax => tmax, :umax => umax
+ if !fmt
+ fmt = "%#{width + 1}s %5d %s, %5d unread"
+ end
+
@text << [[(unread == 0 ? :labellist_old_color :
:labellist_new_color),
- sprintf("%#{width + 1}s %5d %s, %5d unread", string, total,
total == 1 ? " message" : "messages", unread)]]
+ sprintf(fmt, string, total, total == 1 ? " message" :
"messages", unread)]]
@labels << [label, unread]
yield i if block_given?
end.compact
--
1.5.3.2
William Morgan
2009-Oct-01 16:58 UTC
[sup-talk] [PATCH] Add hooks to sort and format label-list-mode display.
Hi William, Sorry for the delay. I like this patch but I can''t get it to apply cleanly to master. git am -3 complains: Did you hand edit your patch? It does not apply to blobs recorded in its index. Cannot fall back to three-way merge. Patch failed at 0001. If you can resubmit against a recent master, I will apply it. Thanks! -- William <wmorgan-sup at masanjin.net>