Skip to content

Commit 9370598

Browse files
slavicaDjgitster
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]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3ad48ca commit 9370598

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;
@@ -421,15 +429,21 @@ static int run_status(struct add_i_state *s, const struct pathspec *ps,
421429
return 0;
422430
}
423431

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

@@ -454,8 +468,9 @@ static void command_prompt_help(struct add_i_state *s)
454468
int run_add_i(struct repository *r, const struct pathspec *ps)
455469
{
456470
struct add_i_state s = { NULL };
471+
struct print_command_item_data data;
457472
struct list_and_choose_options main_loop_opts = {
458-
{ 4, N_("*** Commands ***"), print_command_item, NULL },
473+
{ 4, N_("*** Commands ***"), print_command_item, &data },
459474
N_("What now"), command_prompt_help
460475
};
461476
struct command_item
@@ -478,6 +493,18 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
478493
if (init_add_i_state(r, &s))
479494
return error("could not parse `add -i` config");
480495

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

0 commit comments

Comments
 (0)