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>
This commit is contained in:
Nguyễn Thái Ngọc Duy 2018-05-26 15:55:22 +02:00 committed by Junio C Hamano
parent a73b3680c4
commit fa151dc54d
2 changed files with 61 additions and 64 deletions

104
grep.c
View File

@ -13,6 +13,17 @@ static int grep_source_is_binary(struct grep_source *gs);
static struct grep_opt grep_defaults; static struct grep_opt grep_defaults;
static const char *color_grep_slots[] = {
[GREP_COLOR_CONTEXT] = "context",
[GREP_COLOR_FILENAME] = "filename",
[GREP_COLOR_FUNCTION] = "function",
[GREP_COLOR_LINENO] = "lineNumber",
[GREP_COLOR_MATCH_CONTEXT] = "matchContext",
[GREP_COLOR_MATCH_SELECTED] = "matchSelected",
[GREP_COLOR_SELECTED] = "selected",
[GREP_COLOR_SEP] = "separator",
};
static void std_output(struct grep_opt *opt, const void *buf, size_t size) static void std_output(struct grep_opt *opt, const void *buf, size_t size)
{ {
fwrite(buf, size, 1, stdout); fwrite(buf, size, 1, stdout);
@ -42,14 +53,14 @@ void init_grep_defaults(void)
opt->pathname = 1; opt->pathname = 1;
opt->max_depth = -1; opt->max_depth = -1;
opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED; opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
color_set(opt->color_context, ""); color_set(opt->colors[GREP_COLOR_CONTEXT], "");
color_set(opt->color_filename, ""); color_set(opt->colors[GREP_COLOR_FILENAME], "");
color_set(opt->color_function, ""); color_set(opt->colors[GREP_COLOR_FUNCTION], "");
color_set(opt->color_lineno, ""); color_set(opt->colors[GREP_COLOR_LINENO], "");
color_set(opt->color_match_context, GIT_COLOR_BOLD_RED); color_set(opt->colors[GREP_COLOR_MATCH_CONTEXT], GIT_COLOR_BOLD_RED);
color_set(opt->color_match_selected, GIT_COLOR_BOLD_RED); color_set(opt->colors[GREP_COLOR_MATCH_SELECTED], GIT_COLOR_BOLD_RED);
color_set(opt->color_selected, ""); color_set(opt->colors[GREP_COLOR_SELECTED], "");
color_set(opt->color_sep, GIT_COLOR_CYAN); color_set(opt->colors[GREP_COLOR_SEP], GIT_COLOR_CYAN);
opt->color = -1; opt->color = -1;
opt->output = std_output; opt->output = std_output;
} }
@ -76,7 +87,7 @@ static int parse_pattern_type_arg(const char *opt, const char *arg)
int grep_config(const char *var, const char *value, void *cb) int grep_config(const char *var, const char *value, void *cb)
{ {
struct grep_opt *opt = &grep_defaults; struct grep_opt *opt = &grep_defaults;
char *color = NULL; const char *slot;
if (userdiff_config(var, value) < 0) if (userdiff_config(var, value) < 0)
return -1; return -1;
@ -103,32 +114,18 @@ int grep_config(const char *var, const char *value, void *cb)
if (!strcmp(var, "color.grep")) if (!strcmp(var, "color.grep"))
opt->color = git_config_colorbool(var, value); opt->color = git_config_colorbool(var, value);
else if (!strcmp(var, "color.grep.context")) if (!strcmp(var, "color.grep.match")) {
color = opt->color_context; if (grep_config("color.grep.matchcontext", value, cb) < 0)
else if (!strcmp(var, "color.grep.filename")) return -1;
color = opt->color_filename; if (grep_config("color.grep.matchselected", value, cb) < 0)
else if (!strcmp(var, "color.grep.function")) return -1;
color = opt->color_function; } else if (skip_prefix(var, "color.grep.", &slot)) {
else if (!strcmp(var, "color.grep.linenumber")) int i = LOOKUP_CONFIG(color_grep_slots, slot);
color = opt->color_lineno; char *color;
else if (!strcmp(var, "color.grep.matchcontext"))
color = opt->color_match_context;
else if (!strcmp(var, "color.grep.matchselected"))
color = opt->color_match_selected;
else if (!strcmp(var, "color.grep.selected"))
color = opt->color_selected;
else if (!strcmp(var, "color.grep.separator"))
color = opt->color_sep;
else if (!strcmp(var, "color.grep.match")) {
int rc = 0;
if (!value)
return config_error_nonbool(var);
rc |= color_parse(value, opt->color_match_context);
rc |= color_parse(value, opt->color_match_selected);
return rc;
}
if (color) { if (i < 0)
return -1;
color = opt->colors[i];
if (!value) if (!value)
return config_error_nonbool(var); return config_error_nonbool(var);
return color_parse(value, color); return color_parse(value, color);
@ -144,6 +141,7 @@ int grep_config(const char *var, const char *value, void *cb)
void grep_init(struct grep_opt *opt, const char *prefix) void grep_init(struct grep_opt *opt, const char *prefix)
{ {
struct grep_opt *def = &grep_defaults; struct grep_opt *def = &grep_defaults;
int i;
memset(opt, 0, sizeof(*opt)); memset(opt, 0, sizeof(*opt));
opt->prefix = prefix; opt->prefix = prefix;
@ -160,14 +158,8 @@ void grep_init(struct grep_opt *opt, const char *prefix)
opt->relative = def->relative; opt->relative = def->relative;
opt->output = def->output; opt->output = def->output;
color_set(opt->color_context, def->color_context); for (i = 0; i < NR_GREP_COLORS; i++)
color_set(opt->color_filename, def->color_filename); color_set(opt->colors[i], def->colors[i]);
color_set(opt->color_function, def->color_function);
color_set(opt->color_lineno, def->color_lineno);
color_set(opt->color_match_context, def->color_match_context);
color_set(opt->color_match_selected, def->color_match_selected);
color_set(opt->color_selected, def->color_selected);
color_set(opt->color_sep, def->color_sep);
} }
static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, struct grep_opt *opt) 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)
if (opt->null_following_name) if (opt->null_following_name)
opt->output(opt, "\0", 1); opt->output(opt, "\0", 1);
else else
output_color(opt, &sign, 1, opt->color_sep); output_color(opt, &sign, 1, opt->colors[GREP_COLOR_SEP]);
} }
static void show_name(struct grep_opt *opt, const char *name) static void show_name(struct grep_opt *opt, const char *name)
{ {
output_color(opt, name, strlen(name), opt->color_filename); output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]);
opt->output(opt, opt->null_following_name ? "\0" : "\n", 1); opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
} }
@ -1372,28 +1364,28 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
} else if (opt->pre_context || opt->post_context || opt->funcbody) { } else if (opt->pre_context || opt->post_context || opt->funcbody) {
if (opt->last_shown == 0) { if (opt->last_shown == 0) {
if (opt->show_hunk_mark) { if (opt->show_hunk_mark) {
output_color(opt, "--", 2, opt->color_sep); output_color(opt, "--", 2, opt->colors[GREP_COLOR_SEP]);
opt->output(opt, "\n", 1); opt->output(opt, "\n", 1);
} }
} else if (lno > opt->last_shown + 1) { } else if (lno > opt->last_shown + 1) {
output_color(opt, "--", 2, opt->color_sep); output_color(opt, "--", 2, opt->colors[GREP_COLOR_SEP]);
opt->output(opt, "\n", 1); opt->output(opt, "\n", 1);
} }
} }
if (opt->heading && opt->last_shown == 0) { if (opt->heading && opt->last_shown == 0) {
output_color(opt, name, strlen(name), opt->color_filename); output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]);
opt->output(opt, "\n", 1); opt->output(opt, "\n", 1);
} }
opt->last_shown = lno; opt->last_shown = lno;
if (!opt->heading && opt->pathname) { if (!opt->heading && opt->pathname) {
output_color(opt, name, strlen(name), opt->color_filename); output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]);
output_sep(opt, sign); output_sep(opt, sign);
} }
if (opt->linenum) { if (opt->linenum) {
char buf[32]; char buf[32];
xsnprintf(buf, sizeof(buf), "%d", lno); xsnprintf(buf, sizeof(buf), "%d", lno);
output_color(opt, buf, strlen(buf), opt->color_lineno); output_color(opt, buf, strlen(buf), opt->colors[GREP_COLOR_LINENO]);
output_sep(opt, sign); output_sep(opt, sign);
} }
if (opt->color) { if (opt->color) {
@ -1403,15 +1395,15 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
int eflags = 0; int eflags = 0;
if (sign == ':') if (sign == ':')
match_color = opt->color_match_selected; match_color = opt->colors[GREP_COLOR_MATCH_SELECTED];
else else
match_color = opt->color_match_context; match_color = opt->colors[GREP_COLOR_MATCH_CONTEXT];
if (sign == ':') if (sign == ':')
line_color = opt->color_selected; line_color = opt->colors[GREP_COLOR_SELECTED];
else if (sign == '-') else if (sign == '-')
line_color = opt->color_context; line_color = opt->colors[GREP_COLOR_CONTEXT];
else if (sign == '=') else if (sign == '=')
line_color = opt->color_function; line_color = opt->colors[GREP_COLOR_FUNCTION];
*eol = '\0'; *eol = '\0';
while (next_match(opt, bol, eol, ctx, &match, eflags)) { while (next_match(opt, bol, eol, ctx, &match, eflags)) {
if (match.rm_so == match.rm_eo) 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
if (binary_match_only) { if (binary_match_only) {
opt->output(opt, "Binary file ", 12); opt->output(opt, "Binary file ", 12);
output_color(opt, gs->name, strlen(gs->name), output_color(opt, gs->name, strlen(gs->name),
opt->color_filename); opt->colors[GREP_COLOR_FILENAME]);
opt->output(opt, " matches\n", 9); opt->output(opt, " matches\n", 9);
return 1; return 1;
} }
@ -1892,7 +1884,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
char buf[32]; char buf[32];
if (opt->pathname) { if (opt->pathname) {
output_color(opt, gs->name, strlen(gs->name), output_color(opt, gs->name, strlen(gs->name),
opt->color_filename); opt->colors[GREP_COLOR_FILENAME]);
output_sep(opt, ':'); output_sep(opt, ':');
} }
xsnprintf(buf, sizeof(buf), "%u\n", count); xsnprintf(buf, sizeof(buf), "%u\n", count);

21
grep.h
View File

@ -62,6 +62,18 @@ enum grep_header_field {
GREP_HEADER_FIELD_MAX GREP_HEADER_FIELD_MAX
}; };
enum grep_color {
GREP_COLOR_CONTEXT,
GREP_COLOR_FILENAME,
GREP_COLOR_FUNCTION,
GREP_COLOR_LINENO,
GREP_COLOR_MATCH_CONTEXT,
GREP_COLOR_MATCH_SELECTED,
GREP_COLOR_SELECTED,
GREP_COLOR_SEP,
NR_GREP_COLORS
};
struct grep_pat { struct grep_pat {
struct grep_pat *next; struct grep_pat *next;
const char *origin; const char *origin;
@ -155,14 +167,7 @@ struct grep_opt {
int funcbody; int funcbody;
int extended_regexp_option; int extended_regexp_option;
int pattern_type_option; int pattern_type_option;
char color_context[COLOR_MAXLEN]; char colors[NR_GREP_COLORS][COLOR_MAXLEN];
char color_filename[COLOR_MAXLEN];
char color_function[COLOR_MAXLEN];
char color_lineno[COLOR_MAXLEN];
char color_match_context[COLOR_MAXLEN];
char color_match_selected[COLOR_MAXLEN];
char color_selected[COLOR_MAXLEN];
char color_sep[COLOR_MAXLEN];
unsigned pre_context; unsigned pre_context;
unsigned post_context; unsigned post_context;
unsigned last_shown; unsigned last_shown;