Subject: [sup-talk] [PATCH] Alternative View

From: [email-masked] (Marcus Williams)
Date: Tue, 16 Jun 2009 13:42:08 +0100

Adds a keypress to toggle to an alternative view in thread index mode.
By default this removes the date, authors and number of threads from the
view leaving more room for labels/snippets

This view is extendable via the alternative-view-widget hook. An example
hook is included in the contrib/hooks directory that implements the
standard sup view as an example. If you use this example your
alternative view will not look any different to the standard sup view.
It is meant to give you a base point for your edits.
---
 contrib/hooks/alternate-view-widget.rb |   39 ++++++++++++++++++++++
 lib/sup/modes/thread-index-mode.rb     |   56 ++++++++++++++++++-------------
 2 files changed, 71 insertions(+), 24 deletions(-)
 create mode 100644 contrib/hooks/alternate-view-widget.rb

diff --git a/contrib/hooks/alternate-view-widget.rb b/contrib/hooks/alternate-view-widget.rb
new file mode 100644
index 0000000..cf936fd
--- /dev/null
+++ b/contrib/hooks/alternate-view-widget.rb
@@ -0,0 +1,39 @@
+# This hook recreates the standard sup view as the alternate view
+# Note that this will mean there is no difference when toggling 
+# between the standard and alternate views in sup. This is meant to
+# serve as a starting point for your own view
+
+date_width = Time::TO_NICE_S_MAX_LEN
+date = thread.date.to_nice_s
+starred = thread.has_label? :starred
+    
+dp = thread.direct_participants.any? { |p| AccountManager.is_account? p }
+p = dp || thread.participants.any? { |p| AccountManager.is_account? p }
+    
+snippet = thread.snippet + (thread.snippet.empty? ? "" : "...")
+
+subj_color =
+  if thread.has_label?(:draft)
+    :index_draft_color
+  elsif thread.has_label?(:unread)
+    :index_new_color
+  elsif starred
+    :index_starred_color
+  else 
+    :index_old_color
+  end
+
+[ 
+  [:tagged_color, tagged ? ">" : " "],
+  [:none, sprintf("%#{date_width}s", date)],
+     (starred ? [:starred_color, "*"] : [:none, " "]),
+] + from +
+[
+  [subj_color, size_widget_text],
+  [:to_me_color, thread.labels.member?(:attachment) ? "@" : " "],
+  [:to_me_color, dp ? ">" : (p ? '+' : " ")],
+] + (thread.labels - hidden_labels).map { |label| [:label_color, "#{label} "] } +
+[
+  [subj_color, thread.subj + (thread.subj.empty? ? "" : " ")],
+  [:snippet_color, snippet],
+]
diff --git a/lib/sup/modes/thread-index-mode.rb b/lib/sup/modes/thread-index-mode.rb
index 5fa4f4c..9124dbd 100644
--- a/lib/sup/modes/thread-index-mode.rb
+++ b/lib/sup/modes/thread-index-mode.rb
@@ -8,6 +8,15 @@ class ThreadIndexMode < LineCursorMode
   MIN_FROM_WIDTH = 15
   LOAD_MORE_THREAD_NUM = 20
 
+  HookManager.register "alternate-view-widget", <<EOS
+Generates the per-thread view for each thread.
+Variables:
+  thread: The message thread to be formatted.
+  hidden_labels: the hidden label list
+  tagged: true if this thread is tagged
+  from: the sup default from/author list
+  size_widget_text: the size widget text (from hook or default sup size)
+EOS
   HookManager.register "index-mode-size-widget", <<EOS
 Generates the per-thread size widget for each thread.
 Variables:
@@ -45,7 +54,7 @@ EOS
     k.add :apply_to_tagged, "Apply next command to all tagged threads", '+', '='
     k.add :join_threads, "Force tagged threads to be joined into the same thread", '#'
     k.add :undo, "Undo the previous action", 'u'
-    k.add :toggle_minimalist, "Toggle minimalist view", '~'
+    k.add :toggle_alternative_view, "Toggle alternative view", '~'
   end
 
   def initialize hidden_labels=[], load_thread_opts={}
@@ -63,7 +72,7 @@ EOS
     @hidden_labels = hidden_labels + LabelManager::HIDDEN_RESERVED_LABELS
     @date_width = DATE_WIDTH
 
-    @minimal = false
+    @alternative = false
 
     @interrupt_search = false
     
@@ -264,8 +273,8 @@ EOS
     end
   end  
 
-  def toggle_minimalist 
-    @minimal = !@minimal
+  def toggle_alternative_view 
+    @alternative= !@alternative
     regen_text
   end
 
@@ -841,26 +850,8 @@ protected
 
     size_widget_text = sprintf "%#{ @size_widget_width}s", size_widget
 
-
-    if @minimal
-      if size_widget!=""
-        size_widget_text = "+" 
-      else
-        size_widget_text = " " 
-      end
-      line = []
-    else
-      line =  [ 
-                [:tagged_color, @tags.tagged?(t) ? ">" : " "],
-                [:none, sprintf("%#{@date_width}s", date)],
-                (starred ? [:starred_color, "*"] : [:none, " "]),
-              ] + from
-    end
-
-
-    line +
-    [
-      [subj_color, size_widget_text],
+    alternateView = [
+      [subj_color, @alternative?(size_widget!=""?"+":" "):size_widget_text],
       [:to_me_color, t.labels.member?(:attachment) ? "@" : " "],
       [:to_me_color, dp ? ">" : (p ? '+' : " ")],
     ] +
@@ -869,6 +860,23 @@ protected
       [subj_color, t.subj + (t.subj.empty? ? "" : " ")],
       [:snippet_color, snippet],
     ]
+
+    defaultView = [ 
+      [:tagged_color, @tags.tagged?(t) ? ">" : " "],
+      [:none, sprintf("%#{@date_width}s", date)],
+         (starred ? [:starred_color, "*"] : [:none, " "]),
+    ] + from + alternateView
+
+    alternateView = [
+      [:tagged_color, @tags.tagged?(t) ? ">" : " "],
+    ] + alternateView
+
+
+    if @alternative
+      HookManager.run("alternate-view-widget", :thread => t, :hidden_labels => @hidden_labels, :tagged => @tags.tagged?(t)?true:false, :from => from, :size_widget_text => size_widget_text) || alternateView
+    else
+      defaultView
+    end
   end
 
   def dirty?; @mutex.synchronize { (@hidden_threads.keys + @threads).any? { |t| t.dirty? } } end
-- 
1.5.4.1