Subject: [sup-talk] [PATCH] sup color customization

From: [email-masked] (Lionel Ott)
Date: Sun, 25 May 2008 23:51:49 +0200

- config file is located under $HOME/.sup/config.yaml and has the following
  structure

  :colors:
    :symbol_name:
      :fg: <color>
      :bg: <color>
      :attrs:
      - <attribute>

  <color> and <attribute> can take the standard values available in the curses
  environment.
  There may be multiple attributes, but they need not be present.
- if there is an error in the user provided config file a default value will
  be used (stored in the Colormap class)
---

I started to write such a patch when I saw that there was already some work
done on it so I took a look at Dag Odenhall's patch and took it from there.

Not sure if this is the best way. It moves the whole colormap definition
out of bin/sup into the colormap class itself.
Should this be merged I'd probably write some lines to pass along with sup
so it's clear what to do with the config file and what the possible
options are.

 bin/sup             |   48 +-----------------------------
 lib/sup.rb          |    1 +
 lib/sup/colormap.rb |   80 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 82 insertions(+), 47 deletions(-)

diff --git a/bin/sup b/bin/sup
index 723b1ed..a814b1c 100644
--- a/bin/sup
+++ b/bin/sup
@@ -79,6 +79,7 @@ def start_cursing
   Ncurses.stdscr.keypad 1
   Ncurses.curs_set 0
   Ncurses.start_color
+  Ncurses.use_default_colors
   $cursing = true
 end
 
@@ -140,53 +141,8 @@ begin
   log "starting curses"
   start_cursing
 
-  Colormap.new do |c|
-    c.add :status_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLUE, Ncurses::A_BOLD
-    c.add :index_old_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
-    c.add :index_new_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK, 
-           Ncurses::A_BOLD
-    c.add :index_starred_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK, 
-           Ncurses::A_BOLD
-    c.add :index_draft_color, Ncurses::COLOR_RED, Ncurses::COLOR_BLACK,
-           Ncurses::A_BOLD
-    c.add :labellist_old_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
-    c.add :labellist_new_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK, 
-           Ncurses::A_BOLD
-    c.add :twiddle_color, Ncurses::COLOR_BLUE, Ncurses::COLOR_BLACK
-    c.add :label_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
-    c.add :message_patina_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_GREEN
-    c.add :alternate_patina_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_BLUE
-    c.add :missing_message_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_RED
-    c.add :attachment_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
-    c.add :cryptosig_valid_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK, Ncurses::A_BOLD
-    c.add :cryptosig_unknown_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
-    c.add :cryptosig_invalid_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_RED, Ncurses::A_BOLD
-    c.add :generic_notice_patina_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
-    c.add :quote_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
-    c.add :sig_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
-    c.add :quote_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
-    c.add :sig_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
-    c.add :to_me_color, Ncurses::COLOR_GREEN, Ncurses::COLOR_BLACK
-    c.add :starred_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK,
-          Ncurses::A_BOLD
-    c.add :starred_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_GREEN,
-          Ncurses::A_BOLD
-    c.add :alternate_starred_patina_color, Ncurses::COLOR_YELLOW,
-          Ncurses::COLOR_BLUE, Ncurses::A_BOLD
-    c.add :snippet_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
-    c.add :option_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
-    c.add :tagged_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK,
-          Ncurses::A_BOLD
-    c.add :draft_notification_color, Ncurses::COLOR_RED, Ncurses::COLOR_BLACK,
-          Ncurses::A_BOLD
-    c.add :completion_character_color, Ncurses::COLOR_WHITE,
-          Ncurses::COLOR_BLACK, Ncurses::A_BOLD
-    c.add :horizontal_selector_selected_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK, Ncurses::A_BOLD
-    c.add :horizontal_selector_unselected_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
-    c.add :search_highlight_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_YELLOW, Ncurses::A_BOLD, :highlight => :search_highlight_color
-  end
-
   bm = BufferManager.new
+  Colormap.new
 
   log "initializing mail index buffer"
   imode = InboxMode.new
diff --git a/lib/sup.rb b/lib/sup.rb
index 9e90267..9a4d72d 100644
--- a/lib/sup.rb
+++ b/lib/sup.rb
@@ -37,6 +37,7 @@ module Redwood
 
   BASE_DIR   = ENV["SUP_BASE"] || File.join(ENV["HOME"], ".sup")
   CONFIG_FN  = File.join(BASE_DIR, "config.yaml")
+  COLOR_FN   = File.join(BASE_DIR, "colors.yaml")
   SOURCE_FN  = File.join(BASE_DIR, "sources.yaml")
   LABEL_FN   = File.join(BASE_DIR, "labels.txt")
   PERSON_FN  = File.join(BASE_DIR, "people.txt")
diff --git a/lib/sup/colormap.rb b/lib/sup/colormap.rb
index 9c6869a..8129bcf 100644
--- a/lib/sup/colormap.rb
+++ b/lib/sup/colormap.rb
@@ -1,3 +1,7 @@
+module Curses
+  COLOR_DEFAULT = -1
+end
+
 module Redwood
 
 class Colormap
@@ -6,8 +10,44 @@ class Colormap
   CURSES_COLORS = [Curses::COLOR_BLACK, Curses::COLOR_RED, Curses::COLOR_GREEN,
                    Curses::COLOR_YELLOW, Curses::COLOR_BLUE,
                    Curses::COLOR_MAGENTA, Curses::COLOR_CYAN,
-                   Curses::COLOR_WHITE]
+                   Curses::COLOR_WHITE, Curses::COLOR_DEFAULT]
   NUM_COLORS = 15
