Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit dbfae68

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3f8eccb commit dbfae68

File tree

3 files changed

+30
-38
lines changed

3 files changed

+30
-38
lines changed

builtin/help.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "common-cmds.h"
1010
#include "parse-options.h"
1111
#include "run-command.h"
12+
#include "column.h"
1213
#include "help.h"
1314

1415
static struct man_viewer_list {
@@ -30,6 +31,7 @@ enum help_format {
3031
};
3132

3233
static int show_all = 0;
34+
static unsigned int colopts;
3335
static enum help_format help_format = HELP_FORMAT_NONE;
3436
static struct option builtin_help_options[] = {
3537
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)
251253

252254
static int git_help_config(const char *var, const char *value, void *cb)
253255
{
256+
if (!prefixcmp(var, "column."))
257+
return git_column_config(var, value, "help", &colopts);
254258
if (!strcmp(var, "help.format")) {
255259
if (!value)
256260
return config_error_nonbool(var);
@@ -424,8 +428,9 @@ int cmd_help(int argc, const char **argv, const char *prefix)
424428
parsed_help_format = help_format;
425429

426430
if (show_all) {
431+
git_config(git_help_config, NULL);
427432
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);
429434
printf("%s\n", git_more_info_string);
430435
return 0;
431436
}

help.c

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "levenshtein.h"
55
#include "help.h"
66
#include "common-cmds.h"
7+
#include "string-list.h"
8+
#include "column.h"
79

810
void add_cmdname(struct cmdnames *cmds, const char *name, int len)
911
{
@@ -70,31 +72,25 @@ void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
7072
cmds->cnt = cj;
7173
}
7274

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)
7477
{
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;
8681

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);
9894
}
9995

10096
static int is_executable(const char *name)
@@ -203,25 +199,16 @@ void load_command_list(const char *prefix,
203199
exclude_cmds(other_cmds, main_cmds);
204200
}
205201

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)
208204
{
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-
218205
if (main_cmds->cnt) {
219206
const char *exec_path = git_exec_path();
220207
printf("available %s in '%s'\n", title, exec_path);
221208
printf("----------------");
222209
mput_char('-', strlen(title) + strlen(exec_path));
223210
putchar('\n');
224-
pretty_print_string_list(main_cmds, longest);
211+
pretty_print_string_list(main_cmds, colopts);
225212
putchar('\n');
226213
}
227214

@@ -230,7 +217,7 @@ void list_commands(const char *title, struct cmdnames *main_cmds,
230217
printf("---------------------------------------");
231218
mput_char('-', strlen(title));
232219
putchar('\n');
233-
pretty_print_string_list(other_cmds, longest);
220+
pretty_print_string_list(other_cmds, colopts);
234221
putchar('\n');
235222
}
236223
}

help.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extern void add_cmdname(struct cmdnames *cmds, const char *name, int len);
2525
/* Here we require that excludes is a sorted list. */
2626
extern void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes);
2727
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,
2929
struct cmdnames *main_cmds,
3030
struct cmdnames *other_cmds);
3131

0 commit comments

Comments
 (0)