Subject: Support for 256 colors in zsh/curses
From: Sebastian Gniazdowski <[email-masked]>
To: Zsh hackers list <[email-masked]>
Date: Fri, 29 Apr 2016 13:34:19 +0200
List-Id: Zsh Workers List <zsh-workers.zsh.org>

Hello,
the existing infrastructure in zsh/curses is quite nice. If user
requests color pair "a/b", it is first looked up in a hash table:

!(cpn = (Colorpairnode) gethashnode2(zcurses_colorpairs, colorpair))) {

if it doesn't exist, then "x/y" (e.g. "red/black") is translated to
corresponding integers, and init_pair is called:

if (next_cp >= COLOR_PAIRS || init_pair(next_cp, f, b) == ERR)  {

where "f" and "b" are the translated integers. The color pair is put
into the hash under "x/y" for future reuse.

To support 256 colors, all I had to do is translate num1/num2 into
f=num1, b=num2, i.e. just directly (classic atoi) translate color
number into integer to be passed to curses as its color number. Not
sure what else can I write, reading the code will reveal how
transparent the change is. Any questions maybe?

Best regards,
Sebastian Gniazdowski
diff --git a/Doc/Zsh/mod_curses.yo b/Doc/Zsh/mod_curses.yo
index 8104572..390fce8 100644
--- a/Doc/Zsh/mod_curses.yo
+++ b/Doc/Zsh/mod_curses.yo
@@ -111,7 +111,10 @@ Each var(fg_col)tt(/)var(bg_col) attribute (to be read as
 for character output.  The color tt(default) is sometimes available
 (in particular if the library is ncurses), specifying the foreground
 or background color with which the terminal started.  The color pair
-tt(default/default) is always available.
+tt(default/default) is always available. To use more than the 8 named
+colors (red, green, etc.) construct the var(fg_col)tt(/)var(bg_col)
+pairs with numbers in them, e.g tt(128/200). Maximum color number is
+254 if terminal supports 256 colors.
 
 tt(bg) overrides the color and other attributes of all characters in the
 window.  Its usual use is to set the background initially, but it will
diff --git a/Src/Modules/curses.c b/Src/Modules/curses.c
index 7fff858..63c6748 100644
--- a/Src/Modules/curses.c
+++ b/Src/Modules/curses.c
@@ -350,8 +350,20 @@ zcurses_colorget(const char *nam, char *colorpair)
 	}
 
 	*bg = '\0';        
-	f = zcurses_color(cp);
-	b = zcurses_color(bg+1);
+
+        // cp/bg can be {number}/{number} or {name}/{name}
+
+        if( cp[0] >= '0' && cp[0] <= '9' ) {
+            f = atoi(cp);
+        } else {
+            f = zcurses_color(cp);
+        }
+
+        if( (bg+1)[0] >= '0' && (bg+1)[0] <= '9' ) {
+            b = atoi(bg+1);
+        } else {
+            b = zcurses_color(bg+1);
+        }
 
 	if (f==-2 || b==-2) {
 	    if (f == -2)