+
+  DEFAULT_COLORS = {
+    :status => { :fg => "white", :bg => "blue", :attrs => ["bold"] },
+    :index_old => { :fg => "white", :bg => "black" },
+    :index_new => { :fg => "white", :bg => "black", :attrs => ["bold"] },
+    :index_starred => { :fg => "yellow", :bg => "black", :attrs => ["bold"] },
+    :index_draft => { :fg => "red", :bg => "black", :attrs => ["bold"] },
+    :labellist_old => { :fg => "white", :bg => "black" },
+    :labellist_new => { :fg => "white", :bg => "black", :attrs => ["bold"] },
+    :twiddle => { :fg => "blue", :bg => "black" },
+    :label => { :fg => "yellow", :bg => "black" },
+    :message_patina => { :fg => "black", :bg => "green" },
+    :alternate_patina => { :fg => "black", :bg => "blue" },
+    :missing_message => { :fg => "black", :bg => "red" },
+    :attachment => { :fg => "cyan", :bg => "black" },
+    :cryptosig_valid => { :fg => "yellow", :bg => "black", :attrs => ["bold"] },
+    :cryptosig_unknown => { :fg => "cyan", :bg => "black" },
+    :cryptosig_invalid => { :fg => "yellow", :bg => "red", :attrs => ["bold"] },
+    :generic_notice_patina => { :fg => "cyan", :bg => "black" },
+    :quote_patina => { :fg => "yellow", :bg => "black" },
+    :sig_patina => { :fg => "yellow", :bg => "black" },
+    :quote => { :fg => "yellow", :bg => "black" },
+    :sig => { :fg => "yellow", :bg => "black" },
+    :to_me => { :fg => "green", :bg => "black" },
+    :starred => { :fg => "yellow", :bg => "black", :attrs => ["bold"] },
+    :starred_patina => { :fg => "yellow", :bg => "green", :attrs => ["bold"] },
+    :alternate_starrte_colormap
   end
 
   def add sym, fg, bg, attr=nil, opts={}
@@ -108,6 +149,43 @@ class Colormap
     color
   end
 
+  ## Try to use the user defined colors, in case of an error fall back
+  ## to the default ones.
+  def populate_colormap
+    if File.exists? Redwood::COLOR_FN
+      user_colors = Redwood::load_yaml_obj Redwood::COLOR_FN
+    end
+
+    errors = []
+
+    Colormap::DEFAULT_COLORS.each_pair do |k, v|
+      fg = Curses.const_get "COLOR_#{v[:fg].upcase}"
+      bg = Curses.const_get "COLOR_#{v[:bg].upcase}"
+      attrs = v[:attrs].map { |a| Curses.const_get "A_#{a.upcase}" } rescue attrs
+
+      if(ucolor = user_colors[:colors][k])
+        begin
+          fg = Curses.const_get "COLOR_#{ucolor[:fg].upcase}"
+        rescue NameError
+          errors << "Warning: There is no color named \"#{ucolor[:fg]}\", using fallback."
+          Redwood::log "Warning: There is no color named \"#{ucolor[:fg]}\""
+        end
+        begin
+          bg = Curses.const_get "COLOR_#{ucolor[:bg].upcase}"
+        rescue NameError
+          errors << "Warning: There is no color named \"#{ucolor[:bg]}\", using fallback."
+          Redwood::log "Warning: There is no color named \"#{ucolor[:bg]}\""
+        end
+        attrs = ucolor[:attrs].map {|a| Curses.const_get "A_#{a.upcase}" } rescue attrs
+      end
+
+      symbol = (k.to_s + "_color").to_sym
+      add symbol, fg, bg, attrs
+    end
+
+    errors.each { |e| BufferManager.flash e }
+  end
+
   def self.instance; @@instance; end
   def self.method_missing meth, *a
     Colorcolors.new unless @@instance
-- 
1.5.5.1

---

From: [email-masked] (William Morgan)
Date: Mon, 02 Jun 2008 19:18:42 -0700

Hi Lionel,

This looks great. But I think the patch was corrupted, somewhere around
these lines. Can you please resend?

-- 
William <wmorgan-sup at masanjin.net>

---

From: [email-masked] (Lionel Ott)
Date: Tue, 03 Jun 2008 09:30:52 +0200

> > @@ -108,6 +149,43 @@ class Colormap

Sure I'll attach it this time hopefully it won't break this way.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-sup-color-customization.patch
Type: application/octet-stream
Size: 9511 bytes
Desc: not available
URL: <http://rubyforge.org/pipermail/sup-talk/attachments/20080603/365d6692/attachment.obj>

---

From: [email-masked] (William Morgan)
Date: Tue, 03 Jun 2008 19:27:12 -0700

> Sure I'll attach it this time hopefully it won't break this way.

Ok great. I've merged this into next. I tweaked it a little bit. In
particular:

1. I removed top-level "colors:" from colors.yaml.
2. Single attributes can now be specified in one line without the array
   syntax.

So the following is a valid ~/.sup/colors.yaml:

  :label:
    :fg: green
    :bg: black
    :attrs: bold

Enjoy!

---

From: [email-masked] (Lionel Ott)
Date: Thu, 05 Jun 2008 22:13:26 +0200

> So the following is a valid ~/.sup/colors.yaml:

Cool, I also pondered whether I should remove the colors: thing from the
config file but decided to leave it in because I couldn't decide between
leaving it in and taking it out :-)
The single attribute change is good, I didn't think too much about
streamlining the config.