Skip to content

Commit de65851

Browse files
rwegitster
authored andcommitted
color: allow colors to be prefixed with "reset"
"reset" was previously treated as a standalone special color name representing `\e[m`. Now, it can apply to other color properties, allowing exact specifications without implicit attribute inheritance. For example, "reset green" now renders `\e[;32m`, which is interpreted as "reset everything; then set foreground to green". This means the background and other attributes are also reset to their defaults. Previously, this was impossible to represent in a single color: "reset" could be specified alone, or a color with attributes, but some thing like clearing a background color were impossible. There is a separate change that introduces the "default" color name to assist with that, but even then, the above could only to be represented by explicitly disabling each of the attributes: green default no-bold no-dim no-italic no-ul no-blink no-reverse no-strike Signed-off-by: Robert Estelle <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 05f1f41 commit de65851

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

Documentation/config.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,11 @@ The position of any attributes with respect to the colors
288288
be turned off by prefixing them with `no` or `no-` (e.g., `noreverse`,
289289
`no-ul`, etc).
290290
+
291+
The pseudo-attribute `reset` resets all colors and attributes before
292+
applying the specified coloring. For example, `reset green` will result
293+
in a green foreground and default background without any active
294+
attributes.
295+
+
291296
An empty color string produces no color effect at all. This can be used
292297
to avoid coloring specific elements without disabling color entirely.
293298
+

color.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ int color_parse_mem(const char *value, int value_len, char *dst)
255255
const char *ptr = value;
256256
int len = value_len;
257257
char *end = dst + COLOR_MAXLEN;
258+
unsigned int has_reset = 0;
258259
unsigned int attr = 0;
259260
struct color fg = { COLOR_UNSPECIFIED };
260261
struct color bg = { COLOR_UNSPECIFIED };
@@ -269,12 +270,7 @@ int color_parse_mem(const char *value, int value_len, char *dst)
269270
return 0;
270271
}
271272

272-
if (!strncasecmp(ptr, "reset", len)) {
273-
xsnprintf(dst, end - dst, GIT_COLOR_RESET);
274-
return 0;
275-
}
276-
277-
/* [fg [bg]] [attr]... */
273+
/* [reset] [fg [bg]] [attr]... */
278274
while (len > 0) {
279275
const char *word = ptr;
280276
struct color c = { COLOR_UNSPECIFIED };
@@ -291,6 +287,11 @@ int color_parse_mem(const char *value, int value_len, char *dst)
291287
len--;
292288
}
293289

290+
if (match_word(word, wordlen, "reset")) {
291+
has_reset = 1;
292+
continue;
293+
}
294+
294295
if (!parse_color(&c, word, wordlen)) {
295296
if (fg.type == COLOR_UNSPECIFIED) {
296297
fg = c;
@@ -316,13 +317,16 @@ int color_parse_mem(const char *value, int value_len, char *dst)
316317
*dst++ = (x); \
317318
} while(0)
318319

319-
if (attr || !color_empty(&fg) || !color_empty(&bg)) {
320+
if (has_reset || attr || !color_empty(&fg) || !color_empty(&bg)) {
320321
int sep = 0;
321322
int i;
322323

323324
OUT('\033');
324325
OUT('[');
325326

327+
if (has_reset)
328+
sep++;
329+
326330
for (i = 0; attr; i++) {
327331
unsigned bit = (1 << i);
328332
if (!(attr & bit))

color.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ struct strbuf;
66
/*
77
* The maximum length of ANSI color sequence we would generate:
88
* - leading ESC '[' 2
9+
* - reset ';' .................1
910
* - attr + ';' 2 * num_attr (e.g. "1;")
1011
* - no-attr + ';' 3 * num_attr (e.g. "22;")
1112
* - fg color + ';' 17 (e.g. "38;2;255;255;255;")

t/t4026-color.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ test_expect_success 'fg bg attr...' '
6060
color "blue bold dim ul blink reverse" "[1;2;4;5;7;34m"
6161
'
6262

63+
test_expect_success 'reset fg bg attr...' '
64+
color "reset blue bold dim ul blink reverse" "[;1;2;4;5;7;34m"
65+
'
66+
6367
# note that nobold and nodim are the same code (22)
6468
test_expect_success 'attr negation' '
6569
color "nobold nodim noul noblink noreverse" "[22;24;25;27m"

0 commit comments

Comments
 (0)