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 <tihirvon@gmail.com>
Signed-off-by: Junio C Hamano <junkio@cox.net>
maint
Timo Hirvonen 2006-07-13 19:08:06 +03:00 committed by Junio C Hamano
parent f37399e6b0
commit f5b571fcf7
1 changed files with 18 additions and 6 deletions

24
diff.c
View File

@ -26,8 +26,8 @@ enum color_diff {
DIFF_FILE_NEW = 5, DIFF_FILE_NEW = 5,
}; };


/* "\033[1;30;47m\0" is 11 bytes */ /* "\033[1;38;5;2xx;48;5;2xxm\0" is 23 bytes */
static char diff_colors[][16] = { static char diff_colors[][24] = {
"\033[m", /* reset */ "\033[m", /* reset */
"", /* normal */ "", /* normal */
"\033[1m", /* bold */ "\033[1m", /* bold */
@ -57,12 +57,16 @@ static int parse_color(const char *name, int len)
"normal", "black", "red", "green", "yellow", "normal", "black", "red", "green", "yellow",
"blue", "magenta", "cyan", "white" "blue", "magenta", "cyan", "white"
}; };
char *end;
int i; int i;
for (i = 0; i < ARRAY_SIZE(color_names); i++) { for (i = 0; i < ARRAY_SIZE(color_names); i++) {
const char *str = color_names[i]; const char *str = color_names[i];
if (!strncasecmp(name, str, len) && !str[len]) if (!strncasecmp(name, str, len) && !str[len])
return i - 1; return i - 1;
} }
i = strtol(name, &end, 10);
if (*name && !*end && i >= -1 && i <= 255)
return i;
return -2; return -2;
} }


@ -135,14 +139,22 @@ static void parse_diff_color_value(const char *value, const char *var, char *dst
if (fg >= 0) { if (fg >= 0) {
if (sep++) if (sep++)
*dst++ = ';'; *dst++ = ';';
*dst++ = '3'; if (fg < 8) {
*dst++ = '0' + fg; *dst++ = '3';
*dst++ = '0' + fg;
} else {
dst += sprintf(dst, "38;5;%d", fg);
}
} }
if (bg >= 0) { if (bg >= 0) {
if (sep++) if (sep++)
*dst++ = ';'; *dst++ = ';';
*dst++ = '4'; if (bg < 8) {
*dst++ = '0' + bg; *dst++ = '4';
*dst++ = '0' + bg;
} else {
dst += sprintf(dst, "48;5;%d", bg);
}
} }
*dst++ = 'm'; *dst++ = 'm';
} }