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>

Robert Estelle committed Oct 25, 2021 at 22:32 UTC 05f1f41c9b02b916a5f03c5658bec3270ac3684d
4 files changed +51 -6
Documentation/config.txt
+13 -5
@@ -262,11 +262,19 @@ color::
262 colors (at most two, one for foreground and one for background)
263 and attributes (as many as you want), separated by spaces.
264 +
265 -The basic colors accepted are `normal`, `black`, `red`, `green`, `yellow`,
266 -`blue`, `magenta`, `cyan` and `white`. The first color given is the
267 -foreground; the second is the background. All the basic colors except
268 -`normal` have a bright variant that can be specified by prefixing the
269 -color with `bright`, like `brightred`.
265 +The basic colors accepted are `normal`, `black`, `red`, `green`,
266 +`yellow`, `blue`, `magenta`, `cyan`, `white` and `default`. The first
267 +color given is the foreground; the second is the background. All the
268 +basic colors except `normal` and `default` have a bright variant that can
269 +be specified by prefixing the color with `bright`, like `brightred`.
270 ++
271 +The color `normal` makes no change to the color. It is the same as an
272 +empty string, but can be used as the foreground color when specifying a
273 +background color alone (for example, "normal red").
274 ++
275 +The color `default` explicitly resets the color to the terminal default,
276 +for example to specify a cleared background. Although it varies between
277 +terminals, this is usually not the same as setting to "white black".
278 +
279 Colors may also be given as numbers between 0 and 255; these use ANSI
280 256-color mode (but note that not all terminals may support this). If
color.c
+22 -1
@@ -40,7 +40,7 @@ struct color {
40 enum {
41 COLOR_UNSPECIFIED = 0,
42 COLOR_NORMAL,
43 - COLOR_ANSI, /* basic 0-7 ANSI colors */
43 + COLOR_ANSI, /* basic 0-7 ANSI colors + "default" (value = 9) */
44 COLOR_256,
45 COLOR_RGB
46 } type;
@@ -83,6 +83,27 @@ static int parse_ansi_color(struct color *out, const char *name, int len)
83 int i;
84 int color_offset = COLOR_FOREGROUND_ANSI;
85
86 + if (match_word(name, len, "default")) {
87 + /*
88 + * Restores to the terminal's default color, which may not be
89 + * the same as explicitly setting "white" or "black".
90 + *
91 + * ECMA-48 - Control Functions \
92 + * for Coded Character Sets, 5th edition (June 1991):
93 + * > 39 default display colour (implementation-defined)
94 + * > 49 default background colour (implementation-defined)
95 + *
96 + * Although not supported /everywhere/--according to terminfo,
97 + * some terminals define "op" (original pair) as a blunt
98 + * "set to white on black", or even "send full SGR reset"--
99 + * it's standard and well-supported enough that if a user
100 + * asks for it in their config this will do the right thing.
101 + */
102 + out->type = COLOR_ANSI;
103 + out->value = 9 + color_offset;
104 + return 0;
105 + }
106 +
107 if (strncasecmp(name, "bright", 6) == 0) {
108 color_offset = COLOR_FOREGROUND_BRIGHT_ANSI;
109 name += 6;
color.h
+4
@@ -32,6 +32,7 @@ struct strbuf;
32 #define GIT_COLOR_MAGENTA "\033[35m"
33 #define GIT_COLOR_CYAN "\033[36m"
34 #define GIT_COLOR_WHITE "\033[37m"
35 +#define GIT_COLOR_DEFAULT "\033[39m"
36 #define GIT_COLOR_BOLD_BLACK "\033[1;30m"
37 #define GIT_COLOR_BOLD_RED "\033[1;31m"
38 #define GIT_COLOR_BOLD_GREEN "\033[1;32m"
@@ -40,6 +41,7 @@ struct strbuf;
41 #define GIT_COLOR_BOLD_MAGENTA "\033[1;35m"
42 #define GIT_COLOR_BOLD_CYAN "\033[1;36m"
43 #define GIT_COLOR_BOLD_WHITE "\033[1;37m"
44 +#define GIT_COLOR_BOLD_DEFAULT "\033[1;39m"
45 #define GIT_COLOR_FAINT_BLACK "\033[2;30m"
46 #define GIT_COLOR_FAINT_RED "\033[2;31m"
47 #define GIT_COLOR_FAINT_GREEN "\033[2;32m"
@@ -48,6 +50,7 @@ struct strbuf;
50 #define GIT_COLOR_FAINT_MAGENTA "\033[2;35m"
51 #define GIT_COLOR_FAINT_CYAN "\033[2;36m"
52 #define GIT_COLOR_FAINT_WHITE "\033[2;37m"
53 +#define GIT_COLOR_FAINT_DEFAULT "\033[2;39m"
54 #define GIT_COLOR_BG_BLACK "\033[40m"
55 #define GIT_COLOR_BG_RED "\033[41m"
56 #define GIT_COLOR_BG_GREEN "\033[42m"
@@ -56,6 +59,7 @@ struct strbuf;
59 #define GIT_COLOR_BG_MAGENTA "\033[45m"
60 #define GIT_COLOR_BG_CYAN "\033[46m"
61 #define GIT_COLOR_BG_WHITE "\033[47m"
62 +#define GIT_COLOR_BG_DEFAULT "\033[49m"
63 #define GIT_COLOR_FAINT "\033[2m"
64 #define GIT_COLOR_FAINT_ITALIC "\033[2;3m"
65 #define GIT_COLOR_REVERSE "\033[7m"
t/t4026-color.sh
+12
@@ -96,6 +96,18 @@ test_expect_success '24-bit colors' '
96 color "#ff00ff black" "[38;2;255;0;255;40m"
97 '
98
99 +test_expect_success '"default" foreground' '
100 + color "default" "[39m"
101 +'
102 +
103 +test_expect_success '"normal default" to clear background' '
104 + color "normal default" "[49m"
105 +'
106 +
107 +test_expect_success '"default" can be combined with attributes' '
108 + color "default default no-reverse bold" "[1;27;39;49m"
109 +'
110 +
111 test_expect_success '"normal" yields no color at all"' '
112 color "normal black" "[40m"
113 '