44#include "levenshtein.h"
55#include "help.h"
66#include "common-cmds.h"
7+ #include "string-list.h"
8+ #include "column.h"
79
810void 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
10096static 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}
0 commit comments