Skip to content

Commit 91c54d9

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 Đukić <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 39370d8 commit 91c54d9

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

add-interactive.c

Lines changed: 29 additions & 6 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 void init_add_i_state(struct add_i_state *s, struct repository *r)
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

5056
/*
@@ -240,7 +246,8 @@ static ssize_t list_and_choose(struct add_i_state *s,
240246

241247
list(s, &items->items, &opts->list_opts);
242248

243-
printf("%s%s", opts->prompt, "> ");
249+
color_fprintf(stdout, s->prompt_color, "%s", opts->prompt);
250+
fputs("> ", stdout);
244251
fflush(stdout);
245252

246253
if (strbuf_getline(&input, stdin) == EOF) {
@@ -282,7 +289,8 @@ static ssize_t list_and_choose(struct add_i_state *s,
282289
index = find_unique(p, items);
283290

284291
if (index < 0 || index >= items->items.nr)
285-
printf(_("Huh (%s)?\n"), p);
292+
color_fprintf_ln(stdout, s->error_color,
293+
_("Huh (%s)?"), p);
286294
else {
287295
res = index;
288296
break;
@@ -508,18 +516,23 @@ struct command_item {
508516
command_t command;
509517
};
510518

519+
struct print_command_item_data {
520+
const char *color, *reset;
521+
};
522+
511523
static void print_command_item(int i, struct string_list_item *item,
512524
void *print_command_item_data)
513525
{
526+
struct print_command_item_data *d = print_command_item_data;
514527
struct command_item *util = item->util;
515528

516529
if (!util->prefix_length ||
517530
!is_valid_prefix(item->string, util->prefix_length))
518531
printf(" %2d: %s", i + 1, item->string);
519532
else
520-
printf(" %2d: [%.*s]%s", i + 1,
521-
(int)util->prefix_length, item->string,
522-
item->string + util->prefix_length);
533+
printf(" %2d: %s%.*s%s%s", i + 1,
534+
d->color, (int)util->prefix_length, item->string,
535+
d->reset, item->string + util->prefix_length);
523536
}
524537

525538
static void command_prompt_help(struct add_i_state *s)
@@ -537,8 +550,9 @@ static void command_prompt_help(struct add_i_state *s)
537550
int run_add_i(struct repository *r, const struct pathspec *ps)
538551
{
539552
struct add_i_state s = { NULL };
553+
struct print_command_item_data data = { "[", "]" };
540554
struct list_and_choose_options main_loop_opts = {
541-
{ 4, N_("*** Commands ***"), print_command_item, NULL },
555+
{ 4, N_("*** Commands ***"), print_command_item, &data },
542556
N_("What now"), command_prompt_help
543557
};
544558
struct {
@@ -569,6 +583,15 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
569583

570584
init_add_i_state(&s, r);
571585

586+
/*
587+
* When color was asked for, use the prompt color for
588+
* highlighting, otherwise use square brackets.
589+
*/
590+
if (s.use_color) {
591+
data.color = s.prompt_color;
592+
data.reset = s.reset_color;
593+
}
594+
572595
strbuf_addstr(&header, " ");
573596
strbuf_addf(&header, print_file_item_data.modified_fmt,
574597
_("staged"), _("unstaged"), _("path"));

0 commit comments

Comments
 (0)