Skip to content

Commit 05f1f41

Browse files
rwegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent aeefc18 commit 05f1f41

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

Documentation/config.txt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,19 @@ color::
262262
colors (at most two, one for foreground and one for background)
263263
and attributes (as many as you want), separated by spaces.
264264
+
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".
270278
+
271279
Colors may also be given as numbers between 0 and 255; these use ANSI
272280
256-color mode (but note that not all terminals may support this). If

color.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct color {
4040
enum {
4141
COLOR_UNSPECIFIED = 0,
4242
COLOR_NORMAL,
43-
COLOR_ANSI, /* basic 0-7 ANSI colors */
43+
COLOR_ANSI, /* basic 0-7 ANSI colors + "default" (value = 9) */
4444
COLOR_256,
4545
COLOR_RGB
4646
} type;
@@ -83,6 +83,27 @@ static int parse_ansi_color(struct color *out, const char *name, int len)
8383
int i;
8484
int color_offset = COLOR_FOREGROUND_ANSI;
8585

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+
86107
if (strncasecmp(name, "bright", 6) == 0) {
87108
color_offset = COLOR_FOREGROUND_BRIGHT_ANSI;
88109
name += 6;

color.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct strbuf;
3232
#define GIT_COLOR_MAGENTA "\033[35m"
3333
#define GIT_COLOR_CYAN "\033[36m"
3434
#define GIT_COLOR_WHITE "\033[37m"
35+
#define GIT_COLOR_DEFAULT "\033[39m"
3536
#define GIT_COLOR_BOLD_BLACK "\033[1;30m"
3637
#define GIT_COLOR_BOLD_RED "\033[1;31m"
3738
#define GIT_COLOR_BOLD_GREEN "\033[1;32m"
@@ -40,6 +41,7 @@ struct strbuf;
4041
#define GIT_COLOR_BOLD_MAGENTA "\033[1;35m"
4142
#define GIT_COLOR_BOLD_CYAN "\033[1;36m"
4243
#define GIT_COLOR_BOLD_WHITE "\033[1;37m"
44+
#define GIT_COLOR_BOLD_DEFAULT "\033[1;39m"
4345
#define GIT_COLOR_FAINT_BLACK "\033[2;30m"
4446
#define GIT_COLOR_FAINT_RED "\033[2;31m"
4547
#define GIT_COLOR_FAINT_GREEN "\033[2;32m"
@@ -48,6 +50,7 @@ struct strbuf;
4850
#define GIT_COLOR_FAINT_MAGENTA "\033[2;35m"
4951
#define GIT_COLOR_FAINT_CYAN "\033[2;36m"
5052
#define GIT_COLOR_FAINT_WHITE "\033[2;37m"
53+
#define GIT_COLOR_FAINT_DEFAULT "\033[2;39m"
5154
#define GIT_COLOR_BG_BLACK "\033[40m"
5255
#define GIT_COLOR_BG_RED "\033[41m"
5356
#define GIT_COLOR_BG_GREEN "\033[42m"
@@ -56,6 +59,7 @@ struct strbuf;
5659
#define GIT_COLOR_BG_MAGENTA "\033[45m"
5760
#define GIT_COLOR_BG_CYAN "\033[46m"
5861
#define GIT_COLOR_BG_WHITE "\033[47m"
62+
#define GIT_COLOR_BG_DEFAULT "\033[49m"
5963
#define GIT_COLOR_FAINT "\033[2m"
6064
#define GIT_COLOR_FAINT_ITALIC "\033[2;3m"
6165
#define GIT_COLOR_REVERSE "\033[7m"

t/t4026-color.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@ test_expect_success '24-bit colors' '
9696
color "#ff00ff black" "[38;2;255;0;255;40m"
9797
'
9898

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+
99111
test_expect_success '"normal" yields no color at all"' '
100112
color "normal black" "[40m"
101113
'

0 commit comments

Comments
 (0)