Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
PATCH: autoload nearcolor based on truecolor detection
- X-seq: zsh-workers 53379
- From: Oliver Kiddle <opk@xxxxxxx>
- To: Zsh workers <zsh-workers@xxxxxxx>
- Subject: PATCH: autoload nearcolor based on truecolor detection
- Date: Tue, 25 Feb 2025 01:01:55 +0100
- Archived-at: <https://zsh.org/workers/53379>
- List-id: <zsh-workers.zsh.org>
Given the ability to auto-detect terminal support for 24-bit truecolor,
autoloading the nearcolor module would have the advantage that colors
specified as hex-triples can be used reliably.
The disadvantage is that on a rare terminal that supports 24-bit color
but on which we fail to detect that, it will degrade to picking from the
256 colour palette. That is unless the user knows to add truecolor to
.term.extensions.
This also still allows you to intentionally use nearcolor on a 24-bit
terminal.
Oliver
diff --git a/Src/prompt.c b/Src/prompt.c
index 7467cdfb9..762d25650 100644
--- a/Src/prompt.c
+++ b/Src/prompt.c
@@ -1836,6 +1836,19 @@ match_named_colour(const char **teststrp)
return -1;
}
+static int
+truecolor_terminal()
+{
+ char **f, **flist = getaparam(".term.extensions");
+ int result;
+ for (f = flist; f && *f; f++) {
+ result = **f != '-';
+ if (!strcmp(*f + !result, "truecolor"))
+ return result;
+ }
+ return 0; /* disabled by default */
+}
+
/*
* Match just the colour part of a highlight specification.
* If teststrp is NULL, use the already parsed numeric colour.
@@ -1879,7 +1892,10 @@ match_colour(const char **teststrp, int is_fg, int colour)
return TXT_ERROR;
*teststrp = end;
colour = runhookdef(GETCOLORATTR, &color) - 1;
- if (colour == -1) { /* no hook function added, try true color (24-bit) */
+ if (colour == -1 && !truecolor_terminal() &&
+ !load_module("zsh/nearcolor", NULL, 1))
+ colour = runhookdef(GETCOLORATTR, &color) - 1;
+ if (colour == -1) { /* use true color (24-bit) */
colour = (((color.red << 8) + color.green) << 8) + color.blue;
return on | (is_fg ? TXT_ATTR_FG_24BIT : TXT_ATTR_BG_24BIT) |
(zattr)colour << shft;
Messages sorted by:
Reverse Date,
Date,
Thread,
Author