Re: [PATCH v6 4/7] git-clean: use a git-add-interactive compatible UI

2013-05-07 Thread Jiang Xin
2013/5/7 Junio C Hamano gits...@pobox.com:
 What is this message trying to achieve?  self review???

 A bit puzzled

Maybe I should send a new rerolled patch series after this. Yesterday I
wanted to wait for a while to see suggestions and reviews from others.

-- 
Jiang Xin
--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v6 4/7] git-clean: use a git-add-interactive compatible UI

2013-05-06 Thread Jiang Xin
Rewrite menu using a new method `list_and_choose`, which is borrowed
from `git-add--interactive.perl`. We can reused this method later for
more actions.

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@gmail.com
---
 builtin/clean.c | 410 ++--
 1 file changed, 367 insertions(+), 43 deletions(-)

diff --git a/builtin/clean.c b/builtin/clean.c
index 6bda3..3b9f3 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -16,6 +16,35 @@
 #include column.h
 #include color.h
 
+#define MENU_OPTS_SINGLETON01
+#define MENU_OPTS_IMMEDIATE02
+#define MENU_OPTS_LIST_ONLY04
+
+#define MENU_RETURN_NO_LOOP10
+
+struct menu_opts {
+   const char *header;
+   const char *prompt;
+   int flag;
+};
+
+enum menu_stuff_type {
+   MENU_STUFF_TYPE_STRING_LIST = 1,
+   MENU_STUFF_TYPE_MENU_ITEM
+};
+
+struct menu_stuff {
+   enum menu_stuff_type type;
+   int nr;
+   void *stuff;
+};
+
+struct menu_item {
+   char hotkey;
+   char *title;
+   int (*fn)();
+};
+
 static int force = -1; /* unset */
 static int interactive;
 static struct string_list del_list = STRING_LIST_INIT_DUP;
@@ -240,12 +269,284 @@ void pretty_print_dels()
copts.indent =   ;
copts.padding = 2;
print_columns(list, colopts, copts);
-   putchar('\n');
strbuf_release(buf);
string_list_clear(list, 0);
 }
 
