@matheusbsbs / matheus-bagliano / commits / 68db1cbf8e
Fork 0

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 <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Nov 15, 2019 at 11:11 UTC 68db1cbf8eb412e81ff84b18f6c2ab8d15df99c7
1 file changed +21 -1
add-interactive.c
+21 -1
@@ -11,6 +11,7 @@ struct add_i_state {
11 struct repository *r;
12 int use_color;
13 char header_color[COLOR_MAXLEN];
14 + char help_color[COLOR_MAXLEN];
15 };
16
17 static void init_color(struct repository *r, struct add_i_state *s,
@@ -43,6 +44,7 @@ static void init_add_i_state(struct add_i_state *s, struct repository *r)
44 s->use_color = want_color(s->use_color);
45
46 init_color(r, s, "header", s->header_color, GIT_COLOR_BOLD);
47 + init_color(r, s, "help", s->help_color, GIT_COLOR_BOLD_RED);
48 }
49
50 /*
@@ -210,6 +212,7 @@ struct list_and_choose_options {
212 struct list_options list_opts;
213
214 const char *prompt;
215 + void (*print_help)(struct add_i_state *s);
216 };
217
218 #define LIST_AND_CHOOSE_ERROR (-1)
@@ -250,6 +253,11 @@ static ssize_t list_and_choose(struct add_i_state *s,
253 if (!input.len)
254 break;
255
256 + if (!strcmp(input.buf, "?")) {
257 + opts->print_help(s);
258 + continue;
259 + }
260 +
261 p = input.buf;
262 for (;;) {
263 size_t sep = strcspn(p, " \t\r\n,");
@@ -515,12 +523,24 @@ static void print_command_item(int i, struct string_list_item *item,
523 item->string + util->prefix_length);
524 }
525
526 +static void command_prompt_help(struct add_i_state *s)
527 +{
528 + const char *help_color = s->help_color;
529 + color_fprintf_ln(stdout, help_color, "%s", _("Prompt help:"));
530 + color_fprintf_ln(stdout, help_color, "1 - %s",
531 + _("select a numbered item"));
532 + color_fprintf_ln(stdout, help_color, "foo - %s",
533 + _("select item based on unique prefix"));
534 + color_fprintf_ln(stdout, help_color, " - %s",
535 + _("(empty) select nothing"));
536 +}
537 +
538 int run_add_i(struct repository *r, const struct pathspec *ps)
539 {
540 struct add_i_state s = { NULL };
541 struct list_and_choose_options main_loop_opts = {
542 { 4, N_("*** Commands ***"), print_command_item, NULL },
523 - N_("What now")
543 + N_("What now"), command_prompt_help
544 };
545 struct {
546 const char *string;