degesch: further decoupling from Readline

This commit is contained in:
Přemysl Eric Janouch 2015-05-05 03:42:40 +02:00
parent 87843f47e4
commit 8574c7f450
1 changed files with 35 additions and 7 deletions

View File

@ -57,6 +57,11 @@ enum
#include <pwd.h>
#include <sys/utsname.h>
#include <termios.h>
#ifndef TIOCGWINSZ
#include <sys/ioctl.h>
#endif // ! TIOCGWINSZ
#include <curses.h>
#include <term.h>
@ -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);