help: reuse print_columns() for help -a
"help -a" also respects column.ui (and column.help if presents) Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Apr 13, 2012 at 17:54 UTC
dbfae689690bead0e49130b149c869834795cf81
3 files changed
+30
-38
builtin/help.c
+6
-1
@@ -9,6 +9,7 @@
9
#include "common-cmds.h"
10
#include "parse-options.h"
11
#include "run-command.h"
12
+#include "column.h"
13
#include "help.h"
14
15
static struct man_viewer_list {
@@ -30,6 +31,7 @@ enum help_format {
31
};
32
33
static int show_all = 0;
34
+static unsigned int colopts;
35
static enum help_format help_format = HELP_FORMAT_NONE;
36
static struct option builtin_help_options[] = {
37
OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
@@ -251,6 +253,8 @@ static int add_man_viewer_info(const char *var, const char *value)
253
254
static int git_help_config(const char *var, const char *value, void *cb)
255
{
256
+ if (!prefixcmp(var, "column."))
257
+ return git_column_config(var, value, "help", &colopts);
258
if (!strcmp(var, "help.format")) {
259
if (!value)
260
return config_error_nonbool(var);
@@ -424,8 +428,9 @@ int cmd_help(int argc, const char **argv, const char *prefix)
428
parsed_help_format = help_format;
429
430
if (show_all) {
431
+ git_config(git_help_config, NULL);
432
printf("usage: %s\n\n", git_usage_string);
428
- list_commands("git commands", &main_cmds, &other_cmds);
433
+ list_commands("git commands", colopts, &main_cmds, &other_cmds);
434
printf("%s\n", git_more_info_string);
435
return 0;
436
}
help.c
+23
-36
@@ -4,6 +4,8 @@
4
#include "levenshtein.h"
5
#include "help.h"
6
#include "common-cmds.h"
7
+#include "string-list.h"
8
+#include "column.h"
9
10
void add_cmdname(struct cmdnames *cmds, const char *name, int len)
11
{
@@ -70,31 +72,25 @@ void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
72
cmds->cnt = cj;
73
}
74
73
-static void pretty_print_string_list(struct cmdnames *cmds, int longest)
75
+static void pretty_print_string_list(struct cmdnames *cmds,
76
+ unsigned int colopts)
77
{
75
- int cols = 1, rows;
76
- int space = longest + 1; /* min 1 SP between words */
77
- int max_cols = term_columns() - 1; /* don't print *on* the edge */
78
- int i, j;
79
-
80
- if (space < max_cols)
81
- cols = max_cols / space;
82
- rows = DIV_ROUND_UP(cmds->cnt, cols);
83
-
84
- for (i = 0; i < rows; i++) {
85
- printf(" ");
78
+ struct string_list list = STRING_LIST_INIT_NODUP;
79
+ struct column_options copts;
80
+ int i;
81
87
- for (j = 0; j < cols; j++) {
88
- int n = j * rows + i;
89
- int size = space;
90
- if (n >= cmds->cnt)
91
- break;
92
- if (j == cols-1 || n + rows >= cmds->cnt)
93
- size = 1;
94
- printf("%-*s", size, cmds->names[n]->name);
95
- }
96
- putchar('\n');
97
- }
82
+ for (i = 0; i < cmds->cnt; i++)
83
+ string_list_append(&list, cmds->names[i]->name);
84
+ /*
85
+ * always enable column display, we only consult column.*
86
+ * about layout strategy and stuff
87
+ */
88
+ colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
89
+ memset(&copts, 0, sizeof(copts));
90
+ copts.indent = " ";
91
+ copts.padding = 2;
92
+ print_columns(&list, colopts, &copts);
93
+ string_list_clear(&list, 0);
94
}
95
96
static int is_executable(const char *name)
@@ -203,25 +199,16 @@ void load_command_list(const char *prefix,
199
exclude_cmds(other_cmds, main_cmds);
200
}
201
206
-void list_commands(const char *title, struct cmdnames *main_cmds,
207
- struct cmdnames *other_cmds)
202
+void list_commands(const char *title, unsigned int colopts,
203
+ struct cmdnames *main_cmds, struct cmdnames *other_cmds)
204
{
209
- int i, longest = 0;
210
-
211
- for (i = 0; i < main_cmds->cnt; i++)
212
- if (longest < main_cmds->names[i]->len)
213
- longest = main_cmds->names[i]->len;
214
- for (i = 0; i < other_cmds->cnt; i++)
215
- if (longest < other_cmds->names[i]->len)
216
- longest = other_cmds->names[i]->len;
217
-
205
if (main_cmds->cnt) {
206
const char *exec_path = git_exec_path();
207
printf("available %s in '%s'\n", title, exec_path);
208
printf("----------------");
209
mput_char('-', strlen(title) + strlen(exec_path));
210
putchar('\n');
224
- pretty_print_string_list(main_cmds, longest);
211
+ pretty_print_string_list(main_cmds, colopts);
212
putchar('\n');
213
}
214
@@ -230,7 +217,7 @@ void list_commands(const char *title, struct cmdnames *main_cmds,
217
printf("---------------------------------------");
218
mput_char('-', strlen(title));
219
putchar('\n');
233
- pretty_print_string_list(other_cmds, longest);
220
+ pretty_print_string_list(other_cmds, colopts);
221
putchar('\n');
222
}
223
}
help.h
+1
-1
@@ -25,7 +25,7 @@ extern void add_cmdname(struct cmdnames *cmds, const char *name, int len);
25
/* Here we require that excludes is a sorted list. */
26
extern void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes);
27
extern int is_in_cmdlist(struct cmdnames *cmds, const char *name);
28
-extern void list_commands(const char *title,
28
+extern void list_commands(const char *title, unsigned int colopts,
29
struct cmdnames *main_cmds,
30
struct cmdnames *other_cmds);
31