zsh-workers
 help / color / mirror / code / Atom feed
* Support for 256 colors in zsh/curses
@ 2016-04-29 11:34 Sebastian Gniazdowski
  2016-04-29 11:35 ` Sebastian Gniazdowski
  0 siblings, 1 reply; 2+ messages in thread
From: Sebastian Gniazdowski @ 2016-04-29 11:34 UTC (permalink / raw)
  To: Zsh hackers list

[-- Attachment #1: Type: text/plain, Size: 859 bytes --]

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

[-- Attachment #2: zshcurses_256colors.patch.txt --]
[-- Type: text/plain, Size: 1563 bytes --]

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)

[-- Attachment #3: cursescolors --]
[-- Type: application/octet-stream, Size: 257 bytes --]

#!/usr/local/bin/zsh-5.2-dev-1

zmodload zsh/curses

zcurses init

zcurses bg stdscr 10/12 @_
for (( i = 0; i <= 260; i ++ )); do
    zcurses attr stdscr $i/black
    zcurses string stdscr $i" "
done

zcurses refresh stdscr
zcurses input stdscr
zcurses end

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Support for 256 colors in zsh/curses
  2016-04-29 11:34 Support for 256 colors in zsh/curses Sebastian Gniazdowski
@ 2016-04-29 11:35 ` Sebastian Gniazdowski
  0 siblings, 0 replies; 2+ messages in thread
From: Sebastian Gniazdowski @ 2016-04-29 11:35 UTC (permalink / raw)
  To: Zsh hackers list

The "a/b" should be "x/y", so:

Hello,
the existing infrastructure in zsh/curses is quite nice. If user
requests color pair "x/y", 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.

On 29 April 2016 at 13:34, Sebastian Gniazdowski <sgniazdowski@gmail.com> wrote:
> 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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-04-29 11:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-29 11:34 Support for 256 colors in zsh/curses Sebastian Gniazdowski
2016-04-29 11:35 ` Sebastian Gniazdowski

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).