mirror of
https://github.com/troglobit/finit.git
synced 2026-10-23 11:44:37 +00:00
util: Import screen_width() developed for the SMCRoute project
This patch introduces the screen_width() method from the SMCRoute project. The code is hereby also available under MIT/X11. Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
1 parent
9bcb3e5778
commit
90793c71a2
2 files changed
+42
-4
No files matched your search
+37
@@ -21,10 +21,17 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
#include <ctype.h> /* isprint() */
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#ifdef HAVE_TERMIOS_H /* for screen_width() */
|
||||
#include <poll.h>
|
||||
#include <stdio.h>
|
||||
#include <termios.h>
|
||||
#endif
|
||||
|
||||
char *prognm = NULL;
|
||||
|
||||
char *progname(char *arg0)
|
||||
@@ -69,6 +76,36 @@ char *sanitize(char *arg, size_t len)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#define ESC "\033"
|
||||
int screen_width(void)
|
||||
{
|
||||
int ret = 80;
|
||||
#ifdef HAVE_TERMIOS_H
|
||||
char buf[42];
|
||||
struct termios tc, saved;
|
||||
struct pollfd fd = { STDIN_FILENO, POLLIN, 0 };
|
||||
|
||||
memset(buf, 0, sizeof(buf));
|
||||
tcgetattr(STDERR_FILENO, &tc);
|
||||
saved = tc;
|
||||
tc.c_cflag |= (CLOCAL | CREAD);
|
||||
tc.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
|
||||
tcsetattr(STDERR_FILENO, TCSANOW, &tc);
|
||||
fprintf(stderr, ESC "7" ESC "[r" ESC "[999;999H" ESC "[6n");
|
||||
|
||||
if (poll(&fd, 1, 300) > 0) {
|
||||
int row, col;
|
||||
|
||||
if (scanf(ESC "[%d;%dR", &row, &col) == 2)
|
||||
ret = col;
|
||||
}
|
||||
|
||||
fprintf(stderr, ESC "8");
|
||||
tcsetattr(STDERR_FILENO, TCSANOW, &saved);
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Local Variables:
|
||||
* indent-tabs-mode: t
|
||||
|
||||
Reference in new issue
Block a user