-void edit_by_patterns_cmd()
+void pretty_print_menus(struct string_list *menu_list)
+{
+   struct strbuf buf = STRBUF_INIT;
+   unsigned int local_colopts = 0;
+   struct column_options copts;
+
+   /*
+* always enable column display, we only consult column.*
+* about layout strategy and stuff
+*/
+   local_colopts = COL_ENABLED | COL_ROW;
+   memset(copts, 0, sizeof(copts));
+   copts.indent =   ;
+   copts.padding = 2;
+   print_columns(menu_list, local_colopts, copts);
+   strbuf_release(buf);
+}
+
+void prompt_help_cmd(int singleton)
+{
+   clean_print_color(CLEAN_COLOR_HELP);
+   printf_ln(singleton ?
+ _(Prompt help:\n
+   1  - select a numbered item\n
+   foo- select item based on unique prefix\n
+  - (empty) select nothing) :
+ _(Prompt help:\n
+   1  - select a single item\n
+   3-5- select a range of items\n
+   2-3,6-9- select multiple ranges\n
+   foo- select item based on unique prefix\n
+   -...   - unselect specified items\n
+   *  - choose all items\n
+  - (empty) finish selecting));
+   clean_print_color(CLEAN_COLOR_RESET);
+}
+
+/*
+ * Implement a git-add-interactive compatible UI, which is borrowed
+ * from git-add--interactive.perl.
+ *
+ * Return value:
+ *
+ *   - Return an array of integers
+ *   - , and it is up to you to free the allocated memory.
+ *   - The array ends with EOF.
+ *   - If user pressed CTRL-D (i.e. EOF), no selection returned.
+ */
+int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)
+{
+   static struct string_list menu_list = STRING_LIST_INIT_DUP;
+   struct strbuf menu = STRBUF_INIT;
+   struct strbuf choice = STRBUF_INIT;
+   struct strbuf **choice_list;
+   int *chosen, *result;
+   char *p;
+   int nr = 0;
+   int i, j;
+   int eof = 0;
+
+   chosen = xmalloc(sizeof(int) * stuff-nr);
+   memset(chosen, 0, sizeof(int) * stuff-nr);
+
+   while (1) {
+   int i = 0, j = 0;
+   string_list_clear(menu_list, 0);
+
+   if (opts-header) {
+   printf_ln(%s%s%s,
+ clean_get_color(CLEAN_COLOR_HEADER),
+ opts-header,
+ clean_get_color(CLEAN_COLOR_RESET));
+   }
+
+   /* highlight hotkey in menu */
+   if (MENU_STUFF_TYPE_MENU_ITEM == stuff-type) {
+   struct menu_item *item;
+
+   item = (struct menu_item *)stuff-stuff;
+   for (i = 0; i  stuff-nr; i++, item++) {
+   p = item-title;
+   strbuf_addf(menu, %s%2d: , chosen[i] ? * : 
 , i+1);
+   for (; *p; p++) {
+   if (*p == item-hotkey) {
+   strbuf_addstr(menu, 
clean_get_color(CLEAN_COLOR_PROMPT));
+   strbuf_addch(menu, 

Re: [PATCH v6 4/7] git-clean: use a git-add-interactive compatible UI

2013-05-06 Thread Jiang Xin
2013/5/7 Jiang Xin worldhello@gmail.com:
 Rewrite menu using a new method `list_and_choose`, which is borrowed
 from `git-add--interactive.perl`. We can reused this method later for
 more actions.

 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@gmail.com
 ---
  builtin/clean.c | 410 
 ++--
  1 file changed, 367 insertions(+), 43 deletions(-)

 diff --git a/builtin/clean.c b/builtin/clean.c
 index 6bda3..3b9f3 100644
 --- a/builtin/clean.c
 +++ b/builtin/clean.c
 @@ -16,6 +16,35 @@
  #include column.h
  #include color.h

 +#define MENU_OPTS_SINGLETON01
 +#define MENU_OPTS_IMMEDIATE02
 +#define MENU_OPTS_LIST_ONLY04
 +
 +#define MENU_RETURN_NO_LOOP10
 +
 +struct menu_opts {
 +   const char *header;
 +   const char *prompt;
 +   int flag;
 +};
 +
 +enum menu_stuff_type {
 +   MENU_STUFF_TYPE_STRING_LIST = 1,
 +   MENU_STUFF_TYPE_MENU_ITEM
 +};
 +
 +struct menu_stuff {
 +   enum menu_stuff_type type;
 +   int nr;
 +   void *stuff;
 +};
 +
 +struct menu_item {
 +   char hotkey;
 +   char *title;
 +   int (*fn)();
 +};
 +
  static int force = -1; /* unset */
  static int interactive;
  static struct string_list del_list = STRING_LIST_INIT_DUP;
 @@ -240,12 +269,284 @@ void pretty_print_dels()
 copts.indent =   ;
 copts.padding = 2;
 print_columns(list, colopts, copts);
 -   putchar('\n');
 strbuf_release(buf);
 string_list_clear(list, 0);
  }

 -void edit_by_patterns_cmd()
 +void pretty_print_menus(struct string_list *menu_list)
 +{
 +   struct strbuf buf = STRBUF_INIT;
unused buf should be deleted.

 +   unsigned int local_colopts = 0;
 +   struct column_options copts;
 +
 +   /*
 +* always enable column display, we only consult column.*
 +* about layout strategy and stuff
 +*/
remove the above comments.

 +   local_colopts = COL_ENABLED | COL_ROW;
 +   memset(copts, 0, sizeof(copts));
 +   copts.indent =   ;
 +   copts.padding = 2;
 +   print_columns(menu_list, local_colopts, copts);
 +   strbuf_release(buf);
remove strbuf_release of unused variable : buf.

 +}
 +
 +void prompt_help_cmd(int singleton)
 +{
 +   clean_print_color(CLEAN_COLOR_HELP);
 +   printf_ln(singleton ?
 + _(Prompt help:\n
 +   1  - select a numbered item\n
 +   foo- select item based on unique prefix\n
 +  - (empty) select nothing) :
 + _(Prompt help:\n
 +   1  - select a single item\n
 +   3-5- select a range of items\n
 +   2-3,6-9- select multiple ranges\n
 +   foo- select item based on unique prefix\n
 +   -...   - unselect specified items\n
 +   *  - choose all items\n
 +  - (empty) finish selecting));
 +   clean_print_color(CLEAN_COLOR_RESET);
 +}
 +
 +/*
 + * Implement a git-add-interactive compatible UI, which is borrowed
 + * from git-add--interactive.perl.
 + *
 + * Return value:
 + *
 + *   - Return an array of integers
 + *   - , and it is up to you to free the allocated memory.
 + *   - The array ends with EOF.
 + *   - If user pressed CTRL-D (i.e. EOF), no selection returned.
 + */
 +int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)
 +{
 +   static struct string_list menu_list = STRING_LIST_INIT_DUP;
 +   struct strbuf menu = STRBUF_INIT;
 +   struct strbuf choice = STRBUF_INIT;
 +   struct strbuf **choice_list;
 +   int *chosen, *result;
 +   char *p;
 +   int nr = 0;
 +   int i, j;
 +   int eof = 0;
 +
 +   chosen = xmalloc(sizeof(int) * stuff-nr);
 +   memset(chosen, 0, sizeof(int) * stuff-nr);
 +
 +   while (1) {
 +   int i = 0, j = 0;
 +   string_list_clear(menu_list, 0);
 +
 +   if (opts-header) {
 +   printf_ln(%s%s%s,
 + clean_get_color(CLEAN_COLOR_HEADER),
 + opts-header,
 + clean_get_color(CLEAN_COLOR_RESET));
 +   }
 +
 +   /* highlight hotkey in menu */
 +   if (MENU_STUFF_TYPE_MENU_ITEM == stuff-type) {
 +   struct menu_item *item;
 +
 +   item = (struct menu_item *)stuff-stuff;
 +   for (i = 0; i  stuff-nr; i++, item++) {
 +   p = item-title;
 +   strbuf_addf(menu, %s%2d: , chosen[i] ? * 
 :  ,