Skip to content

Commit 3e1a88d

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 ddd0ee5 commit 3e1a88d

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

add-interactive.c

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,17 @@ static int use_color = -1;
1313
enum color_add_i {
1414
COLOR_HEADER = 0,
1515
COLOR_HELP,
16+
COLOR_PROMPT,
17+
COLOR_ERROR,
18+
COLOR_RESET,
1619
};
1720

1821
static char list_colors[][COLOR_MAXLEN] = {
1922
GIT_COLOR_BOLD, /* Header */
2023
GIT_COLOR_BOLD_RED, /* Help */
24+
GIT_COLOR_BOLD_BLUE, /* Prompt */
25+
GIT_COLOR_BOLD_RED, /* Error */
26+
GIT_COLOR_RESET, /* Reset */
2127
};
2228

2329
static const char *get_add_i_color(enum color_add_i ix)
@@ -33,6 +39,12 @@ static int parse_color_slot(const char *slot)
3339
return COLOR_HEADER;
3440
if (!strcasecmp(slot, "help"))
3541
return COLOR_HELP;
42+
if (!strcasecmp(slot, "prompt"))
43+
return COLOR_PROMPT;
44+
if (!strcasecmp(slot, "error"))
45+
return COLOR_ERROR;
46+
if (!strcasecmp(slot, "reset"))
47+
return COLOR_RESET;
3648

3749
return -1;
3850
}
@@ -125,6 +137,8 @@ struct list_and_choose_options {
125137
static ssize_t list_and_choose(struct prefix_item **items, size_t nr,
126138
struct list_and_choose_options *opts)
127139
{
140+
const char *prompt_color = get_add_i_color(COLOR_PROMPT);
141+
const char *error_color = get_add_i_color(COLOR_ERROR);
128142
struct strbuf input = STRBUF_INIT;
129143
ssize_t res = -1;
130144

@@ -137,7 +151,8 @@ static ssize_t list_and_choose(struct prefix_item **items, size_t nr,
137151

138152
list(items, nr, &opts->list_opts);
139153

140-
printf("%s%s", opts->prompt, "> ");
154+
color_fprintf(stdout, prompt_color, "%s", opts->prompt);
155+
fputs("> ", stdout);
141156
fflush(stdout);
142157

143158
if (strbuf_getline(&input, stdin) == EOF) {
@@ -178,7 +193,8 @@ static ssize_t list_and_choose(struct prefix_item **items, size_t nr,
178193
index = find_unique(p, items, nr);
179194

180195
if (index < 0 || index >= nr)
181-
printf(_("Huh (%s)?\n"), p);
196+
color_fprintf_ln(stdout, error_color,
197+
_("Huh (%s)?"), p);
182198
else {
183199
res = index;
184200
break;
@@ -423,15 +439,21 @@ static int run_status(struct repository *r, const struct pathspec *ps,
423439
return 0;
424440
}
425441

442+
struct print_command_item_data {
443+
const char *color, *reset;
444+
};
445+
426446
static void print_command_item(int i, struct prefix_item *item,
427447
void *print_command_item_data)
428448
{
449+
struct print_command_item_data *d = print_command_item_data;
450+
429451
if (!item->prefix_length ||
430452
!is_valid_prefix(item->name, item->prefix_length))
431453
printf(" %2d: %s", i + 1, item->name);
432454
else
433-
printf(" %3d: [%.*s]%s", i + 1,
434-
(int)item->prefix_length, item->name,
455+
printf(" %2d: %s%.*s%s%s", i + 1,
456+
d->color, (int)item->prefix_length, item->name, d->reset,
435457
item->name + item->prefix_length);
436458
}
437459

@@ -455,8 +477,16 @@ static void command_prompt_help(void)
455477

456478
int run_add_i(struct repository *r, const struct pathspec *ps)
457479
{
480+
struct print_command_item_data data = {
481+
/*
482+
* When color was asked for, use the prompt color for
483+
* highlighting, otherwise use square brackets.
484+
*/
485+
want_color(use_color) ? get_add_i_color(COLOR_PROMPT) : "[",
486+
want_color(use_color) ? get_add_i_color(COLOR_RESET) : "]"
487+
};
458488
struct list_and_choose_options main_loop_opts = {
459-
{ 4, N_("*** Commands ***"), print_command_item, NULL },
489+
{ 4, N_("*** Commands ***"), print_command_item, &data },
460490
N_("What now"), command_prompt_help
461491
};
462492
struct command_item

0 commit comments

Comments
 (0)