diff options
author | Arun Prakash Jana <engineerarun@gmail.com> | 2020-08-02 16:18:35 +0530 |
---|---|---|
committer | Arun Prakash Jana <engineerarun@gmail.com> | 2020-08-02 16:28:25 +0530 |
commit | 83fa6a0ff62289c9f420a435d4457042f7c420c6 (patch) | |
tree | da9cb12f36dd06cc1905c27b4d6c28e52dfeca70 /src | |
parent | 110a4eb108586fb1e2c3e05e17bd0e9f5fbf9b42 (diff) | |
download | nnn-83fa6a0ff62289c9f420a435d4457042f7c420c6.tar.gz |
Support xterm 256 color
Diffstat (limited to 'src')
-rw-r--r-- | src/nnn.c | 71 |
1 files changed, 52 insertions, 19 deletions
@@ -757,6 +757,21 @@ static char *xitoa(uint val) return &ascbuf[++i]; } +/* Return the integer value of a char representing HEX */ +static uchar xchartohex(uchar c) +{ + if (xisdigit(c)) + return c - '0'; + + if (c >= 'a' && c <= 'f') + return c - 'a' + 10; + + if (c >= 'A' && c <= 'F') + return c - 'A' + 10; + + return c; +} + /* * Source: https://elixir.bootlin.com/linux/latest/source/arch/alpha/include/asm/bitops.h */ @@ -1571,18 +1586,51 @@ static bool initcurses(void *oldmask) char *colors = getenv(env_cfg[NNN_COLORS]); if (colors || !getenv("NO_COLOR")) { + uint *pcode; + char ch; + bool ext = FALSE; + start_color(); use_default_colors(); + if (colors && *colors == '#') { + if (COLORS >= 256) { + ++colors; + ext = TRUE; + + /* + * If fallback colors are specified, set the separator + * to NULL so we don't interpret separator and fallback + * if fewer than CTX_MAX xterm 256 colors are specified. + */ + char *sep = strchr(colors, ';'); + if (sep) + *sep = '\0'; + } else + /* Check if 8 colors fallback is appended */ + colors = strchr(colors, ';') + 1; + } + /* Get and set the context colors */ for (uchar i = 0; i < CTX_MAX; ++i) { + pcode = &g_ctx[i].color; + if (colors && *colors) { - g_ctx[i].color = (*colors < '0' || *colors > '7') ? 4 : *colors - '0'; - ++colors; + if (ext) { + ch = *colors; + if (*++colors) { + *pcode = (16 * xchartohex(ch)) + xchartohex(*colors); + ++colors; + } else + *pcode = xchartohex(ch); + } else { + *pcode = (*colors < '0' || *colors > '7') ? 4 : *colors - '0'; + ++colors; + } } else - g_ctx[i].color = 4; + *pcode = 4; - init_pair(i + 1, g_ctx[i].color, -1); + init_pair(i + 1, *pcode, -1); } } @@ -2217,21 +2265,6 @@ static int xstrverscasecmp(const char * const s1, const char * const s2) static int (*namecmpfn)(const char * const s1, const char * const s2) = &xstricmp; -/* Return the integer value of a char representing HEX */ -static char xchartohex(char c) -{ - if (xisdigit(c)) - return c - '0'; - - if (c >= 'a' && c <= 'f') - return c - 'a' + 10; - - if (c >= 'A' && c <= 'F') - return c - 'A' + 10; - - return c; -} - static char * (*fnstrstr)(const char *haystack, const char *needle) = &strcasestr; #ifdef PCRE static const unsigned char *tables; |