2 * Copyright (c) 2021 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.
45 #include "minibuffer.h"
47 #include "telescope.h"
51 static struct event stdioev, winchev;
53 static void restore_curs_x(struct buffer *);
55 static int readkey(void);
56 static void dispatch_stdio(int, short, void*);
57 static void handle_resize(int, short, void*);
58 static void handle_resize_nodelay(int, short, void*);
59 static void rearrange_windows(void);
60 static void line_prefix_and_text(struct vline *, char *, size_t, const char **, const char **);
61 static void print_vline(int, int, WINDOW*, struct vline*);
62 static void redraw_tabline(void);
63 static void redraw_window(WINDOW*, int, int, int, struct buffer*);
64 static void redraw_download(void);
65 static void redraw_help(void);
66 static void redraw_body(struct tab*);
67 static void redraw_modeline(struct tab*);
68 static void redraw_minibuffer(void);
69 static void do_redraw_echoarea(void);
70 static void do_redraw_minibuffer(void);
71 static void do_redraw_minibuffer_compl(void);
72 static void place_cursor(int);
73 static void redraw_tab(struct tab*);
74 static void update_loading_anim(int, short, void*);
75 static void stop_loading_anim(struct tab*);
77 static int should_rearrange_windows;
78 static int show_tab_bar;
82 struct thiskey thiskey;
83 struct tab *current_tab;
85 static struct event resizeev;
86 static struct timeval resize_timer = { 0, 250000 };
88 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
90 int body_lines, body_cols;
93 /* not static so we can see them from help.c */
94 struct buffer helpwin;
95 int help_lines, help_cols;
97 static WINDOW *download;
98 /* not static so we can see them from download.c */
99 struct buffer downloadwin;
103 static int side_window;
104 static int in_side_window;
106 static struct timeval loadingev_timer = { 0, 250000 };
108 static char keybuf[64];
110 /* XXX: don't forget to init these in main() */
111 struct kmap global_map,
117 update_x_offset(void)
119 if (olivetti_mode && fill_column < body_cols)
120 x_offset = (body_cols - fill_column)/2;
126 save_excursion(struct excursion *place, struct buffer *buffer)
128 place->curs_x = buffer->curs_x;
129 place->curs_y = buffer->curs_y;
130 place->line_off = buffer->line_off;
131 place->top_line = buffer->top_line;
132 place->current_line = buffer->current_line;
133 place->cpoff = buffer->cpoff;
137 restore_excursion(struct excursion *place, struct buffer *buffer)
139 buffer->curs_x = place->curs_x;
140 buffer->curs_y = place->curs_y;
141 buffer->line_off = place->line_off;
142 buffer->top_line = place->top_line;
143 buffer->current_line = place->current_line;
144 buffer->cpoff = place->cpoff;
148 restore_curs_x(struct buffer *buffer)
151 const char *prfx, *text;
153 vl = buffer->current_line;
154 if (vl == NULL || vl->line == NULL)
155 buffer->curs_x = buffer->cpoff = 0;
156 else if (vl->parent->data != NULL) {
157 text = vl->parent->data;
158 buffer->curs_x = utf8_snwidth(text + 1, buffer->cpoff) + 1;
160 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
162 /* small hack: don't olivetti-mode the download pane */
163 if (buffer != &downloadwin)
164 buffer->curs_x += x_offset;
169 if (vl->parent->data != NULL)
170 buffer->curs_x += utf8_swidth_between(vl->parent->line,
173 prfx = line_prefixes[vl->parent->type].prfx1;
174 buffer->curs_x += utf8_swidth(prfx);
179 global_key_unbound(void)
181 message("%s is undefined", keybuf);
188 return &ministate.buffer;
189 if (in_side_window & SIDE_WINDOW_LEFT)
191 if (in_side_window & SIDE_WINDOW_BOTTOM)
193 return ¤t_tab->buffer;
201 if ((thiskey.key = wgetch(body)) == ERR)
204 thiskey.meta = thiskey.key == '\e';
206 thiskey.key = wgetch(body);
207 if (thiskey.key == ERR || thiskey.key == '\e') {
215 if ((unsigned int)thiskey.key >= UINT8_MAX)
219 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
221 if ((thiskey.key = wgetch(body)) == ERR) {
222 message("Error decoding user input");
231 dispatch_stdio(int fd, short ev, void *d)
237 /* TODO: schedule a redraw? */
244 if (keybuf[0] != '\0')
245 strlcat(keybuf, " ", sizeof(keybuf));
247 strlcat(keybuf, "M-", sizeof(keybuf));
248 if (thiskey.cp != 0) {
249 utf8_encode(thiskey.cp, tmp);
250 strlcat(keybuf, tmp, sizeof(keybuf));
251 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
252 strlcat(keybuf, keyname, sizeof(keybuf));
254 tmp[0] = thiskey.key;
255 strlcat(keybuf, tmp, sizeof(keybuf));
258 lk = lookup_key(¤t_map, &thiskey, current_buffer());
259 if (lk == LK_UNBOUND) {
260 if (current_map->unhandled_input != NULL)
261 current_map->unhandled_input();
263 global_key_unbound();
265 if (lk != LK_ADVANCED_MAP) {
266 current_map = base_map;
267 strlcpy(keybuf, "", sizeof(keybuf));
270 if (side_window & SIDE_WINDOW_LEFT)
273 if (should_rearrange_windows)
275 redraw_tab(current_tab);
279 handle_resize(int sig, short ev, void *d)
281 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
282 event_del(&resizeev);
284 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
285 evtimer_add(&resizeev, &resize_timer);
289 handle_resize_nodelay(int s, short ev, void *d)
299 should_show_tab_bar(void)
301 if (tab_bar_show == -1)
303 if (tab_bar_show == 0)
306 return TAILQ_NEXT(TAILQ_FIRST(&tabshead), tabs) != NULL;
310 rearrange_windows(void)
313 int minibuffer_lines;
315 should_rearrange_windows = 0;
316 show_tab_bar = should_show_tab_bar();
320 /* 3 lines for the ui and 12 for the */
321 if ((too_small = lines < 15)) {
323 printw("Window too small.");
328 /* move and resize the windows, in reverse order! */
330 if (in_minibuffer == MB_COMPREAD) {
331 minibuffer_lines = MIN(10, lines/2);
332 mvwin(minibuffer, lines - minibuffer_lines, 0);
333 wresize(minibuffer, minibuffer_lines, COLS);
334 lines -= minibuffer_lines;
336 wrap_page(&ministate.compl.buffer, COLS);
339 mvwin(echoarea, --lines, 0);
340 wresize(echoarea, 1, COLS);
342 mvwin(modeline, --lines, 0);
343 wresize(modeline, 1, COLS);
345 if (side_window & SIDE_WINDOW_BOTTOM) {
346 download_lines = MIN(5, lines/2);
347 download_cols = COLS;
348 mvwin(download, lines - download_lines, 0);
349 wresize(download, download_lines, download_cols);
350 lines -= download_lines;
352 wrap_page(&downloadwin, download_cols);
355 body_lines = show_tab_bar ? --lines : lines;
359 * Here we make the assumption that show_tab_bar is either 0
360 * or 1, and reuse that as argument to mvwin.
362 if (side_window & SIDE_WINDOW_LEFT) {
363 help_cols = 0.3 * COLS;
365 mvwin(help, show_tab_bar, 0);
366 wresize(help, help_lines, help_cols);
368 wrap_page(&helpwin, help_cols);
370 body_cols = COLS - help_cols - 1;
371 mvwin(body, show_tab_bar, help_cols);
373 mvwin(body, show_tab_bar, 0);
376 wresize(body, body_lines, body_cols);
379 wresize(tabline, 1, COLS);
381 wrap_page(¤t_tab->buffer, body_cols);
382 redraw_tab(current_tab);
386 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
387 const char **prfx_ret, const char **text_ret)
389 int type, i, cont, width;
392 if ((*text_ret = vl->line) == NULL)
395 cont = vl->flags & L_CONTINUATION;
396 type = vl->parent->type;
398 *prfx_ret = line_prefixes[type].prfx1;
400 *prfx_ret = line_prefixes[type].prfx2;
402 space = vl->parent->data;
403 if (!emojify_link || type != LINE_LINK || space == NULL)
408 width = utf8_swidth_between(vl->parent->line, space);
409 for (i = 0; i < width + 1; ++i)
410 strlcat(buf, " ", len);
412 strlcpy(buf, *text_ret, len);
413 if ((t = strchr(buf, ' ')) != NULL)
415 strlcat(buf, " ", len);
418 *text_ret += (space - vl->parent->line) + 1;
425 print_vline_descr(int width, WINDOW *window, struct vline *vl)
429 switch (vl->parent->type) {
431 case LINE_COMPL_CURRENT:
435 case LINE_DOWNLOAD_DONE:
445 if (vl->parent->alt == NULL)
452 wprintw(window, " ");
453 for (; goal > x; ++x)
454 wprintw(window, " ");
456 wprintw(window, "%s", vl->parent->alt);
460 * Core part of the rendering. It prints a vline starting from the
461 * current cursor position. Printing a vline consists of skipping
462 * `off' columns (for olivetti-mode), print the correct prefix (which
463 * may be the emoji in case of emojified links-lines), printing the
464 * text itself, filling until width - off and filling off columns
468 print_vline(int off, int width, WINDOW *window, struct vline *vl)
471 * Believe me or not, I've seen emoji ten code points long!
472 * That means, to stay large, 4*10 bytes + NUL.
474 char emojibuf[41] = {0};
475 const char *text, *prfx;
479 f = &line_faces[vl->parent->type];
481 /* unused, set by getyx */
484 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
486 wattr_on(window, body_face.left, NULL);
487 for (i = 0; i < off; i++)
489 wattr_off(window, body_face.left, NULL);
491 wattr_on(window, f->prefix, NULL);
492 wprintw(window, "%s", prfx);
493 wattr_off(window, f->prefix, NULL);
495 wattr_on(window, f->text, NULL);
496 wprintw(window, "%s", text);
497 print_vline_descr(width, window, vl);
498 wattr_off(window, f->text, NULL);
504 wattr_on(window, f->trail, NULL);
505 for (i = 0; i < left - off; ++i)
507 wattr_off(window, f->trail, NULL);
509 wattr_on(window, body_face.right, NULL);
510 for (i = 0; i < off; i++)
512 wattr_off(window, body_face.right, NULL);
520 size_t toskip, ots, tabwidth, space, x;
521 int current, y, truncated, pair;
527 /* unused, but setted by a getyx */
530 tabwidth = sizeof(buf)+1;
534 TAILQ_FOREACH(tab, &tabshead, tabs) {
536 if (tab == current_tab)
540 if (toskip * tabwidth <= space)
545 while (toskip != 0 &&
546 (ots - toskip+1) * tabwidth < space)
551 wattr_on(tabline, tab_face.background, NULL);
552 wprintw(tabline, toskip == 0 ? " " : "<");
553 wattr_off(tabline, tab_face.background, NULL);
556 TAILQ_FOREACH(tab, &tabshead, tabs) {
564 getyx(tabline, y, x);
565 if (x + sizeof(buf)+2 >= (size_t)COLS)
568 current = tab == current_tab;
570 if (*(title = tab->buffer.page.title) == '\0')
571 title = tab->hist_cur->h;
573 if (tab->flags & TAB_URGENT)
574 strlcpy(buf, "!", sizeof(buf));
576 strlcpy(buf, " ", sizeof(buf));
578 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
579 /* truncation happens */
580 strlcpy(&buf[sizeof(buf)-4], "...", 4);
582 /* pad with spaces */
583 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
587 pair = current ? tab_face.current : tab_face.tab;
588 wattr_on(tabline, pair, NULL);
589 wprintw(tabline, "%s", buf);
590 wattr_off(tabline, pair, NULL);
592 wattr_on(tabline, tab_face.background, NULL);
593 if (TAILQ_NEXT(tab, tabs) != NULL)
594 wprintw(tabline, "┃");
595 wattr_off(tabline, tab_face.background, NULL);
598 wattr_on(tabline, tab_face.background, NULL);
599 for (; x < (size_t)COLS; ++x)
600 waddch(tabline, ' ');
602 mvwprintw(tabline, 0, COLS-1, ">");
603 wattr_off(tabline, tab_face.background, NULL);
607 * Compute the first visible line around vl. Try to search forward
608 * until the end of the buffer; if a visible line is not found, search
609 * backward. Return NULL if no viable line was found.
612 adjust_line(struct vline *vl, struct buffer *buffer)
619 if (!(vl->parent->flags & L_HIDDEN))
624 t != NULL && t->parent->flags & L_HIDDEN;
625 t = TAILQ_NEXT(t, vlines))
631 /* search backward */
633 t != NULL && t->parent->flags & L_HIDDEN;
634 t = TAILQ_PREV(t, vhead, vlines))
641 redraw_window(WINDOW *win, int off, int height, int width,
642 struct buffer *buffer)
647 restore_curs_x(buffer);
650 * TODO: ignoring buffer->force_update and always
651 * re-rendering. In theory we can recompute the y position
652 * without a re-render, and optimize here. It's not the only
653 * optimisation possible here, wscrl wolud also be an
661 if (TAILQ_EMPTY(&buffer->head))
664 if (buffer->top_line == NULL)
665 buffer->top_line = TAILQ_FIRST(&buffer->head);
667 buffer->top_line = adjust_line(buffer->top_line, buffer);
668 if (buffer->top_line == NULL)
671 buffer->current_line = adjust_line(buffer->current_line, buffer);
675 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
676 if (vl->parent->flags & L_HIDDEN)
680 print_vline(off, width, win, vl);
682 if (vl == buffer->current_line)
694 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
695 if (vl == buffer->current_line)
697 if (vl->parent->flags & L_HIDDEN)
700 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
707 buffer->last_line_off = buffer->line_off;
708 buffer->force_redraw = 0;
710 wmove(win, buffer->curs_y, buffer->curs_x);
714 redraw_download(void)
716 redraw_window(download, 0, download_lines, COLS, &downloadwin);
722 redraw_window(help, 0, help_lines, help_cols, &helpwin);
726 redraw_body(struct tab *tab)
728 static struct tab *last_tab;
731 tab->buffer.force_redraw =1;
734 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
738 trust_status_char(enum trust_state ts)
741 case TS_UNKNOWN: return '-';
742 case TS_UNTRUSTED: return '!';
743 case TS_TEMP_TRUSTED: return '!';
744 case TS_TRUSTED: return 'v';
745 case TS_VERIFIED: return 'V';
751 redraw_modeline(struct tab *tab)
753 struct buffer *buffer;
755 int x, y, max_x, max_y;
757 const char *spin = "-\\|/";
759 buffer = current_buffer();
760 mode = buffer->page.name;
763 wattr_on(modeline, modeline_face.background, NULL);
764 wmove(modeline, 0, 0);
766 wprintw(modeline, "-%c%c %s ",
767 spin[tab->loading_anim_step],
768 trust_status_char(tab->trust),
769 mode == NULL ? "(none)" : mode);
771 pct = (buffer->line_off + buffer->curs_y) * 100.0
774 if (buffer->line_max <= (size_t)body_lines)
775 wprintw(modeline, "All ");
776 else if (buffer->line_off == 0)
777 wprintw(modeline, "Top ");
778 else if (buffer->line_off + body_lines >= buffer->line_max)
779 wprintw(modeline, "Bottom ");
781 wprintw(modeline, "%.0f%% ", pct);
783 wprintw(modeline, "%d/%d %s ",
784 buffer->line_off + buffer->curs_y,
788 getyx(modeline, y, x);
789 getmaxyx(modeline, max_y, max_x);
794 for (; x < max_x; ++x)
795 waddstr(modeline, "-");
797 wattr_off(modeline, modeline_face.background, NULL);
801 redraw_minibuffer(void)
803 wattr_on(echoarea, minibuffer_face.background, NULL);
807 do_redraw_minibuffer();
809 do_redraw_echoarea();
811 if (in_minibuffer == MB_COMPREAD)
812 do_redraw_minibuffer_compl();
814 wattr_off(echoarea, minibuffer_face.background, NULL);
818 do_redraw_echoarea(void)
822 if (ministate.curmesg != NULL)
823 wprintw(echoarea, "%s", ministate.curmesg);
824 else if (*keybuf != '\0')
825 waddstr(echoarea, keybuf);
827 /* If nothing else, show the URL at point */
828 vl = current_tab->buffer.current_line;
829 if (vl != NULL && vl->parent->type == LINE_LINK)
830 waddstr(echoarea, vl->parent->alt);
835 do_redraw_minibuffer(void)
837 struct buffer *cmplbuf, *buffer;
838 size_t off_y, off_x = 0;
839 const char *start, *c;
841 cmplbuf = &ministate.compl.buffer;
842 buffer = &ministate.buffer;
843 (void)off_y; /* unused, set by getyx */
845 wmove(echoarea, 0, 0);
847 if (in_minibuffer == MB_COMPREAD)
848 wprintw(echoarea, "(%2d) ",
851 wprintw(echoarea, "%s", ministate.prompt);
852 if (ministate.hist_cur != NULL)
853 wprintw(echoarea, "(%zu/%zu) ",
854 ministate.hist_off + 1,
855 ministate.history->len);
857 getyx(echoarea, off_y, off_x);
859 start = ministate.hist_cur != NULL
860 ? ministate.hist_cur->h
862 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
863 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
864 start = utf8_next_cp(start);
867 waddstr(echoarea, start);
869 if (ministate.curmesg != NULL)
870 wprintw(echoarea, " [%s]", ministate.curmesg);
872 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
876 do_redraw_minibuffer_compl(void)
878 redraw_window(minibuffer, 0, 10, COLS,
879 &ministate.compl.buffer);
883 * Place the cursor in the right ncurses window. If soft is 1, use
884 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
888 place_cursor(int soft)
890 int (*touch)(WINDOW *);
893 touch = wnoutrefresh;
898 if (side_window & SIDE_WINDOW_LEFT)
900 if (side_window & SIDE_WINDOW_BOTTOM)
904 } else if (in_side_window & SIDE_WINDOW_LEFT) {
907 if (in_side_window & SIDE_WINDOW_BOTTOM)
910 } else if (in_side_window & SIDE_WINDOW_BOTTOM) {
913 if (in_side_window & SIDE_WINDOW_LEFT)
917 if (side_window & SIDE_WINDOW_LEFT)
919 if (side_window & SIDE_WINDOW_BOTTOM)
927 redraw_tab(struct tab *tab)
932 if (side_window & SIDE_WINDOW_LEFT) {
937 if (side_window & SIDE_WINDOW_BOTTOM) {
939 wnoutrefresh(download);
946 redraw_modeline(tab);
949 wnoutrefresh(tabline);
950 wnoutrefresh(modeline);
952 if (in_minibuffer == MB_COMPREAD)
953 wnoutrefresh(minibuffer);
960 dprintf(1, "\033]2;%s - Telescope\a",
961 current_tab->buffer.page.title);
965 start_loading_anim(struct tab *tab)
967 if (tab->loading_anim)
969 tab->loading_anim = 1;
970 evtimer_set(&tab->loadingev, update_loading_anim, tab);
971 evtimer_add(&tab->loadingev, &loadingev_timer);
975 update_loading_anim(int fd, short ev, void *d)
979 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
981 if (tab == current_tab) {
982 redraw_modeline(tab);
989 evtimer_add(&tab->loadingev, &loadingev_timer);
993 stop_loading_anim(struct tab *tab)
995 if (!tab->loading_anim)
997 evtimer_del(&tab->loadingev);
998 tab->loading_anim = 0;
999 tab->loading_anim_step = 0;
1001 if (tab != current_tab)
1004 redraw_modeline(tab);
1013 ui_print_colors(void)
1015 int colors = 0, pairs = 0, can_change = 0;
1016 int columns = 16, lines, color, i, j;
1021 use_default_colors();
1024 pairs = COLOR_PAIRS;
1025 can_change = can_change_color();
1029 printf("Term info:\n");
1030 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1031 getenv("TERM"), colors, pairs, can_change);
1035 printf("No color support\n");
1039 printf("Available colors:\n\n");
1040 lines = (colors - 1) / columns + 1;
1042 for (i = 0; i < lines; ++i) {
1043 for (j = 0; j < columns; ++j, ++color) {
1044 printf("\033[0;38;5;%dm %03d", color, color);
1057 setlocale(LC_ALL, "");
1059 if (TAILQ_EMPTY(&global_map.m)) {
1060 fprintf(stderr, "no keys defined!\n");
1066 /* initialize download window */
1067 TAILQ_INIT(&downloadwin.head);
1068 TAILQ_INIT(&downloadwin.page.head);
1070 /* initialize help window */
1071 TAILQ_INIT(&helpwin.head);
1072 TAILQ_INIT(&helpwin.page.head);
1074 base_map = &global_map;
1075 current_map = &global_map;
1079 if (enable_colors) {
1082 use_default_colors();
1087 config_apply_style();
1092 intrflush(stdscr, FALSE);
1094 if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1096 if ((body = newwin(1, 1, 0, 0)) == NULL)
1098 if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1100 if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1102 if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1104 if ((download = newwin(1, 1, 0, 0)) == NULL)
1106 if ((help = newwin(1, 1, 0, 0)) == NULL)
1109 wbkgd(body, body_face.body);
1110 wbkgd(download, download_face.background);
1111 wbkgd(echoarea, minibuffer_face.background);
1116 scrollok(body, FALSE);
1118 /* non-blocking input */
1122 mvwprintw(body, 0, 0, "");
1130 evtimer_set(&resizeev, handle_resize, NULL);
1132 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1133 event_add(&stdioev, NULL);
1135 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1136 signal_add(&winchev, NULL);
1138 switch_to_tab(current_tab);
1139 rearrange_windows();
1143 ui_on_tab_loaded(struct tab *tab)
1145 stop_loading_anim(tab);
1146 message("Loaded %s", tab->hist_cur->h);
1156 ui_on_tab_refresh(struct tab *tab)
1158 wrap_page(&tab->buffer, body_cols);
1159 if (tab == current_tab)
1162 tab->flags |= TAB_URGENT;
1166 ui_on_download_refresh(void)
1168 if (side_window & SIDE_WINDOW_BOTTOM) {
1169 recompute_downloads();
1170 redraw_tab(current_tab);
1181 ui_toggle_side_window(int kind)
1183 if (in_side_window & kind)
1186 side_window ^= kind;
1187 if (side_window & SIDE_WINDOW_LEFT)
1189 if (side_window & SIDE_WINDOW_BOTTOM)
1190 recompute_downloads();
1193 * ugly hack, but otherwise the window doesn't get updated
1194 * until I call rearrange_windows a second time (e.g. via
1195 * C-l). I will be happy to know why something like this is
1198 rearrange_windows();
1199 rearrange_windows();
1203 ui_show_downloads_pane(void)
1205 if (!(side_window & SIDE_WINDOW_BOTTOM))
1206 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1210 ui_schedule_redraw(void)
1212 should_rearrange_windows = 1;
1216 ui_require_input(struct tab *tab, int hide, int proto)
1220 if (proto == PROTO_GEMINI)
1221 fn = ir_select_gemini;
1222 else if (proto == PROTO_GOPHER)
1223 fn = ir_select_gopher;
1227 /* TODO: hard-switching to another tab is ugly */
1230 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1231 &ir_history, NULL, NULL);
1232 strlcpy(ministate.prompt, "Input required: ",
1233 sizeof(ministate.prompt));
1238 ui_after_message_hook(void)
1240 redraw_minibuffer();
1245 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1248 yornp(prompt, fn, data);
1249 redraw_tab(current_tab);
1253 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1254 struct tab *data, const char *input)
1256 minibuffer_read(prompt, fn, data);
1258 if (input != NULL) {
1259 strlcpy(ministate.buf, input, sizeof(ministate.buf));
1260 cmd_move_end_of_line(&ministate.buffer);
1263 redraw_tab(current_tab);
1267 ui_other_window(void)
1269 if (in_side_window & SIDE_WINDOW_LEFT &&
1270 side_window & SIDE_WINDOW_BOTTOM)
1271 in_side_window = SIDE_WINDOW_BOTTOM;
1272 else if (in_side_window)
1274 else if (!in_side_window && side_window & SIDE_WINDOW_LEFT)
1275 in_side_window = SIDE_WINDOW_LEFT;
1276 else if (!in_side_window && side_window)
1277 in_side_window = SIDE_WINDOW_BOTTOM;
1279 message("No other window to select");
1287 kill(getpid(), SIGSTOP);
1291 rearrange_windows();