zsh-workers
 help / color / mirror / code / Atom feed
From: Sebastian Gniazdowski <sgniazdowski@gmail.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Support for 256 colors in zsh/curses
Date: Fri, 29 Apr 2016 13:34:19 +0200	[thread overview]
Message-ID: <CAKc7PVDAUh=he-N+s1ZSGS8JfuCJ+66EB+bpkmOBhFaKXX-k=A@mail.gmail.com> (raw)

[-- 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

             reply	other threads:[~2016-04-29 11:34 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-29 11:34 Sebastian Gniazdowski [this message]
2016-04-29 11:35 ` Sebastian Gniazdowski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAKc7PVDAUh=he-N+s1ZSGS8JfuCJ+66EB+bpkmOBhFaKXX-k=A@mail.gmail.com' \
    --to=sgniazdowski@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).