# HG changeset patch # User Matti Hamalainen # Date 1578853523 -7200 # Node ID 041c8d3030e03a48c8c95b6061ed93db68a9ad00 # Parent f2aa3c80924716d89acd11751c13c3fd4aae8602 Add new argument to th_args_help() to specify the desired print width. diff -r f2aa3c809247 -r 041c8d3030e0 th_args.c --- a/th_args.c Sun Jan 12 20:17:39 2020 +0200 +++ b/th_args.c Sun Jan 12 20:25:23 2020 +0200 @@ -256,7 +256,9 @@ } -static void th_args_help_print_item(FILE *fh, const th_optarg *opt, int *lineWidth, const int maxLineWidth, const BOOL doPrint) +static void th_args_help_print_item(FILE *fh, const th_optarg *opt, + int *optWidth, const int maxOptWidth, const int termWidth, + const BOOL doPrint) { const char *arg = th_args_get_optarg(opt); char fmtBuf[32]; @@ -276,16 +278,16 @@ opt->o_short); } - *lineWidth = strlen(fmtBuf); + *optWidth = strlen(fmtBuf); if (doPrint) - padWidth = hasLongOpt ? 2 : maxLineWidth - *lineWidth; + padWidth = hasLongOpt ? 2 : maxOptWidth - *optWidth; else padWidth = 2; } else { fmtBuf[0] = 0; - *lineWidth = 0; + *optWidth = 0; padWidth = 4 + 2; } @@ -294,7 +296,7 @@ fputs(fmtBuf, fh); th_print_pad(fh, padWidth, ' '); } - *lineWidth += padWidth; + *optWidth += padWidth; if (hasLongOpt) { @@ -309,19 +311,19 @@ opt->o_long); } - *lineWidth += strlen(fmtBuf); + *optWidth += strlen(fmtBuf); } else fmtBuf[0] = 0; if (doPrint) { - padWidth = hasLongOpt ? maxLineWidth - *lineWidth : 0; - *lineWidth += padWidth; + padWidth = hasLongOpt ? maxOptWidth - *optWidth : 0; + *optWidth += padWidth; fputs(fmtBuf, fh); th_print_pad(fh, padWidth, ' '); - th_print_wrap(fh, *lineWidth, *lineWidth, th_term_width() - 2, opt->desc); + th_print_wrap(fh, *optWidth, *optWidth, termWidth, opt->desc); } } @@ -334,28 +336,27 @@ * @param flags flags (currently unused) */ void th_args_help(FILE *fh, const th_optarg *opts, - const int nopts, const int flags) + const int nopts, const int flags, const int width) { - int index, maxLineWidth; - + int index, maxOptWidth; (void) flags; // Determine width of the options and arguments - maxLineWidth = 0; + maxOptWidth = 0; for (index = 0; index < nopts; index++) { - int lineWidth = 0; - th_args_help_print_item(NULL, &opts[index], &lineWidth, 0, FALSE); - if (lineWidth > maxLineWidth) - maxLineWidth = lineWidth; + int optWidth = 0; + th_args_help_print_item(NULL, &opts[index], &optWidth, 0, width, FALSE); + if (optWidth > maxOptWidth) + maxOptWidth = optWidth; } - maxLineWidth += 2; + maxOptWidth += 2; // Print out the formatted option list for (index = 0; index < nopts; index++) { - int lineWidth; - th_args_help_print_item(fh, &opts[index], &lineWidth, maxLineWidth, TRUE); + int optWidth; + th_args_help_print_item(fh, &opts[index], &optWidth, maxOptWidth, width, TRUE); } } diff -r f2aa3c809247 -r 041c8d3030e0 th_args.h --- a/th_args.h Sun Jan 12 20:17:39 2020 +0200 +++ b/th_args.h Sun Jan 12 20:25:23 2020 +0200 @@ -52,7 +52,7 @@ BOOL (*handle_other)(char *), const int flags); void th_args_help(FILE *fh, const th_optarg *opts, - const int nopts, const int flags); + const int nopts, const int flags, const int width); void th_print_wrap(FILE *fh, const int spad, const int rpad, const int width, const char *str);