@@ -737,6 +737,7 @@ int cli_done(struct cli_def *cli) {
737737
738738 if (cli -> buildmode ) cli_int_free_buildmode (cli );
739739 cli_unregister_tree (cli , cli -> commands , CLI_ANY_COMMAND );
740+ free_z (cli -> promptchar );
740741 free_z (cli -> modestring );
741742 free_z (cli -> banner );
742743 free_z (cli -> promptchar );
@@ -962,7 +963,7 @@ void cli_get_completions(struct cli_def *cli, const char *command, char lastchar
962963 }
963964 }
964965 if (asprintf (& nameptr , "%s%s%s" , delim_start , c -> command , delim_end ) != -1 ) {
965- if (asprintf (& strptr , " %-20s " , nameptr ) != -1 ) {
966+ if (asprintf (& strptr , " %s " , nameptr ) != -1 ) {
966967 cli_int_wrap_help_line (strptr , c -> help , comphelp );
967968 free_z (strptr );
968969 }
@@ -3106,9 +3107,22 @@ int cli_int_execute_pipeline(struct cli_def *cli, struct cli_pipeline *pipeline)
31063107}
31073108
31083109/*
3109- * Attemp quick dirty wrapping of helptext taking into account the offset from name, embedded
3110- * cr/lf in helptext, and trying to split on last white-text before the margin
3110+ * Attempt quick dirty wrapping of helptext taking into account the offset from name, embedded
3111+ * cr/lf in helptext, and trying to split on last white-text before the right margin. If there is
3112+ * no identifiable whitespace to split on, then the split will be done on the last character to fit
3113+ * that line (currently max line with is 80 characters).
3114+ * The firstcolumn width will be a greater of 22 characters or the width of nameptr, which ever is
3115+ * greater, and will be offset from the rest of the line by one space. However, if nameptr is
3116+ * greater than 22 characters it will be put on a line by itself. The first column will be formatted
3117+ * as spaces (22 of em) for all subsequent lines.
3118+ .
3119+ * This routine assumes any 'indenting' of the nameptr field has already been done, and is solely
3120+ * concerned about wrapping the combination of nameptr and helpptr to look 'nice'.
31113121 */
3122+
3123+ #define MAX (a ,b ) ((a) >(b) ? (a) : (b))
3124+ #define MAXWIDTHCOL1 22
3125+
31123126void cli_int_wrap_help_line (char * nameptr , char * helpptr , struct cli_comphelp * comphelp ) {
31133127 int maxwidth = 80 ; // temporary assumption, to be fixed later when libcli 'understands' screen dimensions
31143128 int availwidth ;
@@ -3117,8 +3131,6 @@ void cli_int_wrap_help_line(char *nameptr, char *helpptr, struct cli_comphelp *c
31173131 char * crlf ;
31183132 char * line ;
31193133 char emptystring [] = "" ;
3120- namewidth = strlen (nameptr );
3121- availwidth = maxwidth - namewidth ;
31223134
31233135 if (!helpptr ) helpptr = emptystring ;
31243136 /*
@@ -3128,13 +3140,15 @@ void cli_int_wrap_help_line(char *nameptr, char *helpptr, struct cli_comphelp *c
31283140 */
31293141
31303142 do {
3131- // note - 22 is used because name is always indented 2 spaces
3132- if ((nameptr != emptystring ) && (namewidth > 22 )) {
3143+ if ((nameptr != emptystring ) && (strlen (nameptr ) > MAXWIDTHCOL1 )) {
31333144 if (asprintf (& line , "%s" , nameptr ) < 0 ) break ;
31343145 cli_add_comphelp_entry (comphelp , line );
3146+ free_z (line );
31353147 nameptr = emptystring ;
3136- namewidth = 22 ;
3148+ namewidth = MAXWIDTHCOL1 ;
31373149 }
3150+ namewidth = MAX (MAXWIDTHCOL1 ,strlen (nameptr ));
3151+ availwidth = maxwidth - namewidth - 1 ; // subtract 1 for space separating col1 from rest of line
31383152 toprint = strlen (helpptr );
31393153 if (toprint > availwidth ) {
31403154 toprint = availwidth ;
@@ -3152,7 +3166,7 @@ void cli_int_wrap_help_line(char *nameptr, char *helpptr, struct cli_comphelp *c
31523166 }
31533167 }
31543168
3155- if (asprintf (& line , "%*.*s%.*s" , namewidth , namewidth , nameptr , toprint , helpptr ) < 0 ) break ;
3169+ if (asprintf (& line , "%- *.*s %.*s" , namewidth , namewidth , nameptr , toprint , helpptr ) < 0 ) break ;
31563170 cli_add_comphelp_entry (comphelp , line );
31573171 free_z (line );
31583172
@@ -3267,7 +3281,7 @@ static void cli_get_optarg_comphelp(struct cli_def *cli, struct cli_optarg *opta
32673281 do {
32683282 char * leftcolumn ;
32693283 if (asprintf (& tname , "%s%s%s" , delim_start , nameptr , delim_end ) == -1 ) break ;
3270- if (asprintf (& leftcolumn , "%*.*s%-20s " , indent , indent , "" , tname ) == -1 ) break ;
3284+ if (asprintf (& leftcolumn , "%*.*s%s " , indent , indent , "" , tname ) == -1 ) break ;
32713285
32723286 cli_int_wrap_help_line (leftcolumn , helpptr , comphelp );
32733287
0 commit comments