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) {
148 tab->buffer.current_line = vl;
154 tab->buffer.top_line = TAILQ_FIRST(&tab->buffer.head);
155 tab->buffer.current_line = tab->buffer.top_line;
159 tab->buffer.current_line = tab->buffer.top_line;
163 get_scroll_position(struct tab *tab, size_t *top, size_t *cur)
171 if (tab->buffer.top_line == NULL ||
172 tab->buffer.current_line == NULL)
175 TAILQ_FOREACH(l, &tab->buffer.page.head, lines) {
176 if (tab->buffer.top_line->parent == l)
178 if (tab->buffer.current_line->parent == l)
188 save_excursion(struct excursion *place, struct buffer *buffer)
190 place->curs_x = buffer->curs_x;
191 place->curs_y = buffer->curs_y;
192 place->line_off = buffer->line_off;
193 place->top_line = buffer->top_line;
194 place->current_line = buffer->current_line;
195 place->cpoff = buffer->cpoff;
199 restore_excursion(struct excursion *place, struct buffer *buffer)
201 buffer->curs_x = place->curs_x;
202 buffer->curs_y = place->curs_y;
203 buffer->line_off = place->line_off;
204 buffer->top_line = place->top_line;
205 buffer->current_line = place->current_line;
206 buffer->cpoff = place->cpoff;
210 restore_curs_x(struct buffer *buffer)
213 const char *prfx, *text;
215 vl = buffer->current_line;
216 if (vl == NULL || vl->line == NULL)
217 buffer->curs_x = buffer->cpoff = 0;
218 else if (vl->parent->data != NULL) {
219 text = vl->parent->data;
220 buffer->curs_x = utf8_snwidth(text + 1, buffer->cpoff) + 1;
222 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
224 /* small hack: don't olivetti-mode the download pane */
225 if (buffer != &downloadwin)
226 buffer->curs_x += x_offset;
231 if (vl->parent->data != NULL)
232 buffer->curs_x += utf8_swidth_between(vl->parent->line,
235 prfx = line_prefixes[vl->parent->type].prfx1;
236 buffer->curs_x += utf8_swidth(prfx);
241 global_key_unbound(void)
243 message("%s is undefined", keybuf);
250 return &ministate.buffer;
251 if (in_side_window & SIDE_WINDOW_LEFT)
253 if (in_side_window & SIDE_WINDOW_BOTTOM)
255 return ¤t_tab->buffer;
263 if ((thiskey.key = wgetch(body)) == ERR)
266 thiskey.meta = thiskey.key == '\e';
268 thiskey.key = wgetch(body);
269 if (thiskey.key == ERR || thiskey.key == '\e') {
277 if ((unsigned int)thiskey.key >= UINT8_MAX)
281 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
283 if ((thiskey.key = wgetch(body)) == ERR) {
284 message("Error decoding user input");
293 dispatch_stdio(int fd, short ev, void *d)
299 /* TODO: schedule a redraw? */
306 if (keybuf[0] != '\0')
307 strlcat(keybuf, " ", sizeof(keybuf));
309 strlcat(keybuf, "M-", sizeof(keybuf));
310 if (thiskey.cp != 0) {
311 utf8_encode(thiskey.cp, tmp);
312 strlcat(keybuf, tmp, sizeof(keybuf));
313 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
314 strlcat(keybuf, keyname, sizeof(keybuf));
316 tmp[0] = thiskey.key;
317 strlcat(keybuf, tmp, sizeof(keybuf));
320 lk = lookup_key(¤t_map, &thiskey, current_buffer());
321 if (lk == LK_UNBOUND) {
322 if (current_map->unhandled_input != NULL)
323 current_map->unhandled_input();
325 global_key_unbound();
327 if (lk != LK_ADVANCED_MAP) {
328 current_map = base_map;
329 strlcpy(keybuf, "", sizeof(keybuf));
332 if (side_window & SIDE_WINDOW_LEFT)
335 if (should_rearrange_windows)
337 redraw_tab(current_tab);
341 handle_resize(int sig, short ev, void *d)
343 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
344 event_del(&resizeev);
346 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
347 evtimer_add(&resizeev, &resize_timer);
351 handle_resize_nodelay(int s, short ev, void *d)
361 should_show_tab_bar(void)
363 if (tab_bar_show == -1)
365 if (tab_bar_show == 0)
368 return TAILQ_NEXT(TAILQ_FIRST(&tabshead), tabs) != NULL;
372 rearrange_windows(void)
375 int minibuffer_lines;
377 should_rearrange_windows = 0;
378 show_tab_bar = should_show_tab_bar();
382 /* 3 lines for the ui and 12 for the */
383 if ((too_small = lines < 15)) {
385 printw("Window too small.");
390 /* move and resize the windows, in reverse order! */
392 if (in_minibuffer == MB_COMPREAD) {
393 minibuffer_lines = MIN(10, lines/2);
394 mvwin(minibuffer, lines - minibuffer_lines, 0);
395 wresize(minibuffer, minibuffer_lines, COLS);
396 lines -= minibuffer_lines;
398 wrap_page(&ministate.compl.buffer, COLS);
401 mvwin(echoarea, --lines, 0);
402 wresize(echoarea, 1, COLS);
404 mvwin(modeline, --lines, 0);
405 wresize(modeline, 1, COLS);
407 if (side_window & SIDE_WINDOW_BOTTOM) {
408 download_lines = MIN(5, lines/2);
409 download_cols = COLS;
410 mvwin(download, lines - download_lines, 0);
411 wresize(download, download_lines, download_cols);
412 lines -= download_lines;
414 wrap_page(&downloadwin, download_cols);
417 body_lines = show_tab_bar ? --lines : lines;
421 * Here we make the assumption that show_tab_bar is either 0
422 * or 1, and reuse that as argument to mvwin.
424 if (side_window & SIDE_WINDOW_LEFT) {
425 help_cols = 0.3 * COLS;
427 mvwin(help, show_tab_bar, 0);
428 wresize(help, help_lines, help_cols);
430 wrap_page(&helpwin, help_cols);
432 body_cols = COLS - help_cols - 1;
433 mvwin(body, show_tab_bar, help_cols);
435 mvwin(body, show_tab_bar, 0);
438 wresize(body, body_lines, body_cols);
441 wresize(tabline, 1, COLS);
443 wrap_page(¤t_tab->buffer, body_cols);
444 redraw_tab(current_tab);
448 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
449 const char **prfx_ret, const char **text_ret)
451 int type, i, cont, width;
454 if ((*text_ret = vl->line) == NULL)
457 cont = vl->flags & L_CONTINUATION;
458 type = vl->parent->type;
460 *prfx_ret = line_prefixes[type].prfx1;
462 *prfx_ret = line_prefixes[type].prfx2;
464 space = vl->parent->data;
465 if (!emojify_link || type != LINE_LINK || space == NULL)
470 width = utf8_swidth_between(vl->parent->line, space);
471 for (i = 0; i < width + 1; ++i)
472 strlcat(buf, " ", len);
474 strlcpy(buf, *text_ret, len);
475 if ((t = strchr(buf, ' ')) != NULL)
477 strlcat(buf, " ", len);
480 *text_ret += (space - vl->parent->line) + 1;
487 print_vline_descr(int width, WINDOW *window, struct vline *vl)
491 switch (vl->parent->type) {
493 case LINE_COMPL_CURRENT:
497 case LINE_DOWNLOAD_DONE:
507 if (vl->parent->alt == NULL)
514 wprintw(window, " ");
515 for (; goal > x; ++x)
516 wprintw(window, " ");
518 wprintw(window, "%s", vl->parent->alt);
522 * Core part of the rendering. It prints a vline starting from the
523 * current cursor position. Printing a vline consists of skipping
524 * `off' columns (for olivetti-mode), print the correct prefix (which
525 * may be the emoji in case of emojified links-lines), printing the
526 * text itself, filling until width - off and filling off columns
530 print_vline(int off, int width, WINDOW *window, struct vline *vl)
533 * Believe me or not, I've seen emoji ten code points long!
534 * That means, to stay large, 4*10 bytes + NUL.
536 char emojibuf[41] = {0};
537 const char *text, *prfx;
541 f = &line_faces[vl->parent->type];
543 /* unused, set by getyx */
546 if (vl->parent->type == LINE_FRINGE && fringe_ignore_offset)
549 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
551 wattr_on(window, body_face.left, NULL);
552 for (i = 0; i < off; i++)
554 wattr_off(window, body_face.left, NULL);
556 wattr_on(window, f->prefix, NULL);
557 wprintw(window, "%s", prfx);
558 wattr_off(window, f->prefix, NULL);
560 wattr_on(window, f->text, NULL);
561 wprintw(window, "%s", text);
562 print_vline_descr(width, window, vl);
563 wattr_off(window, f->text, NULL);
569 wattr_on(window, f->trail, NULL);
570 for (i = 0; i < left - off; ++i)
572 wattr_off(window, f->trail, NULL);
574 wattr_on(window, body_face.right, NULL);
575 for (i = 0; i < off; i++)
577 wattr_off(window, body_face.right, NULL);
585 size_t toskip, ots, tabwidth, space, x;
586 int current, y, truncated, pair;
592 /* unused, but setted by a getyx */
595 tabwidth = sizeof(buf)+1;
599 TAILQ_FOREACH(tab, &tabshead, tabs) {
601 if (tab == current_tab)
605 if (toskip * tabwidth <= space)
610 while (toskip != 0 &&
611 (ots - toskip+1) * tabwidth < space)
616 wattr_on(tabline, tab_face.background, NULL);
617 wprintw(tabline, toskip == 0 ? " " : "<");
618 wattr_off(tabline, tab_face.background, NULL);
621 TAILQ_FOREACH(tab, &tabshead, tabs) {
629 getyx(tabline, y, x);
630 if (x + sizeof(buf)+2 >= (size_t)COLS)
633 current = tab == current_tab;
635 if (*(title = tab->buffer.page.title) == '\0')
636 title = tab->hist_cur->h;
638 if (tab->flags & TAB_URGENT)
639 strlcpy(buf, "!", sizeof(buf));
641 strlcpy(buf, " ", sizeof(buf));
643 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
644 /* truncation happens */
645 strlcpy(&buf[sizeof(buf)-4], "...", 4);
647 /* pad with spaces */
648 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
652 pair = current ? tab_face.current : tab_face.tab;
653 wattr_on(tabline, pair, NULL);
654 wprintw(tabline, "%s", buf);
655 wattr_off(tabline, pair, NULL);
657 wattr_on(tabline, tab_face.background, NULL);
658 if (TAILQ_NEXT(tab, tabs) != NULL)
659 wprintw(tabline, "┃");
660 wattr_off(tabline, tab_face.background, NULL);
663 wattr_on(tabline, tab_face.background, NULL);
664 for (; x < (size_t)COLS; ++x)
665 waddch(tabline, ' ');
667 mvwprintw(tabline, 0, COLS-1, ">");
668 wattr_off(tabline, tab_face.background, NULL);
672 * Compute the first visible line around vl. Try to search forward
673 * until the end of the buffer; if a visible line is not found, search
674 * backward. Return NULL if no viable line was found.
677 adjust_line(struct vline *vl, struct buffer *buffer)
684 if (!(vl->parent->flags & L_HIDDEN))
689 t != NULL && t->parent->flags & L_HIDDEN;
690 t = TAILQ_NEXT(t, vlines))
696 /* search backward */
698 t != NULL && t->parent->flags & L_HIDDEN;
699 t = TAILQ_PREV(t, vhead, vlines))
706 redraw_window(WINDOW *win, int off, int height, int width,
707 int show_fringe, struct buffer *buffer)
710 int onscreen = 0, l = 0;
712 restore_curs_x(buffer);
715 * TODO: ignoring buffer->force_update and always
716 * re-rendering. In theory we can recompute the y position
717 * without a re-render, and optimize here. It's not the only
718 * optimisation possible here, wscrl wolud also be an
726 if (TAILQ_EMPTY(&buffer->head))
729 if (buffer->top_line == NULL)
730 buffer->top_line = TAILQ_FIRST(&buffer->head);
732 buffer->top_line = adjust_line(buffer->top_line, buffer);
733 if (buffer->top_line == NULL)
736 buffer->current_line = adjust_line(buffer->current_line, buffer);
738 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
739 if (vl->parent->flags & L_HIDDEN)
743 print_vline(off, width, win, vl);
745 if (vl == buffer->current_line)
757 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
758 if (vl == buffer->current_line)
760 if (vl->parent->flags & L_HIDDEN)
763 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
770 buffer->last_line_off = buffer->line_off;
771 buffer->force_redraw = 0;
773 for (; show_fringe && l < height; l++)
774 print_vline(off, width, win, &fringe);
776 wmove(win, buffer->curs_y, buffer->curs_x);
780 redraw_download(void)
782 redraw_window(download, 0, download_lines, COLS, 0, &downloadwin);
788 redraw_window(help, 0, help_lines, help_cols, 1, &helpwin);
792 redraw_body(struct tab *tab)
794 static struct tab *last_tab;
797 tab->buffer.force_redraw =1;
800 redraw_window(body, x_offset, body_lines, body_cols, 1, &tab->buffer);
804 trust_status_char(enum trust_state ts)
807 case TS_UNKNOWN: return '-';
808 case TS_UNTRUSTED: return '!';
809 case TS_TEMP_TRUSTED: return '!';
810 case TS_TRUSTED: return 'v';
811 case TS_VERIFIED: return 'V';
817 redraw_modeline(struct tab *tab)
819 struct buffer *buffer;
821 int x, y, max_x, max_y;
823 const char *spin = "-\\|/";
825 buffer = current_buffer();
826 mode = buffer->page.name;
829 wattr_on(modeline, modeline_face.background, NULL);
830 wmove(modeline, 0, 0);
832 wprintw(modeline, "-%c%c- %s ",
833 spin[tab->loading_anim_step],
834 trust_status_char(tab->trust),
835 mode == NULL ? "(none)" : mode);
837 pct = (buffer->line_off + buffer->curs_y) * 100.0
840 if (buffer->line_max <= (size_t)body_lines)
841 wprintw(modeline, "All ");
842 else if (buffer->line_off == 0)
843 wprintw(modeline, "Top ");
844 else if (buffer->line_off + body_lines >= buffer->line_max)
845 wprintw(modeline, "Bottom ");
847 wprintw(modeline, "%.0f%% ", pct);
849 wprintw(modeline, "%zu/%zu %s ",
850 buffer->line_off + buffer->curs_y,
854 getyx(modeline, y, x);
855 getmaxyx(modeline, max_y, max_x);
860 for (; x < max_x; ++x)
861 waddstr(modeline, "-");
863 wattr_off(modeline, modeline_face.background, NULL);
867 redraw_minibuffer(void)
869 wattr_on(echoarea, minibuffer_face.background, NULL);
873 do_redraw_minibuffer();
875 do_redraw_echoarea();
877 if (in_minibuffer == MB_COMPREAD)
878 do_redraw_minibuffer_compl();
880 wattr_off(echoarea, minibuffer_face.background, NULL);
884 do_redraw_echoarea(void)
888 if (ministate.curmesg != NULL)
889 wprintw(echoarea, "%s", ministate.curmesg);
890 else if (*keybuf != '\0')
891 waddstr(echoarea, keybuf);
893 /* If nothing else, show the URL at point */
894 vl = current_tab->buffer.current_line;
895 if (vl != NULL && vl->parent->type == LINE_LINK)
896 waddstr(echoarea, vl->parent->alt);
901 do_redraw_minibuffer(void)
903 struct buffer *cmplbuf, *buffer;
904 size_t off_y, off_x = 0;
905 const char *start, *c;
907 cmplbuf = &ministate.compl.buffer;
908 buffer = &ministate.buffer;
909 (void)off_y; /* unused, set by getyx */
911 wmove(echoarea, 0, 0);
913 if (in_minibuffer == MB_COMPREAD)
914 wprintw(echoarea, "(%2zu) ",
917 wprintw(echoarea, "%s", ministate.prompt);
918 if (ministate.hist_cur != NULL)
919 wprintw(echoarea, "(%zu/%zu) ",
920 ministate.hist_off + 1,
921 ministate.history->len);
923 getyx(echoarea, off_y, off_x);
925 start = ministate.hist_cur != NULL
926 ? ministate.hist_cur->h
928 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
929 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
930 start = utf8_next_cp(start);
933 waddstr(echoarea, start);
935 if (ministate.curmesg != NULL)
936 wprintw(echoarea, " [%s]", ministate.curmesg);
938 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
942 do_redraw_minibuffer_compl(void)
944 redraw_window(minibuffer, 0, 10, COLS, 1,
945 &ministate.compl.buffer);
949 * Place the cursor in the right ncurses window. If soft is 1, use
950 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
954 place_cursor(int soft)
956 int (*touch)(WINDOW *);
959 touch = wnoutrefresh;
964 if (side_window & SIDE_WINDOW_LEFT)
966 if (side_window & SIDE_WINDOW_BOTTOM)
970 } else if (in_side_window & SIDE_WINDOW_LEFT) {
973 if (in_side_window & SIDE_WINDOW_BOTTOM)
976 } else if (in_side_window & SIDE_WINDOW_BOTTOM) {
979 if (in_side_window & SIDE_WINDOW_LEFT)
983 if (side_window & SIDE_WINDOW_LEFT)
985 if (side_window & SIDE_WINDOW_BOTTOM)
993 redraw_tab(struct tab *tab)
998 if (side_window & SIDE_WINDOW_LEFT) {
1003 if (side_window & SIDE_WINDOW_BOTTOM) {
1005 wnoutrefresh(download);
1012 redraw_modeline(tab);
1013 redraw_minibuffer();
1015 wnoutrefresh(tabline);
1016 wnoutrefresh(modeline);
1018 if (in_minibuffer == MB_COMPREAD)
1019 wnoutrefresh(minibuffer);
1026 dprintf(1, "\033]2;%s - Telescope\a",
1027 current_tab->buffer.page.title);
1031 start_loading_anim(struct tab *tab)
1033 if (tab->loading_anim)
1035 tab->loading_anim = 1;
1036 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1037 evtimer_add(&tab->loadingev, &loadingev_timer);
1041 update_loading_anim(int fd, short ev, void *d)
1043 struct tab *tab = d;
1045 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1047 if (tab == current_tab) {
1048 redraw_modeline(tab);
1055 evtimer_add(&tab->loadingev, &loadingev_timer);
1059 stop_loading_anim(struct tab *tab)
1061 if (!tab->loading_anim)
1063 evtimer_del(&tab->loadingev);
1064 tab->loading_anim = 0;
1065 tab->loading_anim_step = 0;
1067 if (tab != current_tab)
1070 redraw_modeline(tab);
1079 ui_print_colors(void)
1081 int colors = 0, pairs = 0, can_change = 0;
1082 int columns = 16, lines, color, i, j;
1087 use_default_colors();
1090 pairs = COLOR_PAIRS;
1091 can_change = can_change_color();
1095 printf("Term info:\n");
1096 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1097 getenv("TERM"), colors, pairs, can_change);
1101 printf("No color support\n");
1105 printf("Available colors:\n\n");
1106 lines = (colors - 1) / columns + 1;
1108 for (i = 0; i < lines; ++i) {
1109 for (j = 0; j < columns; ++j, ++color) {
1110 printf("\033[0;38;5;%dm %03d", color, color);
1123 setlocale(LC_ALL, "");
1125 if (TAILQ_EMPTY(&global_map.m)) {
1126 fprintf(stderr, "no keys defined!\n");
1132 /* initialize download window */
1133 TAILQ_INIT(&downloadwin.head);
1134 TAILQ_INIT(&downloadwin.page.head);
1136 /* initialize help window */
1137 TAILQ_INIT(&helpwin.head);
1138 TAILQ_INIT(&helpwin.page.head);
1140 base_map = &global_map;
1141 current_map = &global_map;
1145 if (enable_colors) {
1148 use_default_colors();
1153 config_apply_style();
1158 intrflush(stdscr, FALSE);
1160 if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1162 if ((body = newwin(1, 1, 0, 0)) == NULL)
1164 if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1166 if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1168 if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1170 if ((download = newwin(1, 1, 0, 0)) == NULL)
1172 if ((help = newwin(1, 1, 0, 0)) == NULL)
1175 wbkgd(body, body_face.body);
1176 wbkgd(download, download_face.background);
1177 wbkgd(echoarea, minibuffer_face.background);
1182 scrollok(body, FALSE);
1184 /* non-blocking input */
1188 mvwprintw(body, 0, 0, "");
1196 evtimer_set(&resizeev, handle_resize, NULL);
1198 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1199 event_add(&stdioev, NULL);
1201 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1202 signal_add(&winchev, NULL);
1204 switch_to_tab(current_tab);
1205 rearrange_windows();
1209 ui_on_tab_loaded(struct tab *tab)
1211 stop_loading_anim(tab);
1212 message("Loaded %s", tab->hist_cur->h);
1214 if (tab->hist_cur->current_off != 0 &&
1215 tab->buffer.current_line == TAILQ_FIRST(&tab->buffer.head)) {
1216 set_scroll_position(tab, tab->hist_cur->line_off,
1217 tab->hist_cur->current_off);
1230 ui_on_tab_refresh(struct tab *tab)
1232 wrap_page(&tab->buffer, body_cols);
1233 if (tab == current_tab)
1236 tab->flags |= TAB_URGENT;
1240 ui_on_download_refresh(void)
1242 if (side_window & SIDE_WINDOW_BOTTOM) {
1243 recompute_downloads();
1244 redraw_tab(current_tab);
1255 ui_toggle_side_window(int kind)
1257 if (in_side_window & kind)
1260 side_window ^= kind;
1261 if (side_window & SIDE_WINDOW_LEFT)
1263 if (side_window & SIDE_WINDOW_BOTTOM)
1264 recompute_downloads();
1267 * ugly hack, but otherwise the window doesn't get updated
1268 * until I call rearrange_windows a second time (e.g. via
1269 * C-l). I will be happy to know why something like this is
1272 rearrange_windows();
1273 rearrange_windows();
1277 ui_show_downloads_pane(void)
1279 if (!(side_window & SIDE_WINDOW_BOTTOM))
1280 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1284 ui_schedule_redraw(void)
1286 should_rearrange_windows = 1;
1290 ui_require_input(struct tab *tab, int hide, int proto)
1294 if (proto == PROTO_GEMINI)
1295 fn = ir_select_gemini;
1296 else if (proto == PROTO_GOPHER)
1297 fn = ir_select_gopher;
1301 /* TODO: hard-switching to another tab is ugly */
1304 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1305 &ir_history, NULL, NULL);
1306 strlcpy(ministate.prompt, "Input required: ",
1307 sizeof(ministate.prompt));
1312 ui_after_message_hook(void)
1314 redraw_minibuffer();
1319 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1322 yornp(prompt, fn, data);
1323 redraw_tab(current_tab);
1327 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1328 struct tab *data, const char *input)
1330 minibuffer_read(prompt, fn, data);
1332 if (input != NULL) {
1333 strlcpy(ministate.buf, input, sizeof(ministate.buf));
1334 cmd_move_end_of_line(&ministate.buffer);
1337 redraw_tab(current_tab);
1341 ui_other_window(void)
1343 if (in_side_window & SIDE_WINDOW_LEFT &&
1344 side_window & SIDE_WINDOW_BOTTOM)
1345 in_side_window = SIDE_WINDOW_BOTTOM;
1346 else if (in_side_window)
1348 else if (!in_side_window && side_window & SIDE_WINDOW_LEFT)
1349 in_side_window = SIDE_WINDOW_LEFT;
1350 else if (!in_side_window && side_window)
1351 in_side_window = SIDE_WINDOW_BOTTOM;
1353 message("No other window to select");
1361 kill(getpid(), SIGSTOP);
1365 rearrange_windows();