Skip to content

Commit db97839

Browse files
committed
built-in add -i: implement the main loop
The reason why we did not start with the main loop to begin with is that it is the first user of `list_and_choose()`, which uses the `list()` function that we conveniently introduced for use by the `status` command. Apart from the "and choose" part, there are more differences between the way the `status` command calls the `list_and_choose()` function in the Perl version of `git add -i` compared to the other callers of said function. The most important ones: - The list is not only shown, but the user is also asked to make a choice, possibly selecting multiple entries. - The list of items is prefixed with a marker indicating what items have been selected, if multi-selection is allowed. - Initially, for each item a unique prefix (if there exists any within the given parameters) is determined, and shown in the list, and accepted as a shortcut for the selection. These features will be implemented later, except the part where the user can choose a command. At this stage, though, the built-in `git add -i` still only supports the `status` command, with the remaining commands to follow over the course of the next commits. In addition, we also modify `list()` to support displaying the commands in columns, even if there is currently only one. The Perl script `git-add--interactive.perl` mixed the purposes of the "list" and the "and choose" part into the same function. In the C version, we will keep them separate instead, calling the `list()` function from the `list_and_choose()` function. Note that we only have a prompt ending in a single ">" at this stage; later commits will add commands that display a double ">>" to indicate that the user is in a different loop than the main one. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 83d92a9 commit db97839

File tree

1 file changed

+121
-2
lines changed

1 file changed

+121
-2
lines changed

add-interactive.c

Lines changed: 121 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ struct item {
5151
};
5252

5353
struct list_options {
54+
int columns;
5455
const char *header;
5556
void (*print_item)(int i, struct item *item, void *print_item_data);
5657
void *print_item_data;
@@ -59,7 +60,7 @@ struct list_options {
5960
static void list(struct item **list, size_t nr,
6061
struct add_i_state *s, struct list_options *opts)
6162
{
62-
int i;
63+
int i, last_lf = 0;
6364

6465
if (!nr)
6566
return;
@@ -70,8 +71,91 @@ static void list(struct item **list, size_t nr,
7071

7172
for (i = 0; i < nr; i++) {
7273
opts->print_item(i, list[i], opts->print_item_data);
74+
75+
if ((opts->columns) && ((i + 1) % (opts->columns))) {
76+
putchar('\t');
77+
last_lf = 0;
78+
}
79+
else {
80+
putchar('\n');
81+
last_lf = 1;
82+
}
83+
}
84+
85+
if (!last_lf)
7386
putchar('\n');
87+
}
88+
struct list_and_choose_options {
89+
struct list_options list_opts;
90+
91+
const char *prompt;
92+
};
93+
94+
/*
95+
* Returns the selected index.
96+
*/
97+
static ssize_t list_and_choose(struct item **items, size_t nr,
98+
struct add_i_state *s,
99+
struct list_and_choose_options *opts)
100+
{
101+
struct strbuf input = STRBUF_INIT;
102+
ssize_t res = -1;
103+
104+
for (;;) {
105+
char *p, *endp;
106+
107+
strbuf_reset(&input);
108+
109+
list(items, nr, s, &opts->list_opts);
110+
111+
printf("%s%s", opts->prompt, "> ");
112+
fflush(stdout);
113+
114+
if (strbuf_getline(&input, stdin) == EOF) {
115+
putchar('\n');
116+
res = -2;
117+
break;
118+
}
119+
strbuf_trim(&input);
120+
121+
if (!input.len)
122+
break;
123+
124+
p = input.buf;
125+
for (;;) {
126+
size_t sep = strcspn(p, " \t\r\n,");
127+
ssize_t index = -1;
128+
129+
if (!sep) {
130+
if (!*p)
131+
break;
132+
p++;
133+
continue;
134+
}
135+
136+
if (isdigit(*p)) {
137+
index = strtoul(p, &endp, 10) - 1;
138+
if (endp != p + sep)
139+
index = -1;
140+
}
141+
142+
p[sep] = '\0';
143+
if (index < 0 || index >= nr)
144+
printf(_("Huh (%s)?\n"), p);
145+
else {
146+
res = index;
147+
break;
148+
}
149+
150+
p += sep + 1;
151+
}
152+
153+
if (res >= 0)
154+
break;
74155
}
156+
157+
strbuf_release(&input);
158+
return res;
75159
}
76160

77161
struct adddel {
@@ -285,17 +369,40 @@ static int run_status(struct add_i_state *s, const struct pathspec *ps,
285369
return 0;
286370
}
287371

372+
static void print_command_item(int i, struct item *item,
373+
void *print_command_item_data)
374+
{
375+
printf(" %2d: %s", i + 1, item->name);
376+
}
377+
378+
struct command_item {
379+
struct item item;
380+
int (*command)(struct add_i_state *s, const struct pathspec *ps,
381+
struct file_list *files, struct list_options *opts);
382+
};
383+
288384
int run_add_i(struct repository *r, const struct pathspec *ps)
289385
{
290386
struct add_i_state s = { NULL };
387+
struct list_and_choose_options main_loop_opts = {
388+
{ 4, N_("*** Commands ***"), print_command_item, NULL },
389+
N_("What now")
390+
};
391+
struct command_item
392+
status = { { "status" }, run_status };
393+
struct command_item *commands[] = {
394+
&status
395+
};
396+
291397
struct print_file_item_data print_file_item_data = {
292398
"%12s %12s %s", STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
293399
};
294400
struct list_options opts = {
295-
NULL, print_file_item, &print_file_item_data
401+
0, NULL, print_file_item, &print_file_item_data
296402
};
297403
struct strbuf header = STRBUF_INIT;
298404
struct file_list files = { NULL };
405+
ssize_t i;
299406
int res = 0;
300407

301408
if (init_add_i_state(r, &s))
@@ -310,6 +417,18 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
310417
if (run_status(&s, ps, &files, &opts) < 0)
311418
res = -1;
312419

420+
for (;;) {
421+
i = list_and_choose((struct item **)commands,
422+
ARRAY_SIZE(commands), &s, &main_loop_opts);
423+
if (i < -1) {
424+
printf(_("Bye.\n"));
425+
res = 0;
426+
break;
427+
}
428+
if (i >= 0)
429+
res = commands[i]->command(&s, ps, &files, &opts);
430+
}
431+
313432
release_file_list(&files);
314433
strbuf_release(&print_file_item_data.buf);
315434
strbuf_release(&print_file_item_data.index);

0 commit comments

Comments
 (0)