changeset 590:041c8d3030e0

Add new argument to th_args_help() to specify the desired print width.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 12 Jan 2020 20:25:23 +0200
parents f2aa3c809247
children 1e9fa00cc63f
files th_args.c th_args.h
diffstat 2 files changed, 22 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- 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);
     }
 }
--- 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);