Properly handle window resizes

This commit is contained in:
Bruce Hill 2019-05-23 04:02:11 -07:00
parent fcfb1a3995
commit ce72a889e2
2 changed files with 11 additions and 10 deletions

19
bb.c
View File

@ -35,6 +35,7 @@ static struct termios orig_termios;
static int termfd;
static int width, height;
static int mouse_x, mouse_y;
static int force_redraw = 0;
typedef enum {
SORT_ALPHA = 0,
@ -71,12 +72,13 @@ typedef struct {
} while (0)
static void _err();
static void update_term_size(void)
static void update_term_size(int _)
{
struct winsize sz = {0};
ioctl(termfd, TIOCGWINSZ, &sz);
width = sz.ws_col;
height = sz.ws_row;
force_redraw = 1;
}
static inline int clamped(int x, int low, int high)
@ -86,11 +88,6 @@ static inline int clamped(int x, int low, int high)
return x;
}
static void do_nothing(int _)
{
// Used as SIGINT handler
}
static void init_term()
{
termfd = open("/dev/tty", O_RDWR);
@ -108,7 +105,8 @@ static void init_term()
tcsetattr(termfd, TCSAFLUSH, &tios);
// xterm-specific:
writez(termfd, "\e[?1049h");
update_term_size();
update_term_size(0);
signal(SIGWINCH, update_term_size);
// Initiate mouse tracking:
writez(termfd, "\e[?1000h\e[?1002h\e[?1015h\e[?1006h");
// hide cursor
@ -117,6 +115,7 @@ static void init_term()
static void close_term()
{
signal(SIGWINCH, SIG_IGN);
// xterm-specific:
writez(termfd, "\e[?1049l");
// Show cursor:
@ -141,7 +140,7 @@ static char bb_tmpfile[] = "/tmp/bb.XXXXXX";
static int run_cmd_on_selection(bb_state_t *state, const char *cmd)
{
pid_t child;
sig_t old_handler = signal(SIGINT, do_nothing);
sig_t old_handler = signal(SIGINT, SIG_IGN);
strcpy(bb_tmpfile, "/tmp/bb.XXXXXX");
if (!mktemp(bb_tmpfile))
@ -632,10 +631,12 @@ static void explore(char *path, int print_dir, int print_selection, char sep)
int picked, scrolloff, lazy = 0;
while (1) {
redraw:
render(&state, lazy);
render(&state, lazy && !force_redraw);
force_redraw = 0;
lazy = 0;
skip_redraw:
scrolloff = MIN(SCROLLOFF, (height-4)/2);
if (force_redraw) goto redraw;
int key = term_getkey(termfd, &mouse_x, &mouse_y, KEY_DELAY);
switch (key) {
case KEY_MOUSE_LEFT: {

View File

@ -3,9 +3,9 @@
*/
#include "keys.h"
#define PROG_FUZZY "fzf"
#define PIPE_SELECTION_TO " printf '%s\\n' \"$@\" | "
#define AND_PAUSE " && read -n1 -p '\n\e[2m...press any key to continue...\e[0m\e[?25l'"
#define SCROLLOFF 5
#define NORMAL_TERM (1<<0)