Skip to content

Commit 9cf3260

Browse files
slavicaDjdscho
authored andcommitted
built-in add -i: use color in the main loop
The error messages as well as the unique prefixes are colored in `git add -i` by default; We need to do the same in the built-in version. Signed-off-by: Slavica Djukic <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent f48c70e commit 9cf3260

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

add-interactive.c

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ struct add_i_state {
1212
int use_color;
1313
char header_color[COLOR_MAXLEN];
1414
char help_color[COLOR_MAXLEN];
15+
char prompt_color[COLOR_MAXLEN];
16+
char error_color[COLOR_MAXLEN];
17+
char reset_color[COLOR_MAXLEN];
1518
};
1619

1720
static void init_color(struct repository *r, struct add_i_state *s,
@@ -45,6 +48,9 @@ static int init_add_i_state(struct repository *r, struct add_i_state *s)
4548

4649
init_color(r, s, "header", s->header_color, GIT_COLOR_BOLD);
4750
init_color(r, s, "help", s->help_color, GIT_COLOR_BOLD_RED);
51+
init_color(r, s, "prompt", s->prompt_color, GIT_COLOR_BOLD_BLUE);
52+
init_color(r, s, "error", s->error_color, GIT_COLOR_BOLD_RED);
53+
init_color(r, s, "reset", s->reset_color, GIT_COLOR_RESET);
4854

4955
return 0;
5056
}
@@ -128,7 +134,8 @@ static ssize_t list_and_choose(struct prefix_item **items, size_t nr,
128134

129135
list(items, nr, s, &opts->list_opts);
130136

131-
printf("%s%s", opts->prompt, "> ");
137+
color_fprintf(stdout, s->prompt_color, "%s", opts->prompt);
138+
fputs("> ", stdout);
132139
fflush(stdout);
133140

134141
if (strbuf_getline(&input, stdin) == EOF) {
@@ -169,7 +176,8 @@ static ssize_t list_and_choose(struct prefix_item **items, size_t nr,
169176
index = find_unique(p, items, nr);
170177

171178
if (index < 0 || index >= nr)
172-
printf(_("Huh (%s)?\n"), p);
179+
color_fprintf_ln(stdout, s->error_color,
180+
_("Huh (%s)?"), p);
173181
else {
174182
res = index;
175183
break;
@@ -414,15 +422,21 @@ static int run_status(struct add_i_state *s, const struct pathspec *ps,
414422
return 0;
415423
}
416424

425+
struct print_command_item_data {
426+
const char *color, *reset;
427+
};
428+
417429
static void print_command_item(int i, struct prefix_item *item,
418430
void *print_command_item_data)
419431
{
432+
struct print_command_item_data *d = print_command_item_data;
433+
420434
if (!item->prefix_length ||
421435
!is_valid_prefix(item->name, item->prefix_length))
422436
printf(" %2d: %s", i + 1, item->name);
423437
else
424-
printf(" %3d: [%.*s]%s", i + 1,
425-
(int)item->prefix_length, item->name,
438+
printf(" %2d: %s%.*s%s%s", i + 1,
439+
d->color, (int)item->prefix_length, item->name, d->reset,
426440
item->name + item->prefix_length);
427441
}
428442

@@ -447,8 +461,9 @@ static void command_prompt_help(struct add_i_state *s)
447461
int run_add_i(struct repository *r, const struct pathspec *ps)
448462
{
449463
struct add_i_state s = { NULL };
464+
struct print_command_item_data data;
450465
struct list_and_choose_options main_loop_opts = {
451-
{ 4, N_("*** Commands ***"), print_command_item, NULL },
466+
{ 4, N_("*** Commands ***"), print_command_item, &data },
452467
N_("What now"), command_prompt_help
453468
};
454469
struct command_item
@@ -471,6 +486,18 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
471486
if (init_add_i_state(r, &s))
472487
return error("could not parse `add -i` config");
473488

489+
/*
490+
* When color was asked for, use the prompt color for
491+
* highlighting, otherwise use square brackets.
492+
*/
493+
if (s.use_color) {
494+
data.color = s.prompt_color;
495+
data.reset = s.reset_color;
496+
} else {
497+
data.color = "[";
498+
data.reset = "]";
499+
}
500+
474501
strbuf_addstr(&header, " ");
475502
strbuf_addf(&header, print_file_item_data.modified_fmt,
476503
_("staged"), _("unstaged"), _("path"));

0 commit comments

Comments
 (0)