Mercurial > hg > dmlib
diff src/dmargs.c @ 2402:b7cd5dd0b82e
Merge one more change from th-libs args processing.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 12 Jan 2020 20:50:45 +0200 |
parents | 263093248f26 |
children | 8d057a2459e3 |
line wrap: on
line diff
--- a/src/dmargs.c Sun Jan 12 19:32:29 2020 +0200 +++ b/src/dmargs.c Sun Jan 12 20:50:45 2020 +0200 @@ -254,7 +254,9 @@ } -static void dmArgsPrintHelpPrintItem(FILE *fh, const DMOptArg *opt, int *lineWidth, const int maxLineWidth, const BOOL doPrint) +static void dmArgsPrintHelpPrintItem(FILE *fh, const DMOptArg *opt, + int *optWidth, const int maxOptWidth, const int termWidth, + const BOOL doPrint) { const char *arg = dmArgsGetOptArg(opt); char fmtBuf[32]; @@ -274,16 +276,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; } @@ -292,7 +294,7 @@ fputs(fmtBuf, fh); dmPrintPad(fh, padWidth, ' '); } - *lineWidth += padWidth; + *optWidth += padWidth; if (hasLongOpt) { @@ -307,19 +309,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); dmPrintPad(fh, padWidth, ' '); - dmPrintWrap(fh, *lineWidth, *lineWidth, 79, opt->desc); + dmPrintWrap(fh, *optWidth, *optWidth, termWidth, opt->desc); } } @@ -330,30 +332,30 @@ * @param opts options list array * @param nopts number of elements in options list array * @param flags flags (currently unused) + * @param width width of the terminal or desired width of the print out */ void dmArgsPrintHelp(FILE *fh, const DMOptArg *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; - dmArgsPrintHelpPrintItem(NULL, &opts[index], &lineWidth, 0, FALSE); - if (lineWidth > maxLineWidth) - maxLineWidth = lineWidth; + int optWidth = 0; + dmArgsPrintHelpPrintItem(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; - dmArgsPrintHelpPrintItem(fh, &opts[index], &lineWidth, maxLineWidth, TRUE); + int optWidth; + dmArgsPrintHelpPrintItem(fh, &opts[index], &optWidth, maxOptWidth, width, TRUE); } }