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"
46 #include "telescope.h"
50 static struct event stdioev, winchev;
52 static void restore_curs_x(struct buffer *);
54 static int readkey(void);
55 static void dispatch_stdio(int, short, void*);
56 static void handle_resize(int, short, void*);
57 static void handle_resize_nodelay(int, short, void*);
58 static void rearrange_windows(void);
59 static void line_prefix_and_text(struct vline *, char *, size_t, const char **, const char **);
60 static void print_vline(int, int, WINDOW*, struct vline*);
61 static void redraw_tabline(void);
62 static void redraw_window(WINDOW*, int, int, int, struct buffer*);
63 static void redraw_help(void);
64 static void redraw_body(struct tab*);
65 static void redraw_modeline(struct tab*);
66 static void redraw_minibuffer(void);
67 static void do_redraw_echoarea(void);
68 static void do_redraw_minibuffer(void);
69 static void do_redraw_minibuffer_compl(void);
70 static void place_cursor(int);
71 static void redraw_tab(struct tab*);
72 static void update_loading_anim(int, short, void*);
73 static void stop_loading_anim(struct tab*);
75 static int should_rearrange_windows;
79 struct thiskey thiskey;
80 struct tab *current_tab;
82 static struct event resizeev;
83 static struct timeval resize_timer = { 0, 250000 };
85 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
87 int body_lines, body_cols;
90 /* not static so we can see them from help.c */
91 struct buffer helpwin;
92 int help_lines, help_cols;
94 static int side_window;
95 static int in_side_window;
97 static struct timeval loadingev_timer = { 0, 250000 };
99 static char keybuf[64];
101 /* XXX: don't forget to init these in main() */
102 struct kmap global_map,
108 update_x_offset(void)
110 if (olivetti_mode && fill_column < body_cols)
111 x_offset = (body_cols - fill_column)/2;
117 save_excursion(struct excursion *place, struct buffer *buffer)
119 place->curs_x = buffer->curs_x;
120 place->curs_y = buffer->curs_y;
121 place->line_off = buffer->line_off;
122 place->top_line = buffer->top_line;
123 place->current_line = buffer->current_line;
124 place->cpoff = buffer->cpoff;
128 restore_excursion(struct excursion *place, struct buffer *buffer)
130 buffer->curs_x = place->curs_x;
131 buffer->curs_y = place->curs_y;
132 buffer->line_off = place->line_off;
133 buffer->top_line = place->top_line;
134 buffer->current_line = place->current_line;
135 buffer->cpoff = place->cpoff;
139 restore_curs_x(struct buffer *buffer)
142 const char *prfx, *text;
144 vl = buffer->current_line;
145 if (vl == NULL || vl->line == NULL)
146 buffer->curs_x = buffer->cpoff = 0;
147 else if (vl->parent->data != NULL) {
148 text = vl->parent->data;
149 buffer->curs_x = utf8_snwidth(text + 1, buffer->cpoff) + 1;
151 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
153 buffer->curs_x += x_offset;
158 if (vl->parent->data != NULL)
159 buffer->curs_x += utf8_swidth_between(vl->parent->line,
162 prfx = line_prefixes[vl->parent->type].prfx1;
163 buffer->curs_x += utf8_swidth(prfx);
168 global_key_unbound(void)
170 message("%s is undefined", keybuf);
177 return &ministate.buffer;
180 return ¤t_tab->buffer;
188 if ((thiskey.key = wgetch(body)) == ERR)
191 thiskey.meta = thiskey.key == 27;
193 thiskey.key = wgetch(body);
194 if (thiskey.key == ERR || thiskey.key == 27) {
202 if ((unsigned int)thiskey.key >= UINT8_MAX)
206 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
208 if ((thiskey.key = wgetch(body)) == ERR) {
209 message("Error decoding user input");
218 dispatch_stdio(int fd, short ev, void *d)
224 /* TODO: schedule a redraw? */
231 if (keybuf[0] != '\0')
232 strlcat(keybuf, " ", sizeof(keybuf));
234 strlcat(keybuf, "M-", sizeof(keybuf));
235 if (thiskey.cp != 0) {
236 utf8_encode(thiskey.cp, tmp);
237 strlcat(keybuf, tmp, sizeof(keybuf));
238 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
239 strlcat(keybuf, keyname, sizeof(keybuf));
241 tmp[0] = thiskey.key;
242 strlcat(keybuf, tmp, sizeof(keybuf));
245 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
246 if (k->meta == thiskey.meta &&
247 k->key == thiskey.key) {
249 current_map = &k->map;
251 current_map = base_map;
252 strlcpy(keybuf, "", sizeof(keybuf));
253 k->fn(current_buffer());
259 if (current_map->unhandled_input != NULL)
260 current_map->unhandled_input();
262 global_key_unbound();
264 strlcpy(keybuf, "", sizeof(keybuf));
265 current_map = base_map;
271 if (should_rearrange_windows)
273 redraw_tab(current_tab);
277 handle_resize(int sig, short ev, void *d)
279 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
280 event_del(&resizeev);
282 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
283 evtimer_add(&resizeev, &resize_timer);
287 handle_resize_nodelay(int s, short ev, void *d)
297 rearrange_windows(void)
301 should_rearrange_windows = 0;
305 if ((too_small = lines < 15)) {
307 printw("Window too small.");
312 /* move and resize the windows, in reverse order! */
314 if (in_minibuffer == MB_COMPREAD) {
315 mvwin(minibuffer, lines-10, 0);
316 wresize(minibuffer, 10, COLS);
319 wrap_page(&ministate.compl.buffer, COLS);
322 mvwin(echoarea, --lines, 0);
323 wresize(echoarea, 1, COLS);
325 mvwin(modeline, --lines, 0);
326 wresize(modeline, 1, COLS);
328 body_lines = --lines;
332 help_cols = 0.3 * COLS;
335 wresize(help, help_lines, help_cols);
337 wrap_page(&helpwin, help_cols);
339 body_cols = COLS - help_cols - 1;
340 mvwin(body, 1, help_cols);
345 wresize(body, body_lines, body_cols);
347 wresize(tabline, 1, COLS);
349 wrap_page(¤t_tab->buffer, body_cols);
350 redraw_tab(current_tab);
354 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
355 const char **prfx_ret, const char **text_ret)
357 int type, i, cont, width;
360 if ((*text_ret = vl->line) == NULL)
363 cont = vl->flags & L_CONTINUATION;
364 type = vl->parent->type;
366 *prfx_ret = line_prefixes[type].prfx1;
368 *prfx_ret = line_prefixes[type].prfx2;
370 space = vl->parent->data;
371 if (!emojify_link || type != LINE_LINK || space == NULL)
376 width = utf8_swidth_between(vl->parent->line, space);
377 for (i = 0; i < width + 1; ++i)
378 strlcat(buf, " ", len);
380 strlcpy(buf, *text_ret, len);
381 if ((t = strchr(buf, ' ')) != NULL)
383 strlcat(buf, " ", len);
386 *text_ret += (space - vl->parent->line) + 1;
393 print_vline_descr(int width, WINDOW *window, struct vline *vl)
397 if (vl->parent->type != LINE_COMPL &&
398 vl->parent->type != LINE_COMPL_CURRENT &&
399 vl->parent->type != LINE_HELP)
402 if (vl->parent->alt == NULL)
408 if (vl->parent->type == LINE_HELP)
414 wprintw(window, " ");
415 for (; goal > x; ++x)
416 wprintw(window, " ");
418 wprintw(window, "%s", vl->parent->alt);
422 * Core part of the rendering. It prints a vline starting from the
423 * current cursor position. Printing a vline consists of skipping
424 * `off' columns (for olivetti-mode), print the correct prefix (which
425 * may be the emoji in case of emojified links-lines), printing the
426 * text itself, filling until width - off and filling off columns
430 print_vline(int off, int width, WINDOW *window, struct vline *vl)
433 * Believe me or not, I've seen emoji ten code points long!
434 * That means, to stay large, 4*10 bytes + NUL.
436 char emojibuf[41] = {0};
437 const char *text, *prfx;
441 f = &line_faces[vl->parent->type];
443 /* unused, set by getyx */
446 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
448 wattr_on(window, body_face.left, NULL);
449 for (i = 0; i < off; i++)
451 wattr_off(window, body_face.left, NULL);
453 wattr_on(window, f->prefix, NULL);
454 wprintw(window, "%s", prfx);
455 wattr_off(window, f->prefix, NULL);
457 wattr_on(window, f->text, NULL);
458 wprintw(window, "%s", text);
459 print_vline_descr(width, window, vl);
460 wattr_off(window, f->text, NULL);
466 wattr_on(window, f->trail, NULL);
467 for (i = 0; i < left - off; ++i)
469 wattr_off(window, f->trail, NULL);
471 wattr_on(window, body_face.right, NULL);
472 for (i = 0; i < off; i++)
474 wattr_off(window, body_face.right, NULL);
482 size_t toskip, ots, tabwidth, space, x;
483 int current, y, truncated, pair;
489 /* unused, but setted by a getyx */
492 tabwidth = sizeof(buf)+1;
496 TAILQ_FOREACH(tab, &tabshead, tabs) {
498 if (tab == current_tab)
502 if (toskip * tabwidth < space)
507 while (toskip != 0 &&
508 (ots - toskip+1) * tabwidth < space)
513 wattr_on(tabline, tab_face.background, NULL);
514 wprintw(tabline, toskip == 0 ? " " : "<");
515 wattr_off(tabline, tab_face.background, NULL);
518 TAILQ_FOREACH(tab, &tabshead, tabs) {
526 getyx(tabline, y, x);
527 if (x + sizeof(buf)+2 >= (size_t)COLS)
530 current = tab == current_tab;
532 if (*(title = tab->buffer.page.title) == '\0')
533 title = tab->hist_cur->h;
535 if (tab->flags & TAB_URGENT)
536 strlcpy(buf, "!", sizeof(buf));
538 strlcpy(buf, " ", sizeof(buf));
540 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
541 /* truncation happens */
542 strlcpy(&buf[sizeof(buf)-4], "...", 4);
544 /* pad with spaces */
545 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
549 pair = current ? tab_face.current : tab_face.tab;
550 wattr_on(tabline, pair, NULL);
551 wprintw(tabline, "%s", buf);
552 wattr_off(tabline, pair, NULL);
554 wattr_on(tabline, tab_face.background, NULL);
555 if (TAILQ_NEXT(tab, tabs) != NULL)
556 wprintw(tabline, "┃");
557 wattr_off(tabline, tab_face.background, NULL);
560 wattr_on(tabline, tab_face.background, NULL);
561 for (; x < (size_t)COLS; ++x)
562 waddch(tabline, ' ');
564 mvwprintw(tabline, 0, COLS-1, ">");
565 wattr_off(tabline, tab_face.background, NULL);
569 * Compute the first visible line around vl. Try to search forward
570 * until the end of the buffer; if a visible line is not found, search
571 * backward. Return NULL if no viable line was found.
574 adjust_line(struct vline *vl, struct buffer *buffer)
581 if (!(vl->parent->flags & L_HIDDEN))
586 t != NULL && t->parent->flags & L_HIDDEN;
587 t = TAILQ_NEXT(t, vlines))
593 /* search backward */
595 t != NULL && t->parent->flags & L_HIDDEN;
596 t = TAILQ_PREV(t, vhead, vlines))
603 redraw_window(WINDOW *win, int off, int height, int width,
604 struct buffer *buffer)
609 restore_curs_x(buffer);
612 * TODO: ignoring buffer->force_update and always
613 * re-rendering. In theory we can recompute the y position
614 * without a re-render, and optimize here. It's not the only
615 * optimisation possible here, wscrl wolud also be an
623 if (TAILQ_EMPTY(&buffer->head))
626 if (buffer->top_line == NULL)
627 buffer->top_line = TAILQ_FIRST(&buffer->head);
629 buffer->top_line = adjust_line(buffer->top_line, buffer);
630 if (buffer->top_line == NULL)
633 buffer->current_line = adjust_line(buffer->current_line, buffer);
637 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
638 if (vl->parent->flags & L_HIDDEN)
642 print_vline(off, width, win, vl);
644 if (vl == buffer->current_line)
656 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
657 if (vl == buffer->current_line)
659 if (vl->parent->flags & L_HIDDEN)
662 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
669 buffer->last_line_off = buffer->line_off;
670 buffer->force_redraw = 0;
672 wmove(win, buffer->curs_y, buffer->curs_x);
678 redraw_window(help, 0, help_lines, help_cols, &helpwin);
682 redraw_body(struct tab *tab)
684 static struct tab *last_tab;
687 tab->buffer.force_redraw =1;
690 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
694 trust_status_char(enum trust_state ts)
697 case TS_UNKNOWN: return '-';
698 case TS_UNTRUSTED: return '!';
699 case TS_TEMP_TRUSTED: return '!';
700 case TS_TRUSTED: return 'v';
701 case TS_VERIFIED: return 'V';
707 redraw_modeline(struct tab *tab)
709 struct buffer *buffer;
711 int x, y, max_x, max_y;
713 const char *spin = "-\\|/";
715 buffer = current_buffer();
716 mode = buffer->page.name;
719 wattr_on(modeline, modeline_face.background, NULL);
720 wmove(modeline, 0, 0);
722 wprintw(modeline, "-%c%c %s ",
723 spin[tab->loading_anim_step],
724 trust_status_char(tab->trust),
725 mode == NULL ? "(none)" : mode);
727 pct = (buffer->line_off + buffer->curs_y) * 100.0
730 if (buffer->line_max <= (size_t)body_lines)
731 wprintw(modeline, "All ");
732 else if (buffer->line_off == 0)
733 wprintw(modeline, "Top ");
734 else if (buffer->line_off + body_lines >= buffer->line_max)
735 wprintw(modeline, "Bottom ");
737 wprintw(modeline, "%.0f%% ", pct);
739 wprintw(modeline, "%d/%d %s ",
740 buffer->line_off + buffer->curs_y,
744 getyx(modeline, y, x);
745 getmaxyx(modeline, max_y, max_x);
750 for (; x < max_x; ++x)
751 waddstr(modeline, "-");
753 wattr_off(modeline, modeline_face.background, NULL);
757 redraw_minibuffer(void)
759 wattr_on(echoarea, minibuffer_face.background, NULL);
763 do_redraw_minibuffer();
765 do_redraw_echoarea();
767 if (in_minibuffer == MB_COMPREAD)
768 do_redraw_minibuffer_compl();
770 wattr_off(echoarea, minibuffer_face.background, NULL);
774 do_redraw_echoarea(void)
778 if (ministate.curmesg != NULL)
779 wprintw(echoarea, "%s", ministate.curmesg);
780 else if (*keybuf != '\0')
781 waddstr(echoarea, keybuf);
783 /* If nothing else, show the URL at point */
784 vl = current_tab->buffer.current_line;
785 if (vl != NULL && vl->parent->type == LINE_LINK)
786 waddstr(echoarea, vl->parent->alt);
791 do_redraw_minibuffer(void)
793 struct buffer *cmplbuf, *buffer;
794 size_t off_y, off_x = 0;
795 const char *start, *c;
797 cmplbuf = &ministate.compl.buffer;
798 buffer = &ministate.buffer;
799 (void)off_y; /* unused, set by getyx */
801 wmove(echoarea, 0, 0);
803 if (in_minibuffer == MB_COMPREAD)
804 wprintw(echoarea, "(%2d) ",
807 wprintw(echoarea, "%s", ministate.prompt);
808 if (ministate.hist_cur != NULL)
809 wprintw(echoarea, "(%zu/%zu) ",
810 ministate.hist_off + 1,
811 ministate.history->len);
813 getyx(echoarea, off_y, off_x);
815 start = ministate.hist_cur != NULL
816 ? ministate.hist_cur->h
818 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
819 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
820 start = utf8_next_cp(start);
823 waddstr(echoarea, start);
825 if (ministate.curmesg != NULL)
826 wprintw(echoarea, " [%s]", ministate.curmesg);
828 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
832 do_redraw_minibuffer_compl(void)
834 redraw_window(minibuffer, 0, 10, COLS,
835 &ministate.compl.buffer);
839 * Place the cursor in the right ncurses window. If soft is 1, use
840 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
844 place_cursor(int soft)
846 int (*touch)(WINDOW *);
849 touch = wnoutrefresh;
858 } else if (in_side_window) {
871 redraw_tab(struct tab *tab)
883 redraw_modeline(tab);
886 wnoutrefresh(tabline);
887 wnoutrefresh(modeline);
889 if (in_minibuffer == MB_COMPREAD)
890 wnoutrefresh(minibuffer);
897 dprintf(1, "\033]2;%s - Telescope\a",
898 current_tab->buffer.page.title);
902 start_loading_anim(struct tab *tab)
904 if (tab->loading_anim)
906 tab->loading_anim = 1;
907 evtimer_set(&tab->loadingev, update_loading_anim, tab);
908 evtimer_add(&tab->loadingev, &loadingev_timer);
912 update_loading_anim(int fd, short ev, void *d)
916 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
918 if (tab == current_tab) {
919 redraw_modeline(tab);
926 evtimer_add(&tab->loadingev, &loadingev_timer);
930 stop_loading_anim(struct tab *tab)
932 if (!tab->loading_anim)
934 evtimer_del(&tab->loadingev);
935 tab->loading_anim = 0;
936 tab->loading_anim_step = 0;
938 if (tab != current_tab)
941 redraw_modeline(tab);
950 ui_print_colors(void)
952 int colors = 0, pairs = 0, can_change = 0;
953 int columns = 16, lines, color, i, j;
958 use_default_colors();
962 can_change = can_change_color();
966 printf("Term info:\n");
967 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
968 getenv("TERM"), colors, pairs, can_change);
972 printf("No color support\n");
976 printf("Available colors:\n\n");
977 lines = (colors - 1) / columns + 1;
979 for (i = 0; i < lines; ++i) {
980 for (j = 0; j < columns; ++j, ++color) {
981 printf("\033[0;38;5;%dm %03d", color, color);
994 setlocale(LC_ALL, "");
996 if (TAILQ_EMPTY(&global_map.m)) {
997 fprintf(stderr, "no keys defined!\n");
1003 /* initialize help window */
1004 TAILQ_INIT(&helpwin.head);
1005 TAILQ_INIT(&helpwin.page.head);
1007 base_map = &global_map;
1008 current_map = &global_map;
1012 if (enable_colors) {
1015 use_default_colors();
1020 config_apply_style();
1025 intrflush(stdscr, FALSE);
1027 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1029 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1031 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1033 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1035 if ((minibuffer = newwin(1, COLS, LINES-1, 0)) == NULL)
1037 if ((help = newwin(1, 1, 1, 0)) == NULL)
1040 body_lines = LINES-3;
1043 wbkgd(body, body_face.body);
1044 wbkgd(echoarea, minibuffer_face.background);
1049 scrollok(body, FALSE);
1051 /* non-blocking input */
1055 mvwprintw(body, 0, 0, "");
1057 evtimer_set(&resizeev, handle_resize, NULL);
1059 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1060 event_add(&stdioev, NULL);
1062 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1063 signal_add(&winchev, NULL);
1071 switch_to_tab(current_tab);
1072 redraw_tab(current_tab);
1078 ui_on_tab_loaded(struct tab *tab)
1080 stop_loading_anim(tab);
1081 message("Loaded %s", tab->hist_cur->h);
1089 ui_on_tab_refresh(struct tab *tab)
1091 wrap_page(&tab->buffer, body_cols);
1092 if (tab == current_tab)
1095 tab->flags |= TAB_URGENT;
1105 ui_toggle_side_window(void)
1107 side_window = !side_window;
1114 * ugly hack, but otherwise the window doesn't get updated
1115 * until I call rearrange_windows a second time (e.g. via
1116 * C-l). I will be happy to know why something like this is
1119 rearrange_windows();
1120 rearrange_windows();
1124 ui_schedule_redraw(void)
1126 should_rearrange_windows = 1;
1130 ui_require_input(struct tab *tab, int hide, int proto)
1134 if (proto == PROTO_GEMINI)
1135 fn = ir_select_gemini;
1136 else if (proto == PROTO_GOPHER)
1137 fn = ir_select_gopher;
1141 /* TODO: hard-switching to another tab is ugly */
1144 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1145 &ir_history, NULL, NULL);
1146 strlcpy(ministate.prompt, "Input required: ",
1147 sizeof(ministate.prompt));
1152 ui_after_message_hook(void)
1154 redraw_minibuffer();
1159 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1162 yornp(prompt, fn, data);
1163 redraw_tab(current_tab);
1167 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1170 minibuffer_read(prompt, fn, data);
1171 redraw_tab(current_tab);
1175 ui_other_window(void)
1178 in_side_window = !in_side_window;
1180 message("No other window to select");
1188 kill(getpid(), SIGSTOP);
1192 rearrange_windows();