diff: Support both attributes and colors

Make it possible to set both colors and a attribute for diff colors.
Background colors are supported too.

Syntax is now:

	[attr] [fg [bg]]
	[fg [bg]] [attr]

Empty value is same as "normal normal", ie use default colors.  The new
syntax is backwards compatible.

Signed-off-by: Timo Hirvonen <tihirvon@gmail.com>
Signed-off-by: Junio C Hamano <junkio@cox.net>
This commit is contained in:
Timo Hirvonen 2006-07-13 19:06:12 +03:00 committed by Junio C Hamano
parent ff4d78041e
commit f37399e6b0

162
diff.c
View File

@ -26,30 +26,14 @@ enum color_diff {
DIFF_FILE_NEW = 5, DIFF_FILE_NEW = 5,
}; };
#define COLOR_NORMAL "" /* "\033[1;30;47m\0" is 11 bytes */
#define COLOR_BOLD "\033[1m" static char diff_colors[][16] = {
#define COLOR_DIM "\033[2m" "\033[m", /* reset */
#define COLOR_UL "\033[4m" "", /* normal */
#define COLOR_BLINK "\033[5m" "\033[1m", /* bold */
#define COLOR_REVERSE "\033[7m" "\033[36m", /* cyan */
#define COLOR_RESET "\033[m" "\033[31m", /* red */
"\033[32m" /* green */
#define COLOR_BLACK "\033[30m"
#define COLOR_RED "\033[31m"
#define COLOR_GREEN "\033[32m"
#define COLOR_YELLOW "\033[33m"
#define COLOR_BLUE "\033[34m"
#define COLOR_MAGENTA "\033[35m"
#define COLOR_CYAN "\033[36m"
#define COLOR_WHITE "\033[37m"
static const char *diff_colors[] = {
COLOR_RESET,
COLOR_NORMAL,
COLOR_BOLD,
COLOR_CYAN,
COLOR_RED,
COLOR_GREEN
}; };
static int parse_diff_color_slot(const char *var, int ofs) static int parse_diff_color_slot(const char *var, int ofs)
@ -67,38 +51,104 @@ static int parse_diff_color_slot(const char *var, int ofs)
die("bad config variable '%s'", var); die("bad config variable '%s'", var);
} }
static const char *parse_diff_color_value(const char *value, const char *var) static int parse_color(const char *name, int len)
{ {
if (!strcasecmp(value, "normal")) static const char * const color_names[] = {
return COLOR_NORMAL; "normal", "black", "red", "green", "yellow",
if (!strcasecmp(value, "bold")) "blue", "magenta", "cyan", "white"
return COLOR_BOLD; };
if (!strcasecmp(value, "dim")) int i;
return COLOR_DIM; for (i = 0; i < ARRAY_SIZE(color_names); i++) {
if (!strcasecmp(value, "ul")) const char *str = color_names[i];
return COLOR_UL; if (!strncasecmp(name, str, len) && !str[len])
if (!strcasecmp(value, "blink")) return i - 1;
return COLOR_BLINK; }
if (!strcasecmp(value, "reverse")) return -2;
return COLOR_REVERSE; }
if (!strcasecmp(value, "reset"))
return COLOR_RESET; static int parse_attr(const char *name, int len)
if (!strcasecmp(value, "black")) {
return COLOR_BLACK; static const int attr_values[] = { 1, 2, 4, 5, 7 };
if (!strcasecmp(value, "red")) static const char * const attr_names[] = {
return COLOR_RED; "bold", "dim", "ul", "blink", "reverse"
if (!strcasecmp(value, "green")) };
return COLOR_GREEN; int i;
if (!strcasecmp(value, "yellow")) for (i = 0; i < ARRAY_SIZE(attr_names); i++) {
return COLOR_YELLOW; const char *str = attr_names[i];
if (!strcasecmp(value, "blue")) if (!strncasecmp(name, str, len) && !str[len])
return COLOR_BLUE; return attr_values[i];
if (!strcasecmp(value, "magenta")) }
return COLOR_MAGENTA; return -1;
if (!strcasecmp(value, "cyan")) }
return COLOR_CYAN;
if (!strcasecmp(value, "white")) static void parse_diff_color_value(const char *value, const char *var, char *dst)
return COLOR_WHITE; {
const char *ptr = value;
int attr = -1;
int fg = -2;
int bg = -2;
if (!strcasecmp(value, "reset")) {
strcpy(dst, "\033[m");
return;
}
/* [fg [bg]] [attr] */
while (*ptr) {
const char *word = ptr;
int val, len = 0;
while (word[len] && !isspace(word[len]))
len++;
ptr = word + len;
while (*ptr && isspace(*ptr))
ptr++;
val = parse_color(word, len);
if (val >= -1) {
if (fg == -2) {
fg = val;
continue;
}
if (bg == -2) {
bg = val;
continue;
}
goto bad;
}
val = parse_attr(word, len);
if (val < 0 || attr != -1)
goto bad;
attr = val;
}
if (attr >= 0 || fg >= 0 || bg >= 0) {
int sep = 0;
*dst++ = '\033';
*dst++ = '[';
if (attr >= 0) {
*dst++ = '0' + attr;
sep++;
}
if (fg >= 0) {
if (sep++)
*dst++ = ';';
*dst++ = '3';
*dst++ = '0' + fg;
}
if (bg >= 0) {
if (sep++)
*dst++ = ';';
*dst++ = '4';
*dst++ = '0' + bg;
}
*dst++ = 'm';
}
*dst = 0;
return;
bad:
die("bad config value '%s' for variable '%s'", value, var); die("bad config value '%s' for variable '%s'", value, var);
} }
@ -145,7 +195,7 @@ int git_diff_ui_config(const char *var, const char *value)
} }
if (!strncmp(var, "diff.color.", 11)) { if (!strncmp(var, "diff.color.", 11)) {
int slot = parse_diff_color_slot(var, 11); int slot = parse_diff_color_slot(var, 11);
diff_colors[slot] = parse_diff_color_value(value, var); parse_diff_color_value(value, var, diff_colors[slot]);
return 0; return 0;
} }
return git_default_config(var, value); return git_default_config(var, value);