@matheusbsbs / matheus-bagliano / commits / 50f575fc98
Fork 0

Tweak diff colors

This patch does: - always reset the color _before_ printing out the newline. This is actually important. You (and Johannes) didn't see it, because it only matters if you set the background, but if you don't do this, you get some random and funky behaviour if you pick a color with a non-default background (which still potentially has problems with tabs etc, but less so). - allow people to have a different color for the "file headers" (DIFF_METAINFO) and for the "fragment header" (DIFF_FRAGINFO). Also, make a difference between "normal color" and "reset colors" - default to red/green for old/new lines. That's the norm, I'd think. - instead of that eye-popping (and eye-ball-with-a-fondue-fork-popping) purple color for metadata, use bold-face for file headers, and cyan for the frag headers. I actually prefer the "gray background" for that, but it only works well in xterms, so COLOR_CYAN it is.. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed Jun 22, 2006 at 13:53 UTC 50f575fc9836704d45a5f732125b8f58103425a4
1 file changed +58 -41
diff.c
+58 -41
@@ -26,17 +26,41 @@ int git_diff_config(const char *var, const char *value)
26 }
27
28 enum color_diff {
29 - DIFF_PLAIN = 0,
30 - DIFF_METAINFO = 1,
31 - DIFF_FILE_OLD = 2,
32 - DIFF_FILE_NEW = 3,
29 + DIFF_RESET = 0,
30 + DIFF_PLAIN = 1,
31 + DIFF_METAINFO = 2,
32 + DIFF_FRAGINFO = 3,
33 + DIFF_FILE_OLD = 4,
34 + DIFF_FILE_NEW = 5,
35 };
36
37 +#define COLOR_NORMAL ""
38 +#define COLOR_BOLD "\033[1m"
39 +#define COLOR_DIM "\033[2m"
40 +#define COLOR_UL "\033[4m"
41 +#define COLOR_BLINK "\033[5m"
42 +#define COLOR_REVERSE "\033[7m"
43 +#define COLOR_RESET "\033[m"
44 +
45 +#define COLOR_BLACK "\033[30m"
46 +#define COLOR_RED "\033[31m"
47 +#define COLOR_GREEN "\033[32m"
48 +#define COLOR_YELLOW "\033[33m"
49 +#define COLOR_BLUE "\033[34m"
50 +#define COLOR_MAGENTA "\033[35m"
51 +#define COLOR_CYAN "\033[36m"
52 +#define COLOR_WHITE "\033[37m"
53 +
54 +#define COLOR_CYANBG "\033[46m"
55 +#define COLOR_GRAYBG "\033[47m" // Good for xterm
56 +
57 static const char *diff_colors[] = {
36 - "\033[0;0m",
37 - "\033[1;35m",
38 - "\033[1;31m",
39 - "\033[1;34m",
58 + [DIFF_RESET] = COLOR_RESET,
59 + [DIFF_PLAIN] = COLOR_NORMAL,
60 + [DIFF_METAINFO] = COLOR_BOLD,
61 + [DIFF_FRAGINFO] = COLOR_CYAN,
62 + [DIFF_FILE_OLD] = COLOR_RED,
63 + [DIFF_FILE_NEW] = COLOR_GREEN,
64 };
65
66 static char *quote_one(const char *str)
@@ -196,22 +220,23 @@ struct emit_callback {
220 const char **label_path;
221 };
222
199 -static inline void color_diff(int diff_use_color, enum color_diff ix)
223 +static inline const char *get_color(int diff_use_color, enum color_diff ix)
224 {
225 if (diff_use_color)
202 - fputs(diff_colors[ix], stdout);
226 + return diff_colors[ix];
227 + return "";
228 }
229
230 static void fn_out_consume(void *priv, char *line, unsigned long len)
231 {
232 int i;
233 struct emit_callback *ecbdata = priv;
234 + const char *set = get_color(ecbdata->color_diff, DIFF_METAINFO);
235 + const char *reset = get_color(ecbdata->color_diff, DIFF_RESET);
236
237 if (ecbdata->label_path[0]) {
211 - color_diff(ecbdata->color_diff, DIFF_METAINFO);
212 - printf("--- %s\n", ecbdata->label_path[0]);
213 - color_diff(ecbdata->color_diff, DIFF_METAINFO);
214 - printf("+++ %s\n", ecbdata->label_path[1]);
238 + printf("%s--- %s%s\n", set, ecbdata->label_path[0], reset);
239 + printf("%s+++ %s%s\n", set, ecbdata->label_path[1], reset);
240 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
241 }
242
@@ -222,10 +247,10 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
247 ;
248 if (2 <= i && i < len && line[i] == ' ') {
249 ecbdata->nparents = i - 1;
225 - color_diff(ecbdata->color_diff, DIFF_METAINFO);
250 + set = get_color(ecbdata->color_diff, DIFF_FRAGINFO);
251 }
252 else if (len < ecbdata->nparents)
228 - color_diff(ecbdata->color_diff, DIFF_PLAIN);
253 + set = reset;
254 else {
255 int nparents = ecbdata->nparents;
256 int color = DIFF_PLAIN;
@@ -235,10 +260,11 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
260 else if (line[i] == '+')
261 color = DIFF_FILE_NEW;
262 }
238 - color_diff(ecbdata->color_diff, color);
263 + set = get_color(ecbdata->color_diff, color);
264 }
240 - fwrite(line, len, 1, stdout);
241 - color_diff(ecbdata->color_diff, DIFF_PLAIN);
265 + if (len > 0 && line[len-1] == '\n')
266 + len--;
267 + printf("%s%.*s%s\n", set, (int) len, line, reset);
268 }
269
270 static char *pprint_rename(const char *a, const char *b)
@@ -589,40 +615,32 @@ static void builtin_diff(const char *name_a,
615 mmfile_t mf1, mf2;
616 const char *lbl[2];
617 char *a_one, *b_two;
618 + const char *set = get_color(o->color_diff, DIFF_METAINFO);
619 + const char *reset = get_color(o->color_diff, DIFF_PLAIN);
620
621 a_one = quote_two("a/", name_a);
622 b_two = quote_two("b/", name_b);
623 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
624 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
597 - color_diff(o->color_diff, DIFF_METAINFO);
598 - printf("diff --git %s %s\n", a_one, b_two);
625 + printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
626 if (lbl[0][0] == '/') {
627 /* /dev/null */
601 - color_diff(o->color_diff, DIFF_METAINFO);
602 - printf("new file mode %06o\n", two->mode);
603 - if (xfrm_msg && xfrm_msg[0]) {
604 - color_diff(o->color_diff, DIFF_METAINFO);
605 - puts(xfrm_msg);
606 - }
628 + printf("%snew file mode %06o%s\n", set, two->mode, reset);
629 + if (xfrm_msg && xfrm_msg[0])
630 + printf("%s%s%s\n", set, xfrm_msg, reset);
631 }
632 else if (lbl[1][0] == '/') {
609 - printf("deleted file mode %06o\n", one->mode);
610 - if (xfrm_msg && xfrm_msg[0]) {
611 - color_diff(o->color_diff, DIFF_METAINFO);
612 - puts(xfrm_msg);
613 - }
633 + printf("%sdeleted file mode %06o%s\n", set, one->mode, reset);
634 + if (xfrm_msg && xfrm_msg[0])
635 + printf("%s%s%s\n", set, xfrm_msg, reset);
636 }
637 else {
638 if (one->mode != two->mode) {
617 - color_diff(o->color_diff, DIFF_METAINFO);
618 - printf("old mode %06o\n", one->mode);
619 - color_diff(o->color_diff, DIFF_METAINFO);
620 - printf("new mode %06o\n", two->mode);
621 - }
622 - if (xfrm_msg && xfrm_msg[0]) {
623 - color_diff(o->color_diff, DIFF_METAINFO);
624 - puts(xfrm_msg);
639 + printf("%sold mode %06o%s\n", set, one->mode, reset);
640 + printf("%snew mode %06o%s\n", set, two->mode, reset);
641 }
642 + if (xfrm_msg && xfrm_msg[0])
643 + printf("%s%s%s\n", set, xfrm_msg, reset);
644 /*
645 * we do not run diff between different kind
646 * of objects.
@@ -630,7 +648,6 @@ static void builtin_diff(const char *name_a,
648 if ((one->mode ^ two->mode) & S_IFMT)
649 goto free_ab_and_return;
650 if (complete_rewrite) {
633 - color_diff(o->color_diff, DIFF_PLAIN);
651 emit_rewrite_diff(name_a, name_b, one, two);
652 goto free_ab_and_return;
653 }