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 handle_download_refresh(int, short, void *);
62 static void rearrange_windows(void);
63 static void line_prefix_and_text(struct vline *, char *, size_t, const char **, const char **, int *);
64 static void print_vline(int, int, WINDOW*, struct vline*);
65 static void redraw_tabline(void);
66 static void redraw_window(WINDOW *, int, int, int, int, struct buffer *);
67 static void redraw_download(void);
68 static void redraw_help(void);
69 static void redraw_body(struct tab*);
70 static void redraw_modeline(struct tab*);
71 static void redraw_minibuffer(void);
72 static void do_redraw_echoarea(void);
73 static void do_redraw_minibuffer(void);
74 static void do_redraw_minibuffer_compl(void);
75 static void place_cursor(int);
76 static void redraw_tab(struct tab*);
77 static void update_loading_anim(int, short, void*);
78 static void stop_loading_anim(struct tab*);
80 static int should_rearrange_windows;
81 static int show_tab_bar;
85 struct thiskey thiskey;
86 struct tab *current_tab;
88 static struct event resizeev;
89 static struct timeval resize_timer = { 0, 250000 };
91 static struct event download_refreshev;
92 static struct timeval download_refresh_timer = { 0, 250000 };
94 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
96 int body_lines, body_cols;
99 /* not static so we can see them from help.c */
100 struct buffer helpwin;
101 int help_lines, help_cols;
103 static WINDOW *download;
104 /* not static so we can see them from download.c */
105 struct buffer downloadwin;
109 static int side_window;
110 static int in_side_window;
112 static struct timeval loadingev_timer = { 0, 250000 };
114 static char keybuf[64];
116 /* XXX: don't forget to init these in main() */
117 struct kmap global_map,
123 update_x_offset(void)
125 if (olivetti_mode && fill_column < body_cols)
126 x_offset = (body_cols - fill_column)/2;
132 set_scroll_position(struct tab *tab, size_t top, size_t cur)
139 last = TAILQ_FIRST(&tab->buffer.page.head);
140 TAILQ_FOREACH(vl, &tab->buffer.head, vlines) {
141 if (last != vl->parent) {
146 if (!topfound && i == top) {
148 tab->buffer.top_line = vl;
152 tab->buffer.current_line = vl;
158 tab->buffer.top_line = TAILQ_FIRST(&tab->buffer.head);
160 tab->buffer.current_line = tab->buffer.top_line;
164 get_scroll_position(struct tab *tab, size_t *top, size_t *cur)
172 if (tab->buffer.top_line == NULL ||
173 tab->buffer.current_line == NULL)
176 TAILQ_FOREACH(l, &tab->buffer.page.head, lines) {
177 if (tab->buffer.top_line->parent == l)
179 if (tab->buffer.current_line->parent == l)
189 save_excursion(struct excursion *place, struct buffer *buffer)
191 place->curs_x = buffer->curs_x;
192 place->curs_y = buffer->curs_y;
193 place->line_off = buffer->line_off;
194 place->top_line = buffer->top_line;
195 place->current_line = buffer->current_line;
196 place->cpoff = buffer->cpoff;
200 restore_excursion(struct excursion *place, struct buffer *buffer)
202 buffer->curs_x = place->curs_x;
203 buffer->curs_y = place->curs_y;
204 buffer->line_off = place->line_off;
205 buffer->top_line = place->top_line;
206 buffer->current_line = place->current_line;
207 buffer->cpoff = place->cpoff;
211 restore_curs_x(struct buffer *buffer)
214 const char *prfx, *text;
216 vl = buffer->current_line;
217 if (vl == NULL || vl->len == 0)
218 buffer->curs_x = buffer->cpoff = 0;
219 else if (vl->parent->data != NULL) {
220 text = vl->parent->data;
221 buffer->curs_x = utf8_snwidth(text + 1, buffer->cpoff) + 1;
223 text = vl->parent->line + vl->from;
224 buffer->curs_x = utf8_snwidth(text, buffer->cpoff);
227 /* small hack: don't olivetti-mode the download pane */
228 if (buffer != &downloadwin)
229 buffer->curs_x += x_offset;
234 if (vl->parent->data != NULL)
235 buffer->curs_x += utf8_swidth_between(vl->parent->line,
238 prfx = line_prefixes[vl->parent->type].prfx1;
239 buffer->curs_x += utf8_swidth(prfx);
244 global_key_unbound(void)
246 message("%s is undefined", keybuf);
253 return &ministate.buffer;
254 if (in_side_window & SIDE_WINDOW_LEFT)
256 if (in_side_window & SIDE_WINDOW_BOTTOM)
258 return ¤t_tab->buffer;
266 if ((thiskey.key = wgetch(body)) == ERR)
269 thiskey.meta = thiskey.key == '\e';
271 thiskey.key = wgetch(body);
272 if (thiskey.key == ERR || thiskey.key == '\e') {
280 if ((unsigned int)thiskey.key >= UINT8_MAX)
284 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
286 if ((thiskey.key = wgetch(body)) == ERR) {
287 message("Error decoding user input");
296 dispatch_stdio(int fd, short ev, void *d)
302 /* TODO: schedule a redraw? */
309 if (keybuf[0] != '\0')
310 strlcat(keybuf, " ", sizeof(keybuf));
312 strlcat(keybuf, "M-", sizeof(keybuf));
313 if (thiskey.cp != 0) {
314 utf8_encode(thiskey.cp, tmp);
315 strlcat(keybuf, tmp, sizeof(keybuf));
316 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
317 strlcat(keybuf, keyname, sizeof(keybuf));
319 tmp[0] = thiskey.key;
320 strlcat(keybuf, tmp, sizeof(keybuf));
323 lk = lookup_key(¤t_map, &thiskey, current_buffer());
324 if (lk == LK_UNBOUND) {
325 if (current_map->unhandled_input != NULL)
326 current_map->unhandled_input();
328 global_key_unbound();
330 if (lk != LK_ADVANCED_MAP) {
331 current_map = base_map;
332 strlcpy(keybuf, "", sizeof(keybuf));
335 if (side_window & SIDE_WINDOW_LEFT)
338 if (should_rearrange_windows)
340 redraw_tab(current_tab);
344 handle_resize(int sig, short ev, void *d)
346 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
347 event_del(&resizeev);
349 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
350 evtimer_add(&resizeev, &resize_timer);
354 handle_resize_nodelay(int s, short ev, void *d)
364 handle_download_refresh(int s, short v, void *d)
366 if (side_window & SIDE_WINDOW_BOTTOM) {
367 recompute_downloads();
368 redraw_tab(current_tab);
373 should_show_tab_bar(void)
375 if (tab_bar_show == -1)
377 if (tab_bar_show == 0)
380 return TAILQ_NEXT(TAILQ_FIRST(&tabshead), tabs) != NULL;
384 rearrange_windows(void)
387 int minibuffer_lines;
389 should_rearrange_windows = 0;
390 show_tab_bar = should_show_tab_bar();
394 /* 3 lines for the ui and 12 for the */
395 if ((too_small = lines < 15)) {
397 printw("Window too small.");
402 /* move and resize the windows, in reverse order! */
404 if (in_minibuffer == MB_COMPREAD) {
405 minibuffer_lines = MIN(10, lines/2);
406 mvwin(minibuffer, lines - minibuffer_lines, 0);
407 wresize(minibuffer, minibuffer_lines, COLS);
408 lines -= minibuffer_lines;
410 wrap_page(&ministate.compl.buffer, COLS);
413 mvwin(echoarea, --lines, 0);
414 wresize(echoarea, 1, COLS);
416 mvwin(modeline, --lines, 0);
417 wresize(modeline, 1, COLS);
419 if (side_window & SIDE_WINDOW_BOTTOM) {
420 download_lines = MIN(5, lines/2);
421 download_cols = COLS;
422 mvwin(download, lines - download_lines, 0);
423 wresize(download, download_lines, download_cols);
424 lines -= download_lines;
426 wrap_page(&downloadwin, download_cols);
429 body_lines = show_tab_bar ? --lines : lines;
433 * Here we make the assumption that show_tab_bar is either 0
434 * or 1, and reuse that as argument to mvwin.
436 if (side_window & SIDE_WINDOW_LEFT) {
437 help_cols = 0.3 * COLS;
439 mvwin(help, show_tab_bar, 0);
440 wresize(help, help_lines, help_cols);
442 wrap_page(&helpwin, help_cols);
444 body_cols = COLS - help_cols - 1;
445 mvwin(body, show_tab_bar, help_cols);
447 mvwin(body, show_tab_bar, 0);
450 wresize(body, body_lines, body_cols);
453 wresize(tabline, 1, COLS);
455 wrap_page(¤t_tab->buffer, body_cols);
456 redraw_tab(current_tab);
460 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
461 const char **prfx_ret, const char **text_ret, int *text_len)
463 int type, i, cont, width;
471 cont = vl->flags & L_CONTINUATION;
472 type = vl->parent->type;
474 *prfx_ret = line_prefixes[type].prfx1;
476 *prfx_ret = line_prefixes[type].prfx2;
478 space = vl->parent->data;
479 if (!emojify_link || type != LINE_LINK || space == NULL) {
480 *text_ret = vl->parent->line + vl->from;
481 *text_len = MIN(INT_MAX, vl->len);
487 width = utf8_swidth_between(vl->parent->line, space);
488 for (i = 0; i < width + 1; ++i)
489 strlcat(buf, " ", len);
491 strlcpy(buf, *text_ret, len);
492 if ((t = strchr(buf, ' ')) != NULL)
494 strlcat(buf, " ", len);
497 *text_ret += (space - vl->parent->line) + 1;
505 print_vline_descr(int width, WINDOW *window, struct vline *vl)
509 switch (vl->parent->type) {
511 case LINE_COMPL_CURRENT:
515 case LINE_DOWNLOAD_DONE:
525 if (vl->parent->alt == NULL)
532 wprintw(window, " ");
533 for (; goal > x; ++x)
534 wprintw(window, " ");
536 wprintw(window, "%s", vl->parent->alt);
540 * Core part of the rendering. It prints a vline starting from the
541 * current cursor position. Printing a vline consists of skipping
542 * `off' columns (for olivetti-mode), print the correct prefix (which
543 * may be the emoji in case of emojified links-lines), printing the
544 * text itself, filling until width - off and filling off columns
548 print_vline(int off, int width, WINDOW *window, struct vline *vl)
551 * Believe me or not, I've seen emoji ten code points long!
552 * That means, to stay large, 4*10 bytes + NUL.
554 char emojibuf[41] = {0};
555 const char *text, *prfx;
557 int i, left, x, y, textlen;
559 f = &line_faces[vl->parent->type];
561 /* unused, set by getyx */
564 if (vl->parent->type == LINE_FRINGE && fringe_ignore_offset)
567 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx,
570 wattr_on(window, body_face.left, NULL);
571 for (i = 0; i < off; i++)
573 wattr_off(window, body_face.left, NULL);
575 wattr_on(window, f->prefix, NULL);
576 wprintw(window, "%s", prfx);
577 wattr_off(window, f->prefix, NULL);
579 wattr_on(window, f->text, NULL);
580 wprintw(window, "%.*s", textlen, text);
581 print_vline_descr(width, window, vl);
582 wattr_off(window, f->text, NULL);
588 wattr_on(window, f->trail, NULL);
589 for (i = 0; i < left - off; ++i)
591 wattr_off(window, f->trail, NULL);
593 wattr_on(window, body_face.right, NULL);
594 for (i = 0; i < off; i++)
596 wattr_off(window, body_face.right, NULL);
604 size_t toskip, ots, tabwidth, space, x;
605 int current, y, truncated, pair;
611 /* unused, but setted by a getyx */
614 tabwidth = sizeof(buf)+1;
618 TAILQ_FOREACH(tab, &tabshead, tabs) {
620 if (tab == current_tab)
624 if (toskip * tabwidth <= space)
629 while (toskip != 0 &&
630 (ots - toskip+1) * tabwidth < space)
635 wattr_on(tabline, tab_face.background, NULL);
636 wprintw(tabline, toskip == 0 ? " " : "<");
637 wattr_off(tabline, tab_face.background, NULL);
640 TAILQ_FOREACH(tab, &tabshead, tabs) {
648 getyx(tabline, y, x);
649 if (x + sizeof(buf)+2 >= (size_t)COLS)
652 current = tab == current_tab;
654 if (*(title = tab->buffer.page.title) == '\0')
655 title = tab->hist_cur->h;
657 if (tab->flags & TAB_URGENT)
658 strlcpy(buf, "!", sizeof(buf));
660 strlcpy(buf, " ", sizeof(buf));
662 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
663 /* truncation happens */
664 strlcpy(&buf[sizeof(buf)-4], "...", 4);
666 /* pad with spaces */
667 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
671 pair = current ? tab_face.current : tab_face.tab;
672 wattr_on(tabline, pair, NULL);
673 wprintw(tabline, "%s", buf);
674 wattr_off(tabline, pair, NULL);
676 wattr_on(tabline, tab_face.background, NULL);
677 if (TAILQ_NEXT(tab, tabs) != NULL)
678 wprintw(tabline, "┃");
679 wattr_off(tabline, tab_face.background, NULL);
682 wattr_on(tabline, tab_face.background, NULL);
683 for (; x < (size_t)COLS; ++x)
684 waddch(tabline, ' ');
686 mvwprintw(tabline, 0, COLS-1, ">");
687 wattr_off(tabline, tab_face.background, NULL);
691 * Compute the first visible line around vl. Try to search forward
692 * until the end of the buffer; if a visible line is not found, search
693 * backward. Return NULL if no viable line was found.
696 adjust_line(struct vline *vl, struct buffer *buffer)
703 if (!(vl->parent->flags & L_HIDDEN))
708 t != NULL && t->parent->flags & L_HIDDEN;
709 t = TAILQ_NEXT(t, vlines))
715 /* search backward */
717 t != NULL && t->parent->flags & L_HIDDEN;
718 t = TAILQ_PREV(t, vhead, vlines))
725 redraw_window(WINDOW *win, int off, int height, int width,
726 int show_fringe, struct buffer *buffer)
729 int onscreen = 0, l = 0;
731 restore_curs_x(buffer);
734 * TODO: ignoring buffer->force_update and always
735 * re-rendering. In theory we can recompute the y position
736 * without a re-render, and optimize here. It's not the only
737 * optimisation possible here, wscrl wolud also be an
745 if (TAILQ_EMPTY(&buffer->head))
748 if (buffer->top_line == NULL)
749 buffer->top_line = TAILQ_FIRST(&buffer->head);
751 buffer->top_line = adjust_line(buffer->top_line, buffer);
752 if (buffer->top_line == NULL)
755 buffer->current_line = adjust_line(buffer->current_line, buffer);
757 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
758 if (vl->parent->flags & L_HIDDEN)
762 print_vline(off, width, win, vl);
764 if (vl == buffer->current_line)
776 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
777 if (vl == buffer->current_line)
779 if (vl->parent->flags & L_HIDDEN)
782 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
789 buffer->last_line_off = buffer->line_off;
790 buffer->force_redraw = 0;
792 for (; show_fringe && l < height; l++)
793 print_vline(off, width, win, &fringe);
795 wmove(win, buffer->curs_y, buffer->curs_x);
799 redraw_download(void)
801 redraw_window(download, 0, download_lines, COLS, 0, &downloadwin);
807 redraw_window(help, 0, help_lines, help_cols, 1, &helpwin);
811 redraw_body(struct tab *tab)
813 static struct tab *last_tab;
816 tab->buffer.force_redraw =1;
819 redraw_window(body, x_offset, body_lines, body_cols, 1, &tab->buffer);
823 trust_status_char(enum trust_state ts)
826 case TS_UNKNOWN: return '-';
827 case TS_UNTRUSTED: return '!';
828 case TS_TEMP_TRUSTED: return '!';
829 case TS_TRUSTED: return 'v';
830 case TS_VERIFIED: return 'V';
836 redraw_modeline(struct tab *tab)
838 struct buffer *buffer;
840 int x, y, max_x, max_y;
842 const char *spin = "-\\|/";
844 buffer = current_buffer();
845 mode = buffer->page.name;
848 wattr_on(modeline, modeline_face.background, NULL);
849 wmove(modeline, 0, 0);
851 wprintw(modeline, "-%c%c- %s ",
852 spin[tab->loading_anim_step],
853 trust_status_char(tab->trust),
854 mode == NULL ? "(none)" : mode);
856 pct = (buffer->line_off + buffer->curs_y) * 100.0
859 if (buffer->line_max <= (size_t)body_lines)
860 wprintw(modeline, "All ");
861 else if (buffer->line_off == 0)
862 wprintw(modeline, "Top ");
863 else if (buffer->line_off + body_lines >= buffer->line_max)
864 wprintw(modeline, "Bottom ");
866 wprintw(modeline, "%.0f%% ", pct);
868 wprintw(modeline, "%zu/%zu %s ",
869 buffer->line_off + buffer->curs_y,
873 getyx(modeline, y, x);
874 getmaxyx(modeline, max_y, max_x);
879 for (; x < max_x; ++x)
880 waddstr(modeline, "-");
882 wattr_off(modeline, modeline_face.background, NULL);
886 redraw_minibuffer(void)
888 wattr_on(echoarea, minibuffer_face.background, NULL);
892 do_redraw_minibuffer();
894 do_redraw_echoarea();
896 if (in_minibuffer == MB_COMPREAD)
897 do_redraw_minibuffer_compl();
899 wattr_off(echoarea, minibuffer_face.background, NULL);
903 do_redraw_echoarea(void)
907 if (ministate.curmesg != NULL)
908 wprintw(echoarea, "%s", ministate.curmesg);
909 else if (*keybuf != '\0')
910 waddstr(echoarea, keybuf);
912 /* If nothing else, show the URL at point */
913 vl = current_tab->buffer.current_line;
914 if (vl != NULL && vl->parent->type == LINE_LINK)
915 waddstr(echoarea, vl->parent->alt);
920 do_redraw_minibuffer(void)
922 struct buffer *cmplbuf, *buffer;
923 size_t off_y, off_x = 0;
924 const char *start, *c;
927 cmplbuf = &ministate.compl.buffer;
928 buffer = &ministate.buffer;
929 (void)off_y; /* unused, set by getyx */
931 wmove(echoarea, 0, 0);
933 if (in_minibuffer == MB_COMPREAD)
934 wprintw(echoarea, "(%2zu) ",
937 wprintw(echoarea, "%s", ministate.prompt);
938 if (ministate.hist_cur != NULL)
939 wprintw(echoarea, "(%zu/%zu) ",
940 ministate.hist_off + 1,
941 ministate.history->len);
943 getyx(echoarea, off_y, off_x);
945 start = ministate.hist_cur != NULL
946 ? ministate.hist_cur->h
948 line = buffer->current_line->parent->line + buffer->current_line->from;
949 c = utf8_nth(line, buffer->cpoff);
950 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
951 start = utf8_next_cp(start);
954 waddstr(echoarea, start);
956 if (ministate.curmesg != NULL)
957 wprintw(echoarea, " [%s]", ministate.curmesg);
959 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
963 do_redraw_minibuffer_compl(void)
965 redraw_window(minibuffer, 0, 10, COLS, 1,
966 &ministate.compl.buffer);
970 * Place the cursor in the right ncurses window. If soft is 1, use
971 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
975 place_cursor(int soft)
977 int (*touch)(WINDOW *);
980 touch = wnoutrefresh;
985 if (side_window & SIDE_WINDOW_LEFT)
987 if (side_window & SIDE_WINDOW_BOTTOM)
991 } else if (in_side_window & SIDE_WINDOW_LEFT) {
994 if (in_side_window & SIDE_WINDOW_BOTTOM)
997 } else if (in_side_window & SIDE_WINDOW_BOTTOM) {
1000 if (in_side_window & SIDE_WINDOW_LEFT)
1004 if (side_window & SIDE_WINDOW_LEFT)
1006 if (side_window & SIDE_WINDOW_BOTTOM)
1014 redraw_tab(struct tab *tab)
1019 if (side_window & SIDE_WINDOW_LEFT) {
1024 if (side_window & SIDE_WINDOW_BOTTOM) {
1026 wnoutrefresh(download);
1033 redraw_modeline(tab);
1034 redraw_minibuffer();
1036 wnoutrefresh(tabline);
1037 wnoutrefresh(modeline);
1039 if (in_minibuffer == MB_COMPREAD)
1040 wnoutrefresh(minibuffer);
1047 dprintf(1, "\033]2;%s - Telescope\a",
1048 current_tab->buffer.page.title);
1052 start_loading_anim(struct tab *tab)
1054 if (tab->loading_anim)
1056 tab->loading_anim = 1;
1057 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1058 evtimer_add(&tab->loadingev, &loadingev_timer);
1062 update_loading_anim(int fd, short 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 evtimer_add(&tab->loadingev, &loadingev_timer);
1080 stop_loading_anim(struct tab *tab)
1082 if (!tab->loading_anim)
1084 evtimer_del(&tab->loadingev);
1085 tab->loading_anim = 0;
1086 tab->loading_anim_step = 0;
1088 if (tab != current_tab)
1091 redraw_modeline(tab);
1100 ui_print_colors(void)
1102 int colors = 0, pairs = 0, can_change = 0;
1103 int columns = 16, lines, color, i, j;
1108 use_default_colors();
1111 pairs = COLOR_PAIRS;
1112 can_change = can_change_color();
1116 printf("Term info:\n");
1117 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1118 getenv("TERM"), colors, pairs, can_change);
1122 printf("No color support\n");
1126 printf("Available colors:\n\n");
1127 lines = (colors - 1) / columns + 1;
1129 for (i = 0; i < lines; ++i) {
1130 for (j = 0; j < columns; ++j, ++color) {
1131 printf("\033[0;38;5;%dm %03d", color, color);
1144 setlocale(LC_ALL, "");
1146 if (TAILQ_EMPTY(&global_map.m)) {
1147 fprintf(stderr, "no keys defined!\n");
1153 /* initialize download window */
1154 TAILQ_INIT(&downloadwin.head);
1155 TAILQ_INIT(&downloadwin.page.head);
1157 /* initialize help window */
1158 TAILQ_INIT(&helpwin.head);
1159 TAILQ_INIT(&helpwin.page.head);
1161 base_map = &global_map;
1162 current_map = &global_map;
1166 if (enable_colors) {
1169 use_default_colors();
1174 config_apply_style();
1179 intrflush(stdscr, FALSE);
1181 if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1183 if ((body = newwin(1, 1, 0, 0)) == NULL)
1185 if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1187 if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1189 if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1191 if ((download = newwin(1, 1, 0, 0)) == NULL)
1193 if ((help = newwin(1, 1, 0, 0)) == NULL)
1196 wbkgd(body, body_face.body);
1197 wbkgd(download, download_face.background);
1198 wbkgd(echoarea, minibuffer_face.background);
1203 scrollok(body, FALSE);
1205 /* non-blocking input */
1209 mvwprintw(body, 0, 0, "");
1217 evtimer_set(&resizeev, handle_resize, NULL);
1218 evtimer_set(&download_refreshev, handle_download_refresh, NULL);
1220 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1221 event_add(&stdioev, NULL);
1223 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1224 signal_add(&winchev, NULL);
1226 switch_to_tab(current_tab);
1227 rearrange_windows();
1233 ui_on_tab_loaded(struct tab *tab)
1235 stop_loading_anim(tab);
1236 message("Loaded %s", tab->hist_cur->h);
1238 if (tab->hist_cur->current_off != 0 &&
1239 tab->buffer.current_line == TAILQ_FIRST(&tab->buffer.head)) {
1240 set_scroll_position(tab, tab->hist_cur->line_off,
1241 tab->hist_cur->current_off);
1254 ui_on_tab_refresh(struct tab *tab)
1256 wrap_page(&tab->buffer, body_cols);
1257 if (tab == current_tab)
1260 tab->flags |= TAB_URGENT;
1264 ui_on_download_refresh(void)
1266 if (event_pending(&download_refreshev, EV_TIMEOUT, NULL))
1269 evtimer_set(&download_refreshev, handle_download_refresh, NULL);
1270 evtimer_add(&download_refreshev, &download_refresh_timer);
1274 ui_remotely_open_link(const char *uri)
1276 new_tab(uri, NULL, NULL);
1277 ui_on_tab_refresh(current_tab);
1291 ui_toggle_side_window(int kind)
1293 if (in_side_window & kind)
1296 side_window ^= kind;
1297 if (side_window & SIDE_WINDOW_LEFT)
1299 if (side_window & SIDE_WINDOW_BOTTOM)
1300 recompute_downloads();
1303 * ugly hack, but otherwise the window doesn't get updated
1304 * until I call rearrange_windows a second time (e.g. via
1305 * C-l). I will be happy to know why something like this is
1308 rearrange_windows();
1309 rearrange_windows();
1313 ui_show_downloads_pane(void)
1315 if (!(side_window & SIDE_WINDOW_BOTTOM))
1316 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1320 ui_schedule_redraw(void)
1322 should_rearrange_windows = 1;
1326 ui_require_input(struct tab *tab, int hide, void (*fn)(void))
1328 /* TODO: hard-switching to another tab is ugly */
1331 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1332 &ir_history, NULL, NULL, 0);
1333 strlcpy(ministate.prompt, "Input required: ",
1334 sizeof(ministate.prompt));
1339 ui_after_message_hook(void)
1341 redraw_minibuffer();
1346 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1349 yornp(prompt, fn, data);
1350 redraw_tab(current_tab);
1354 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1355 struct tab *data, const char *input)
1357 minibuffer_read(prompt, fn, data);
1359 if (input != NULL) {
1360 strlcpy(ministate.buf, input, sizeof(ministate.buf));
1361 cmd_move_end_of_line(&ministate.buffer);
1364 redraw_tab(current_tab);
1368 ui_other_window(void)
1370 if (in_side_window & SIDE_WINDOW_LEFT &&
1371 side_window & SIDE_WINDOW_BOTTOM)
1372 in_side_window = SIDE_WINDOW_BOTTOM;
1373 else if (in_side_window)
1375 else if (!in_side_window && side_window & SIDE_WINDOW_LEFT)
1376 in_side_window = SIDE_WINDOW_LEFT;
1377 else if (!in_side_window && side_window)
1378 in_side_window = SIDE_WINDOW_BOTTOM;
1380 message("No other window to select");
1388 kill(getpid(), SIGSTOP);
1392 rearrange_windows();