Pranjal Kole: 1 nsvi: add error handling for commands 5 files changed, 123 insertions(+), 0 deletions(-)
Copy & paste the following snippet into your terminal to import this patchset into git:
curl -s https://lists.sr.ht/~sircmpwn/visurf-devel/patches/27484/mbox | git am -3Learn more about email & git
A new nsvi_window_mode, ERROR, has been added for error handling. In the nsvi_window struct, a new item, error_message, has been added for storing the error message. In commands.c, the handle_error function was added which takes a dynamically allocated string to set as the error message, and sets the window mode to ERROR. This dynamically allocated string is freed when the window goes back to NORMAL mode. fg and bg color settings for the ERROR mode have been added in settings.c and settings.h Implements: https://todo.sr.ht/~sircmpwn/visurf/29 --- I wasn't sure where to put the error_message item. Please let me know if I should put it somewhere else. frontends/visurf/commands.c | 88 +++++++++++++++++++++++++++++++++++++ frontends/visurf/settings.c | 4 ++ frontends/visurf/settings.h | 4 ++ frontends/visurf/window.c | 25 +++++++++++ frontends/visurf/window.h | 2 + 5 files changed, 123 insertions(+) diff --git a/frontends/visurf/commands.c b/frontends/visurf/commands.c index 7dbe6b2d3..9600bde73 100644 --- a/frontends/visurf/commands.c +++ b/frontends/visurf/commands.c @@ -50,16 +50,25 @@ join_args(int argc, char *argv[]) return res; } +static void +handle_error(char *error_message) { + struct nsvi_window *win = global_state->keyboard_focus; + win->error_message = error_message; + win->mode = ERROR; +} + static int cmd_back(struct nsvi_state *state, int argc, char *argv[]) { if (argc != 1) { NSLOG(netsurf, ERROR, "back: unexpected argument"); + handle_error(strdup("back: unexpected argument")); return 1; } struct nsvi_window *win = global_state->keyboard_focus; if (!win) { NSLOG(netsurf, ERROR, "back: no active window"); + handle_error(strdup("back: no active window")); return 1; } struct gui_window *gw = win->tabs[win->tab]; @@ -72,10 +81,12 @@ cmd_bind(struct nsvi_state *state, int argc, char *argv[]) { if (argc != 3) { NSLOG(netsurf, ERROR, "bind: incorrect number of arguments"); + handle_error(strdup("bind: incorrect number of arguments")); return 1; } if (nsvi_bindings_new(&state->bindings, argv[1], argv[2]) != NSERROR_OK) { NSLOG(netsurf, ERROR, "bind: invalid binding"); + handle_error(strdup("bind: invalid binding")); return 1; } return 0; @@ -86,11 +97,13 @@ cmd_close(struct nsvi_state *state, int argc, char *argv[]) { if (argc != 1) { NSLOG(netsurf, ERROR, "close: unexpected argument"); + handle_error(strdup("close: unexpected argument")); return 1; } struct nsvi_window *win = global_state->keyboard_focus; if (!win) { NSLOG(netsurf, ERROR, "close: no active window"); + handle_error(strdup("close: no active window")); return 1; } @@ -132,6 +145,7 @@ cmd_exline(struct nsvi_state *state, int argc, char *argv[]) struct nsvi_window *win = global_state->keyboard_focus; if (!win) { NSLOG(netsurf, ERROR, "exline: no active window"); + handle_error(strdup("exline: no active window")); return 1; } free(win->exline.cmd); @@ -311,33 +325,41 @@ cmd_follow(struct nsvi_state *state, int argc, char *argv[]) break; default: NSLOG(netsurf, ERROR, "follow: invalid flag '%c'", c); + char *error_message = malloc(25); + sprintf(error_message, "follow: invalid flag '%c'", c); + handle_error(error_message); return 1; } } if (ns_optind != argc) { NSLOG(netsurf, ERROR, "follow: unexpected argument"); + handle_error(strdup("follow: unexpected argument")); return 1; } struct nsvi_window *win = global_state->keyboard_focus; if (!win) { NSLOG(netsurf, ERROR, "follow: no active window"); + handle_error(strdup("follow: no active window")); return 1; } struct gui_window *gw = win->tabs[win->tab]; struct hlcache_handle *handle = browser_window_get_content(gw->bw); if (!handle) { NSLOG(netsurf, ERROR, "follow: no active content"); + handle_error(strdup("follow: no active content")); return 1; } if (content_get_type(handle) != CONTENT_HTML) { NSLOG(netsurf, ERROR, "follow: cannot work with non-HTML content"); + handle_error(strdup("follow: cannot work with non-HTML content")); return 1; } struct box *tree = html_get_box_tree(handle); follow_collect_hints(gw, tree, 0, 0); if (gw->follow.nhint == 0) { NSLOG(netsurf, ERROR, "follow: no hints"); + handle_error(strdup("follow: no hints")); return 1; } follow_label_hints(gw); @@ -355,11 +377,13 @@ cmd_forward(struct nsvi_state *state, int argc, char *argv[]) { if (argc != 1) { NSLOG(netsurf, ERROR, "forward: unexpected argument"); + handle_error(strdup("forward: unexpected argument")); return 1; } struct nsvi_window *win = global_state->keyboard_focus; if (!win) { NSLOG(netsurf, ERROR, "forward: no active window"); + handle_error(strdup("forward: no active window")); return 1; } struct gui_window *gw = win->tabs[win->tab]; @@ -372,11 +396,13 @@ cmd_fullscreen(struct nsvi_state *state, int argc, char *argv[]) { if (argc != 1) { NSLOG(netsurf, ERROR, "fullscreen: unexpected argument"); + handle_error(strdup("fullscreen: unexpected argument")); return 1; } struct nsvi_window *win = global_state->keyboard_focus; if (!win) { NSLOG(netsurf, ERROR, "fullscreen: no active window"); + handle_error(strdup("fullscreen: no active window")); return 1; } if (win->fullscreen) { @@ -392,11 +418,13 @@ cmd_insert(struct nsvi_state *state, int argc, char *argv[]) { if (argc != 1) { NSLOG(netsurf, ERROR, "insert: unexpected argument"); + handle_error(strdup("insert: unexpected argument")); return 1; } struct nsvi_window *win = global_state->keyboard_focus; if (!win) { NSLOG(netsurf, ERROR, "insert: no active window"); + handle_error(strdup("insert: no active window")); return 1; } win->mode = INSERT; @@ -427,11 +455,15 @@ cmd_open(struct nsvi_state *state, int argc, char *argv[]) break; default: NSLOG(netsurf, ERROR, "open: invalid flag '%c'", c); + char *error_message = malloc(23); + sprintf(error_message, "open: invalid flag '%c'", c); + handle_error(error_message); return 1; } } if (ns_optind >= argc) { NSLOG(netsurf, ERROR, "open: expected an argument"); + handle_error(strdup("open: expected an argument")); return 1; } @@ -452,6 +484,7 @@ cmd_open(struct nsvi_state *state, int argc, char *argv[]) if (error != NSERROR_OK) { NSLOG(netsurf, ERROR, "open: invalid url"); + handle_error(strdup("open: invalid url")); ret = 1; goto exit; } @@ -459,6 +492,7 @@ cmd_open(struct nsvi_state *state, int argc, char *argv[]) struct nsvi_window *win = global_state->keyboard_focus; if (!win) { NSLOG(netsurf, ERROR, "open: no active window"); + handle_error(strdup("open: no active window")); ret = 1; goto exit; } @@ -480,6 +514,7 @@ cmd_open(struct nsvi_state *state, int argc, char *argv[]) nsurl_unref(url); if (error != NSERROR_OK) { NSLOG(netsurf, ERROR, "open: browser_window_navigate failed"); + handle_error(strdup("open: browser_window_navigate failed")); ret = 1; goto exit; } @@ -495,6 +530,7 @@ cmd_page(struct nsvi_state *state, int argc, char *argv[]) { if (argc != 2) { NSLOG(netsurf, ERROR, "page: expected argument"); + handle_error(strdup("page: expected argument")); return 1; } @@ -502,11 +538,13 @@ cmd_page(struct nsvi_state *state, int argc, char *argv[]) if (!win) { NSLOG(netsurf, ERROR, "page: no active window"); + handle_error(strdup("page: no active window")); return 1; } if ((strcmp(argv[1], "next") != 0) && (strcmp(argv[1], "prev") != 0)) { NSLOG(netsurf, ERROR, "page: expected <next|prev>"); + handle_error(strdup("page: expected <next|prev>")); return 1; } @@ -515,11 +553,13 @@ cmd_page(struct nsvi_state *state, int argc, char *argv[]) if (!handle) { NSLOG(netsurf, ERROR, "page: no active content"); + handle_error(strdup("page: no active content")); return 1; } if (content_get_type(handle) != CONTENT_HTML) { NSLOG(netsurf, ERROR, "page: cannot work with non-HTML content"); + handle_error(strdup("page: cannot work with non-HTML content")); return 1; } @@ -575,11 +615,13 @@ cmd_paste(struct nsvi_state *state, int argc, char *argv[]) } if (ns_optind != argc) { NSLOG(netsurf, ERROR, "paste: expected one argument"); + handle_error(strdup("paste: expected one argument")); return 1; } struct nsvi_window *win = state->keyboard_focus; if (!win) { NSLOG(netsurf, ERROR, "paste: no active window"); + handle_error(strdup("paste: no active window")); return 1; } @@ -620,6 +662,7 @@ cmd_paste(struct nsvi_state *state, int argc, char *argv[]) free(data); if (error != NSERROR_OK) { NSLOG(netsurf, ERROR, "paste: invalid url"); + handle_error(strdup("paste: invalid url")); return 1; } @@ -642,6 +685,7 @@ cmd_paste(struct nsvi_state *state, int argc, char *argv[]) if (error != NSERROR_OK) { NSLOG(netsurf, ERROR, "open: browser_window_navigate failed"); + handle_error(strdup("open: browser_window_navigate failed")); return 1; } return 0; @@ -668,11 +712,15 @@ cmd_reload(struct nsvi_state *state, int argc, char *argv[]) break; default: NSLOG(netsurf, ERROR, "reload: invalid flag '%c'", c); + char *error_message = malloc(25); + sprintf(error_message, "reload: invalid flag '%c'", c); + handle_error(error_message); return 1; } } if (ns_optind < argc) { NSLOG(netsurf, ERROR, "reload: unexpected argument"); + handle_error(strdup("reload: unexpected argument")); return 1; } @@ -706,11 +754,15 @@ cmd_scroll(struct nsvi_state *state, int argc, char *argv[]) break; default: NSLOG(netsurf, ERROR, "scroll: invalid flag '%c'", c); + char *error_message = malloc(25); + sprintf(error_message, "scroll: invalid flag '%c'", c); + handle_error(error_message); return 1; } } if (ns_optind >= argc) { NSLOG(netsurf, ERROR, "scroll: expected argument"); + handle_error(strdup("scroll: expected argument")); return 1; } @@ -732,6 +784,7 @@ cmd_scroll(struct nsvi_state *state, int argc, char *argv[]) break; default: NSLOG(netsurf, ERROR, "scroll: invalid argument"); + handle_error(strdup("scroll: invalid argument")); return 1; } ++endptr; @@ -816,6 +869,9 @@ cmd_search(struct nsvi_state *state, int argc, char *argv[]) break; default: NSLOG(netsurf, ERROR, "search: invalid flag '%c'", c); + char *error_message = malloc(25); + sprintf(error_message, "search: invalid flag '%c'", c); + handle_error(error_message); return 1; } } @@ -835,10 +891,14 @@ cmd_set(struct nsvi_state *state, int argc, char *argv[]) { if (argc != 3) { NSLOG(netsurf, ERROR, "set: expected two arguments"); + handle_error(strdup("set: expected two arguments")); return 1; } if (!nsvi_config_set(argv[1], argv[2])) { NSLOG(netsurf, ERROR, "set: invalid setting %s", argv[1]); + char *error_message = malloc(strlen(argv[1]) + 22); + sprintf(error_message, "set: invalid setting %s", argv[1]); + handle_error(error_message); return 1; } if (state->keyboard_focus) { @@ -852,6 +912,7 @@ cmd_source(struct nsvi_state *state, int argc, char *argv[]) { if (argc < 2) { NSLOG(netsurf, ERROR, "source: not enough arguments"); + handle_error(strdup("source: not enough arguments")); return 1; } return nsvi_command_source(state, argv[1]); @@ -862,11 +923,13 @@ cmd_stop(struct nsvi_state *state, int argc, char *argv[]) { if (argc != 1) { NSLOG(netsurf, ERROR, "stop: unexpected argument"); + handle_error(strdup("stop: unexpected argument")); return 1; } struct nsvi_window *win = global_state->keyboard_focus; if (!win) { NSLOG(netsurf, ERROR, "stop: no active window"); + handle_error(strdup("stop: no active window")); return 1; } struct gui_window *gw = win->tabs[win->tab]; @@ -879,11 +942,13 @@ cmd_tab(struct nsvi_state *state, int argc, char *argv[]) { if (argc != 2) { NSLOG(netsurf, ERROR, "tab: expected argument"); + handle_error(strdup("tab: expected argument")); return 1; } struct nsvi_window *win = global_state->keyboard_focus; if (!win) { NSLOG(netsurf, ERROR, "tab: no active window"); + handle_error(strdup("tab: no active window")); return 1; } int i = win->tab; @@ -896,6 +961,7 @@ cmd_tab(struct nsvi_state *state, int argc, char *argv[]) i = strtoul(argv[1], &endptr, 10); if (*endptr) { NSLOG(netsurf, ERROR, "tab: expected <next|prev|[index]>"); + handle_error(strdup("tab: expected <next|prev|[index]>")); return 1; } } @@ -912,11 +978,13 @@ static int cmd_tabmove(struct nsvi_state *state, int argc, char *argv[]) { if (argc != 2) { NSLOG(netsurf, ERROR, "tabmove: expected argument"); + handle_error(strdup("tabmove: expected argument")); return 1; } struct nsvi_window *win = global_state->keyboard_focus; if (!win) { NSLOG(netsurf, ERROR, "tabmove: no active window"); + handle_error(strdup("tabmove: no active window")); return 1; } @@ -925,6 +993,7 @@ cmd_tabmove(struct nsvi_state *state, int argc, char *argv[]) { int j = strtoul(argv[1], &endptr, 10); if (*endptr) { NSLOG(netsurf, ERROR, "tabmove: expected [index]"); + handle_error(strdup("tabmove: expected [index]")); return 1; } @@ -950,10 +1019,12 @@ cmd_unbind(struct nsvi_state *state, int argc, char *argv[]) { if (argc != 2) { NSLOG(netsurf, ERROR, "unbind: unexpected argument"); + handle_error(strdup("unbind: unexpected argument")); return 1; } if (nsvi_bindings_remove(&state->bindings, argv[1]) != NSERROR_OK) { NSLOG(netsurf, ERROR, "unbind: invalid binding"); + handle_error(strdup("unbind: invalid binding")); return 1; } return 0; @@ -964,16 +1035,19 @@ cmd_undo(struct nsvi_state *state, int argc, char *argv[]) { if (argc != 1) { NSLOG(netsurf, ERROR, "undo: unexpected argument"); + handle_error(strdup("undo: unexpected argument")); return 1; } struct nsvi_window *win = global_state->keyboard_focus; if (!win) { NSLOG(netsurf, ERROR, "undo: no active window"); + handle_error(strdup("undo: no active window")); return 1; } struct nsvi_undo *undo = state->undo; if (!undo) { NSLOG(netsurf, ERROR, "undo: nothing to undo"); + handle_error(strdup("undo: nothing to undo")); return 1; } @@ -987,6 +1061,7 @@ cmd_undo(struct nsvi_state *state, int argc, char *argv[]) undo->urls[0], NULL, gw->bw, NULL); if (error != NSERROR_OK) { NSLOG(netsurf, ERROR, "undo: failed to create tab"); + handle_error(strdup("undo: failed to create tab")); return 1; } break; @@ -1026,11 +1101,15 @@ cmd_yank(struct nsvi_state *state, int argc, char *argv[]) break; default: NSLOG(netsurf, ERROR, "yank: invalid flag '%c'", c); + char *error_message = malloc(23); + sprintf(error_message, "yank: invalid flag '%c'", c); + handle_error(error_message); return 1; } } if (ns_optind < argc) { NSLOG(netsurf, ERROR, "yank: unexpected argument"); + handle_error(strdup("yank: unexpected argument")); return 1; } @@ -1059,6 +1138,7 @@ cmd_yank(struct nsvi_state *state, int argc, char *argv[]) } if (err != NSERROR_OK) { NSLOG(netsurf, ERROR, "yank: error setting clipboard"); + handle_error(strdup("yank: error setting clipboard")); return 1; } @@ -1080,6 +1160,7 @@ cmd_zoom(struct nsvi_state *state, int argc, char *argv[]) bool relative = false; if (argc < 2) { NSLOG(netsurf, ERROR, "zoom: expected argument"); + handle_error(strdup("zoom: expected argument")); return 1; } @@ -1101,6 +1182,7 @@ cmd_zoom(struct nsvi_state *state, int argc, char *argv[]) break; default: NSLOG(netsurf, ERROR, "zoom: invalid argument"); + handle_error(strdup("zoom: invalid argument")); return 1; } ++endptr; @@ -1230,6 +1312,9 @@ nsvi_command(void *user, const char *cmd) int ret = wordexp(cmd, &p, 0); if (ret) { NSLOG(netsurf, ERROR, "Unable to expand line: '%s'", cmd); + char *error_message = malloc(strlen(cmd) + 26); + sprintf(error_message, "Unable to expand line: '%s'", cmd); + handle_error(error_message); return; } char **argv = p.we_wordv; @@ -1241,6 +1326,9 @@ nsvi_command(void *user, const char *cmd) cmd_compar); if (!command) { NSLOG(netsurf, ERROR, "Invalid command '%s'", argv[0]); + char *error_message = malloc(strlen(argv[0]) + 19); + sprintf(error_message, "Invalid command '%s'", argv[0]); + handle_error(error_message); goto exit; } NSLOG(netsurf, INFO, "Running command %s", cmd); diff --git a/frontends/visurf/settings.c b/frontends/visurf/settings.c index d5b25bb11..5789683f2 100644 --- a/frontends/visurf/settings.c +++ b/frontends/visurf/settings.c @@ -49,6 +49,10 @@ struct nsvi_config config = { .fg = 0xFFFFFFFF, .bg = 0x111111FF, }, + .error = { + .fg = 0xFFFFFFFF, + .bg = 0xFF0000FF, + }, }, .tabs = { diff --git a/frontends/visurf/settings.h b/frontends/visurf/settings.h index 076911ef8..37d8f2016 100644 --- a/frontends/visurf/settings.h +++ b/frontends/visurf/settings.h @@ -43,6 +43,10 @@ struct nsvi_config { uint32_t bg; uint32_t fg; } normal; + struct { + uint32_t bg; + uint32_t fg; + } error; } status; struct { diff --git a/frontends/visurf/window.c b/frontends/visurf/window.c index 94d420f36..7e6d4d7c3 100644 --- a/frontends/visurf/window.c +++ b/frontends/visurf/window.c @@ -313,6 +313,23 @@ draw_follow_statusbar(struct nsvi_window *win, struct pool_buffer *buf) return height + config.margin * 2; } +static int +draw_error_statusbar(struct nsvi_window *win, struct pool_buffer *buf) +{ + int width, height; + get_text_size(buf->cairo, config.font, &width, &height, NULL, win->error_message); + + cairo_set_source_u32(buf->cairo, config.status.error.bg); + cairo_rectangle(buf->cairo, 0, win->height - (config.margin * 2 + height), + win->width, config.margin * 2 + height); + cairo_fill(buf->cairo); + + cairo_set_source_u32(buf->cairo, config.status.error.fg); + cairo_move_to(buf->cairo, config.margin, win->height - height - config.margin); + pango_printf(buf->cairo, config.font, win->error_message); + return height + config.margin * 2; +} + static int draw_status(struct nsvi_window *win, struct pool_buffer *buf) { @@ -325,6 +342,8 @@ draw_status(struct nsvi_window *win, struct pool_buffer *buf) return draw_exline_statusbar(win, buf); case FOLLOW: return draw_follow_statusbar(win, buf); + case ERROR: + return draw_error_statusbar(win, buf); } abort(); } @@ -1538,6 +1557,12 @@ nsvi_window_key_event(struct nsvi_window *win, case FOLLOW: nsvi_window_handle_follow_key(win, keycode, pressed); return; + case ERROR: + if (!pressed) { + win->mode = NORMAL; + free(win->error_message); + } + return; } } diff --git a/frontends/visurf/window.h b/frontends/visurf/window.h index f001af6f9..931786a08 100644 --- a/frontends/visurf/window.h +++ b/frontends/visurf/window.h @@ -18,6 +18,7 @@ enum window_mode { INSERT, EXLINE, FOLLOW, + ERROR, }; struct exline_state { @@ -86,6 +87,7 @@ struct nsvi_window { int scale; bool dirty; bool pending_frame; + char *error_message; bool fullscreen; enum window_mode mode; -- 2.34.1
Thanks! To git@git.sr.ht:~sircmpwn/visurf 06af14be9..6196e731e master -> master