color: support "default" to restore fg/bg color

The name "default" can now be used in foreground or background colors,
and means to use the terminal's default color, discarding any
explicitly-set color without affecting the other attributes. On many
modern terminals, this is *not* the same as specifying "white" or
"black".

Although attributes could previously be cleared like "no-bold", there
had not been a similar mechanism available for colors, other than a full
"reset", which cannot currently be combined with other settings.

Note that this is *not* the same as the existing name "normal", which is
a no-op placeholder to permit setting the background without changing
the foreground. (i.e. what is currently called "normal" might have been
more descriptively named "inherit", "none", "pass" or similar).

Signed-off-by: Robert Estelle <robertestelle@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Robert Estelle
2021-10-25 22:32:36 +00:00
committed by Junio C Hamano
parent aeefc1866c
commit 05f1f41c9b
4 changed files with 51 additions and 6 deletions

23
color.c
View File

@@ -40,7 +40,7 @@ struct color {
enum {
COLOR_UNSPECIFIED = 0,
COLOR_NORMAL,
COLOR_ANSI, /* basic 0-7 ANSI colors */
COLOR_ANSI, /* basic 0-7 ANSI colors + "default" (value = 9) */
COLOR_256,
COLOR_RGB
} type;
@@ -83,6 +83,27 @@ static int parse_ansi_color(struct color *out, const char *name, int len)
int i;
int color_offset = COLOR_FOREGROUND_ANSI;
if (match_word(name, len, "default")) {
/*
* Restores to the terminal's default color, which may not be
* the same as explicitly setting "white" or "black".
*
* ECMA-48 - Control Functions \
* for Coded Character Sets, 5th edition (June 1991):
* > 39 default display colour (implementation-defined)
* > 49 default background colour (implementation-defined)
*
* Although not supported /everywhere/--according to terminfo,
* some terminals define "op" (original pair) as a blunt
* "set to white on black", or even "send full SGR reset"--
* it's standard and well-supported enough that if a user
* asks for it in their config this will do the right thing.
*/
out->type = COLOR_ANSI;
out->value = 9 + color_offset;
return 0;
}
if (strncasecmp(name, "bright", 6) == 0) {
color_offset = COLOR_FOREGROUND_BRIGHT_ANSI;
name += 6;