git-clean: use a git-add-interactive compatible UI
Rewrite menu using a new method `list_and_choose`, which is borrowed from `git-add--interactive.perl`. We will use this framework to add new actions for interactive git-clean later. Please NOTE: * Method `list_and_choose` return an array of integers, and * it is up to you to free the allocated memory of the array. * The array ends with EOF. * If user pressed CTRL-D (i.e. EOF), no selection returned. Signed-off-by: Jiang Xin <worldhello.net@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jiang Xin committed
Jun 25, 2013 at 23:53 UTC
9f93e4611f72577306e369a64d0a4da847be9751
1 file changed
+427
-29
builtin/clean.c
+427
-29
@@ -50,6 +50,36 @@ enum color_clean {
50
CLEAN_COLOR_ERROR = 5,
51
};
52
53
+#define MENU_OPTS_SINGLETON 01
54
+#define MENU_OPTS_IMMEDIATE 02
55
+#define MENU_OPTS_LIST_ONLY 04
56
+
57
+struct menu_opts {
58
+ const char *header;
59
+ const char *prompt;
60
+ int flags;
61
+};
62
+
63
+#define MENU_RETURN_NO_LOOP 10
64
+
65
+struct menu_item {
66
+ char hotkey;
67
+ const char *title;
68
+ int selected;
69
+ int (*fn)();
70
+};
71
+
72
+enum menu_stuff_type {
73
+ MENU_STUFF_TYPE_STRING_LIST = 1,
74
+ MENU_STUFF_TYPE_MENU_ITEM
75
+};
76
+
77
+struct menu_stuff {
78
+ enum menu_stuff_type type;
79
+ int nr;
80
+ void *stuff;
81
+};
82
+
83
static int parse_clean_color_slot(const char *var)
84
{
85
if (!strcasecmp(var, "reset"))
@@ -240,54 +270,422 @@ static void pretty_print_dels(void)
270
copts.indent = " ";
271
copts.padding = 2;
272
print_columns(&list, colopts, &copts);
243
- putchar('\n');
273
strbuf_release(&buf);
274
string_list_clear(&list, 0);
275
}
276
248
-static void interactive_main_loop(void)
277
+static void pretty_print_menus(struct string_list *menu_list)
278
+{
279
+ unsigned int local_colopts = 0;
280
+ struct column_options copts;
281
+
282
+ local_colopts = COL_ENABLED | COL_ROW;
283
+ memset(&copts, 0, sizeof(copts));
284
+ copts.indent = " ";
285
+ copts.padding = 2;
286
+ print_columns(menu_list, local_colopts, &copts);
287
+}
288
+
289
+static void prompt_help_cmd(int singleton)
290
+{
291
+ clean_print_color(CLEAN_COLOR_HELP);
292
+ printf_ln(singleton ?
293
+ _("Prompt help:\n"
294
+ "1 - select a numbered item\n"
295
+ "foo - select item based on unique prefix\n"
296
+ " - (empty) select nothing") :
297
+ _("Prompt help:\n"
298
+ "1 - select a single item\n"
299
+ "3-5 - select a range of items\n"
300
+ "2-3,6-9 - select multiple ranges\n"
301
+ "foo - select item based on unique prefix\n"
302
+ "-... - unselect specified items\n"
303
+ "* - choose all items\n"
304
+ " - (empty) finish selecting"));
305
+ clean_print_color(CLEAN_COLOR_RESET);
306
+}
307
+
308
+/*
309
+ * display menu stuff with number prefix and hotkey highlight
310
+ */
311
+static void print_highlight_menu_stuff(struct menu_stuff *stuff, int **chosen)
312
+{
313
+ struct string_list menu_list = STRING_LIST_INIT_DUP;
314
+ struct strbuf menu = STRBUF_INIT;
315
+ struct strbuf buf = STRBUF_INIT;
316
+ struct menu_item *menu_item;
317
+ struct string_list_item *string_list_item;
318
+ int i;
319
+
320
+ switch (stuff->type) {
321
+ default:
322
+ die("Bad type of menu_staff when print menu");
323
+ case MENU_STUFF_TYPE_MENU_ITEM:
324
+ menu_item = (struct menu_item *)stuff->stuff;
325
+ for (i = 0; i < stuff->nr; i++, menu_item++) {
326
+ const char *p;
327
+ int highlighted = 0;
328
+
329
+ p = menu_item->title;
330
+ if ((*chosen)[i] < 0)
331
+ (*chosen)[i] = menu_item->selected ? 1 : 0;
332
+ strbuf_addf(&menu, "%s%2d: ", (*chosen)[i] ? "*" : " ", i+1);
333
+ for (; *p; p++) {
334
+ if (!highlighted && *p == menu_item->hotkey) {
335
+ strbuf_addstr(&menu, clean_get_color(CLEAN_COLOR_PROMPT));
336
+ strbuf_addch(&menu, *p);
337
+ strbuf_addstr(&menu, clean_get_color(CLEAN_COLOR_RESET));
338
+ highlighted = 1;
339
+ } else {
340
+ strbuf_addch(&menu, *p);
341
+ }
342
+ }
343
+ string_list_append(&menu_list, menu.buf);
344
+ strbuf_reset(&menu);
345
+ }
346
+ break;
347
+ case MENU_STUFF_TYPE_STRING_LIST:
348
+ i = 0;
349
+ for_each_string_list_item(string_list_item, (struct string_list *)stuff->stuff) {
350
+ if ((*chosen)[i] < 0)
351
+ (*chosen)[i] = 0;
352
+ strbuf_addf(&menu, "%s%2d: %s",
353
+ (*chosen)[i] ? "*" : " ", i+1, string_list_item->string);
354
+ string_list_append(&menu_list, menu.buf);
355
+ strbuf_reset(&menu);
356
+ i++;
357
+ }
358
+ break;
359
+ }
360
+
361
+ pretty_print_menus(&menu_list);
362
+
363
+ strbuf_release(&menu);
364
+ strbuf_release(&buf);
365
+ string_list_clear(&menu_list, 0);
366
+}
367
+
368
+/*
369
+ * Parse user input, and return choice(s) for menu (menu_stuff).
370
+ *
371
+ * Input
372
+ * (for single choice)
373
+ * 1 - select a numbered item
374
+ * foo - select item based on menu title
375
+ * - (empty) select nothing
376
+ *
377
+ * (for multiple choice)
378
+ * 1 - select a single item
379
+ * 3-5 - select a range of items
380
+ * 2-3,6-9 - select multiple ranges
381
+ * foo - select item based on menu title
382
+ * -... - unselect specified items
383
+ * * - choose all items
384
+ * - (empty) finish selecting
385
+ *
386
+ * The parse result will be saved in array **chosen, and
387
+ * return number of total selections.
388
+ */
389
+static int parse_choice(struct menu_stuff *menu_stuff,
390
+ int is_single,
391
+ struct strbuf input,
392
+ int **chosen)
393
+{
394
+ struct strbuf **choice_list, **ptr;
395
+ struct menu_item *menu_item;
396
+ struct string_list_item *string_list_item;
397
+ int nr = 0;
398
+ int i;
399
+
400
+ if (is_single) {
401
+ choice_list = strbuf_split_max(&input, '\n', 0);
402
+ } else {
403
+ char *p = input.buf;
404
+ do {
405
+ if (*p == ',')
406
+ *p = ' ';
407
+ } while (*p++);
408
+ choice_list = strbuf_split_max(&input, ' ', 0);
409
+ }
410
+
411
+ for (ptr = choice_list; *ptr; ptr++) {
412
+ char *p;
413
+ int choose = 1;
414
+ int bottom = 0, top = 0;
415
+ int is_range, is_number;
416
+
417
+ strbuf_trim(*ptr);
418
+ if (!(*ptr)->len)
419
+ continue;
420
+
421
+ /* Input that begins with '-'; unchoose */
422
+ if (*(*ptr)->buf == '-') {
423
+ choose = 0;
424
+ strbuf_remove((*ptr), 0, 1);
425
+ }
426
+
427
+ is_range = 0;
428
+ is_number = 1;
429
+ for (p = (*ptr)->buf; *p; p++) {
430
+ if ('-' == *p) {
431
+ if (!is_range) {
432
+ is_range = 1;
433
+ is_number = 0;
434
+ } else {
435
+ is_number = 0;
436
+ is_range = 0;
437
+ break;
438
+ }
439
+ } else if (!isdigit(*p)) {
440
+ is_number = 0;
441
+ is_range = 0;
442
+ break;
443
+ }
444
+ }
445
+
446
+ if (is_number) {
447
+ bottom = atoi((*ptr)->buf);
448
+ top = bottom;
449
+ } else if (is_range) {
450
+ bottom = atoi((*ptr)->buf);
451
+ /* a range can be specified like 5-7 or 5- */
452
+ if (!*(strchr((*ptr)->buf, '-') + 1))
453
+ top = menu_stuff->nr;
454
+ else
455
+ top = atoi(strchr((*ptr)->buf, '-') + 1);
456
+ } else if (!strcmp((*ptr)->buf, "*")) {
457
+ bottom = 1;
458
+ top = menu_stuff->nr;
459
+ } else {
460
+ switch (menu_stuff->type) {
461
+ default:
462
+ die("Bad type of menu_stuff when parse choice");
463
+ case MENU_STUFF_TYPE_MENU_ITEM:
464
+ menu_item = (struct menu_item *)menu_stuff->stuff;
465
+ for (i = 0; i < menu_stuff->nr; i++, menu_item++) {
466
+ if (((*ptr)->len == 1 &&
467
+ *(*ptr)->buf == menu_item->hotkey) ||
468
+ !strcasecmp((*ptr)->buf, menu_item->title)) {
469
+ bottom = i + 1;
470
+ top = bottom;
471
+ break;
472
+ }
473
+ }
474
+ break;
475
+ case MENU_STUFF_TYPE_STRING_LIST:
476
+ string_list_item = ((struct string_list *)menu_stuff->stuff)->items;
477
+ for (i = 0; i < menu_stuff->nr; i++, string_list_item++) {
478
+ if (!strcasecmp((*ptr)->buf, string_list_item->string)) {
479
+ bottom = i + 1;
480
+ top = bottom;
481
+ break;
482
+ }
483
+ }
484
+ break;
485
+ }
486
+ }
487
+
488
+ if (top <= 0 || bottom <= 0 || top > menu_stuff->nr || bottom > top ||
489
+ (is_single && bottom != top)) {
490
+ clean_print_color(CLEAN_COLOR_ERROR);
491
+ printf_ln(_("Huh (%s)?"), (*ptr)->buf);
492
+ clean_print_color(CLEAN_COLOR_RESET);
493
+ continue;
494
+ }
495
+
496
+ for (i = bottom; i <= top; i++)
497
+ (*chosen)[i-1] = choose;
498
+ }
499
+
500
+ strbuf_list_free(choice_list);
501
+
502
+ for (i = 0; i < menu_stuff->nr; i++)
503
+ nr += (*chosen)[i];
504
+ return nr;
505
+}
506
+
507
+/*
508
+ * Implement a git-add-interactive compatible UI, which is borrowed
509
+ * from git-add--interactive.perl.
510
+ *
511
+ * Return value:
512
+ *
513
+ * - Return an array of integers
514
+ * - , and it is up to you to free the allocated memory.
515
+ * - The array ends with EOF.
516
+ * - If user pressed CTRL-D (i.e. EOF), no selection returned.
517
+ */
518
+static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)
519
+{
520
+ struct strbuf choice = STRBUF_INIT;
521
+ int *chosen, *result;
522
+ int nr = 0;
523
+ int eof = 0;
524
+ int i;
525
+
526
+ chosen = xmalloc(sizeof(int) * stuff->nr);
527
+ /* set chosen as uninitialized */
528
+ for (i = 0; i < stuff->nr; i++)
529
+ chosen[i] = -1;
530
+
531
+ for (;;) {
532
+ if (opts->header) {
533
+ printf_ln("%s%s%s",
534
+ clean_get_color(CLEAN_COLOR_HEADER),
535
+ _(opts->header),
536
+ clean_get_color(CLEAN_COLOR_RESET));
537
+ }
538
+
539
+ /* chosen will be initialized by print_highlight_menu_stuff */
540
+ print_highlight_menu_stuff(stuff, &chosen);
541
+
542
+ if (opts->flags & MENU_OPTS_LIST_ONLY)
543
+ break;
544
+
545
+ if (opts->prompt) {
546
+ printf("%s%s%s%s",
547
+ clean_get_color(CLEAN_COLOR_PROMPT),
548
+ _(opts->prompt),
549
+ opts->flags & MENU_OPTS_SINGLETON ? "> " : ">> ",
550
+ clean_get_color(CLEAN_COLOR_RESET));
551
+ }
552
+
553
+ if (strbuf_getline(&choice, stdin, '\n') != EOF) {
554
+ strbuf_trim(&choice);
555
+ } else {
556
+ eof = 1;
557
+ break;
558
+ }
559
+
560
+ /* help for prompt */
561
+ if (!strcmp(choice.buf, "?")) {
562
+ prompt_help_cmd(opts->flags & MENU_OPTS_SINGLETON);
563
+ continue;
564
+ }
565
+
566
+ /* for a multiple-choice menu, press ENTER (empty) will return back */
567
+ if (!(opts->flags & MENU_OPTS_SINGLETON) && !choice.len)
568
+ break;
569
+
570
+ nr = parse_choice(stuff,
571
+ opts->flags & MENU_OPTS_SINGLETON,
572
+ choice,
573
+ &chosen);
574
+
575
+ if (opts->flags & MENU_OPTS_SINGLETON) {
576
+ if (nr)
577
+ break;
578
+ } else if (opts->flags & MENU_OPTS_IMMEDIATE) {
579
+ break;
580
+ }
581
+ }
582
+
583
+ if (eof) {
584
+ result = xmalloc(sizeof(int));
585
+ *result = EOF;
586
+ } else {
587
+ int j = 0;
588
+
589
+ /*
590
+ * recalculate nr, if return back from menu directly with
591
+ * default selections.
592
+ */
593
+ if (!nr) {
594
+ for (i = 0; i < stuff->nr; i++)
595
+ nr += chosen[i];
596
+ }
597
+
598
+ result = xmalloc(sizeof(int) * (nr + 1));
599
+ memset(result, 0, sizeof(int) * (nr + 1));
600
+ for (i = 0; i < stuff->nr && j < nr; i++) {
601
+ if (chosen[i])
602
+ result[j++] = i;
603
+ }
604
+ result[j] = EOF;
605
+ }
606
+
607
+ free(chosen);
608
+ strbuf_release(&choice);
609
+ return result;
610
+}
611
+
612
+static int clean_cmd(void)
613
{
250
- struct strbuf confirm = STRBUF_INIT;
614
+ return MENU_RETURN_NO_LOOP;
615
+}
616
+
617
+static int quit_cmd(void)
618
+{
619
+ string_list_clear(&del_list, 0);
620
+ printf_ln(_("Bye."));
621
+ return MENU_RETURN_NO_LOOP;
622
+}
623
+
624
+static int help_cmd(void)
625
+{
626
+ clean_print_color(CLEAN_COLOR_HELP);
627
+ printf_ln(_(
628
+ "clean - start cleaning\n"
629
+ "quit - stop cleaning\n"
630
+ "help - this screen\n"
631
+ "? - help for prompt selection"
632
+ ));
633
+ clean_print_color(CLEAN_COLOR_RESET);
634
+ return 0;
635
+}
636
637
+static void interactive_main_loop(void)
638
+{
639
while (del_list.nr) {
253
- putchar('\n');
640
+ struct menu_opts menu_opts;
641
+ struct menu_stuff menu_stuff;
642
+ struct menu_item menus[] = {
643
+ {'c', "clean", 0, clean_cmd},
644
+ {'q', "quit", 0, quit_cmd},
645
+ {'h', "help", 0, help_cmd},
646
+ };
647
+ int *chosen;
648
+
649
+ menu_opts.header = N_("*** Commands ***");
650
+ menu_opts.prompt = N_("What now");
651
+ menu_opts.flags = MENU_OPTS_SINGLETON;
652
+
653
+ menu_stuff.type = MENU_STUFF_TYPE_MENU_ITEM;
654
+ menu_stuff.stuff = menus;
655
+ menu_stuff.nr = sizeof(menus) / sizeof(struct menu_item);
656
+
657
clean_print_color(CLEAN_COLOR_HEADER);
658
printf_ln(Q_("Would remove the following item:",
659
"Would remove the following items:",
660
del_list.nr));
661
clean_print_color(CLEAN_COLOR_RESET);
259
- putchar('\n');
662
663
pretty_print_dels();
664
263
- clean_print_color(CLEAN_COLOR_PROMPT);
264
- printf(_("Remove [y/n]? "));
265
- clean_print_color(CLEAN_COLOR_RESET);
266
- if (strbuf_getline(&confirm, stdin, '\n') != EOF) {
267
- strbuf_trim(&confirm);
268
- } else {
269
- /* Ctrl-D is the same as "quit" */
270
- string_list_clear(&del_list, 0);
271
- putchar('\n');
272
- printf_ln("Bye.");
273
- break;
274
- }
275
-
276
- if (confirm.len) {
277
- if (!strncasecmp(confirm.buf, "yes", confirm.len)) {
278
- break;
279
- } else if (!strncasecmp(confirm.buf, "no", confirm.len) ||
280
- !strncasecmp(confirm.buf, "quit", confirm.len)) {
281
- string_list_clear(&del_list, 0);
282
- printf_ln("Bye.");
283
- break;
284
- } else {
665
+ chosen = list_and_choose(&menu_opts, &menu_stuff);
666
+
667
+ if (*chosen != EOF) {
668
+ int ret;
669
+ ret = menus[*chosen].fn();
670
+ if (ret != MENU_RETURN_NO_LOOP) {
671
+ free(chosen);
672
+ chosen = NULL;
673
+ if (!del_list.nr) {
674
+ clean_print_color(CLEAN_COLOR_ERROR);
675
+ printf_ln(_("No more files to clean, exiting."));
676
+ clean_print_color(CLEAN_COLOR_RESET);
677
+ break;
678
+ }
679
continue;
680
}
681
+ } else {
682
+ quit_cmd();
683
}
288
- }
684
290
- strbuf_release(&confirm);
685
+ free(chosen);
686
+ chosen = NULL;
687
+ break;
688
+ }
689
}
690
691
int cmd_clean(int argc, const char **argv, const char *prefix)