From 785447bde15f3a6fe0035f70ae48e9688f5bffb9 Mon Sep 17 00:00:00 2001 From: Sebastian Gniazdowski Date: Sat, 8 Dec 2018 22:42:49 +0100 Subject: [PATCH] Instead of using termcap, 256-colours are based on zle_highlight entries MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Following code snippet performs the conversion of colour number to the code to be emitted to terminal (the example shows both foreground and background color handling – this depends on `fg_bg' value): ... strcpy(colseq_buf, fg_bg_sequences[fg_bg].start); ... } else if (colour > 7 && colour <= 255) { ptr += sprintf(ptr, "8;5;%d", colour); ... --- Src/prompt.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/Src/prompt.c b/Src/prompt.c index 568bfc2a9..135aca942 100644 --- a/Src/prompt.c +++ b/Src/prompt.c @@ -1764,7 +1764,12 @@ output_colour(int colour, int fg_bg, int use_tc, int truecol, char *buf) /* length of hex triplet always 7, don't need sprintf to count */ atrlen += buf ? sprintf(ptr, "#%02x%02x%02x", colour >> 16, (colour >> 8) & 0xff, colour & 0xff) : 7; - /* colour should only be > 7 if using termcap but let's be safe */ + /* colour should only be > 7 if using termcap but let's be safe. Update: + * currently other places in code don't always imply that colour > 7 => + * using-termcap - if zle_highlight will be non-default, then it will be + * used instead of termcap even for colour > 7. Here this just emits the + * color number, so it works fine for both zle_highlight and tercap cases + */ } else if (use_tc || colour > 7) { char digbuf[DIGBUFSIZE]; sprintf(digbuf, "%d", colour); @@ -1995,6 +2000,7 @@ set_colour_attribute(zattr atr, int fg_bg, int flags) char *ptr; int do_free, is_prompt = (flags & TSC_PROMPT) ? 1 : 0; int colour, tc, def, use_termcap, use_truecolor; + int is_default_zle_highlight = 1; if (fg_bg == COL_SEQ_FG) { colour = txtchangeget(atr, TXT_ATTR_FG_COL); @@ -2010,6 +2016,15 @@ set_colour_attribute(zattr atr, int fg_bg, int flags) use_termcap = txtchangeisset(atr, TXT_ATTR_BG_TERMCAP); } + /* Test if current zle_highlight settings are customized, or + * the typical "standard" codes */ + if (0 != strcmp(fg_bg_sequences[fg_bg].start, fg_bg == COL_SEQ_FG ? "\e[3" : "\e[4") || + 0 != strcmp(fg_bg_sequences[fg_bg].def, "9") || /* the same in-fix for both FG and BG */ + 0 != strcmp(fg_bg_sequences[fg_bg].end, "m") /* the same suffix for both FG and BG */ + ) { + is_default_zle_highlight = 0; + } + /* * If we're not restoring the default, and either have a * colour value that is too large for ANSI, or have been told @@ -2020,7 +2035,7 @@ set_colour_attribute(zattr atr, int fg_bg, int flags) * highlighting variables, so much of this shouldn't be * necessary at this point, but we might as well be safe. */ - if (!def && !use_truecolor && (colour > 7 || use_termcap)) { + if (!def && !use_truecolor && (is_default_zle_highlight && (colour > 7 || use_termcap))) { /* * We can if it's available, and either we couldn't get * the maximum number of colours, or the colour is in range. @@ -2046,9 +2061,10 @@ set_colour_attribute(zattr atr, int fg_bg, int flags) } /* * Nope, that didn't work. - * If 0 to 7, assume standard ANSI works, otherwise it won't. + * If 0 to 7, assume standard ANSI works, if 8 to 255, assume + * typical 256-color escapes works, otherwise it won't. */ - if (colour > 7) + if (colour > 255) return; } @@ -2057,6 +2073,10 @@ set_colour_attribute(zattr atr, int fg_bg, int flags) allocate_colour_buffer(); } + /* Build the reset-code: .start + .def + . end + * or the typical true-color code: .start + 8;2;%d;%d;%d + .end + * or the typical 256-color code: .start + 8;5;%d + .end + */ strcpy(colseq_buf, fg_bg_sequences[fg_bg].start); ptr = colseq_buf + strlen(colseq_buf); @@ -2067,6 +2087,8 @@ set_colour_attribute(zattr atr, int fg_bg, int flags) } else if (use_truecolor) { ptr += sprintf(ptr, "8;2;%d;%d;%d", colour >> 16, (colour >> 8) & 0xff, colour & 0xff); + } else if (colour > 7 && colour <= 255) { + ptr += sprintf(ptr, "8;5;%d", colour); } else *ptr++ = colour + '0'; strcpy(ptr, fg_bg_sequences[fg_bg].end); -- 2.15.1 (Apple Git-101)