changeset 1108:fb8467436e6a

Tweak terminal_size to never set either to 0, and return true/false whether it could determine at least one coordinate. (If you set $COLUMNS but not $ROWS, we assume you're happy with the 80x25 default for the other.)
author Rob Landley <rob@landley.net>
date Thu, 07 Nov 2013 09:04:50 -0600
parents bbed38cf7236
children 060217c83f0f
files lib/lib.c lib/lib.h
diffstat 2 files changed, 11 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/lib/lib.c	Sun Nov 03 17:09:33 2013 -0600
+++ b/lib/lib.c	Thu Nov 07 09:04:50 2013 -0600
@@ -482,15 +482,16 @@
 }
 
 // Quick and dirty query size of terminal, doesn't do ANSI probe fallback.
-// set *x=0 and *y=0 before calling to detect failure to set either, or
-// x=80 y=25 to provide defaults
+// set x=80 y=25 before calling to provide defaults. Returns 0 if couldn't
+// determine size.
 
-void terminal_size(unsigned *xx, unsigned *yy)
+int terminal_size(unsigned *xx, unsigned *yy)
 {
   struct winsize ws;
-  unsigned i, x = xx ? *xx : 0, y = yy ? *yy : 0;
+  unsigned i, x = 0, y = 0;
   char *s;
 
+  // stdin, stdout, stderr
   for (i=0; i<3; i++) {
     memset(&ws, 0, sizeof(ws));
     if (!ioctl(i, TIOCGWINSZ, &ws)) {
@@ -505,8 +506,11 @@
   s = getenv("ROWS");
   if (s) sscanf(s, "%u", &y);
 
-  if (xx) *xx = x;
-  if (yy) *yy = y;
+  // Never return 0 for either value, leave it at default instead.
+  if (xx && x) *xx = x;
+  if (yy && y) *yy = y;
+
+  return x || y;
 }
 
 int yesno(char *prompt, int def)
--- a/lib/lib.h	Sun Nov 03 17:09:33 2013 -0600
+++ b/lib/lib.h	Thu Nov 07 09:04:50 2013 -0600
@@ -156,7 +156,7 @@
 void delete_tempfile(int fdin, int fdout, char **tempname);
 void replace_tempfile(int fdin, int fdout, char **tempname);
 void crc_init(unsigned int *crc_table, int little_endian);
-void terminal_size(unsigned *x, unsigned *y);
+int terminal_size(unsigned *x, unsigned *y);
 int yesno(char *prompt, int def);
 void names_to_pid(char **names, int (*callback)(pid_t pid, char *name));