Skip to content

Commit f1aa782

Browse files
committed
Merge branch 'ml/color-grep'
* ml/color-grep: grep: Colorize selected, context, and function lines grep: Colorize filename, line number, and separator Add GIT_COLOR_BOLD_* and GIT_COLOR_BG_*
2 parents d7173d9 + 00588bb commit f1aa782

File tree

6 files changed

+123
-43
lines changed

6 files changed

+123
-43
lines changed

Documentation/config.txt

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -690,9 +690,29 @@ color.grep::
690690
`never`), never. When set to `true` or `auto`, use color only
691691
when the output is written to the terminal. Defaults to `false`.
692692

693-
color.grep.match::
694-
Use customized color for matches. The value of this variable
695-
may be specified as in color.branch.<slot>.
693+
color.grep.<slot>::
694+
Use customized color for grep colorization. `<slot>` specifies which
695+
part of the line to use the specified color, and is one of
696+
+
697+
--
698+
`context`;;
699+
non-matching text in context lines (when using `-A`, `-B`, or `-C`)
700+
`filename`;;
701+
filename prefix (when not using `-h`)
702+
`function`;;
703+
function name lines (when using `-p`)
704+
`linenumber`;;
705+
line number prefix (when using `-n`)
706+
`match`;;
707+
matching text
708+
`selected`;;
709+
non-matching text in selected lines
710+
`separator`;;
711+
separators between fields on a line (`:`, `-`, and `=`)
712+
and between hunks (`--`)
713+
--
714+
+
715+
The values of these variables may be specified as in color.branch.<slot>.
696716

697717
color.interactive::
698718
When set to `always`, always use colors for interactive prompts

