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 set_scroll_position(struct tab *, size_t, size_t);
55 static void restore_curs_x(struct buffer *);
57 static int readkey(void);
58 static void dispatch_stdio(int, short, void*);
59 static void handle_resize(int, short, void*);
60 static void handle_resize_nodelay(int, short, void*);
61 static void rearrange_windows(void);
62 static void line_prefix_and_text(struct vline *, char *, size_t, const char **, const char **);
63 static void print_vline(int, int, WINDOW*, struct vline*);
64 static void redraw_tabline(void);
65 static void redraw_window(WINDOW *, int, int, int, int, struct buffer *);
66 static void redraw_download(void);
67 static void redraw_help(void);
68 static void redraw_body(struct tab*);
69 static void redraw_modeline(struct tab*);
70 static void redraw_minibuffer(void);
71 static void do_redraw_echoarea(void);
72 static void do_redraw_minibuffer(void);
73 static void do_redraw_minibuffer_compl(void);
74 static void place_cursor(int);
75 static void redraw_tab(struct tab*);
76 static void update_loading_anim(int, short, void*);
77 static void stop_loading_anim(struct tab*);
79 static int should_rearrange_windows;
80 static int show_tab_bar;
84 struct thiskey thiskey;
85 struct tab *current_tab;
87 static struct event resizeev;
88 static struct timeval resize_timer = { 0, 250000 };
90 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
92 int body_lines, body_cols;
95 /* not static so we can see them from help.c */
96 struct buffer helpwin;
97 int help_lines, help_cols;
99 static WINDOW *download;
100 /* not static so we can see them from download.c */
101 struct buffer downloadwin;
105 static int side_window;
106 static int in_side_window;
108 static struct timeval loadingev_timer = { 0, 250000 };
110 static char keybuf[64];
112 /* XXX: don't forget to init these in main() */
113 struct kmap global_map,
119 update_x_offset(void)
121 if (olivetti_mode && fill_column < body_cols)
122 x_offset = (body_cols - fill_column)/2;
128 set_scroll_position(struct tab *tab, size_t top, size_t cur)
133 int topfound = 0, curfound = 0;
135 last = TAILQ_FIRST(&tab->buffer.page.head);
136 TAILQ_FOREACH(vl, &tab->buffer.head, vlines) {
137 if (last != vl->parent) {
142 if (!topfound && i == top) {
144 tab->buffer.top_line = vl;
147 if (!curfound && i == cur) {
149 tab->buffer.current_line = vl;
155 tab->buffer.top_line = TAILQ_FIRST(&tab->buffer.head);
156 tab->buffer.current_line = tab->buffer.top_line;
161 get_scroll_position(struct tab *tab, size_t *top, size_t *cur)
169 if (tab->buffer.top_line == NULL ||
170 tab->buffer.current_line == NULL)
173 TAILQ_FOREACH(l, &tab->buffer.page.head, lines) {
174 if (tab->buffer.top_line->parent == l)
176 if (tab->buffer.current_line->parent == l)
186 save_excursion(struct excursion *place, struct buffer *buffer)
188 place->curs_x = buffer->curs_x;
189 place->curs_y = buffer->curs_y;
190 place->line_off = buffer->line_off;
191 place->top_line = buffer->top_line;
192 place->current_line = buffer->current_line;
193 place->cpoff = buffer->cpoff;
197 restore_excursion(struct excursion *place, struct buffer *buffer)
199 buffer->curs_x = place->curs_x;
200 buffer->curs_y = place->curs_y;
201 buffer->line_off = place->line_off;
202 buffer->top_line = place->top_line;
203 buffer->current_line = place->current_line;
204 buffer->cpoff = place->cpoff;
208 restore_curs_x(struct buffer *buffer)
211 const char *prfx, *text;
213 vl = buffer->current_line;
214 if (vl == NULL || vl->line == NULL)
215 buffer->curs_x = buffer->cpoff = 0;
216 else if (vl->parent->data != NULL) {
217 text = vl->parent->data;
218 buffer->curs_x = utf8_snwidth(text + 1, buffer->cpoff) + 1;
220 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
222 /* small hack: don't olivetti-mode the download pane */
223 if (buffer != &downloadwin)
224 buffer->curs_x += x_offset;
229 if (vl->parent->data != NULL)
230 buffer->curs_x += utf8_swidth_between(vl->parent->line,
233 prfx = line_prefixes[vl->parent->type].prfx1;
234 buffer->curs_x += utf8_swidth(prfx);
239 global_key_unbound(void)
241 message("%s is undefined", keybuf);
248 return &ministate.buffer;
249 if (in_side_window & SIDE_WINDOW_LEFT)
251 if (in_side_window & SIDE_WINDOW_BOTTOM)
253 return ¤t_tab->buffer;
261 if ((thiskey.key = wgetch(body)) == ERR)
264 thiskey.meta = thiskey.key == '\e';
266 thiskey.key = wgetch(body);
267 if (thiskey.key == ERR || thiskey.key == '\e') {
275 if ((unsigned int)thiskey.key >= UINT8_MAX)
279 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
281 if ((thiskey.key = wgetch(body)) == ERR) {
282 message("Error decoding user input");
291 dispatch_stdio(int fd, short ev, void *d)
297 /* TODO: schedule a redraw? */
304 if (keybuf[0] != '\0')
305 strlcat(keybuf, " ", sizeof(keybuf));
307 strlcat(keybuf, "M-", sizeof(keybuf));
308 if (thiskey.cp != 0) {
309 utf8_encode(thiskey.cp, tmp);
310 strlcat(keybuf, tmp, sizeof(keybuf));
311 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
312 strlcat(keybuf, keyname, sizeof(keybuf));
314 tmp[0] = thiskey.key;
315 strlcat(keybuf, tmp, sizeof(keybuf));
318 lk = lookup_key(¤t_map, &thiskey, current_buffer());
319 if (lk == LK_UNBOUND) {
320 if (current_map->unhandled_input != NULL)
321 current_map->unhandled_input();
323 global_key_unbound();
325 if (lk != LK_ADVANCED_MAP) {
326 current_map = base_map;
327 strlcpy(keybuf, "", sizeof(keybuf));
330 if (side_window & SIDE_WINDOW_LEFT)
333 if (should_rearrange_windows)
335 redraw_tab(current_tab);
339 handle_resize(int sig, short ev, void *d)
341 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
342 event_del(&resizeev);
344 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
345 evtimer_add(&resizeev, &resize_timer);
349 handle_resize_nodelay(int s, short ev, void *d)
359 should_show_tab_bar(void)
361 if (tab_bar_show == -1)
363 if (tab_bar_show == 0)
366 return TAILQ_NEXT(TAILQ_FIRST(&tabshead), tabs) != NULL;
370 rearrange_windows(void)
373 int minibuffer_lines;
375 should_rearrange_windows = 0;
376 show_tab_bar = should_show_tab_bar();
380 /* 3 lines for the ui and 12 for the */
381 if ((too_small = lines < 15)) {
383 printw("Window too small.");
388 /* move and resize the windows, in reverse order! */
390 if (in_minibuffer == MB_COMPREAD) {
391 minibuffer_lines = MIN(10, lines/2);
392 mvwin(minibuffer, lines - minibuffer_lines, 0);
393 wresize(minibuffer, minibuffer_lines, COLS);
394 lines -= minibuffer_lines;
396 wrap_page(&ministate.compl.buffer, COLS);
399 mvwin(echoarea, --lines, 0);
400 wresize(echoarea, 1, COLS);
402 mvwin(modeline, --lines, 0);
403 wresize(modeline, 1, COLS);
405 if (side_window & SIDE_WINDOW_BOTTOM) {
406 download_lines = MIN(5, lines/2);
407 download_cols = COLS;
408 mvwin(download, lines - download_lines, 0);
409 wresize(download, download_lines, download_cols);
410 lines -= download_lines;
412 wrap_page(&downloadwin, download_cols);
415 body_lines = show_tab_bar ? --lines : lines;
419 * Here we make the assumption that show_tab_bar is either 0
420 * or 1, and reuse that as argument to mvwin.
422 if (side_window & SIDE_WINDOW_LEFT) {
423 help_cols = 0.3 * COLS;
425 mvwin(help, show_tab_bar, 0);
426 wresize(help, help_lines, help_cols);
428 wrap_page(&helpwin, help_cols);
430 body_cols = COLS - help_cols - 1;
431 mvwin(body, show_tab_bar, help_cols);
433 mvwin(body, show_tab_bar, 0);
436 wresize(body, body_lines, body_cols);
439 wresize(tabline, 1, COLS);
441 wrap_page(¤t_tab->buffer, body_cols);
442 redraw_tab(current_tab);
446 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
447 const char **prfx_ret, const char **text_ret)
449 int type, i, cont, width;
452 if ((*text_ret = vl->line) == NULL)
455 cont = vl->flags & L_CONTINUATION;
456 type = vl->parent->type;
458 *prfx_ret = line_prefixes[type].prfx1;
460 *prfx_ret = line_prefixes[type].prfx2;
462 space = vl->parent->data;
463 if (!emojify_link || type != LINE_LINK || space == NULL)
468 width = utf8_swidth_between(vl->parent->line, space);
469 for (i = 0; i < width + 1; ++i)
470 strlcat(buf, " ", len);
472 strlcpy(buf, *text_ret, len);
473 if ((t = strchr(buf, ' ')) != NULL)
475 strlcat(buf, " ", len);
478 *text_ret += (space - vl->parent->line) + 1;
485 print_vline_descr(int width, WINDOW *window, struct vline *vl)
489 switch (vl->parent->type) {
491 case LINE_COMPL_CURRENT:
495 case LINE_DOWNLOAD_DONE:
505 if (vl->parent->alt == NULL)
512 wprintw(window, " ");
513 for (; goal > x; ++x)
514 wprintw(window, " ");
516 wprintw(window, "%s", vl->parent->alt);
520 * Core part of the rendering. It prints a vline starting from the
521 * current cursor position. Printing a vline consists of skipping
522 * `off' columns (for olivetti-mode), print the correct prefix (which
523 * may be the emoji in case of emojified links-lines), printing the
524 * text itself, filling until width - off and filling off columns
528 print_vline(int off, int width, WINDOW *window, struct vline *vl)
531 * Believe me or not, I've seen emoji ten code points long!
532 * That means, to stay large, 4*10 bytes + NUL.
534 char emojibuf[41] = {0};
535 const char *text, *prfx;
539 f = &line_faces[vl->parent->type];
541 /* unused, set by getyx */
544 if (vl->parent->type == LINE_FRINGE && fringe_ignore_offset)
547 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
549 wattr_on(window, body_face.left, NULL);
550 for (i = 0; i < off; i++)
552 wattr_off(window, body_face.left, NULL);
554 wattr_on(window, f->prefix, NULL);
555 wprintw(window, "%s", prfx);
556 wattr_off(window, f->prefix, NULL);
558 wattr_on(window, f->text, NULL);
559 wprintw(window, "%s", text);
560 print_vline_descr(width, window, vl);
561 wattr_off(window, f->text, NULL);
567 wattr_on(window, f->trail, NULL);
568 for (i = 0; i < left - off; ++i)
570 wattr_off(window, f->trail, NULL);
572 wattr_on(window, body_face.right, NULL);
573 for (i = 0; i < off; i++)
575 wattr_off(window, body_face.right, NULL);
583 size_t toskip, ots, tabwidth, space, x;
584 int current, y, truncated, pair;
590 /* unused, but setted by a getyx */
593 tabwidth = sizeof(buf)+1;
597 TAILQ_FOREACH(tab, &tabshead, tabs) {
599 if (tab == current_tab)
603 if (toskip * tabwidth <= space)
608 while (toskip != 0 &&
609 (ots - toskip+1) * tabwidth < space)
614 wattr_on(tabline, tab_face.background, NULL);
615 wprintw(tabline, toskip == 0 ? " " : "<");
616 wattr_off(tabline, tab_face.background, NULL);
619 TAILQ_FOREACH(tab, &tabshead, tabs) {
627 getyx(tabline, y, x);
628 if (x + sizeof(buf)+2 >= (size_t)COLS)
631 current = tab == current_tab;
633 if (*(title = tab->buffer.page.title) == '\0')
634 title = tab->hist_cur->h;
636 if (tab->flags & TAB_URGENT)
637 strlcpy(buf, "!", sizeof(buf));
639 strlcpy(buf, " ", sizeof(buf));
641 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
642 /* truncation happens */
643 strlcpy(&buf[sizeof(buf)-4], "...", 4);
645 /* pad with spaces */
646 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
650 pair = current ? tab_face.current : tab_face.tab;
651 wattr_on(tabline, pair, NULL);
652 wprintw(tabline, "%s", buf);
653 wattr_off(tabline, pair, NULL);
655 wattr_on(tabline, tab_face.background, NULL);
656 if (TAILQ_NEXT(tab, tabs) != NULL)
657 wprintw(tabline, "┃");
658 wattr_off(tabline, tab_face.background, NULL);
661 wattr_on(tabline, tab_face.background, NULL);
662 for (; x < (size_t)COLS; ++x)
663 waddch(tabline, ' ');
665 mvwprintw(tabline, 0, COLS-1, ">");
666 wattr_off(tabline, tab_face.background, NULL);
670 * Compute the first visible line around vl. Try to search forward
671 * until the end of the buffer; if a visible line is not found, search
672 * backward. Return NULL if no viable line was found.
675 adjust_line(struct vline *vl, struct buffer *buffer)
682 if (!(vl->parent->flags & L_HIDDEN))
687 t != NULL && t->parent->flags & L_HIDDEN;
688 t = TAILQ_NEXT(t, vlines))
694 /* search backward */
696 t != NULL && t->parent->flags & L_HIDDEN;
697 t = TAILQ_PREV(t, vhead, vlines))
704 redraw_window(WINDOW *win, int off, int height, int width,
705 int show_fringe, struct buffer *buffer)
708 int onscreen = 0, l = 0;
710 restore_curs_x(buffer);
713 * TODO: ignoring buffer->force_update and always
714 * re-rendering. In theory we can recompute the y position
715 * without a re-render, and optimize here. It's not the only
716 * optimisation possible here, wscrl wolud also be an
724 if (TAILQ_EMPTY(&buffer->head))
727 if (buffer->top_line == NULL)
728 buffer->top_line = TAILQ_FIRST(&buffer->head);
730 buffer->top_line = adjust_line(buffer->top_line, buffer);
731 if (buffer->top_line == NULL)
734 buffer->current_line = adjust_line(buffer->current_line, buffer);
736 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
737 if (vl->parent->flags & L_HIDDEN)
741 print_vline(off, width, win, vl);
743 if (vl == buffer->current_line)
755 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
756 if (vl == buffer->current_line)
758 if (vl->parent->flags & L_HIDDEN)
761 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
768 buffer->last_line_off = buffer->line_off;
769 buffer->force_redraw = 0;
771 for (; show_fringe && l < height; l++)
772 print_vline(off, width, win, &fringe);
774 wmove(win, buffer->curs_y, buffer->curs_x);
778 redraw_download(void)
780 redraw_window(download, 0, download_lines, COLS, 0, &downloadwin);
786 redraw_window(help, 0, help_lines, help_cols, 1, &helpwin);
790 redraw_body(struct tab *tab)
792 static struct tab *last_tab;
795 tab->buffer.force_redraw =1;
798 redraw_window(body, x_offset, body_lines, body_cols, 1, &tab->buffer);
802 trust_status_char(enum trust_state ts)
805 case TS_UNKNOWN: return '-';
806 case TS_UNTRUSTED: return '!';
807 case TS_TEMP_TRUSTED: return '!';
808 case TS_TRUSTED: return 'v';
809 case TS_VERIFIED: return 'V';
815 redraw_modeline(struct tab *tab)
817 struct buffer *buffer;
819 int x, y, max_x, max_y;
821 const char *spin = "-\\|/";
823 buffer = current_buffer();
824 mode = buffer->page.name;
827 wattr_on(modeline, modeline_face.background, NULL);
828 wmove(modeline, 0, 0);
830 wprintw(modeline, "-%c%c- %s ",
831 spin[tab->loading_anim_step],
832 trust_status_char(tab->trust),
833 mode == NULL ? "(none)" : mode);
835 pct = (buffer->line_off + buffer->curs_y) * 100.0
838 if (buffer->line_max <= (size_t)body_lines)
839 wprintw(modeline, "All ");
840 else if (buffer->line_off == 0)
841 wprintw(modeline, "Top ");
842 else if (buffer->line_off + body_lines >= buffer->line_max)
843 wprintw(modeline, "Bottom ");
845 wprintw(modeline, "%.0f%% ", pct);
847 wprintw(modeline, "%zu/%zu %s ",
848 buffer->line_off + buffer->curs_y,
852 getyx(modeline, y, x);
853 getmaxyx(modeline, max_y, max_x);
858 for (; x < max_x; ++x)
859 waddstr(modeline, "-");
861 wattr_off(modeline, modeline_face.background, NULL);
865 redraw_minibuffer(void)
867 wattr_on(echoarea, minibuffer_face.background, NULL);
871 do_redraw_minibuffer();
873 do_redraw_echoarea();
875 if (in_minibuffer == MB_COMPREAD)
876 do_redraw_minibuffer_compl();
878 wattr_off(echoarea, minibuffer_face.background, NULL);
882 do_redraw_echoarea(void)
886 if (ministate.curmesg != NULL)
887 wprintw(echoarea, "%s", ministate.curmesg);
888 else if (*keybuf != '\0')
889 waddstr(echoarea, keybuf);
891 /* If nothing else, show the URL at point */
892 vl = current_tab->buffer.current_line;
893 if (vl != NULL && vl->parent->type == LINE_LINK)
894 waddstr(echoarea, vl->parent->alt);
899 do_redraw_minibuffer(void)
901 struct buffer *cmplbuf, *buffer;
902 size_t off_y, off_x = 0;
903 const char *start, *c;
905 cmplbuf = &ministate.compl.buffer;
906 buffer = &ministate.buffer;
907 (void)off_y; /* unused, set by getyx */
909 wmove(echoarea, 0, 0);
911 if (in_minibuffer == MB_COMPREAD)
912 wprintw(echoarea, "(%2zu) ",
915 wprintw(echoarea, "%s", ministate.prompt);
916 if (ministate.hist_cur != NULL)
917 wprintw(echoarea, "(%zu/%zu) ",
918 ministate.hist_off + 1,
919 ministate.history->len);
921 getyx(echoarea, off_y, off_x);
923 start = ministate.hist_cur != NULL
924 ? ministate.hist_cur->h
926 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
927 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
928 start = utf8_next_cp(start);
931 waddstr(echoarea, start);
933 if (ministate.curmesg != NULL)
934 wprintw(echoarea, " [%s]", ministate.curmesg);
936 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
940 do_redraw_minibuffer_compl(void)
942 redraw_window(minibuffer, 0, 10, COLS, 1,
943 &ministate.compl.buffer);
947 * Place the cursor in the right ncurses window. If soft is 1, use
948 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
952 place_cursor(int soft)
954 int (*touch)(WINDOW *);
957 touch = wnoutrefresh;
962 if (side_window & SIDE_WINDOW_LEFT)
964 if (side_window & SIDE_WINDOW_BOTTOM)
968 } else if (in_side_window & SIDE_WINDOW_LEFT) {
971 if (in_side_window & SIDE_WINDOW_BOTTOM)
974 } else if (in_side_window & SIDE_WINDOW_BOTTOM) {
977 if (in_side_window & SIDE_WINDOW_LEFT)
981 if (side_window & SIDE_WINDOW_LEFT)
983 if (side_window & SIDE_WINDOW_BOTTOM)
991 redraw_tab(struct tab *tab)
996 if (side_window & SIDE_WINDOW_LEFT) {
1001 if (side_window & SIDE_WINDOW_BOTTOM) {
1003 wnoutrefresh(download);
1010 redraw_modeline(tab);
1011 redraw_minibuffer();
1013 wnoutrefresh(tabline);
1014 wnoutrefresh(modeline);
1016 if (in_minibuffer == MB_COMPREAD)
1017 wnoutrefresh(minibuffer);
1024 dprintf(1, "\033]2;%s - Telescope\a",
1025 current_tab->buffer.page.title);
1029 start_loading_anim(struct tab *tab)
1031 if (tab->loading_anim)
1033 tab->loading_anim = 1;
1034 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1035 evtimer_add(&tab->loadingev, &loadingev_timer);
1039 update_loading_anim(int fd, short ev, void *d)
1041 struct tab *tab = d;
1043 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1045 if (tab == current_tab) {
1046 redraw_modeline(tab);
1053 evtimer_add(&tab->loadingev, &loadingev_timer);
1057 stop_loading_anim(struct tab *tab)
1059 if (!tab->loading_anim)
1061 evtimer_del(&tab->loadingev);
1062 tab->loading_anim = 0;
1063 tab->loading_anim_step = 0;
1065 if (tab != current_tab)
1068 redraw_modeline(tab);
1077 ui_print_colors(void)
1079 int colors = 0, pairs = 0, can_change = 0;
1080 int columns = 16, lines, color, i, j;
1085 use_default_colors();
1088 pairs = COLOR_PAIRS;
1089 can_change = can_change_color();
1093 printf("Term info:\n");
1094 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1095 getenv("TERM"), colors, pairs, can_change);
1099 printf("No color support\n");
1103 printf("Available colors:\n\n");
1104 lines = (colors - 1) / columns + 1;
1106 for (i = 0; i < lines; ++i) {
1107 for (j = 0; j < columns; ++j, ++color) {
1108 printf("\033[0;38;5;%dm %03d", color, color);
1121 setlocale(LC_ALL, "");
1123 if (TAILQ_EMPTY(&global_map.m)) {
1124 fprintf(stderr, "no keys defined!\n");
1130 /* initialize download window */
1131 TAILQ_INIT(&downloadwin.head);
1132 TAILQ_INIT(&downloadwin.page.head);
1134 /* initialize help window */
1135 TAILQ_INIT(&helpwin.head);
1136 TAILQ_INIT(&helpwin.page.head);
1138 base_map = &global_map;
1139 current_map = &global_map;
1143 if (enable_colors) {
1146 use_default_colors();
1151 config_apply_style();
1156 intrflush(stdscr, FALSE);
1158 if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1160 if ((body = newwin(1, 1, 0, 0)) == NULL)
1162 if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1164 if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1166 if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1168 if ((download = newwin(1, 1, 0, 0)) == NULL)
1170 if ((help = newwin(1, 1, 0, 0)) == NULL)
1173 wbkgd(body, body_face.body);
1174 wbkgd(download, download_face.background);
1175 wbkgd(echoarea, minibuffer_face.background);
1180 scrollok(body, FALSE);
1182 /* non-blocking input */
1186 mvwprintw(body, 0, 0, "");
1194 evtimer_set(&resizeev, handle_resize, NULL);
1196 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1197 event_add(&stdioev, NULL);
1199 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1200 signal_add(&winchev, NULL);
1202 switch_to_tab(current_tab);
1203 rearrange_windows();
1207 ui_on_tab_loaded(struct tab *tab)
1209 stop_loading_anim(tab);
1210 message("Loaded %s", tab->hist_cur->h);
1212 if (tab->hist_cur->current_off != 0 &&
1213 tab->buffer.current_line == TAILQ_FIRST(&tab->buffer.head)) {
1214 set_scroll_position(tab, tab->hist_cur->line_off,
1215 tab->hist_cur->current_off);
1228 ui_on_tab_refresh(struct tab *tab)
1230 wrap_page(&tab->buffer, body_cols);
1231 if (tab == current_tab)
1234 tab->flags |= TAB_URGENT;
1238 ui_on_download_refresh(void)
1240 if (side_window & SIDE_WINDOW_BOTTOM) {
1241 recompute_downloads();
1242 redraw_tab(current_tab);
1253 ui_toggle_side_window(int kind)
1255 if (in_side_window & kind)
1258 side_window ^= kind;
1259 if (side_window & SIDE_WINDOW_LEFT)
1261 if (side_window & SIDE_WINDOW_BOTTOM)
1262 recompute_downloads();
1265 * ugly hack, but otherwise the window doesn't get updated
1266 * until I call rearrange_windows a second time (e.g. via
1267 * C-l). I will be happy to know why something like this is
1270 rearrange_windows();
1271 rearrange_windows();
1275 ui_show_downloads_pane(void)
1277 if (!(side_window & SIDE_WINDOW_BOTTOM))
1278 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1282 ui_schedule_redraw(void)
1284 should_rearrange_windows = 1;
1288 ui_require_input(struct tab *tab, int hide, int proto)
1292 if (proto == PROTO_GEMINI)
1293 fn = ir_select_gemini;
1294 else if (proto == PROTO_GOPHER)
1295 fn = ir_select_gopher;
1299 /* TODO: hard-switching to another tab is ugly */
1302 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1303 &ir_history, NULL, NULL);
1304 strlcpy(ministate.prompt, "Input required: ",
1305 sizeof(ministate.prompt));
1310 ui_after_message_hook(void)
1312 redraw_minibuffer();
1317 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1320 yornp(prompt, fn, data);
1321 redraw_tab(current_tab);
1325 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1326 struct tab *data, const char *input)
1328 minibuffer_read(prompt, fn, data);
1330 if (input != NULL) {
1331 strlcpy(ministate.buf, input, sizeof(ministate.buf));
1332 cmd_move_end_of_line(&ministate.buffer);
1335 redraw_tab(current_tab);
1339 ui_other_window(void)
1341 if (in_side_window & SIDE_WINDOW_LEFT &&
1342 side_window & SIDE_WINDOW_BOTTOM)
1343 in_side_window = SIDE_WINDOW_BOTTOM;
1344 else if (in_side_window)
1346 else if (!in_side_window && side_window & SIDE_WINDOW_LEFT)
1347 in_side_window = SIDE_WINDOW_LEFT;
1348 else if (!in_side_window && side_window)
1349 in_side_window = SIDE_WINDOW_BOTTOM;
1351 message("No other window to select");
1359 kill(getpid(), SIGSTOP);
1363 rearrange_windows();