color.c: support bright aixterm colors
These colors are the bright variants of the 3-bit colors. Instead of 30-37 range for the foreground and 40-47 range for the background, they live in 90-97 and 100-107 range, respectively. Signed-off-by: Eyal Soha <shawarmakarma@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Eyal Soha committed
Jan 21, 2020 at 08:56 UTC
1751b09a92e176b164a3c5527f0458d5b1154d00
3 files changed
+39
-9
Documentation/config.txt
+3
-1
@@ -263,7 +263,9 @@ color::
263
+
264
The basic colors accepted are `normal`, `black`, `red`, `green`, `yellow`,
265
`blue`, `magenta`, `cyan` and `white`. The first color given is the
266
-foreground; the second is the background.
266
+foreground; the second is the background. All the basic colors except
267
+`normal` have a bright variant that can be speficied by prefixing the
268
+color with `bright`, like `brightred`.
269
+
270
Colors may also be given as numbers between 0 and 255; these use ANSI
271
256-color mode (but note that not all terminals may support this). If
color.c
+28
-8
@@ -29,6 +29,7 @@ enum {
29
COLOR_FOREGROUND_ANSI = 30,
30
COLOR_FOREGROUND_RGB = 38,
31
COLOR_FOREGROUND_256 = 38,
32
+ COLOR_FOREGROUND_BRIGHT_ANSI = 90,
33
};
34
35
/* Ignore the RESET at the end when giving the size */
@@ -68,15 +69,38 @@ static int get_hex_color(const char *in, unsigned char *out)
69
return 0;
70
}
71
71
-static int parse_color(struct color *out, const char *name, int len)
72
+/*
73
+ * If an ANSI color is recognized in "name", fill "out" and return 0.
74
+ * Otherwise, leave out unchanged and return -1.
75
+ */
76
+static int parse_ansi_color(struct color *out, const char *name, int len)
77
{
78
/* Positions in array must match ANSI color codes */
79
static const char * const color_names[] = {
80
"black", "red", "green", "yellow",
81
"blue", "magenta", "cyan", "white"
82
};
78
- char *end;
83
int i;
84
+ int color_offset = COLOR_FOREGROUND_ANSI;
85
+
86
+ if (strncasecmp(name, "bright", 6) == 0) {
87
+ color_offset = COLOR_FOREGROUND_BRIGHT_ANSI;
88
+ name += 6;
89
+ len -= 6;
90
+ }
91
+ for (i = 0; i < ARRAY_SIZE(color_names); i++) {
92
+ if (match_word(name, len, color_names[i])) {
93
+ out->type = COLOR_ANSI;
94
+ out->value = i + color_offset;
95
+ return 0;
96
+ }
97
+ }
98
+ return -1;
99
+}
100
+
101
+static int parse_color(struct color *out, const char *name, int len)
102
+{
103
+ char *end;
104
long val;
105
106
/* First try the special word "normal"... */
@@ -96,12 +120,8 @@ static int parse_color(struct color *out, const char *name, int len)
120
}
121
122
/* Then pick from our human-readable color names... */
99
- for (i = 0; i < ARRAY_SIZE(color_names); i++) {
100
- if (match_word(name, len, color_names[i])) {
101
- out->type = COLOR_ANSI;
102
- out->value = i + COLOR_FOREGROUND_ANSI;
103
- return 0;
104
- }
123
+ if (parse_ansi_color(out, name, len) == 0) {
124
+ return 0;
125
}
126
127
/* And finally try a literal 256-color-mode number */
t/t4026-color.sh
+8
@@ -30,6 +30,14 @@ test_expect_success 'attribute before color name' '
30
color "bold red" "[1;31m"
31
'
32
33
+test_expect_success 'aixterm bright fg color' '
34
+ color "brightred" "[91m"
35
+'
36
+
37
+test_expect_success 'aixterm bright bg color' '
38
+ color "green brightblue" "[32;104m"
39
+'
40
+
41
test_expect_success 'color name before attribute' '
42
color "red bold" "[1;31m"
43
'