changeset 145:fdea1c3acc53

Merge.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 01 Jan 2015 09:22:27 +0200
parents 51eec969b07a (current diff) c878cdcfea9d (diff)
children 0d2a46526349
files th_args.c th_util.c th_util.h
diffstat 3 files changed, 40 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/th_args.c	Thu Jan 01 09:22:08 2015 +0200
+++ b/th_args.c	Thu Jan 01 09:22:27 2015 +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);
     }
 }
--- a/th_util.c	Thu Jan 01 09:22:08 2015 +0200
+++ b/th_util.c	Thu Jan 01 09:22:27 2015 +0200
@@ -56,6 +56,24 @@
 }
 
 
+int th_term_width()
+{
+    char *var = getenv("COLUMNS");
+    int res = (var != NULL) ? atoi(var) : 80;
+    if (res < 5) res = 80;
+    return res;
+}
+
+
+int th_term_height()
+{
+    char *var = getenv("LINESS");
+    int res = (var != NULL) ? atoi(var) : 25;
+    if (res < 1) res = 1;
+    return res;
+}
+
+
 /* Print formatted error, warning and information messages
  * TODO: Implement th_vfprintf() and friends?
  */
--- a/th_util.h	Thu Jan 01 09:22:08 2015 +0200
+++ b/th_util.h	Thu Jan 01 09:22:27 2015 +0200
@@ -120,6 +120,9 @@
                char *author, char *license);
 void    th_print_banner(FILE *outFile, const char *binName, const char *usage);
 
+int     th_term_width();
+int     th_term_height();
+
 int     th_get_error();
 int     th_errno_to_error(int error);
 const char *th_error_str(int error);