4
4
#include "levenshtein.h"
5
5
#include "help.h"
6
6
#include "common-cmds.h"
7
+ #include "string-list.h"
8
+ #include "column.h"
7
9
8
10
void add_cmdname (struct cmdnames * cmds , const char * name , int len )
9
11
{
@@ -70,31 +72,25 @@ void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
70
72
cmds -> cnt = cj ;
71
73
}
72
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 )
74
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 ;
86
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 );
98
94
}
99
95
100
96
static int is_executable (const char * name )
@@ -203,25 +199,16 @@ void load_command_list(const char *prefix,
203
199
exclude_cmds (other_cmds , main_cmds );
204
200
}
205
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 )
208
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
-
218
205
if (main_cmds -> cnt ) {
219
206
const char * exec_path = git_exec_path ();
220
207
printf ("available %s in '%s'\n" , title , exec_path );
221
208
printf ("----------------" );
222
209
mput_char ('-' , strlen (title ) + strlen (exec_path ));
223
210
putchar ('\n' );
224
- pretty_print_string_list (main_cmds , longest );
211
+ pretty_print_string_list (main_cmds , colopts );
225
212
putchar ('\n' );
226
213
}
227
214
@@ -230,7 +217,7 @@ void list_commands(const char *title, struct cmdnames *main_cmds,
230
217
printf ("---------------------------------------" );
231
218
mput_char ('-' , strlen (title ));
232
219
putchar ('\n' );
233
- pretty_print_string_list (other_cmds , longest );
220
+ pretty_print_string_list (other_cmds , colopts );
234
221
putchar ('\n' );
235
222
}
236
223
}
0 commit comments