* Returns the selected index in singleton mode, the number of selected items * otherwise. * * If an error occurred, returns `LIST_AND_CHOOSE_ERROR`. Upon EOF, * `LIST_AND_CHOOSE_QUIT` is returned. */
| 226 | * `LIST_AND_CHOOSE_QUIT` is returned. |
| 227 | */ |
| 228 | static ssize_t list_and_choose(struct add_i_state *s, |
| 229 | struct prefix_item_list *items, |
| 230 | struct list_and_choose_options *opts) |
| 231 | { |
| 232 | int singleton = opts->flags & SINGLETON; |
| 233 | int immediate = opts->flags & IMMEDIATE; |
| 234 | |
| 235 | struct strbuf input = STRBUF_INIT; |
| 236 | ssize_t res = singleton ? LIST_AND_CHOOSE_ERROR : 0; |
| 237 | |
| 238 | if (!singleton) { |
| 239 | free(items->selected); |
| 240 | CALLOC_ARRAY(items->selected, items->items.nr); |
| 241 | } |
| 242 | |
| 243 | if (singleton && !immediate) |
| 244 | BUG("singleton requires immediate"); |
| 245 | |
| 246 | find_unique_prefixes(items); |
| 247 | |
| 248 | for (;;) { |
| 249 | char *p; |
| 250 | |
| 251 | strbuf_reset(&input); |
| 252 | |
| 253 | list(s, &items->items, items->selected, &opts->list_opts); |
| 254 | |
| 255 | color_fprintf(stdout, s->cfg.prompt_color, "%s", opts->prompt); |
| 256 | fputs(singleton ? "> " : ">> ", stdout); |
| 257 | fflush(stdout); |
| 258 | |
| 259 | if (git_read_line_interactively(&input) == EOF) { |
| 260 | putchar('\n'); |
| 261 | if (immediate) |
| 262 | res = LIST_AND_CHOOSE_QUIT; |
| 263 | break; |
| 264 | } |
| 265 | |
| 266 | if (!input.len) |
| 267 | break; |
| 268 | |
| 269 | if (!strcmp(input.buf, "?")) { |
| 270 | opts->print_help(s); |
| 271 | continue; |
| 272 | } |
| 273 | |
| 274 | p = input.buf; |
| 275 | for (;;) { |
| 276 | size_t sep = strcspn(p, " \t\r\n,"); |
| 277 | int choose = 1; |
| 278 | /* `from` is inclusive, `to` is exclusive */ |
| 279 | ssize_t from = -1, to = -1; |
| 280 | |
| 281 | if (!sep) { |
| 282 | if (!*p) |
| 283 | break; |
| 284 | p++; |
| 285 | continue; |
no test coverage detected