changeset 589:f2aa3c809247

Use TIOCGWINSZ ioctl on *NIX to get terminal dimensions.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 12 Jan 2020 20:17:39 +0200
parents ef71ef9b5862
children 041c8d3030e0
files th_util.c
diffstat 1 files changed, 19 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/th_util.c	Sun Jan 12 19:30:05 2020 +0200
+++ b/th_util.c	Sun Jan 12 20:17:39 2020 +0200
@@ -8,6 +8,9 @@
 #include "th_util.h"
 #include <stdio.h>
 #include <errno.h>
+#ifdef TH_PLAT_UNIX
+#  include <sys/ioctl.h>
+#endif
 
 
 /* Default settings
@@ -76,19 +79,35 @@
 
 int th_term_width()
 {
+#ifdef TH_PLAT_UNIX
+    struct winsize cwz;
+    ioctl(0, TIOCGWINSZ, &cwz);
+    if (cwz.ws_col < 5)
+        cwz.ws_col = 80;
+    return cwz.ws_col;
+#else
     char *var = getenv("COLUMNS");
     int res = (var != NULL) ? atoi(var) : 80;
     if (res < 5) res = 80;
     return res;
+#endif
 }
 
 
 int th_term_height()
 {
+#ifdef TH_PLAT_UNIX
+    struct winsize cwz;
+    ioctl(0, TIOCGWINSZ, &cwz);
+    if (cwz.ws_row < 5)
+        cwz.ws_row = 25;
+    return cwz.ws_row;
+#else
     char *var = getenv("LINES");
     int res = (var != NULL) ? atoi(var) : 25;
     if (res < 1) res = 1;
     return res;
+#endif
 }