Add util.c:screen_init() to probe screen size once per boot

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson committed 2017-07-04 20:58:43 +02:00
1 parent 405cf4d997
commit be6f0de0df
6 files changed
+41 -13

No files matched your search

+26 -6
View File
@@ -32,7 +32,10 @@
#include <termios.h>
#endif
char *prognm = NULL;
int screen_setup = -1;
int screen_rows = 24;
int screen_cols = 80;
char *prognm = NULL;
char *progname(char *arg0)
{
@@ -77,14 +80,16 @@ char *sanitize(char *arg, size_t len)
}
#define ESC "\033"
int screen_width(void)
void screen_init(void)
{
int ret = 80;
#ifdef HAVE_TERMIOS_H
char buf[42];
struct termios tc, saved;
struct pollfd fd = { STDIN_FILENO, POLLIN, 0 };
if (screen_setup != -1)
return;
memset(buf, 0, sizeof(buf));
tcgetattr(STDERR_FILENO, &tc);
saved = tc;
@@ -96,14 +101,29 @@ int screen_width(void)
if (poll(&fd, 1, 300) > 0) {
int row, col;
if (scanf(ESC "[%d;%dR", &row, &col) == 2)
ret = col;
if (scanf(ESC "[%d;%dR", &row, &col) == 2) {
screen_rows = row;
screen_cols = col;
screen_setup = 1;
}
}
fprintf(stderr, ESC "8");
tcsetattr(STDERR_FILENO, TCSANOW, &saved);
#endif
return ret;
if (screen_cols > 132)
screen_cols = 132;
}
int screen_width(void)
{
screen_init();
if (screen_cols < 1)
return 80;
return screen_cols;
}
/**