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ć <slawica92@hotmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Slavica Đukić committed
Nov 15, 2019 at 11:11 UTC
3d965c76744482b50669a7d1b445c0ddbf92209a
1 file changed
+29
-6
add-interactive.c
+29
-6
@@ -12,6 +12,9 @@ struct add_i_state {
12
int use_color;
13
char header_color[COLOR_MAXLEN];
14
char help_color[COLOR_MAXLEN];
15
+ char prompt_color[COLOR_MAXLEN];
16
+ char error_color[COLOR_MAXLEN];
17
+ char reset_color[COLOR_MAXLEN];
18
};
19
20
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)
48
49
init_color(r, s, "header", s->header_color, GIT_COLOR_BOLD);
50
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);
54
}
55
56
/*
@@ -240,7 +246,8 @@ static ssize_t list_and_choose(struct add_i_state *s,
246
247
list(s, &items->items, &opts->list_opts);
248
243
- printf("%s%s", opts->prompt, "> ");
249
+ color_fprintf(stdout, s->prompt_color, "%s", opts->prompt);
250
+ fputs("> ", stdout);
251
fflush(stdout);
252
253
if (strbuf_getline(&input, stdin) == EOF) {
@@ -283,7 +290,8 @@ static ssize_t list_and_choose(struct add_i_state *s,
290
index = find_unique(p, items);
291
292
if (index < 0 || index >= items->items.nr)
286
- printf(_("Huh (%s)?\n"), p);
293
+ color_fprintf_ln(stdout, s->error_color,
294
+ _("Huh (%s)?"), p);
295
else {
296
res = index;
297
break;
@@ -509,18 +517,23 @@ struct command_item {
517
command_t command;
518
};
519
520
+struct print_command_item_data {
521
+ const char *color, *reset;
522
+};
523
+
524
static void print_command_item(int i, struct string_list_item *item,
525
void *print_command_item_data)
526
{
527
+ struct print_command_item_data *d = print_command_item_data;
528
struct command_item *util = item->util;
529
530
if (!util->prefix_length ||
531
!is_valid_prefix(item->string, util->prefix_length))
532
printf(" %2d: %s", i + 1, item->string);
533
else
521
- printf(" %2d: [%.*s]%s", i + 1,
522
- (int)util->prefix_length, item->string,
523
- item->string + util->prefix_length);
534
+ printf(" %2d: %s%.*s%s%s", i + 1,
535
+ d->color, (int)util->prefix_length, item->string,
536
+ d->reset, item->string + util->prefix_length);
537
}
538
539
static void command_prompt_help(struct add_i_state *s)
@@ -538,8 +551,9 @@ static void command_prompt_help(struct add_i_state *s)
551
int run_add_i(struct repository *r, const struct pathspec *ps)
552
{
553
struct add_i_state s = { NULL };
554
+ struct print_command_item_data data = { "[", "]" };
555
struct list_and_choose_options main_loop_opts = {
542
- { 4, N_("*** Commands ***"), print_command_item, NULL },
556
+ { 4, N_("*** Commands ***"), print_command_item, &data },
557
N_("What now"), command_prompt_help
558
};
559
struct {
@@ -570,6 +584,15 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
584
585
init_add_i_state(&s, r);
586
587
+ /*
588
+ * When color was asked for, use the prompt color for
589
+ * highlighting, otherwise use square brackets.
590
+ */
591
+ if (s.use_color) {
592
+ data.color = s.prompt_color;
593
+ data.reset = s.reset_color;
594
+ }
595
+
596
strbuf_addstr(&header, " ");
597
strbuf_addf(&header, print_file_item_data.modified_fmt,
598
_("staged"), _("unstaged"), _("path"));