diff th_args.c @ 143:c878cdcfea9d

Implement terminal width.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 07 Dec 2014 20:49:10 +0200
parents 0a4fd9cfb929
children fdea1c3acc53
line wrap: on
line diff
--- a/th_args.c	Sat Nov 22 23:37:44 2014 +0200
+++ b/th_args.c	Sun Dec 07 20:49:10 2014 +0200
@@ -179,37 +179,35 @@
 
         // Skip whitespace at line start
         while (th_isspace(str[pos]) || str[pos] == '\n') pos++;
-        
+
         // Handle each word
         while (str[pos] && str[pos] != '\n')
         {
             size_t next;
             int wlen;
+
+            // Find word length and next break
             for (wlen = 0, next = pos; str[next] && !th_isspace(str[next]) && str[next] != '\n'; next++, wlen++);
-//            fprintf(stdout, "X '%c', %d .. linelen=%d/%d, wlen=%d\n", str[pos], pos, linelen, width, wlen);
-            if (linelen + wlen < width)
-            {
-                for (;pos < next; pos++, linelen++)
-                    fputc(str[pos], fh);
+
+            // Check if we have too much of text?
+            if (linelen + wlen >= width)
+                break;
 
-                if (str[next] == '\n' || str[next] == 0)
-                {
-                    fprintf(fh, "\n");
-                    break;
-                }
-                else
-                {
-                    fputc(str[pos], fh);
-                    pos++;
-                    linelen++;
-                }
-            }
+            // Print what we have
+            for (;pos < next; pos++, linelen++)
+                fputc(str[pos], fh);
+
+            // Check if we are at end of input or hard linefeed
+            if (str[next] == '\n' || str[next] == 0)
+                break;
             else
             {
-                fprintf(fh, "\n");
-                break;
+                fputc(str[pos], fh);
+                pos++;
+                linelen++;
             }
         }
+        fprintf(fh, "\n");
     }
 }
 
@@ -250,6 +248,6 @@
 
         fprintf(fh, "%-20s", tmpStr);
 
-        th_print_wrap(fh, opt->desc, 26, 26, 73);
+        th_print_wrap(fh, opt->desc, 26, 26, th_term_width() - 2);
     }
 }