Skip to content

Commit ddd0ee5

Browse files
committed
built-in add -i: support ? (prompt help)
With this change, we print out the same colored help text that the Perl-based `git add -i` prints in the main loop when question mark is entered. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 56acc31 commit ddd0ee5

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

add-interactive.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ static int use_color = -1;
1212

1313
enum color_add_i {
1414
COLOR_HEADER = 0,
15+
COLOR_HELP,
1516
};
1617

1718
static char list_colors[][COLOR_MAXLEN] = {
1819
GIT_COLOR_BOLD, /* Header */
20+
GIT_COLOR_BOLD_RED, /* Help */
1921
};
2022

2123
static const char *get_add_i_color(enum color_add_i ix)
@@ -29,6 +31,8 @@ static int parse_color_slot(const char *slot)
2931
{
3032
if (!strcasecmp(slot, "header"))
3133
return COLOR_HEADER;
34+
if (!strcasecmp(slot, "help"))
35+
return COLOR_HELP;
3236

3337
return -1;
3438
}
@@ -112,6 +116,7 @@ struct list_and_choose_options {
112116
struct list_options list_opts;
113117

114118
const char *prompt;
119+
void (*print_help)(void);
115120
};
116121

117122
/*
@@ -145,6 +150,11 @@ static ssize_t list_and_choose(struct prefix_item **items, size_t nr,
145150
if (!input.len)
146151
break;
147152

153+
if (!strcmp(input.buf, "?")) {
154+
opts->print_help();
155+
continue;
156+
}
157+
148158
p = input.buf;
149159
for (;;) {
150160
size_t sep = strcspn(p, " \t\r\n,");
@@ -431,11 +441,23 @@ struct command_item {
431441
struct file_list *files, struct list_options *opts);
432442
};
433443

444+
static void command_prompt_help(void)
445+
{
446+
const char *help_color = get_add_i_color(COLOR_HELP);
447+
color_fprintf_ln(stdout, help_color, "%s", _("Prompt help:"));
448+
color_fprintf_ln(stdout, help_color, "1 - %s",
449+
_("select a numbered item"));
450+
color_fprintf_ln(stdout, help_color, "foo - %s",
451+
_("select item based on unique prefix"));
452+
color_fprintf_ln(stdout, help_color, " - %s",
453+
_("(empty) select nothing"));
454+
}
455+
434456
int run_add_i(struct repository *r, const struct pathspec *ps)
435457
{
436458
struct list_and_choose_options main_loop_opts = {
437459
{ 4, N_("*** Commands ***"), print_command_item, NULL },
438-
N_("What now")
460+
N_("What now"), command_prompt_help
439461
};
440462
struct command_item
441463
status = { { "status" }, run_status };

0 commit comments

Comments
 (0)