From 8574c7f450f93b15c031e975f72e734f4f72a2ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Tue, 5 May 2015 03:42:40 +0200 Subject: [PATCH] degesch: further decoupling from Readline --- degesch.c | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/degesch.c b/degesch.c index 75aff82..2e9286d 100644 --- a/degesch.c +++ b/degesch.c @@ -57,6 +57,11 @@ enum #include #include +#include +#ifndef TIOCGWINSZ +#include +#endif // ! TIOCGWINSZ + #include #include @@ -777,9 +782,6 @@ struct app_context iconv_t term_from_utf8; ///< UTF-8 to terminal encoding iconv_t latin1_to_utf8; ///< ISO Latin 1 to UTF-8 - int lines; ///< Current terminal height - int columns; ///< Current ternimal width - struct input input; ///< User interface } *g_ctx; @@ -1128,9 +1130,33 @@ static struct char *color_set_fg[8]; ///< Codes to set the foreground colour char *color_set_bg[8]; ///< Codes to set the background colour + + int lines; ///< Number of lines + int columns; ///< Number of columns } g_terminal; +static void +update_screen_size (void) +{ +#ifdef TIOCGWINSZ + if (!g_terminal.stdout_is_tty) + return; + + struct winsize size; + if (!ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &size)) + { + char *row = getenv ("LINES"); + char *col = getenv ("COLUMNS"); + unsigned long tmp; + g_terminal.lines = + (row && xstrtoul (&tmp, row, 10)) ? tmp : size.ws_row; + g_terminal.columns = + (col && xstrtoul (&tmp, col, 10)) ? tmp : size.ws_col; + } +#endif // TIOCGWINSZ +} + static bool init_terminal (void) { @@ -1152,6 +1178,10 @@ init_terminal (void) return false; } + g_terminal.lines = tigetnum ("lines"); + g_terminal.columns = tigetnum ("cols"); + update_screen_size (); + for (size_t i = 0; i < N_ELEMENTS (g_terminal.color_set_fg); i++) { g_terminal.color_set_fg[i] = xstrdup (tparm (set_a_foreground, @@ -1261,7 +1291,6 @@ init_colors (struct app_context *ctx) { bool have_ti = init_terminal (); - // Use escape sequences from terminfo if possible, and SGR as a fallback #define INIT_ATTR(id, ti) init_attribute (ctx, ATTR_ ## id, have_ti ? (ti) : "") INIT_ATTR (PROMPT, enter_bold_mode); @@ -1907,7 +1936,7 @@ buffer_activate (struct app_context *ctx, struct buffer *buffer) print_status ("%s", buffer->name); // That is, minus the buffer switch line and the readline prompt - int to_display = MAX (10, ctx->lines - 2); + int to_display = MAX (10, g_terminal.lines - 2); struct buffer_line *line = buffer->lines_tail; while (line && line->prev && --to_display > 0) line = line->prev; @@ -4858,7 +4887,7 @@ on_signal_pipe_readable (const struct pollfd *fd, struct app_context *ctx) if (g_winch_received) { input_on_terminal_resized (&ctx->input); - rl_get_screen_size (&ctx->lines, &ctx->columns); + update_screen_size (); } } @@ -5081,7 +5110,6 @@ main (int argc, char *argv[]) refresh_prompt (&ctx); input_start (&ctx.input); - rl_get_screen_size (&ctx.lines, &ctx.columns); // Connect to the server ASAP poller_timer_set (&ctx.server.reconnect_tmr, 0);