grep: keep all colors in an array
This is more inline with how we handle color slots in other code. It also allows us to get the list of configurable color slots later. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
May 26, 2018 at 15:55 UTC
fa151dc54dcf7ff32727d5b6fdf78093185ff1cc
2 files changed
+62
-65
grep.c
+49
-57
@@ -13,6 +13,17 @@ static int grep_source_is_binary(struct grep_source *gs);
13
14
static struct grep_opt grep_defaults;
15
16
+static const char *color_grep_slots[] = {
17
+ [GREP_COLOR_CONTEXT] = "context",
18
+ [GREP_COLOR_FILENAME] = "filename",
19
+ [GREP_COLOR_FUNCTION] = "function",
20
+ [GREP_COLOR_LINENO] = "lineNumber",
21
+ [GREP_COLOR_MATCH_CONTEXT] = "matchContext",
22
+ [GREP_COLOR_MATCH_SELECTED] = "matchSelected",
23
+ [GREP_COLOR_SELECTED] = "selected",
24
+ [GREP_COLOR_SEP] = "separator",
25
+};
26
+
27
static void std_output(struct grep_opt *opt, const void *buf, size_t size)
28
{
29
fwrite(buf, size, 1, stdout);
@@ -42,14 +53,14 @@ void init_grep_defaults(void)
53
opt->pathname = 1;
54
opt->max_depth = -1;
55
opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
45
- color_set(opt->color_context, "");
46
- color_set(opt->color_filename, "");
47
- color_set(opt->color_function, "");
48
- color_set(opt->color_lineno, "");
49
- color_set(opt->color_match_context, GIT_COLOR_BOLD_RED);
50
- color_set(opt->color_match_selected, GIT_COLOR_BOLD_RED);
51
- color_set(opt->color_selected, "");
52
- color_set(opt->color_sep, GIT_COLOR_CYAN);
56
+ color_set(opt->colors[GREP_COLOR_CONTEXT], "");
57
+ color_set(opt->colors[GREP_COLOR_FILENAME], "");
58
+ color_set(opt->colors[GREP_COLOR_FUNCTION], "");
59
+ color_set(opt->colors[GREP_COLOR_LINENO], "");
60
+ color_set(opt->colors[GREP_COLOR_MATCH_CONTEXT], GIT_COLOR_BOLD_RED);
61
+ color_set(opt->colors[GREP_COLOR_MATCH_SELECTED], GIT_COLOR_BOLD_RED);
62
+ color_set(opt->colors[GREP_COLOR_SELECTED], "");
63
+ color_set(opt->colors[GREP_COLOR_SEP], GIT_COLOR_CYAN);
64
opt->color = -1;
65
opt->output = std_output;
66
}
@@ -76,7 +87,7 @@ static int parse_pattern_type_arg(const char *opt, const char *arg)
87
int grep_config(const char *var, const char *value, void *cb)
88
{
89
struct grep_opt *opt = &grep_defaults;
79
- char *color = NULL;
90
+ const char *slot;
91
92
if (userdiff_config(var, value) < 0)
93
return -1;
@@ -103,32 +114,18 @@ int grep_config(const char *var, const char *value, void *cb)
114
115
if (!strcmp(var, "color.grep"))
116
opt->color = git_config_colorbool(var, value);
106
- else if (!strcmp(var, "color.grep.context"))
107
- color = opt->color_context;
108
- else if (!strcmp(var, "color.grep.filename"))
109
- color = opt->color_filename;
110
- else if (!strcmp(var, "color.grep.function"))
111
- color = opt->color_function;
112
- else if (!strcmp(var, "color.grep.linenumber"))
113
- color = opt->color_lineno;
114
- else if (!strcmp(var, "color.grep.matchcontext"))
115
- color = opt->color_match_context;
116
- else if (!strcmp(var, "color.grep.matchselected"))
117
- color = opt->color_match_selected;
118
- else if (!strcmp(var, "color.grep.selected"))
119
- color = opt->color_selected;
120
- else if (!strcmp(var, "color.grep.separator"))
121
- color = opt->color_sep;
122
- else if (!strcmp(var, "color.grep.match")) {
123
- int rc = 0;
124
- if (!value)
125
- return config_error_nonbool(var);
126
- rc |= color_parse(value, opt->color_match_context);
127
- rc |= color_parse(value, opt->color_match_selected);
128
- return rc;
129
- }
130
-
131
- if (color) {
117
+ if (!strcmp(var, "color.grep.match")) {
118
+ if (grep_config("color.grep.matchcontext", value, cb) < 0)
119
+ return -1;
120
+ if (grep_config("color.grep.matchselected", value, cb) < 0)
121
+ return -1;
122
+ } else if (skip_prefix(var, "color.grep.", &slot)) {
123
+ int i = LOOKUP_CONFIG(color_grep_slots, slot);
124
+ char *color;
125
+
126
+ if (i < 0)
127
+ return -1;
128
+ color = opt->colors[i];
129
if (!value)
130
return config_error_nonbool(var);
131
return color_parse(value, color);
@@ -144,6 +141,7 @@ int grep_config(const char *var, const char *value, void *cb)
141
void grep_init(struct grep_opt *opt, const char *prefix)
142
{
143
struct grep_opt *def = &grep_defaults;
144
+ int i;
145
146
memset(opt, 0, sizeof(*opt));
147
opt->prefix = prefix;
@@ -160,14 +158,8 @@ void grep_init(struct grep_opt *opt, const char *prefix)
158
opt->relative = def->relative;
159
opt->output = def->output;
160
163
- color_set(opt->color_context, def->color_context);
164
- color_set(opt->color_filename, def->color_filename);
165
- color_set(opt->color_function, def->color_function);
166
- color_set(opt->color_lineno, def->color_lineno);
167
- color_set(opt->color_match_context, def->color_match_context);
168
- color_set(opt->color_match_selected, def->color_match_selected);
169
- color_set(opt->color_selected, def->color_selected);
170
- color_set(opt->color_sep, def->color_sep);
161
+ for (i = 0; i < NR_GREP_COLORS; i++)
162
+ color_set(opt->colors[i], def->colors[i]);
163
}
164
165
static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, struct grep_opt *opt)
@@ -1100,12 +1092,12 @@ static void output_sep(struct grep_opt *opt, char sign)
1092
if (opt->null_following_name)
1093
opt->output(opt, "\0", 1);
1094
else
1103
- output_color(opt, &sign, 1, opt->color_sep);
1095
+ output_color(opt, &sign, 1, opt->colors[GREP_COLOR_SEP]);
1096
}
1097
1098
static void show_name(struct grep_opt *opt, const char *name)
1099
{
1108
- output_color(opt, name, strlen(name), opt->color_filename);
1100
+ output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]);
1101
opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
1102
}
1103
@@ -1372,28 +1364,28 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
1364
} else if (opt->pre_context || opt->post_context || opt->funcbody) {
1365
if (opt->last_shown == 0) {
1366
if (opt->show_hunk_mark) {
1375
- output_color(opt, "--", 2, opt->color_sep);
1367
+ output_color(opt, "--", 2, opt->colors[GREP_COLOR_SEP]);
1368
opt->output(opt, "\n", 1);
1369
}
1370
} else if (lno > opt->last_shown + 1) {
1379
- output_color(opt, "--", 2, opt->color_sep);
1371
+ output_color(opt, "--", 2, opt->colors[GREP_COLOR_SEP]);
1372
opt->output(opt, "\n", 1);
1373
}
1374
}
1375
if (opt->heading && opt->last_shown == 0) {
1384
- output_color(opt, name, strlen(name), opt->color_filename);
1376
+ output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]);
1377
opt->output(opt, "\n", 1);
1378
}
1379
opt->last_shown = lno;
1380
1381
if (!opt->heading && opt->pathname) {
1390
- output_color(opt, name, strlen(name), opt->color_filename);
1382
+ output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]);
1383
output_sep(opt, sign);
1384
}
1385
if (opt->linenum) {
1386
char buf[32];
1387
xsnprintf(buf, sizeof(buf), "%d", lno);
1396
- output_color(opt, buf, strlen(buf), opt->color_lineno);
1388
+ output_color(opt, buf, strlen(buf), opt->colors[GREP_COLOR_LINENO]);
1389
output_sep(opt, sign);
1390
}
1391
if (opt->color) {
@@ -1403,15 +1395,15 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
1395
int eflags = 0;
1396
1397
if (sign == ':')
1406
- match_color = opt->color_match_selected;
1398
+ match_color = opt->colors[GREP_COLOR_MATCH_SELECTED];
1399
else
1408
- match_color = opt->color_match_context;
1400
+ match_color = opt->colors[GREP_COLOR_MATCH_CONTEXT];
1401
if (sign == ':')
1410
- line_color = opt->color_selected;
1402
+ line_color = opt->colors[GREP_COLOR_SELECTED];
1403
else if (sign == '-')
1412
- line_color = opt->color_context;
1404
+ line_color = opt->colors[GREP_COLOR_CONTEXT];
1405
else if (sign == '=')
1414
- line_color = opt->color_function;
1406
+ line_color = opt->colors[GREP_COLOR_FUNCTION];
1407
*eol = '\0';
1408
while (next_match(opt, bol, eol, ctx, &match, eflags)) {
1409
if (match.rm_so == match.rm_eo)
@@ -1818,7 +1810,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
1810
if (binary_match_only) {
1811
opt->output(opt, "Binary file ", 12);
1812
output_color(opt, gs->name, strlen(gs->name),
1821
- opt->color_filename);
1813
+ opt->colors[GREP_COLOR_FILENAME]);
1814
opt->output(opt, " matches\n", 9);
1815
return 1;
1816
}
@@ -1892,7 +1884,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
1884
char buf[32];
1885
if (opt->pathname) {
1886
output_color(opt, gs->name, strlen(gs->name),
1895
- opt->color_filename);
1887
+ opt->colors[GREP_COLOR_FILENAME]);
1888
output_sep(opt, ':');
1889
}
1890
xsnprintf(buf, sizeof(buf), "%u\n", count);
grep.h
+13
-8
@@ -62,6 +62,18 @@ enum grep_header_field {
62
GREP_HEADER_FIELD_MAX
63
};
64
65
+enum grep_color {
66
+ GREP_COLOR_CONTEXT,
67
+ GREP_COLOR_FILENAME,
68
+ GREP_COLOR_FUNCTION,
69
+ GREP_COLOR_LINENO,
70
+ GREP_COLOR_MATCH_CONTEXT,
71
+ GREP_COLOR_MATCH_SELECTED,
72
+ GREP_COLOR_SELECTED,
73
+ GREP_COLOR_SEP,
74
+ NR_GREP_COLORS
75
+};
76
+
77
struct grep_pat {
78
struct grep_pat *next;
79
const char *origin;
@@ -155,14 +167,7 @@ struct grep_opt {
167
int funcbody;
168
int extended_regexp_option;
169
int pattern_type_option;
158
- char color_context[COLOR_MAXLEN];
159
- char color_filename[COLOR_MAXLEN];
160
- char color_function[COLOR_MAXLEN];
161
- char color_lineno[COLOR_MAXLEN];
162
- char color_match_context[COLOR_MAXLEN];
163
- char color_match_selected[COLOR_MAXLEN];
164
- char color_selected[COLOR_MAXLEN];
165
- char color_sep[COLOR_MAXLEN];
170
+ char colors[NR_GREP_COLORS][COLOR_MAXLEN];
171
unsigned pre_context;
172
unsigned post_context;
173
unsigned last_shown;