zygo

ncurses gopher client
Log | Files | Refs

commit 43bca3f393f6280a1dae0570b96331d604d2bcb4
parent 0a814b31f89c162b947a8ad42d110d79ab2c71d8
Author: hhvn <dev@hhvn.uk>
Date:   Sat,  5 Feb 2022 12:38:15 +0000

zygo.c zygo.h: combine link/command input in run()

Diffstat:
Mzygo.c | 62+++++++++++++++++++++++---------------------------------------
Mzygo.h | 1+
2 files changed, 24 insertions(+), 39 deletions(-)

diff --git a/zygo.c b/zygo.c @@ -753,7 +753,7 @@ draw_bar(void) { printw("%s", ui.errorbuf); } else if (ui.wantinput) { curs_set(1); - if (ui.wantinput == 1) { + if (ui.cmd) { attron(COLOR_PAIR(PAIR_CMD)); printw("%c", ui.cmd); } @@ -910,6 +910,14 @@ pagescroll(int lines) { draw_page(); } +void +idgo(size_t id) { + if (id > page->lastid || id < 1) + error("no such link: %d", id); + else + go(list_idget(&page, id), 1, 0); +} + /* * Main loop */ @@ -942,7 +950,7 @@ run(void) { if (c == KEY_RESIZE) { draw_page(); draw_bar(); - } else if (ui.wantinput == 1) { + } else if (ui.wantinput) { if (c == 27 /* escape */) { ui.wantinput = 0; } else if (c == '\n') { @@ -991,11 +999,13 @@ run(void) { if (e = strtolink(ui.arg)) yank(e); break; + case '\0': /* links */ + idgo(atoi(ui.arg)); } ui.wantinput = 0; draw_page(); } else if (c == KEY_BACKSPACE || c == 127) { - if (il == 0) { + if ((ui.cmd && il == 0) || (!ui.cmd && il == 1)) { ui.wantinput = 0; } else { ui.input[--il] = '\0'; @@ -1005,29 +1015,11 @@ run(void) { ui.input[il++] = c; ui.input[il] = '\0'; syncinput(); - } - draw_bar(); - } else if (ui.wantinput == 2) { - if (c == 27 /* escape */) { - ui.wantinput = 0; - } else if (c == KEY_BACKSPACE || c == 127) { - if (il <= 1) { + if (!ui.cmd && il + 1 >= digits(page->lastid)) { + idgo(atoi(ui.arg)); ui.wantinput = 0; - } else { - ui.input[--il] = '\0'; - syncinput(); - } - } else if (c == '\n' || il + 1 >= digits(page->lastid)) { - if (c != '\n') { - ui.input[il++] = c; - ui.input[il] = '\0'; - syncinput(); + draw_page(); } - goto gonum; - } else if (isdigit((int)c)) { - ui.input[il++] = c; - ui.input[il] = '\0'; - syncinput(); } draw_bar(); } else { @@ -1100,13 +1092,17 @@ run(void) { /* link numbers */ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': - ui.wantinput = 2; + ui.wantinput = 1; + ui.cmd = '\0'; ui.input[0] = c; ui.input[1] = '\0'; syncinput(); il = 1; - if (digits(page->lastid) == 1) - goto gonum; + if (digits(page->lastid) == 1) { + idgo(atoi(ui.arg)); + ui.wantinput = 0; + draw_page(); + } draw_bar(); break; /* commands with arg */ @@ -1134,19 +1130,7 @@ run(void) { break; } } - - continue; - -gonum: - if (atoi(ui.arg) > page->lastid || atoi(ui.arg) < 1) - error("no such link: %d", atoi(ui.arg)); - else - go(list_idget(&page, atoi(ui.arg)), 1, 0); - ui.wantinput = 0; - draw_page(); - draw_bar(); } - #undef checkcurrent } diff --git a/zygo.h b/zygo.h @@ -104,6 +104,7 @@ void syncinput(void); char *prompt(char *prompt, size_t count); Elem *strtolink(char *str); void pagescroll(int lines); +void idgo(size_t id); /* Main loop */ void run(void);