grep: Colorize selected, context, and function lines
Colorize non-matching text of selected lines, context lines, and function name lines. The default for all three is no color, but they can be configured using color.grep.<slot>. The first two are similar to the corresponding options in GNU grep, except that GNU grep applies the color to the entire line, not just non-matching text. Signed-off-by: Mark Lodato <lodatom@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Mark Lodato committed
Mar 7, 2010 at 11:52 UTC
00588bb5cd4a7ff5e2b1ded97d4459bfe6aad6eb
4 files changed
+27
-2
Documentation/config.txt
+6
@@ -688,12 +688,18 @@ color.grep.<slot>::
688
part of the line to use the specified color, and is one of
689
+
690
--
691
+`context`;;
692
+ non-matching text in context lines (when using `-A`, `-B`, or `-C`)
693
`filename`;;
694
filename prefix (when not using `-h`)
695
+`function`;;
696
+ function name lines (when using `-p`)
697
`linenumber`;;
698
line number prefix (when using `-n`)
699
`match`;;
700
matching text
701
+`selected`;;
702
+ non-matching text in selected lines
703
`separator`;;
704
separators between fields on a line (`:`, `-`, and `=`)
705
and between hunks (`--`)
builtin-grep.c
+9
@@ -299,12 +299,18 @@ static int grep_config(const char *var, const char *value, void *cb)
299
300
if (!strcmp(var, "color.grep"))
301
opt->color = git_config_colorbool(var, value, -1);
302
+ else if (!strcmp(var, "color.grep.context"))
303
+ color = opt->color_context;
304
else if (!strcmp(var, "color.grep.filename"))
305
color = opt->color_filename;
306
+ else if (!strcmp(var, "color.grep.function"))
307
+ color = opt->color_function;
308
else if (!strcmp(var, "color.grep.linenumber"))
309
color = opt->color_lineno;
310
else if (!strcmp(var, "color.grep.match"))
311
color = opt->color_match;
312
+ else if (!strcmp(var, "color.grep.selected"))
313
+ color = opt->color_selected;
314
else if (!strcmp(var, "color.grep.separator"))
315
color = opt->color_sep;
316
else
@@ -879,9 +885,12 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
885
opt.regflags = REG_NEWLINE;
886
opt.max_depth = -1;
887
888
+ strcpy(opt.color_context, "");
889
strcpy(opt.color_filename, "");
890
+ strcpy(opt.color_function, "");
891
strcpy(opt.color_lineno, "");
892
strcpy(opt.color_match, GIT_COLOR_BOLD_RED);
893
+ strcpy(opt.color_selected, "");
894
strcpy(opt.color_sep, GIT_COLOR_CYAN);
895
opt.color = -1;
896
git_config(grep_config, &opt);
grep.c
+9
-2
@@ -529,6 +529,7 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
529
const char *name, unsigned lno, char sign)
530
{
531
int rest = eol - bol;
532
+ char *line_color = NULL;
533
534
if (opt->pre_context || opt->post_context) {
535
if (opt->last_shown == 0) {
@@ -560,12 +561,18 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
561
int ch = *eol;
562
int eflags = 0;
563
564
+ if (sign == ':')
565
+ line_color = opt->color_selected;
566
+ else if (sign == '-')
567
+ line_color = opt->color_context;
568
+ else if (sign == '=')
569
+ line_color = opt->color_function;
570
*eol = '\0';
571
while (next_match(opt, bol, eol, ctx, &match, eflags)) {
572
if (match.rm_so == match.rm_eo)
573
break;
574
568
- opt->output(opt, bol, match.rm_so);
575
+ output_color(opt, bol, match.rm_so, line_color);
576
output_color(opt, bol + match.rm_so,
577
match.rm_eo - match.rm_so,
578
opt->color_match);
@@ -575,7 +582,7 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
582
}
583
*eol = ch;
584
}
578
- opt->output(opt, bol, rest);
585
+ output_color(opt, bol, rest, line_color);
586
opt->output(opt, "\n", 1);
587
}
588
grep.h
+3
@@ -84,9 +84,12 @@ struct grep_opt {
84
int color;
85
int max_depth;
86
int funcname;
87
+ char color_context[COLOR_MAXLEN];
88
char color_filename[COLOR_MAXLEN];
89
+ char color_function[COLOR_MAXLEN];
90
char color_lineno[COLOR_MAXLEN];
91
char color_match[COLOR_MAXLEN];
92
+ char color_selected[COLOR_MAXLEN];
93
char color_sep[COLOR_MAXLEN];
94
int regflags;
95
unsigned pre_context;