Skip to content

Commit f5b571f

Browse files
tihirvonJunio C Hamano
authored andcommitted
diff: Support 256 colors
Add support for more than 8 colors. Colors can be specified as numbers -1..255. -1 is same as "normal". Signed-off-by: Timo Hirvonen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f37399e commit f5b571f

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

diff.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ enum color_diff {
2626
DIFF_FILE_NEW = 5,
2727
};
2828

29-
/* "\033[1;30;47m\0" is 11 bytes */
30-
static char diff_colors[][16] = {
29+
/* "\033[1;38;5;2xx;48;5;2xxm\0" is 23 bytes */
30+
static char diff_colors[][24] = {
3131
"\033[m", /* reset */
3232
"", /* normal */
3333
"\033[1m", /* bold */
@@ -57,12 +57,16 @@ static int parse_color(const char *name, int len)
5757
"normal", "black", "red", "green", "yellow",
5858
"blue", "magenta", "cyan", "white"
5959
};
60+
char *end;
6061
int i;
6162
for (i = 0; i < ARRAY_SIZE(color_names); i++) {
6263
const char *str = color_names[i];
6364
if (!strncasecmp(name, str, len) && !str[len])
6465
return i - 1;
6566
}
67+
i = strtol(name, &end, 10);
68+
if (*name && !*end && i >= -1 && i <= 255)
69+
return i;
6670
return -2;
6771
}
6872

@@ -135,14 +139,22 @@ static void parse_diff_color_value(const char *value, const char *var, char *dst
135139
if (fg >= 0) {
136140
if (sep++)
137141
*dst++ = ';';
138-
*dst++ = '3';
139-
*dst++ = '0' + fg;
142+
if (fg < 8) {
143+
*dst++ = '3';
144+
*dst++ = '0' + fg;
145+
} else {
146+
dst += sprintf(dst, "38;5;%d", fg);
147+
}
140148
}
141149
if (bg >= 0) {
142150
if (sep++)
143151
*dst++ = ';';
144-
*dst++ = '4';
145-
*dst++ = '0' + bg;
152+
if (bg < 8) {
153+
*dst++ = '4';
154+
*dst++ = '0' + bg;
155+
} else {
156+
dst += sprintf(dst, "48;5;%d", bg);
157+
}
146158
}
147159
*dst++ = 'm';
148160
}

0 commit comments

Comments
 (0)