Skip to content

Commit 00588bb

Browse files
MarkLodatogitster
authored andcommitted
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>
1 parent 55f638b commit 00588bb

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

Documentation/config.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,12 +688,18 @@ color.grep.<slot>::
688688
part of the line to use the specified color, and is one of
689689
+
690690
--
691+
`context`;;
692+
non-matching text in context lines (when using `-A`, `-B`, or `-C`)
691693
`filename`;;
692694
filename prefix (when not using `-h`)
695+
`function`;;
696+
function name lines (when using `-p`)
693697
`linenumber`;;
694698
line number prefix (when using `-n`)
695699
`match`;;
696700
matching text
701+
`selected`;;
702+
non-matching text in selected lines
697703
`separator`;;
698704
separators between fields on a line (`:`, `-`, and `=`)
699705
and between hunks (`--`)

builtin-grep.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,18 @@ static int grep_config(const char *var, const char *value, void *cb)
299299

300300
if (!strcmp(var, "color.grep"))
301301
opt->color = git_config_colorbool(var, value, -1);
302+
else if (!strcmp(var, "color.grep.context"))
303+
color = opt->color_context;
302304
else if (!strcmp(var, "color.grep.filename"))
303305
color = opt->color_filename;
306+
else if (!strcmp(var, "color.grep.function"))
307+
color = opt->color_function;
304308
else if (!strcmp(var, "color.grep.linenumber"))
305309
color = opt->color_lineno;
306310
else if (!strcmp(var, "color.grep.match"))
307311
color = opt->color_match;
312+
else if (!strcmp(var, "color.grep.selected"))
313+
color = opt->color_selected;
308314
else if (!strcmp(var, "color.grep.separator"))
309315
color = opt->color_sep;
310316
else
@@ -879,9 +885,12 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
879885
opt.regflags = REG_NEWLINE;
880886
opt.max_depth = -1;
881887

888+
strcpy(opt.color_context, "");
882889
strcpy(opt.color_filename, "");
890+
strcpy(opt.color_function, "");
883891
strcpy(opt.color_lineno, "");
884892
strcpy(opt.color_match, GIT_COLOR_BOLD_RED);
893+
strcpy(opt.color_selected, "");
885894
strcpy(opt.color_sep, GIT_COLOR_CYAN);
886895
opt.color = -1;
887896
git_config(grep_config, &opt);

grep.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
529529
const char *name, unsigned lno, char sign)
530530
{
531531
int rest = eol - bol;
532+
char *line_color = NULL;
532533

533534
if (opt->pre_context || opt->post_context) {
534535
if (opt->last_shown == 0) {
@@ -560,12 +561,18 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
560561
int ch = *eol;
561562
int eflags = 0;
562563

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;
563570
*eol = '\0';
564571
while (next_match(opt, bol, eol, ctx, &match, eflags)) {
565572
if (match.rm_so == match.rm_eo)
566573
break;
567574

568-
opt->output(opt, bol, match.rm_so);
575+
output_color(opt, bol, match.rm_so, line_color);
569576
output_color(opt, bol + match.rm_so,
570577
match.rm_eo - match.rm_so,
571578
opt->color_match);
@@ -575,7 +582,7 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
575582
}
576583
*eol = ch;
577584
}
578-
opt->output(opt, bol, rest);
585+
output_color(opt, bol, rest, line_color);
579586
opt->output(opt, "\n", 1);
580587
}
581588

grep.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,12 @@ struct grep_opt {
8484
int color;
8585
int max_depth;
8686
int funcname;
87+
char color_context[COLOR_MAXLEN];
8788
char color_filename[COLOR_MAXLEN];
89+
char color_function[COLOR_MAXLEN];
8890
char color_lineno[COLOR_MAXLEN];
8991
char color_match[COLOR_MAXLEN];
92+
char color_selected[COLOR_MAXLEN];
9093
char color_sep[COLOR_MAXLEN];
9194
int regflags;
9295
unsigned pre_context;

0 commit comments

Comments
 (0)