Subject: [sup-talk] [PATCH] use any custom colors configured in config.yaml

From: [email-masked] (Dag Odenhall)
Date: Fri, 25 Apr 2008 21:12:16 +0200

* bin/sup: (start_cursing) Initialize use of default
colors (transparency).

* lib/sup/colormap.rb: Add a COLOR_DEFAULT constant to the
Curses module.
(CURSES_COLOR) Add the new COLOR_DEFAULT constant.
(color_for) Use colors from config if available.

Signed-off-by: Dag Odenhall <dag.odenhall at gmail.com>
---
 bin/sup             |    1 +
 lib/sup/colormap.rb |   11 ++++++++++-
 2 files changed, 11 insertions(+), 1 deletions(-)

diff --git a/bin/sup b/bin/sup
index 6360cde..e791fe5 100644
--- a/bin/sup
+++ b/bin/sup
@@ -78,6 +78,7 @@ def start_cursing
   Ncurses.stdscr.keypad 1
   Ncurses.curs_set 0
   Ncurses.start_color
+  Ncurses.use_default_colors
   $cursing = true
 end
 
diff --git a/lib/sup/colormap.rb b/lib/sup/colormap.rb
index 9c6869a..4c48604 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,7 +10,7 @@ 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
   
   def initialize
@@ -80,6 +84,11 @@ class Colormap
     fg, bg, attrs, color = @entries[sym]
     return color if color
 
+    confcolor = $config[:colors][sym.to_s[0..-7].to_sym] rescue nil
+    fg = Curses.const_get "COLOR_#{confcolor[:fg].upcase}" rescue fg
+    bg = Curses.const_get "COLOR_#{confcolor[:bg].upcase}" rescue bg
+    attrs = confcolor[:attrs].map {|a| Curses.const_get "A_#{a.upcase}" } rescue attrs
+
     if(cp = @color_pairs[[fg, bg]])
       ## nothing
     else ## need to get a new colorpair
-- 
1.5.5.1

---

From: [email-masked] (William Morgan)
Date: Mon, 28 Apr 2008 15:28:29 -0700

Hi Dag,

Thanks for the patch! People have been clamoring for this functionality
forever and I have been cruelly not doing it for them.

A few minor comments:

> +    attrs = confcolor[:attrs].map {|a| Curses.const_get "A_#{a.upcase}" } rescue attrs

1. Can you wrap all this in an if statement, rather than having sequence of
   post-fix rescue stuff? You could do something like "if(confcolor
   = $config[...)" and set fg and bg within that.

2. Errors in the color names will be silently ignored. The
   NameError exception should be caught and turned into a
   BufferManager.flash and a Redwood::log to inform the user.

3. Since I feel like people will write a lot of these descriptions, I
   would load in colors from a colors.yaml file instead of from the
   config file. (I can do this one instead if you're unsure.)

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

---

From: [email-masked] (Dag Odenhall)
Date: Tue, 29 Apr 2008 01:01:41 +0200

> forever and I have been cruelly not doing it for them.

Well thanks for the thanks! :)

>    BufferManager.flash and a Redwood::log to inform the user.

I'll give it a try.

>    config file. (I can do this one instead if you're unsure.)

I could give this a try too, then again you are probably better at
getting it the way you envision it... I guess we'll see. :)