diff --stat: color output.
Under --color option, diffstat shows '+' and '-' in the graph the same color as added and deleted lines. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Sep 26, 2006 at 18:59 UTC
785f743276991567a36420f069c228e4dc9d0df3
1 file changed
+19
-10
diff.c
+19
-10
@@ -555,17 +555,20 @@ static int scale_linear(int it, int width, int max_change)
555
return (it * width * 2 + max_change) / (max_change * 2);
556
}
557
558
-static void show_name(const char *prefix, const char *name, int len)
558
+static void show_name(const char *prefix, const char *name, int len,
559
+ const char *reset, const char *set)
560
{
560
- printf(" %s%-*s |", prefix, len, name);
561
+ printf(" %s%s%-*s%s |", set, prefix, len, name, reset);
562
}
563
563
-static void show_graph(char ch, int cnt)
564
+static void show_graph(char ch, int cnt, const char *set, const char *reset)
565
{
566
if (cnt <= 0)
567
return;
568
+ printf("%s", set);
569
while (cnt--)
570
putchar(ch);
571
+ printf("%s", reset);
572
}
573
574
static void show_stats(struct diffstat_t* data, struct diff_options *options)
@@ -574,6 +577,7 @@ static void show_stats(struct diffstat_t* data, struct diff_options *options)
577
int max_change = 0, max_len = 0;
578
int total_files = data->nr;
579
int width, name_width;
580
+ const char *reset, *set, *add_c, *del_c;
581
582
if (data->nr == 0)
583
return;
@@ -592,6 +596,11 @@ static void show_stats(struct diffstat_t* data, struct diff_options *options)
596
}
597
598
/* Find the longest filename and max number of changes */
599
+ reset = diff_get_color(options->color_diff, DIFF_RESET);
600
+ set = diff_get_color(options->color_diff, DIFF_PLAIN);
601
+ add_c = diff_get_color(options->color_diff, DIFF_FILE_NEW);
602
+ del_c = diff_get_color(options->color_diff, DIFF_FILE_OLD);
603
+
604
for (i = 0; i < data->nr; i++) {
605
struct diffstat_file *file = data->files[i];
606
int change = file->added + file->deleted;
@@ -650,12 +659,12 @@ static void show_stats(struct diffstat_t* data, struct diff_options *options)
659
}
660
661
if (data->files[i]->is_binary) {
653
- show_name(prefix, name, len);
662
+ show_name(prefix, name, len, reset, set);
663
printf(" Bin\n");
664
goto free_diffstat_file;
665
}
666
else if (data->files[i]->is_unmerged) {
658
- show_name(prefix, name, len);
667
+ show_name(prefix, name, len, reset, set);
668
printf(" Unmerged\n");
669
goto free_diffstat_file;
670
}
@@ -679,18 +688,18 @@ static void show_stats(struct diffstat_t* data, struct diff_options *options)
688
add = scale_linear(add, width, max_change);
689
del = total - add;
690
}
682
- show_name(prefix, name, len);
691
+ show_name(prefix, name, len, reset, set);
692
printf("%5d ", added + deleted);
684
- show_graph('+', add);
685
- show_graph('-', del);
693
+ show_graph('+', add, add_c, reset);
694
+ show_graph('-', del, del_c, reset);
695
putchar('\n');
696
free_diffstat_file:
697
free(data->files[i]->name);
698
free(data->files[i]);
699
}
700
free(data->files);
692
- printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
693
- total_files, adds, dels);
701
+ printf("%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
702
+ set, total_files, adds, dels, reset);
703
}
704
705
struct checkdiff_t {