builtin/grep.c

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,24 +289,38 @@ static int wait_all(void)
289289
static int grep_config(const char *var, const char *value, void *cb)
290290
{
291291
struct grep_opt *opt = cb;
292+
char *color = NULL;
292293

293294
switch (userdiff_config(var, value)) {
294295
case 0: break;
295296
case -1: return -1;
296297
default: return 0;
297298
}
298299

299-
if (!strcmp(var, "color.grep")) {
300+
if (!strcmp(var, "color.grep"))
300301
opt->color = git_config_colorbool(var, value, -1);
301-
return 0;
302-
}
303-
if (!strcmp(var, "color.grep.match")) {
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
317+
return git_color_default_config(var, value, cb);
318+
if (color) {
304319
if (!value)
305320
return config_error_nonbool(var);
306-
color_parse(value, var, opt->color_match);
307-
return 0;
321+
color_parse(value, var, color);
308322
}
309-
return git_color_default_config(var, value, cb);
323+
return 0;
310324
}
311325

312326
/*
@@ -872,7 +886,13 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
872886
opt.regflags = REG_NEWLINE;
873887
opt.max_depth = -1;
874888

875-
strcpy(opt.color_match, GIT_COLOR_RED GIT_COLOR_BOLD);
889+
strcpy(opt.color_context, "");
890+
strcpy(opt.color_filename, "");
891+
strcpy(opt.color_function, "");
892+
strcpy(opt.color_lineno, "");
893+
strcpy(opt.color_match, GIT_COLOR_BOLD_RED);
894+
strcpy(opt.color_selected, "");
895+
strcpy(opt.color_sep, GIT_COLOR_CYAN);
876896
opt.color = -1;
877897
git_config(grep_config, &opt);
878898
if (opt.color == -1)

color.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,18 @@
3030
#define GIT_COLOR_BLUE "\033[34m"
3131
#define GIT_COLOR_MAGENTA "\033[35m"
3232
#define GIT_COLOR_CYAN "\033[36m"
33+
#define GIT_COLOR_BOLD_RED "\033[1;31m"
34+
#define GIT_COLOR_BOLD_GREEN "\033[1;32m"
35+
#define GIT_COLOR_BOLD_YELLOW "\033[1;33m"
36+
#define GIT_COLOR_BOLD_BLUE "\033[1;34m"
37+
#define GIT_COLOR_BOLD_MAGENTA "\033[1;35m"
38+
#define GIT_COLOR_BOLD_CYAN "\033[1;36m"
3339
#define GIT_COLOR_BG_RED "\033[41m"
40+
#define GIT_COLOR_BG_GREEN "\033[42m"
41+
#define GIT_COLOR_BG_YELLOW "\033[43m"
42+
#define GIT_COLOR_BG_BLUE "\033[44m"
43+
#define GIT_COLOR_BG_MAGENTA "\033[45m"
44+
#define GIT_COLOR_BG_CYAN "\033[46m"
3445

3546
/*
3647
* This variable stores the value of color.ui

graph.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ static char column_colors[][COLOR_MAXLEN] = {
8080
GIT_COLOR_BLUE,
8181
GIT_COLOR_MAGENTA,
8282
GIT_COLOR_CYAN,
83-
GIT_COLOR_BOLD GIT_COLOR_RED,
84-
GIT_COLOR_BOLD GIT_COLOR_GREEN,
85-
GIT_COLOR_BOLD GIT_COLOR_YELLOW,
86-
GIT_COLOR_BOLD GIT_COLOR_BLUE,
87-
GIT_COLOR_BOLD GIT_COLOR_MAGENTA,
88-
GIT_COLOR_BOLD GIT_COLOR_CYAN,
83+
GIT_COLOR_BOLD_RED,
84+
GIT_COLOR_BOLD_GREEN,
85+
GIT_COLOR_BOLD_YELLOW,
86+
GIT_COLOR_BOLD_BLUE,
87+
GIT_COLOR_BOLD_MAGENTA,
88+
GIT_COLOR_BOLD_CYAN,
8989
};
9090

9191
#define COLUMN_COLORS_MAX (ARRAY_SIZE(column_colors))

grep.c

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,28 @@ static int word_char(char ch)
304304
return isalnum(ch) || ch == '_';
305305
}
306306

307+
static void output_color(struct grep_opt *opt, const void *data, size_t size,
308+
const char *color)
309+
{
310+
if (opt->color && color && color[0]) {
311+
opt->output(opt, color, strlen(color));
312+
opt->output(opt, data, size);
313+
opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET));
314+
} else
315+
opt->output(opt, data, size);
316+
}
317+
318+
static void output_sep(struct grep_opt *opt, char sign)
319+
{
320+
if (opt->null_following_name)
321+
opt->output(opt, "\0", 1);
322+
else
323+
output_color(opt, &sign, 1, opt->color_sep);
324+
}
325+
307326
static void show_name(struct grep_opt *opt, const char *name)
308327
{
309-
opt->output(opt, name, strlen(name));
328+
output_color(opt, name, strlen(name), opt->color_filename);
310329
opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
311330
}
312331

@@ -544,57 +563,60 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
544563
const char *name, unsigned lno, char sign)
545564
{
546565
int rest = eol - bol;
547-
char sign_str[1];
566+
char *line_color = NULL;
548567

549-
sign_str[0] = sign;
550568
if (opt->pre_context || opt->post_context) {
551569
if (opt->last_shown == 0) {
552-
if (opt->show_hunk_mark)
553-
opt->output(opt, "--\n", 3);
554-
else
570+
if (opt->show_hunk_mark) {
571+
output_color(opt, "--", 2, opt->color_sep);
572+
opt->output(opt, "\n", 1);
573+
} else
555574
opt->show_hunk_mark = 1;
556-
} else if (lno > opt->last_shown + 1)
557-
opt->output(opt, "--\n", 3);
575+
} else if (lno > opt->last_shown + 1) {
576+
output_color(opt, "--", 2, opt->color_sep);
577+
opt->output(opt, "\n", 1);
578+
}
558579
}
559580
opt->last_shown = lno;
560581

561-
if (opt->null_following_name)
562-
sign_str[0] = '\0';
563582
if (opt->pathname) {
564-
opt->output(opt, name, strlen(name));
565-
opt->output(opt, sign_str, 1);
583+
output_color(opt, name, strlen(name), opt->color_filename);
584+
output_sep(opt, sign);
566585
}
567586
if (opt->linenum) {
568587
char buf[32];
569588
snprintf(buf, sizeof(buf), "%d", lno);
570-
opt->output(opt, buf, strlen(buf));
571-
opt->output(opt, sign_str, 1);
589+
output_color(opt, buf, strlen(buf), opt->color_lineno);
590+
output_sep(opt, sign);
572591
}
573592
if (opt->color) {
574593
regmatch_t match;
575594
enum grep_context ctx = GREP_CONTEXT_BODY;
576595
int ch = *eol;
577596
int eflags = 0;
578597

598+
if (sign == ':')
599+
line_color = opt->color_selected;
600+
else if (sign == '-')
601+
line_color = opt->color_context;
602+
else if (sign == '=')
603+
line_color = opt->color_function;
579604
*eol = '\0';
580605
while (next_match(opt, bol, eol, ctx, &match, eflags)) {
581606
if (match.rm_so == match.rm_eo)
582607
break;
583608

584-
opt->output(opt, bol, match.rm_so);
585-
opt->output(opt, opt->color_match,
586-
strlen(opt->color_match));
587-
opt->output(opt, bol + match.rm_so,
588-
(int)(match.rm_eo - match.rm_so));
589-
opt->output(opt, GIT_COLOR_RESET,
590-
strlen(GIT_COLOR_RESET));
609+
output_color(opt, bol, match.rm_so, line_color);
610+
output_color(opt, bol + match.rm_so,
611+
match.rm_eo - match.rm_so,
612+
opt->color_match);
591613
bol += match.rm_eo;
592614
rest -= match.rm_eo;
593615
eflags = REG_NOTBOL;
594616
}
595617
*eol = ch;
596618
}
597-
opt->output(opt, bol, rest);
619+
output_color(opt, bol, rest, line_color);
598620
opt->output(opt, "\n", 1);
599621
}
600622

@@ -857,7 +879,8 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
857879
return 1;
858880
if (binary_match_only) {
859881
opt->output(opt, "Binary file ", 12);
860-
opt->output(opt, name, strlen(name));
882+
output_color(opt, name, strlen(name),
883+
opt->color_filename);
861884
opt->output(opt, " matches\n", 9);
862885
return 1;
863886
}
@@ -916,9 +939,9 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
916939
*/
917940
if (opt->count && count) {
918941
char buf[32];
919-
opt->output(opt, name, strlen(name));
920-
snprintf(buf, sizeof(buf), "%c%u\n",
921-
opt->null_following_name ? '\0' : ':', count);
942+
output_color(opt, name, strlen(name), opt->color_filename);
943+
output_sep(opt, ':');
944+
snprintf(buf, sizeof(buf), "%u\n", count);
922945
opt->output(opt, buf, strlen(buf));
923946
}
924947
return !!last_hit;

grep.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,13 @@ struct grep_opt {
8686
int color;
8787
int max_depth;
8888
int funcname;
89+
char color_context[COLOR_MAXLEN];
90+
char color_filename[COLOR_MAXLEN];
91+
char color_function[COLOR_MAXLEN];
92+
char color_lineno[COLOR_MAXLEN];
8993
char color_match[COLOR_MAXLEN];
94+
char color_selected[COLOR_MAXLEN];
95+
char color_sep[COLOR_MAXLEN];
9096
int regflags;
9197
unsigned pre_context;
9298
unsigned post_context;

0 commit comments

Comments
 (0)