Cleaned up a little and made non-browse modes handle resizing.

This commit is contained in:
Bruce Hill 2019-01-05 20:49:29 -08:00
parent d6e65a89e8
commit 3effb01d8f

86
ascii.c
View File

@ -190,6 +190,25 @@ static int find_ascii_match(int start, int fallback, int direction, char *search
return fallback; 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[]) int main(int argc, char *argv[])
{ {
if (argc > 1 && (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0)) { 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); move(y, x);
refresh(); refresh();
next_input:;
int key = getch(); int key;
next_input:
key = getch();
switch (mode) { switch (mode) {
case INSERT_MODE: case INSERT_MODE:
if (key == ESCAPE_KEY) { switch (key) {
mode = BROWSE_MODE; case ESCAPE_KEY:
} else if (key == DELETE_KEY) { // Backspace mode = BROWSE_MODE;
if (buf_i > 0) buf_i--; break;
} else if (0 <= key && key < 127) { case DELETE_KEY:
outbuf[buf_i++] = (char)key; if (buf_i > 0) buf_i--;
if (buf_i >= buf_size) { break;
buf_size *= 2; case KEY_RESIZE:
outbuf = realloc(outbuf, buf_size); handle_resize();
} break;
} else { default:
goto next_input; // skip redraw 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; break;
@ -350,18 +378,21 @@ int main(int argc, char *argv[])
mode = BROWSE_MODE; mode = BROWSE_MODE;
break; break;
case '\n': case KEY_ENTER: case '\n':
mode = BROWSE_MODE; mode = BROWSE_MODE;
break; break;
case DELETE_KEY: case DELETE_KEY:
if (searchlen > 0) { if (searchlen > 0) {
searchlen--; searchbuf[searchlen--] = '\0';
searchbuf[searchlen] = '\0';
selected = find_ascii_match(prev_selected, prev_selected, 1, searchbuf); selected = find_ascii_match(prev_selected, prev_selected, 1, searchbuf);
} }
break; break;
case KEY_RESIZE:
handle_resize();
break;
default: default:
if (0 <= key && key < 127) { if (0 <= key && key < 127) {
if (searchlen < sizeof(searchbuf)-1) { 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); selected = find_ascii_match(prev_selected, prev_selected, 1, searchbuf);
break; break;
} } else goto next_input; // skip redraw
goto next_input; // skip redraw
} }
break; break;
@ -404,11 +434,11 @@ int main(int argc, char *argv[])
case 'i': case 'a': case 'i': case 'a':
mode = INSERT_MODE; mode = INSERT_MODE;
prev_selected = selected;
break; break;
case '/': case '/':
mode = SEARCH_MODE; mode = SEARCH_MODE;
prev_selected = selected;
searchbuf[0] = '\0'; searchbuf[0] = '\0';
searchlen = 0; searchlen = 0;
break; break;
@ -422,21 +452,7 @@ int main(int argc, char *argv[])
break; break;
case KEY_RESIZE: case KEY_RESIZE:
// To prevent wasted work, wait until 200ms has elapsed handle_resize();
// 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);
break; break;
case KEY_ENTER: case '\n': case KEY_ENTER: case '\n':