2 * Copyright (c) 2021, 2024 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 * Ncurses UI for telescope.
23 * ncurses allows you to scroll a window, but when a line goes out of
24 * the visible area it's forgotten. We keep a list of formatted lines
25 * (``visual lines'') that we know fits in the window, and draw them.
27 * This means that on every resize we have to clear our list of lines
28 * and re-render everything. A clever approach would be to do this
29 * ``on-demand'', but it's still missing.
51 #include "minibuffer.h"
53 #include "telescope.h"
57 static void set_scroll_position(struct tab *, size_t, size_t);
59 static void restore_curs_x(struct buffer *);
61 static int readkey(void);
62 static void dispatch_stdio(int, int, void*);
63 static void handle_resize(int, int, void*);
64 static void handle_resize_nodelay(int, int, void*);
65 static void handle_download_refresh(int, int, void *);
66 static void rearrange_windows(void);
67 static void line_prefix_and_text(struct vline *, char *, size_t, const char **, const char **, int *);
68 static void print_vline(int, int, WINDOW*, struct vline*);
69 static void redraw_tabline(void);
70 static void redraw_window(WINDOW *, int, int, int, int, struct buffer *);
71 static void redraw_download(void);
72 static void redraw_help(void);
73 static void redraw_body(struct tab*);
74 static void redraw_modeline(struct tab*);
75 static void redraw_minibuffer(void);
76 static void do_redraw_echoarea(void);
77 static void do_redraw_minibuffer(void);
78 static void do_redraw_minibuffer_compl(void);
79 static void place_cursor(int);
80 static void redraw_tab(struct tab*);
81 static void update_loading_anim(int, int, void*);
82 static void stop_loading_anim(struct tab*);
84 static int should_rearrange_windows;
85 static int show_tab_bar;
89 struct thiskey thiskey;
90 struct tab *current_tab;
92 static unsigned int resize_timer;
93 static struct timeval resize_tv = { 0, 250000 };
95 static unsigned int download_timer;
96 static struct timeval download_refresh_timer = { 0, 250000 };
98 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
100 int body_lines, body_cols;
103 /* not static so we can see them from help.c */
104 struct buffer helpwin;
105 int help_lines, help_cols;
107 static WINDOW *download;
108 /* not static so we can see them from download.c */
109 struct buffer downloadwin;
113 static int side_window;
114 static int in_side_window;
116 static struct timeval loading_tv = { 0, 250000 };
118 static char keybuf[64];
120 /* XXX: don't forget to init these in main() */
121 struct kmap global_map,
127 update_x_offset(void)
129 if (olivetti_mode && fill_column < body_cols)
130 x_offset = (body_cols - fill_column)/2;
136 set_scroll_position(struct tab *tab, size_t top, size_t cur)
143 last = TAILQ_FIRST(&tab->buffer.page.head);
144 TAILQ_FOREACH(vl, &tab->buffer.head, vlines) {
145 if (last != vl->parent) {
150 if (!topfound && i == top) {
152 tab->buffer.top_line = vl;
156 tab->buffer.current_line = vl;
162 tab->buffer.top_line = TAILQ_FIRST(&tab->buffer.head);
164 tab->buffer.current_line = tab->buffer.top_line;
168 get_scroll_position(struct tab *tab, size_t *top, size_t *cur)
176 if (tab->buffer.top_line == NULL ||
177 tab->buffer.current_line == NULL)
180 TAILQ_FOREACH(l, &tab->buffer.page.head, lines) {
181 if (tab->buffer.top_line->parent == l)
183 if (tab->buffer.current_line->parent == l)
193 save_excursion(struct excursion *place, struct buffer *buffer)
195 place->curs_x = buffer->curs_x;
196 place->curs_y = buffer->curs_y;
197 place->line_off = buffer->line_off;
198 place->top_line = buffer->top_line;
199 place->current_line = buffer->current_line;
200 place->cpoff = buffer->cpoff;
204 restore_excursion(struct excursion *place, struct buffer *buffer)
206 buffer->curs_x = place->curs_x;
207 buffer->curs_y = place->curs_y;
208 buffer->line_off = place->line_off;
209 buffer->top_line = place->top_line;
210 buffer->current_line = place->current_line;
211 buffer->cpoff = place->cpoff;
215 restore_curs_x(struct buffer *buffer)
218 const char *prfx, *text;
220 vl = buffer->current_line;
221 if (vl == NULL || vl->len == 0)
222 buffer->curs_x = buffer->cpoff = 0;
223 else if (vl->parent->data != NULL) {
224 text = vl->parent->data;
225 buffer->curs_x = utf8_snwidth(text + 1, buffer->cpoff) + 1;
227 text = vl->parent->line + vl->from;
228 buffer->curs_x = utf8_snwidth(text, buffer->cpoff);
231 /* small hack: don't olivetti-mode the download pane */
232 if (buffer != &downloadwin)
233 buffer->curs_x += x_offset;
238 if (vl->parent->data != NULL)
239 buffer->curs_x += utf8_swidth_between(vl->parent->line,
242 prfx = line_prefixes[vl->parent->type].prfx1;
243 buffer->curs_x += utf8_swidth(prfx);
248 global_key_unbound(void)
250 message("%s is undefined", keybuf);
257 return &ministate.buffer;
258 if (in_side_window & SIDE_WINDOW_LEFT)
260 if (in_side_window & SIDE_WINDOW_BOTTOM)
262 return ¤t_tab->buffer;
270 if ((thiskey.key = wgetch(body)) == ERR)
273 thiskey.meta = thiskey.key == '\e';
275 thiskey.key = wgetch(body);
276 if (thiskey.key == ERR || thiskey.key == '\e') {
284 if ((unsigned int)thiskey.key >= UINT8_MAX)
288 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
290 if ((thiskey.key = wgetch(body)) == ERR) {
291 message("Error decoding user input");
300 dispatch_stdio(int fd, int ev, void *d)
306 /* TODO: schedule a redraw? */
313 if (keybuf[0] != '\0')
314 strlcat(keybuf, " ", sizeof(keybuf));
316 strlcat(keybuf, "M-", sizeof(keybuf));
317 if (thiskey.cp != 0) {
318 utf8_encode(thiskey.cp, tmp);
319 strlcat(keybuf, tmp, sizeof(keybuf));
320 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
321 strlcat(keybuf, keyname, sizeof(keybuf));
323 tmp[0] = thiskey.key;
324 strlcat(keybuf, tmp, sizeof(keybuf));
327 lk = lookup_key(¤t_map, &thiskey, current_buffer());
328 if (lk == LK_UNBOUND) {
329 if (current_map->unhandled_input != NULL)
330 current_map->unhandled_input();
332 global_key_unbound();
334 if (lk != LK_ADVANCED_MAP) {
335 current_map = base_map;
336 strlcpy(keybuf, "", sizeof(keybuf));
339 if (side_window & SIDE_WINDOW_LEFT)
342 if (should_rearrange_windows)
344 redraw_tab(current_tab);
348 handle_resize(int sig, int ev, void *d)
350 ev_timer_cancel(resize_timer);
351 resize_timer = ev_timer(&resize_tv, handle_resize_nodelay, NULL);
355 handle_resize_nodelay(int s, int ev, void *d)
365 handle_download_refresh(int s, int v, void *d)
367 if (side_window & SIDE_WINDOW_BOTTOM) {
368 recompute_downloads();
369 redraw_tab(current_tab);
374 should_show_tab_bar(void)
376 if (tab_bar_show == -1)
378 if (tab_bar_show == 0)
381 return TAILQ_NEXT(TAILQ_FIRST(&tabshead), tabs) != NULL;
385 rearrange_windows(void)
388 int minibuffer_lines;
390 should_rearrange_windows = 0;
391 show_tab_bar = should_show_tab_bar();
395 /* 3 lines for the ui and 12 for the body and minibuffer */
396 if ((too_small = lines < 15)) {
398 printw("Window too small.");
403 /* move and resize the windows, in reverse order! */
405 if (in_minibuffer == MB_COMPREAD) {
406 minibuffer_lines = MIN(10, lines/2);
407 mvwin(minibuffer, lines - minibuffer_lines, 0);
408 wresize(minibuffer, minibuffer_lines, COLS);
409 lines -= minibuffer_lines;
411 wrap_page(&ministate.compl.buffer, COLS);
414 mvwin(echoarea, --lines, 0);
415 wresize(echoarea, 1, COLS);
417 mvwin(modeline, --lines, 0);
418 wresize(modeline, 1, COLS);
420 if (side_window & SIDE_WINDOW_BOTTOM) {
421 download_lines = MIN(5, lines/2);
422 download_cols = COLS;
423 mvwin(download, lines - download_lines, 0);
424 wresize(download, download_lines, download_cols);
425 lines -= download_lines;
427 wrap_page(&downloadwin, download_cols);
430 body_lines = show_tab_bar ? --lines : lines;
434 * Here we make the assumption that show_tab_bar is either 0
435 * or 1, and reuse that as argument to mvwin.
437 if (side_window & SIDE_WINDOW_LEFT) {
438 help_cols = 0.3 * COLS;
440 mvwin(help, show_tab_bar, 0);
441 wresize(help, help_lines, help_cols);
443 wrap_page(&helpwin, help_cols);
445 body_cols = COLS - help_cols - 1;
446 mvwin(body, show_tab_bar, help_cols);
448 mvwin(body, show_tab_bar, 0);
451 wresize(body, body_lines, body_cols);
454 wresize(tabline, 1, COLS);
456 wrap_page(¤t_tab->buffer, body_cols);
457 redraw_tab(current_tab);
461 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
462 const char **prfx_ret, const char **text_ret, int *text_len)
473 cont = vl->flags & L_CONTINUATION;
474 type = vl->parent->type;
476 *prfx_ret = line_prefixes[type].prfx1;
478 *prfx_ret = line_prefixes[type].prfx2;
480 space = vl->parent->data;
481 *text_ret = vl->parent->line + vl->from;
482 *text_len = MIN(INT_MAX, vl->len);
483 if (!emojify_link || type != LINE_LINK || space == NULL) {
489 width = utf8_swidth_between(vl->parent->line, space);
490 for (i = 0; i < width + 1 && i < len - 1; ++i)
493 strlcpy(buf, vl->parent->line, len);
494 if ((t = strchr(buf, ' ')) != NULL)
496 strlcat(buf, " ", len);
503 print_vline_descr(int width, WINDOW *window, struct vline *vl)
507 switch (vl->parent->type) {
509 case LINE_COMPL_CURRENT:
513 case LINE_DOWNLOAD_DONE:
523 if (vl->parent->alt == NULL)
530 wprintw(window, " ");
531 for (; goal > x; ++x)
532 wprintw(window, " ");
534 wprintw(window, "%s", vl->parent->alt);
538 * Core part of the rendering. It prints a vline starting from the
539 * current cursor position. Printing a vline consists of skipping
540 * `off' columns (for olivetti-mode), print the correct prefix (which
541 * may be the emoji in case of emojified links-lines), printing the
542 * text itself, filling until width - off and filling off columns
546 print_vline(int off, int width, WINDOW *window, struct vline *vl)
549 * Believe me or not, I've seen emoji ten code points long!
550 * That means, to stay large, 4*10 bytes + NUL.
552 char emojibuf[41] = {0};
553 const char *text, *prfx;
555 int i, left, x, y, textlen;
557 f = &line_faces[vl->parent->type];
559 /* unused, set by getyx */
562 if (vl->parent->type == LINE_FRINGE && fringe_ignore_offset)
565 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx,
568 wattr_on(window, body_face.left, NULL);
569 for (i = 0; i < off; i++)
571 wattr_off(window, body_face.left, NULL);
573 wattr_on(window, f->prefix, NULL);
574 wprintw(window, "%s", prfx);
575 wattr_off(window, f->prefix, NULL);
577 wattr_on(window, f->text, NULL);
579 wprintw(window, "%.*s", textlen, text);
580 print_vline_descr(width, window, vl);
581 wattr_off(window, f->text, NULL);
587 wattr_on(window, f->trail, NULL);
588 for (i = 0; i < left - off; ++i)
590 wattr_off(window, f->trail, NULL);
592 wattr_on(window, body_face.right, NULL);
593 for (i = 0; i < off; i++)
595 wattr_off(window, body_face.right, NULL);
602 size_t toskip, ots, tabwidth, space, x;
603 int current, y, truncated, pair;
609 /* unused, but setted by a getyx */
612 tabwidth = sizeof(buf)+1;
616 TAILQ_FOREACH(tab, &tabshead, tabs) {
618 if (tab == current_tab)
622 if (toskip * tabwidth <= space)
627 while (toskip != 0 &&
628 (ots - toskip+1) * tabwidth < space)
633 wattr_on(tabline, tab_face.background, NULL);
634 wprintw(tabline, toskip == 0 ? " " : "<");
635 wattr_off(tabline, tab_face.background, NULL);
638 TAILQ_FOREACH(tab, &tabshead, tabs) {
646 getyx(tabline, y, x);
647 if (x + sizeof(buf)+2 >= (size_t)COLS)
650 current = tab == current_tab;
652 if (*(title = tab->buffer.page.title) == '\0')
653 title = hist_cur(tab->hist);
655 if (tab->flags & TAB_URGENT)
656 strlcpy(buf, "!", sizeof(buf));
658 strlcpy(buf, " ", sizeof(buf));
660 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
661 /* truncation happens */
662 strlcpy(&buf[sizeof(buf)-4], "...", 4);
664 /* pad with spaces */
665 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
669 pair = current ? tab_face.current : tab_face.tab;
670 wattr_on(tabline, pair, NULL);
671 wprintw(tabline, "%s", buf);
672 wattr_off(tabline, pair, NULL);
674 wattr_on(tabline, tab_face.background, NULL);
675 if (TAILQ_NEXT(tab, tabs) != NULL)
676 wprintw(tabline, "┃");
677 wattr_off(tabline, tab_face.background, NULL);
680 wattr_on(tabline, tab_face.background, NULL);
681 for (; x < (size_t)COLS; ++x)
682 waddch(tabline, ' ');
684 mvwprintw(tabline, 0, COLS-1, ">");
685 wattr_off(tabline, tab_face.background, NULL);
689 * Compute the first visible line around vl. Try to search forward
690 * until the end of the buffer; if a visible line is not found, search
691 * backward. Return NULL if no viable line was found.
694 adjust_line(struct vline *vl, struct buffer *buffer)
701 if (!(vl->parent->flags & L_HIDDEN))
706 t != NULL && t->parent->flags & L_HIDDEN;
707 t = TAILQ_NEXT(t, vlines))
713 /* search backward */
715 t != NULL && t->parent->flags & L_HIDDEN;
716 t = TAILQ_PREV(t, vhead, vlines))
723 redraw_window(WINDOW *win, int off, int height, int width,
724 int show_fringe, struct buffer *buffer)
727 int onscreen = 0, l = 0;
729 restore_curs_x(buffer);
732 * TODO: ignoring buffer->force_update and always
733 * re-rendering. In theory we can recompute the y position
734 * without a re-render, and optimize here. It's not the only
735 * optimisation possible here, wscrl wolud also be an
743 if (TAILQ_EMPTY(&buffer->head))
746 if (buffer->top_line == NULL)
747 buffer->top_line = TAILQ_FIRST(&buffer->head);
749 buffer->top_line = adjust_line(buffer->top_line, buffer);
750 if (buffer->top_line == NULL)
753 buffer->current_line = adjust_line(buffer->current_line, buffer);
755 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
756 if (vl->parent->flags & L_HIDDEN)
760 print_vline(off, width, win, vl);
762 if (vl == buffer->current_line)
774 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
775 if (vl == buffer->current_line)
777 if (vl->parent->flags & L_HIDDEN)
780 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
787 buffer->last_line_off = buffer->line_off;
788 buffer->force_redraw = 0;
790 for (; show_fringe && l < height; l++)
791 print_vline(off, width, win, &fringe);
793 wmove(win, buffer->curs_y, buffer->curs_x);
797 redraw_download(void)
799 redraw_window(download, 0, download_lines, COLS, 0, &downloadwin);
805 redraw_window(help, 0, help_lines, help_cols, 1, &helpwin);
809 redraw_body(struct tab *tab)
811 static struct tab *last_tab;
814 tab->buffer.force_redraw =1;
817 redraw_window(body, x_offset, body_lines, body_cols, 1, &tab->buffer);
821 trust_status_char(enum trust_state ts)
824 case TS_UNKNOWN: return '-';
825 case TS_UNTRUSTED: return '!';
826 case TS_TEMP_TRUSTED: return '!';
827 case TS_TRUSTED: return 'v';
828 case TS_VERIFIED: return 'V';
834 redraw_modeline(struct tab *tab)
836 struct buffer *buffer;
838 int x, y, max_x, max_y;
840 const char *spin = "-\\|/";
842 buffer = current_buffer();
843 mode = buffer->page.name;
846 wattr_on(modeline, modeline_face.background, NULL);
847 wmove(modeline, 0, 0);
849 wprintw(modeline, "-%c%c%c %s ",
850 spin[tab->loading_anim_step],
851 trust_status_char(tab->trust),
852 tab->client_cert ? 'C' : '-',
853 mode == NULL ? "(none)" : mode);
855 pct = (buffer->line_off + buffer->curs_y) * 100.0
858 if (buffer->line_max <= (size_t)body_lines)
859 wprintw(modeline, "All ");
860 else if (buffer->line_off == 0)
861 wprintw(modeline, "Top ");
862 else if (buffer->line_off + body_lines >= buffer->line_max)
863 wprintw(modeline, "Bottom ");
865 wprintw(modeline, "%.0f%% ", pct);
867 wprintw(modeline, "%zu/%zu %s ",
868 buffer->line_off + buffer->curs_y,
870 hist_cur(tab->hist));
872 getyx(modeline, y, x);
873 getmaxyx(modeline, max_y, max_x);
878 for (; x < max_x; ++x)
879 waddstr(modeline, "-");
881 wattr_off(modeline, modeline_face.background, NULL);
885 redraw_minibuffer(void)
887 wattr_on(echoarea, minibuffer_face.background, NULL);
891 do_redraw_minibuffer();
893 do_redraw_echoarea();
895 if (in_minibuffer == MB_COMPREAD)
896 do_redraw_minibuffer_compl();
898 wattr_off(echoarea, minibuffer_face.background, NULL);
902 do_redraw_echoarea(void)
906 if (ministate.curmesg != NULL)
907 wprintw(echoarea, "%s", ministate.curmesg);
908 else if (*keybuf != '\0')
909 waddstr(echoarea, keybuf);
911 /* If nothing else, show the URL at point */
912 vl = current_tab->buffer.current_line;
913 if (vl != NULL && vl->parent->type == LINE_LINK)
914 waddstr(echoarea, vl->parent->alt);
919 do_redraw_minibuffer(void)
921 struct buffer *cmplbuf, *buffer;
922 size_t off_y, off_x = 0;
923 const char *start, *c;
926 cmplbuf = &ministate.compl.buffer;
927 buffer = &ministate.buffer;
928 (void)off_y; /* unused, set by getyx */
930 wmove(echoarea, 0, 0);
932 if (in_minibuffer == MB_COMPREAD)
933 wprintw(echoarea, "(%2zu) ",
936 wprintw(echoarea, "%s", ministate.prompt);
937 if (!ministate.editing)
938 wprintw(echoarea, "(%zu/%zu) ",
939 hist_off(ministate.hist) + 1,
940 hist_size(ministate.hist));
942 getyx(echoarea, off_y, off_x);
944 start = ministate.buf;
945 if (!ministate.editing)
946 start = hist_cur(ministate.hist);
947 line = buffer->current_line->parent->line + buffer->current_line->from;
948 c = utf8_nth(line, buffer->cpoff);
949 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
950 start = utf8_next_cp(start);
953 waddstr(echoarea, start);
955 if (ministate.curmesg != NULL)
956 wprintw(echoarea, " [%s]", ministate.curmesg);
958 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
962 do_redraw_minibuffer_compl(void)
964 redraw_window(minibuffer, 0, 10, COLS, 1,
965 &ministate.compl.buffer);
969 * Place the cursor in the right ncurses window. If soft is 1, use
970 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
974 place_cursor(int soft)
976 int (*touch)(WINDOW *);
979 touch = wnoutrefresh;
984 if (side_window & SIDE_WINDOW_LEFT)
986 if (side_window & SIDE_WINDOW_BOTTOM)
990 } else if (in_side_window & SIDE_WINDOW_LEFT) {
993 if (in_side_window & SIDE_WINDOW_BOTTOM)
996 } else if (in_side_window & SIDE_WINDOW_BOTTOM) {
999 if (in_side_window & SIDE_WINDOW_LEFT)
1003 if (side_window & SIDE_WINDOW_LEFT)
1005 if (side_window & SIDE_WINDOW_BOTTOM)
1013 redraw_tab(struct tab *tab)
1018 if (side_window & SIDE_WINDOW_LEFT) {
1023 if (side_window & SIDE_WINDOW_BOTTOM) {
1025 wnoutrefresh(download);
1032 redraw_modeline(tab);
1033 redraw_minibuffer();
1035 wnoutrefresh(tabline);
1036 wnoutrefresh(modeline);
1038 if (in_minibuffer == MB_COMPREAD)
1039 wnoutrefresh(minibuffer);
1046 dprintf(1, "\033]2;%s - Telescope\a",
1047 current_tab->buffer.page.title);
1051 start_loading_anim(struct tab *tab)
1053 if (tab->loading_anim)
1055 tab->loading_anim = 1;
1057 ev_timer_cancel(tab->loading_timer);
1058 tab->loading_timer = ev_timer(&loading_tv, update_loading_anim, tab);
1062 update_loading_anim(int fd, int ev, void *d)
1064 struct tab *tab = d;
1066 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1068 if (tab == current_tab) {
1069 redraw_modeline(tab);
1076 tab->loading_timer = ev_timer(&loading_tv, update_loading_anim, tab);
1080 stop_loading_anim(struct tab *tab)
1082 if (!tab->loading_anim)
1085 ev_timer_cancel(tab->loading_timer);
1086 tab->loading_anim = 0;
1087 tab->loading_anim_step = 0;
1089 if (tab != current_tab)
1092 redraw_modeline(tab);
1103 setlocale(LC_ALL, "");
1105 if (TAILQ_EMPTY(&global_map.m)) {
1106 fprintf(stderr, "no keys defined!\n");
1112 /* initialize download window */
1113 TAILQ_INIT(&downloadwin.head);
1114 TAILQ_INIT(&downloadwin.page.head);
1116 /* initialize help window */
1117 TAILQ_INIT(&helpwin.head);
1118 TAILQ_INIT(&helpwin.page.head);
1120 base_map = &global_map;
1121 current_map = &global_map;
1125 if (enable_colors) {
1128 use_default_colors();
1133 config_apply_style();
1138 intrflush(stdscr, FALSE);
1140 if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1142 if ((body = newwin(1, 1, 0, 0)) == NULL)
1144 if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1146 if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1148 if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1150 if ((download = newwin(1, 1, 0, 0)) == NULL)
1152 if ((help = newwin(1, 1, 0, 0)) == NULL)
1155 wbkgd(body, body_face.body);
1156 wbkgd(download, download_face.background);
1157 wbkgd(echoarea, minibuffer_face.background);
1162 scrollok(body, FALSE);
1164 /* non-blocking input */
1176 if (ev_signal(SIGWINCH, handle_resize, NULL) == -1 ||
1177 ev_add(0, EV_READ, dispatch_stdio, NULL) == -1)
1178 err(1, "ev_signal or ev_add failed");
1180 switch_to_tab(current_tab);
1181 rearrange_windows();
1187 ui_on_tab_loaded(struct tab *tab)
1189 size_t line_off, curr_off;
1191 stop_loading_anim(tab);
1192 message("Loaded %s", hist_cur(tab->hist));
1194 hist_cur_offs(tab->hist, &line_off, &curr_off);
1195 if (curr_off != 0 &&
1196 tab->buffer.current_line == TAILQ_FIRST(&tab->buffer.head)) {
1197 set_scroll_position(tab, line_off, curr_off);
1210 ui_on_tab_refresh(struct tab *tab)
1212 wrap_page(&tab->buffer, body_cols);
1213 if (tab == current_tab)
1216 tab->flags |= TAB_URGENT;
1220 ui_on_download_refresh(void)
1222 if (ev_timer_pending(download_timer))
1225 download_timer = ev_timer(&download_refresh_timer,
1226 handle_download_refresh, NULL);
1230 ui_remotely_open_link(const char *uri)
1232 new_tab(uri, NULL, NULL);
1233 ui_on_tab_refresh(current_tab);
1247 ui_toggle_side_window(int kind)
1249 if (in_side_window & kind)
1252 side_window ^= kind;
1253 if (side_window & SIDE_WINDOW_LEFT)
1255 if (side_window & SIDE_WINDOW_BOTTOM)
1256 recompute_downloads();
1259 * ugly hack, but otherwise the window doesn't get updated
1260 * until I call rearrange_windows a second time (e.g. via
1261 * C-l). I will be happy to know why something like this is
1264 rearrange_windows();
1265 rearrange_windows();
1269 ui_show_downloads_pane(void)
1271 if (!(side_window & SIDE_WINDOW_BOTTOM))
1272 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1276 ui_schedule_redraw(void)
1278 should_rearrange_windows = 1;
1282 ui_require_input(struct tab *tab, int hide, void (*fn)(void))
1284 /* TODO: hard-switching to another tab is ugly */
1287 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1288 ir_history, NULL, NULL, 0);
1289 strlcpy(ministate.prompt, "Input required: ",
1290 sizeof(ministate.prompt));
1295 ui_after_message_hook(void)
1297 redraw_minibuffer();
1302 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1305 yornp(prompt, fn, data);
1306 redraw_tab(current_tab);
1310 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1311 struct tab *data, const char *input)
1313 minibuffer_read(prompt, fn, data);
1315 if (input != NULL) {
1316 strlcpy(ministate.buf, input, sizeof(ministate.buf));
1317 cmd_move_end_of_line(&ministate.buffer);
1320 redraw_tab(current_tab);
1324 ui_other_window(void)
1326 if (in_side_window & SIDE_WINDOW_LEFT &&
1327 side_window & SIDE_WINDOW_BOTTOM)
1328 in_side_window = SIDE_WINDOW_BOTTOM;
1329 else if (in_side_window)
1331 else if (!in_side_window && side_window & SIDE_WINDOW_LEFT)
1332 in_side_window = SIDE_WINDOW_LEFT;
1333 else if (!in_side_window && side_window)
1334 in_side_window = SIDE_WINDOW_BOTTOM;
1336 message("No other window to select");
1344 kill(getpid(), SIGSTOP);
1348 rearrange_windows();