Skip to content

Commit 36edef8

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 7378af6 commit 36edef8

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
}
@@ -134,7 +140,8 @@ static ssize_t list_and_choose(struct prefix_item **items, size_t nr,
134140

135141
list(items, nr, s, &opts->list_opts);
136142

137-
printf("%s%s", opts->prompt, "> ");
143+
color_fprintf(stdout, s->prompt_color, "%s", opts->prompt);
144+
fputs("> ", stdout);
138145
fflush(stdout);
139146

140147
if (strbuf_getline(&input, stdin) == EOF) {
@@ -175,7 +182,8 @@ static ssize_t list_and_choose(struct prefix_item **items, size_t nr,
175182
index = find_unique(p, items, nr);
176183

177184
if (index < 0 || index >= nr)
178-
printf(_("Huh (%s)?\n"), p);
185+
color_fprintf_ln(stdout, s->error_color,
186+
_("Huh (%s)?"), p);
179187
else {
180188
res = index;
181189
break;
@@ -420,15 +428,21 @@ static int run_status(struct add_i_state *s, const struct pathspec *ps,
420428
return 0;
421429
}
422430

431+
struct print_command_item_data {
432+
const char *color, *reset;
433+
};
434+
423435
static void print_command_item(int i, struct prefix_item *item,
424436
void *print_command_item_data)
425437
{
438+
struct print_command_item_data *d = print_command_item_data;
439+
426440
if (!item->prefix_length ||
427441
!is_valid_prefix(item->name, item->prefix_length))
428442
printf(" %2d: %s", i + 1, item->name);
429443
else
430-
printf(" %3d: [%.*s]%s", i + 1,
431-
(int)item->prefix_length, item->name,
444+
printf(" %2d: %s%.*s%s%s", i + 1,
445+
d->color, (int)item->prefix_length, item->name, d->reset,
432446
item->name + item->prefix_length);
433447
}
434448

@@ -453,8 +467,9 @@ static void command_prompt_help(struct add_i_state *s)
453467
int run_add_i(struct repository *r, const struct pathspec *ps)
454468
{
455469
struct add_i_state s = { NULL };
470+
struct print_command_item_data data;
456471
struct list_and_choose_options main_loop_opts = {
457-
{ 4, N_("*** Commands ***"), print_command_item, NULL },
472+
{ 4, N_("*** Commands ***"), print_command_item, &data },
458473
N_("What now"), command_prompt_help
459474
};
460475
struct command_item
@@ -477,6 +492,18 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
477492
if (init_add_i_state(r, &s))
478493
return error("could not parse `add -i` config");
479494

495+
/*
496+
* When color was asked for, use the prompt color for
497+
* highlighting, otherwise use square brackets.
498+
*/
499+
if (s.use_color) {
500+
data.color = s.prompt_color;
501+
data.reset = s.reset_color;
502+
} else {
503+
data.color = "[";
504+
data.reset = "]";
505+
}
506+
480507
strbuf_addstr(&header, " ");
481508
strbuf_addf(&header, print_file_item_data.modified_fmt,
482509
_("staged"), _("unstaged"), _("path"));

0 commit comments

Comments
 (0)