From 3effb01d8fd4bcc0432c636867df5e667c5dddd4 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 5 Jan 2019 20:49:29 -0800 Subject: [PATCH] Cleaned up a little and made non-browse modes handle resizing. --- ascii.c | 86 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 51 insertions(+), 35 deletions(-) diff --git a/ascii.c b/ascii.c index fedf85a..fb0c551 100644 --- a/ascii.c +++ b/ascii.c @@ -190,6 +190,25 @@ static int find_ascii_match(int start, int fallback, int direction, char *search return fallback; } +static void handle_resize() +{ + // To prevent wasted work, wait until 200ms has elapsed + // with no additional resizes before doing anything + timeout(0); + do { + // Wait 200ms + usleep(2e5); + // Peek if there's another resize event + int k = getch(); + if (k == KEY_RESIZE) continue; + if (k > 0) ungetch(k); + } while (0); + // Back to blocking input + timeout(-1); + W = getmaxx(stdscr); + H = getmaxy(stdscr); +} + int main(int argc, char *argv[]) { if (argc > 1 && (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0)) { @@ -321,22 +340,31 @@ int main(int argc, char *argv[]) move(y, x); refresh(); - next_input:; - int key = getch(); + + int key; + next_input: + key = getch(); switch (mode) { case INSERT_MODE: - if (key == ESCAPE_KEY) { - mode = BROWSE_MODE; - } else if (key == DELETE_KEY) { // Backspace - if (buf_i > 0) buf_i--; - } else if (0 <= key && key < 127) { - outbuf[buf_i++] = (char)key; - if (buf_i >= buf_size) { - buf_size *= 2; - outbuf = realloc(outbuf, buf_size); - } - } else { - goto next_input; // skip redraw + switch (key) { + case ESCAPE_KEY: + mode = BROWSE_MODE; + break; + case DELETE_KEY: + if (buf_i > 0) buf_i--; + break; + case KEY_RESIZE: + handle_resize(); + break; + default: + if (0 <= key && key < 127) { + outbuf[buf_i++] = (char)key; + if (buf_i >= buf_size) { + buf_size *= 2; + outbuf = realloc(outbuf, buf_size); + } + break; + } else goto next_input; // skip redraw } break; @@ -350,18 +378,21 @@ int main(int argc, char *argv[]) mode = BROWSE_MODE; break; - case '\n': + case KEY_ENTER: case '\n': mode = BROWSE_MODE; break; case DELETE_KEY: if (searchlen > 0) { - searchlen--; - searchbuf[searchlen] = '\0'; + searchbuf[searchlen--] = '\0'; selected = find_ascii_match(prev_selected, prev_selected, 1, searchbuf); } break; + case KEY_RESIZE: + handle_resize(); + break; + default: if (0 <= key && key < 127) { if (searchlen < sizeof(searchbuf)-1) { @@ -370,8 +401,7 @@ int main(int argc, char *argv[]) } selected = find_ascii_match(prev_selected, prev_selected, 1, searchbuf); break; - } - goto next_input; // skip redraw + } else goto next_input; // skip redraw } break; @@ -404,11 +434,11 @@ int main(int argc, char *argv[]) case 'i': case 'a': mode = INSERT_MODE; - prev_selected = selected; break; case '/': mode = SEARCH_MODE; + prev_selected = selected; searchbuf[0] = '\0'; searchlen = 0; break; @@ -422,21 +452,7 @@ int main(int argc, char *argv[]) break; case KEY_RESIZE: - // To prevent wasted work, wait until 200ms has elapsed - // with no additional resizes before doing anything - timeout(0); - do { - // Wait 200ms - usleep(2e5); - // Peek if there's another resize event - int k = getch(); - if (k == KEY_RESIZE) continue; - if (k > 0) ungetch(k); - } while (0); - // Back to blocking input - timeout(-1); - W = getmaxx(stdscr); - H = getmaxy(stdscr); + handle_resize(); break; case KEY_ENTER: case '\n':