Skip to content

Commit ff40d18

Browse files
peffgitster
authored andcommitted
parse_color: recognize "no$foo" to clear the $foo attribute
You can turn on ANSI text attributes like "reverse" by putting "reverse" in your color spec. However, you cannot ask to turn reverse off. For common cases, this does not matter. You would turn on "reverse" at the start of a colored section, and then clear all attributes with a "reset". However, you may wish to turn on some attributes, then selectively disable others. For example: git log --format="%C(bold ul yellow)%h%C(noul) %s" underlines just the hash, but without the need to re-specify the rest of the attributes. This can also help third-party programs, like contrib/diff-highlight, that want to turn some attribute on/off without disrupting existing coloring. Note that some attribute specifications are probably nonsensical (e.g., "bold nobold"). We do not bother to flag such constructs, and instead let the terminal sort it out. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 17a4be2 commit ff40d18

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

Documentation/config.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,8 @@ accepted are `normal`, `black`, `red`, `green`, `yellow`, `blue`,
824824
`magenta`, `cyan` and `white`; the attributes are `bold`, `dim`, `ul`,
825825
`blink` and `reverse`. The first color given is the foreground; the
826826
second is the background. The position of the attribute, if any,
827-
doesn't matter.
827+
doesn't matter. Attributes may be turned off specifically by prefixing
828+
them with `no` (e.g., `noreverse`, `noul`, etc).
828829
+
829830
Colors (foreground and background) may also be given as numbers between
830831
0 and 255; these use ANSI 256-color mode (but note that not all

color.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,11 @@ static int parse_color(struct color *out, const char *name, int len)
124124

125125
static int parse_attr(const char *name, int len)
126126
{
127-
static const int attr_values[] = { 1, 2, 4, 5, 7 };
127+
static const int attr_values[] = { 1, 2, 4, 5, 7,
128+
22, 22, 24, 25, 27 };
128129
static const char * const attr_names[] = {
129-
"bold", "dim", "ul", "blink", "reverse"
130+
"bold", "dim", "ul", "blink", "reverse",
131+
"nobold", "nodim", "noul", "noblink", "noreverse"
130132
};
131133
int i;
132134
for (i = 0; i < ARRAY_SIZE(attr_names); i++) {
@@ -238,7 +240,7 @@ int color_parse_mem(const char *value, int value_len, char *dst)
238240
attr &= ~bit;
239241
if (sep++)
240242
*dst++ = ';';
241-
*dst++ = '0' + i;
243+
dst += sprintf(dst, "%d", i);
242244
}
243245
if (!color_empty(&fg)) {
244246
if (sep++)

color.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ struct strbuf;
88
/*
99
* The maximum length of ANSI color sequence we would generate:
1010
* - leading ESC '[' 2
11-
* - attr + ';' 2 * 8 (e.g. "1;")
11+
* - attr + ';' 3 * 10 (e.g. "1;")
1212
* - fg color + ';' 17 (e.g. "38;2;255;255;255;")
1313
* - bg color + ';' 17 (e.g. "48;2;255;255;255;")
1414
* - terminating 'm' NUL 2
1515
*
1616
* The above overcounts attr (we only use 5 not 8) and one semicolon
1717
* but it is close enough.
1818
*/
19-
#define COLOR_MAXLEN 56
19+
#define COLOR_MAXLEN 70
2020

2121
/*
2222
* IMPORTANT: Due to the way these color codes are emulated on Windows,

t/t4026-color.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,21 @@ test_expect_success 'fg bg attr...' '
4545
color "blue bold dim ul blink reverse" "[1;2;4;5;7;34m"
4646
'
4747

48+
# note that nobold and nodim are the same code (22)
49+
test_expect_success 'attr negation' '
50+
color "nobold nodim noul noblink noreverse" "[22;24;25;27m"
51+
'
52+
4853
test_expect_success 'long color specification' '
4954
color "254 255 bold dim ul blink reverse" "[1;2;4;5;7;38;5;254;48;5;255m"
5055
'
5156

57+
test_expect_success 'absurdly long color specification' '
58+
color \
59+
"#ffffff #ffffff bold nobold dim nodim ul noul blink noblink reverse noreverse" \
60+
"[1;2;4;5;7;22;24;25;27;38;2;255;255;255;48;2;255;255;255m"
61+
'
62+
5263
test_expect_success '256 colors' '
5364
color "254 bold 255" "[1;38;5;254;48;5;255m"
5465
'

0 commit comments

Comments
 (0)