Blob


1 /*
2 * Copyright (c) 2021, 2024 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 /*
18 * Ncurses UI for telescope.
19 *
20 * Text scrolling
21 * ==============
22 *
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.
26 *
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.
30 *
31 */
33 #include "compat.h"
35 #include <sys/time.h>
36 #include <sys/wait.h>
38 #include <curses.h>
39 #include <errno.h>
40 #include <locale.h>
41 #include <signal.h>
42 #include <stdarg.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
47 #include <grapheme.h>
49 #include "cmd.h"
50 #include "defaults.h"
51 #include "ev.h"
52 #include "exec.h"
53 #include "hist.h"
54 #include "keymap.h"
55 #include "mailcap.h"
56 #include "minibuffer.h"
57 #include "session.h"
58 #include "telescope.h"
59 #include "ui.h"
60 #include "utf8.h"
62 static void set_scroll_position(struct tab *, size_t, size_t);
64 static void restore_curs_x(struct buffer *);
66 static int readkey(void);
67 static void dispatch_stdio(int, int, void*);
68 static void handle_signal(int, int, void*);
69 static void handle_resize_nodelay(int, int, void*);
70 static void handle_download_refresh(int, int, void *);
71 static void rearrange_windows(void);
72 static void line_prefix_and_text(int, struct vline *, char *, size_t, const char **, const char **, int *);
73 static void print_vline(int, int, WINDOW*, struct vline*);
74 static void redraw_tabline(void);
75 static void redraw_window(WINDOW *, int, int, int, int, struct buffer *);
76 static void redraw_download(void);
77 static void redraw_help(void);
78 static void redraw_body(struct tab*);
79 static void redraw_modeline(struct tab*);
80 static void redraw_minibuffer(void);
81 static void do_redraw_echoarea(void);
82 static void do_redraw_minibuffer(void);
83 static void do_redraw_minibuffer_compl(void);
84 static void place_cursor(int);
85 static void redraw_tab(struct tab*);
86 static void update_loading_anim(int, int, void*);
87 static void stop_loading_anim(struct tab*);
89 static int should_rearrange_windows;
90 static int show_tab_bar;
91 static int too_small;
92 static int x_offset;
94 struct thiskey thiskey;
95 struct tab *current_tab;
97 static unsigned int resize_timer;
98 static struct timeval resize_tv = { 0, 250000 };
100 static unsigned int download_timer;
101 static struct timeval download_refresh_timer = { 0, 250000 };
103 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
105 int body_lines, body_cols;
107 static WINDOW *help;
108 /* not static so we can see them from help.c */
109 struct buffer helpwin;
110 int help_lines, help_cols;
112 static WINDOW *download;
113 /* not static so we can see them from download.c */
114 struct buffer downloadwin;
115 int download_lines;
116 int download_cols;
118 static int side_window;
119 static int in_side_window;
121 static struct timeval loading_tv = { 0, 250000 };
123 static char keybuf[64];
125 /* XXX: don't forget to init these in main() */
126 struct kmap global_map,
127 minibuffer_map,
128 *current_map,
129 *base_map;
131 static inline void
132 update_x_offset(void)
134 if (olivetti_mode && fill_column < body_cols)
135 x_offset = (body_cols - fill_column)/2;
136 else
137 x_offset = 0;
140 static void
141 set_scroll_position(struct tab *tab, size_t top, size_t cur)
143 struct line *last;
144 struct vline *vl;
145 size_t i = 0;
146 int topfound = 0;
148 last = TAILQ_FIRST(&tab->buffer.head);
149 TAILQ_FOREACH(vl, &tab->buffer.vhead, vlines) {
150 if (last != vl->parent) {
151 last = vl->parent;
152 i++;
155 if (!topfound && i == top) {
156 topfound = 1;
157 tab->buffer.top_line = vl;
160 if (i == cur) {
161 tab->buffer.current_line = vl;
162 return;
166 if (!topfound)
167 tab->buffer.top_line = TAILQ_FIRST(&tab->buffer.vhead);
169 tab->buffer.current_line = tab->buffer.top_line;
172 void
173 get_scroll_position(struct tab *tab, size_t *top, size_t *cur)
175 struct line *l;
176 int topfound = 0;
178 *top = 0;
179 *cur = 0;
181 if (tab->buffer.top_line == NULL ||
182 tab->buffer.current_line == NULL)
183 return;
185 TAILQ_FOREACH(l, &tab->buffer.head, lines) {
186 if (tab->buffer.top_line->parent == l)
187 topfound = 1;
188 if (tab->buffer.current_line->parent == l)
189 return;
191 if (!topfound)
192 (*top)++;
193 (*cur)++;
197 void
198 save_excursion(struct excursion *place, struct buffer *buffer)
200 place->curs_x = buffer->curs_x;
201 place->curs_y = buffer->curs_y;
202 place->line_off = buffer->line_off;
203 place->top_line = buffer->top_line;
204 place->current_line = buffer->current_line;
205 place->point_offset = buffer->point_offset;
208 void
209 restore_excursion(struct excursion *place, struct buffer *buffer)
211 buffer->curs_x = place->curs_x;
212 buffer->curs_y = place->curs_y;
213 buffer->line_off = place->line_off;
214 buffer->top_line = place->top_line;
215 buffer->current_line = place->current_line;
216 buffer->point_offset = place->point_offset;
219 static void
220 restore_curs_x(struct buffer *buffer)
222 struct vline *vl;
223 struct lineprefix *lp = line_prefixes;
224 const char *prfx, *text;
226 if (dont_apply_styling)
227 lp = raw_prefixes;
229 buffer->curs_x = 0;
231 /* small hack: don't olivetti-mode the download pane */
232 if (buffer != &downloadwin)
233 buffer->curs_x += x_offset;
235 vl = buffer->current_line;
236 if (vl == NULL || vl->len == 0 || vl->parent == NULL)
237 buffer->curs_x += buffer->point_offset = 0;
238 else if (vl->parent->data != NULL) {
239 text = vl->parent->data;
240 buffer->curs_x += utf8_snwidth(text, buffer->point_offset,
241 buffer->curs_x);
242 } else {
243 text = vl->parent->line + vl->from;
244 buffer->curs_x += utf8_snwidth(text, buffer->point_offset,
245 buffer->curs_x);
248 if (vl == NULL)
249 return;
251 if (vl->parent->data != NULL)
252 buffer->curs_x += utf8_swidth_between(vl->parent->line,
253 vl->parent->data, buffer->curs_x);
254 else {
255 prfx = lp[vl->parent->type].prfx1;
256 buffer->curs_x += utf8_swidth(prfx, buffer->curs_x);
260 void
261 global_key_unbound(void)
263 message("%s is undefined", keybuf);
266 struct buffer *
267 current_buffer(void)
269 if (in_minibuffer)
270 return &ministate.buffer;
271 if (in_side_window & SIDE_WINDOW_LEFT)
272 return &helpwin;
273 if (in_side_window & SIDE_WINDOW_BOTTOM)
274 return &downloadwin;
275 return &current_tab->buffer;
278 static int
279 readkey(void)
281 uint32_t state = 0;
283 if ((thiskey.key = wgetch(body)) == ERR)
284 return 0;
286 thiskey.meta = thiskey.key == '\e';
287 if (thiskey.meta) {
288 thiskey.key = wgetch(body);
289 if (thiskey.key == ERR || thiskey.key == '\e') {
290 thiskey.meta = 0;
291 thiskey.key = '\e';
295 thiskey.cp = 0;
297 if ((unsigned int)thiskey.key >= UINT8_MAX)
298 return 1;
300 while (1) {
301 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
302 break;
303 if ((thiskey.key = wgetch(body)) == ERR) {
304 message("Error decoding user input");
305 return 0;
309 return 1;
312 static void
313 dispatch_stdio(int fd, int ev, void *d)
315 int lk;
316 const char *keyname;
317 char tmp[5] = {0};
319 /* TODO: schedule a redraw? */
320 if (too_small)
321 return;
323 if (!readkey())
324 return;
326 if (keybuf[0] != '\0')
327 strlcat(keybuf, " ", sizeof(keybuf));
328 if (thiskey.meta)
329 strlcat(keybuf, "M-", sizeof(keybuf));
330 if (thiskey.cp != 0) {
331 grapheme_encode_utf8(thiskey.cp, tmp, sizeof(tmp));
332 strlcat(keybuf, tmp, sizeof(keybuf));
333 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
334 strlcat(keybuf, keyname, sizeof(keybuf));
335 } else {
336 tmp[0] = thiskey.key;
337 strlcat(keybuf, tmp, sizeof(keybuf));
340 lk = lookup_key(&current_map, &thiskey, current_buffer());
341 if (lk == LK_UNBOUND) {
342 if (current_map->unhandled_input != NULL)
343 current_map->unhandled_input();
344 else
345 global_key_unbound();
347 if (lk != LK_ADVANCED_MAP) {
348 current_map = base_map;
349 strlcpy(keybuf, "", sizeof(keybuf));
352 if (side_window & SIDE_WINDOW_LEFT)
353 recompute_help();
355 if (should_rearrange_windows)
356 rearrange_windows();
357 redraw_tab(current_tab);
360 static void
361 handle_signal(int sig, int ev, void *d)
363 int ret;
365 switch (sig) {
366 case SIGWINCH:
367 ev_timer_cancel(resize_timer);
368 resize_timer = ev_timer(&resize_tv, handle_resize_nodelay,
369 NULL);
370 break;
371 case SIGCHLD:
372 do {
373 ret = waitpid(-1, NULL, WNOHANG);
374 } while (ret == -1 && errno == EINTR);
375 break;
379 static void
380 handle_resize_nodelay(int s, int ev, void *d)
382 endwin();
383 refresh();
384 clear();
386 rearrange_windows();
389 static void
390 handle_download_refresh(int s, int v, void *d)
392 if (side_window & SIDE_WINDOW_BOTTOM) {
393 recompute_downloads();
394 redraw_tab(current_tab);
398 static inline int
399 should_show_tab_bar(void)
401 if (tab_bar_show == -1)
402 return 0;
403 if (tab_bar_show == 0)
404 return 1;
406 return TAILQ_NEXT(TAILQ_FIRST(&tabshead), tabs) != NULL;
409 static void
410 rearrange_windows(void)
412 int lines;
413 int minibuffer_lines;
415 should_rearrange_windows = 0;
416 show_tab_bar = should_show_tab_bar();
418 lines = LINES;
420 /* 3 lines for the ui and 12 for the body and minibuffer */
421 if ((too_small = lines < 15)) {
422 erase();
423 printw("Window too small.");
424 refresh();
425 return;
428 /* move and resize the windows, in reverse order! */
430 if (in_minibuffer == MB_COMPREAD) {
431 minibuffer_lines = MIN(10, lines/2);
432 mvwin(minibuffer, lines - minibuffer_lines, 0);
433 wresize(minibuffer, minibuffer_lines, COLS);
434 lines -= minibuffer_lines;
436 wrap_page(&ministate.compl.buffer, COLS, 0);
439 mvwin(echoarea, --lines, 0);
440 wresize(echoarea, 1, COLS);
442 mvwin(modeline, --lines, 0);
443 wresize(modeline, 1, COLS);
445 if (side_window & SIDE_WINDOW_BOTTOM) {
446 download_lines = MIN(5, lines/2);
447 download_cols = COLS;
448 mvwin(download, lines - download_lines, 0);
449 wresize(download, download_lines, download_cols);
450 lines -= download_lines;
452 wrap_page(&downloadwin, download_cols, 0);
455 body_lines = show_tab_bar ? --lines : lines;
456 body_cols = COLS;
458 /*
459 * Here we make the assumption that show_tab_bar is either 0
460 * or 1, and reuse that as argument to mvwin.
461 */
462 if (side_window & SIDE_WINDOW_LEFT) {
463 help_cols = 0.3 * COLS;
464 help_lines = lines;
465 mvwin(help, show_tab_bar, 0);
466 wresize(help, help_lines, help_cols);
468 wrap_page(&helpwin, help_cols, 0);
470 body_cols = COLS - help_cols - 1;
471 mvwin(body, show_tab_bar, help_cols);
472 } else
473 mvwin(body, show_tab_bar, 0);
475 update_x_offset();
476 wresize(body, body_lines, body_cols);
478 if (show_tab_bar)
479 wresize(tabline, 1, COLS);
481 wrap_page(&current_tab->buffer, body_cols, x_offset);
482 redraw_tab(current_tab);
485 static void
486 line_prefix_and_text(int col, struct vline *vl, char *buf, size_t len,
487 const char **prfx_ret, const char **text_ret, int *text_len)
489 struct lineprefix *lp = line_prefixes;
490 int type, cont;
491 size_t i, width;
492 char *space, *t;
494 if (dont_apply_styling)
495 lp = raw_prefixes;
497 if (vl->len == 0) {
498 *text_ret = "";
499 *text_len = 0;
502 cont = vl->flags & L_CONTINUATION;
503 type = vl->parent->type;
504 if (!cont)
505 *prfx_ret = lp[type].prfx1;
506 else
507 *prfx_ret = lp[type].prfx2;
509 space = vl->parent->data;
510 *text_ret = vl->parent->line + vl->from;
511 *text_len = MIN(INT_MAX, vl->len);
512 if (!emojify_link || type != LINE_LINK || space == NULL) {
513 return;
516 if (cont) {
517 memset(buf, 0, len);
518 width = utf8_swidth_between(vl->parent->line, space, col);
519 for (i = 0; i < width + 1 && i < len - 1; ++i)
520 buf[i] = ' ';
521 } else {
522 strlcpy(buf, vl->parent->line, len);
523 if ((t = strchr(buf, ' ')) != NULL)
524 *t = '\0';
525 strlcat(buf, " ", len);
528 *prfx_ret = buf;
531 static inline void
532 print_vline_descr(int width, WINDOW *window, struct vline *vl)
534 int x, y, goal;
536 switch (vl->parent->type) {
537 case LINE_COMPL:
538 case LINE_COMPL_CURRENT:
539 goal = width/2;
540 break;
541 case LINE_DOWNLOAD:
542 case LINE_DOWNLOAD_DONE:
543 goal = width/4;
544 break;
545 case LINE_HELP:
546 goal = 8;
547 break;
548 default:
549 return;
552 if (vl->parent->alt == NULL)
553 return;
555 (void)y;
556 getyx(window, y, x);
558 if (goal <= x)
559 wprintw(window, " ");
560 for (; goal > x; ++x)
561 wprintw(window, " ");
563 wprintw(window, "%s", vl->parent->alt);
566 /*
567 * Core part of the rendering. It prints a vline starting from the
568 * current cursor position. Printing a vline consists of skipping
569 * `off' columns (for olivetti-mode), print the correct prefix (which
570 * may be the emoji in case of emojified links-lines), printing the
571 * text itself, filling until width - off and filling off columns
572 * again.
573 */
574 static void
575 print_vline(int off, int width, WINDOW *window, struct vline *vl)
577 /*
578 * Believe me or not, I've seen emoji ten code points long!
579 * That means, to stay large, 4*10 bytes + NUL.
580 */
581 char emojibuf[41] = {0};
582 const char *text, *prfx;
583 struct line_face *f;
584 int i, left, x, y, textlen;
586 f = &line_faces[vl->parent->type];
588 /* unused, set by getyx */
589 (void)y;
591 if (vl->parent->type == LINE_FRINGE && fringe_ignore_offset)
592 off = 0;
594 line_prefix_and_text(off, vl, emojibuf, sizeof(emojibuf), &prfx,
595 &text, &textlen);
597 wattr_on(window, body_face.left, NULL);
598 for (i = 0; i < off; i++)
599 waddch(window, ' ');
600 wattr_off(window, body_face.left, NULL);
602 wattr_on(window, f->prefix, NULL);
603 wprintw(window, "%s", prfx);
604 wattr_off(window, f->prefix, NULL);
606 wattr_on(window, f->text, NULL);
607 if (text)
608 wprintw(window, "%.*s", textlen, text);
609 print_vline_descr(width, window, vl);
610 wattr_off(window, f->text, NULL);
612 getyx(window, y, x);
614 left = width - x;
616 wattr_on(window, f->trail, NULL);
617 for (i = 0; i < left - off; ++i)
618 waddch(window, ' ');
619 wattr_off(window, f->trail, NULL);
621 wattr_on(window, body_face.right, NULL);
622 for (i = 0; i < off; i++)
623 waddch(window, ' ');
624 wattr_off(window, body_face.right, NULL);
627 static void
628 redraw_tabline(void)
630 struct tab *tab;
631 size_t toskip, ots, tabwidth, space, x;
632 int current, y, truncated, pair;
633 const char *title;
634 char buf[25];
636 x = 0;
638 /* unused, but set by a getyx */
639 (void)y;
641 tabwidth = sizeof(buf)+1;
642 space = COLS-2;
644 toskip = 0;
645 TAILQ_FOREACH(tab, &tabshead, tabs) {
646 toskip++;
647 if (tab == current_tab)
648 break;
651 if (toskip * tabwidth <= space)
652 toskip = 0;
653 else {
654 ots = toskip;
655 toskip--;
656 while (toskip != 0 &&
657 (ots - toskip+1) * tabwidth < space)
658 toskip--;
661 werase(tabline);
662 wattr_on(tabline, tab_face.background, NULL);
663 wprintw(tabline, toskip == 0 ? " " : "<");
664 wattr_off(tabline, tab_face.background, NULL);
666 truncated = 0;
667 TAILQ_FOREACH(tab, &tabshead, tabs) {
668 if (truncated)
669 break;
670 if (toskip != 0) {
671 toskip--;
672 continue;
675 getyx(tabline, y, x);
676 if (x + sizeof(buf)+2 >= (size_t)COLS)
677 truncated = 1;
679 current = tab == current_tab;
681 if (*(title = tab->buffer.title) == '\0')
682 title = hist_cur(tab->hist);
684 if (tab->flags & TAB_URGENT)
685 strlcpy(buf, "!", sizeof(buf));
686 else
687 strlcpy(buf, " ", sizeof(buf));
689 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
690 /* truncation happens */
691 strlcpy(&buf[sizeof(buf)-4], "...", 4);
692 } else {
693 /* pad with spaces */
694 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
695 /* nop */ ;
698 pair = current ? tab_face.current : tab_face.tab;
699 wattr_on(tabline, pair, NULL);
700 wprintw(tabline, "%s", buf);
701 wattr_off(tabline, pair, NULL);
703 wattr_on(tabline, tab_face.background, NULL);
704 if (TAILQ_NEXT(tab, tabs) != NULL)
705 wprintw(tabline, "┃");
706 wattr_off(tabline, tab_face.background, NULL);
709 wattr_on(tabline, tab_face.background, NULL);
710 for (; x < (size_t)COLS; ++x)
711 waddch(tabline, ' ');
712 if (truncated)
713 mvwprintw(tabline, 0, COLS-1, ">");
714 wattr_off(tabline, tab_face.background, NULL);
717 /*
718 * Compute the first visible line around vl. Try to search forward
719 * until the end of the buffer; if a visible line is not found, search
720 * backward. Return NULL if no viable line was found.
721 */
722 struct vline *
723 adjust_line(struct vline *vl, struct buffer *buffer)
725 struct vline *t;
727 if (vl == NULL)
728 return NULL;
730 if (!(vl->parent->flags & L_HIDDEN))
731 return vl;
733 /* search forward */
734 for (t = vl;
735 t != NULL && t->parent->flags & L_HIDDEN;
736 t = TAILQ_NEXT(t, vlines))
737 ; /* nop */
739 if (t != NULL)
740 return t;
742 /* search backward */
743 for (t = vl;
744 t != NULL && t->parent->flags & L_HIDDEN;
745 t = TAILQ_PREV(t, vhead, vlines))
746 ; /* nop */
748 return t;
751 static void
752 redraw_window(WINDOW *win, int off, int height, int width,
753 int show_fringe, struct buffer *buffer)
755 struct vline *vl;
756 int onscreen = 0, l = 0;
758 restore_curs_x(buffer);
760 /*
761 * TODO: ignoring buffer->force_update and always
762 * re-rendering. In theory we can recompute the y position
763 * without a re-render, and optimize here. It's not the only
764 * optimisation possible here, wscrl wolud also be an
765 * interesting one.
766 */
768 again:
769 werase(win);
770 buffer->curs_y = 0;
772 if (TAILQ_EMPTY(&buffer->head))
773 goto end;
775 if (buffer->top_line == NULL)
776 buffer->top_line = TAILQ_FIRST(&buffer->vhead);
778 buffer->top_line = adjust_line(buffer->top_line, buffer);
779 if (buffer->top_line == NULL)
780 goto end;
782 buffer->current_line = adjust_line(buffer->current_line, buffer);
784 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
785 if (vl->parent->flags & L_HIDDEN)
786 continue;
788 wmove(win, l, 0);
789 print_vline(off, width, win, vl);
791 if (vl == buffer->current_line)
792 onscreen = 1;
794 if (!onscreen)
795 buffer->curs_y++;
797 l++;
798 if (l == height)
799 break;
802 if (!onscreen) {
803 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
804 if (vl == buffer->current_line)
805 break;
806 if (vl->parent->flags & L_HIDDEN)
807 continue;
808 buffer->line_off++;
809 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
812 if (vl != NULL)
813 goto again;
816 buffer->last_line_off = buffer->line_off;
817 buffer->force_redraw = 0;
818 end:
819 for (; show_fringe && l < height; l++)
820 print_vline(off, width, win, &fringe);
822 wmove(win, buffer->curs_y, buffer->curs_x);
825 static void
826 redraw_download(void)
828 redraw_window(download, 0, download_lines, COLS, 0, &downloadwin);
831 static void
832 redraw_help(void)
834 redraw_window(help, 0, help_lines, help_cols, 1, &helpwin);
837 static void
838 redraw_body(struct tab *tab)
840 static struct tab *last_tab;
842 if (last_tab != tab)
843 tab->buffer.force_redraw =1;
844 last_tab = tab;
846 redraw_window(body, x_offset, body_lines, body_cols, 1, &tab->buffer);
849 static inline char
850 trust_status_char(enum trust_state ts)
852 switch (ts) {
853 case TS_UNKNOWN: return '-';
854 case TS_UNTRUSTED: return '!';
855 case TS_TEMP_TRUSTED: return '!';
856 case TS_TRUSTED: return 'v';
857 case TS_VERIFIED: return 'V';
858 default: return 'X';
862 static void
863 redraw_modeline(struct tab *tab)
865 struct buffer *buffer;
866 double pct;
867 int x, y, max_x, max_y;
868 const char *mode;
869 const char *spin = "-\\|/";
871 buffer = current_buffer();
872 mode = buffer->mode;
874 werase(modeline);
875 wattr_on(modeline, modeline_face.background, NULL);
876 wmove(modeline, 0, 0);
878 wprintw(modeline, "-%c%c%c%c %s ",
879 spin[tab->loading_anim_step],
880 trust_status_char(tab->trust),
881 tab->client_cert ? 'C' : '-',
882 tab->faulty_gemserver ? 'W' : '-',
883 mode == NULL ? "(none)" : mode);
885 pct = (buffer->line_off + buffer->curs_y) * 100.0
886 / buffer->line_max;
888 if (buffer->line_max <= (size_t)body_lines)
889 wprintw(modeline, "All ");
890 else if (buffer->line_off == 0)
891 wprintw(modeline, "Top ");
892 else if (buffer->line_off + body_lines >= buffer->line_max)
893 wprintw(modeline, "Bottom ");
894 else
895 wprintw(modeline, "%.0f%% ", pct);
897 wprintw(modeline, "%zu/%zu %s ",
898 buffer->line_off + buffer->curs_y,
899 buffer->line_max,
900 hist_cur(tab->hist));
902 getyx(modeline, y, x);
903 getmaxyx(modeline, max_y, max_x);
905 (void)y;
906 (void)max_y;
908 for (; x < max_x; ++x)
909 waddstr(modeline, "-");
911 wattr_off(modeline, modeline_face.background, NULL);
914 static void
915 redraw_minibuffer(void)
917 wattr_on(echoarea, minibuffer_face.background, NULL);
918 werase(echoarea);
920 if (in_minibuffer)
921 do_redraw_minibuffer();
922 else
923 do_redraw_echoarea();
925 if (in_minibuffer == MB_COMPREAD)
926 do_redraw_minibuffer_compl();
928 wattr_off(echoarea, minibuffer_face.background, NULL);
931 static void
932 do_redraw_echoarea(void)
934 struct vline *vl;
936 if (ministate.curmesg != NULL)
937 wprintw(echoarea, "%s", ministate.curmesg);
938 else if (*keybuf != '\0')
939 waddstr(echoarea, keybuf);
940 else {
941 /* If nothing else, show the URL at point */
942 vl = current_tab->buffer.current_line;
943 if (vl != NULL && vl->parent->type == LINE_LINK)
944 waddstr(echoarea, vl->parent->alt);
948 static void
949 do_redraw_minibuffer(void)
951 struct buffer *cmplbuf, *buffer;
952 size_t off_y, off_x = 0;
953 const char *start, *c;
954 char *line;
956 cmplbuf = &ministate.compl.buffer;
957 buffer = &ministate.buffer;
958 (void)off_y; /* unused, set by getyx */
960 wmove(echoarea, 0, 0);
962 if (in_minibuffer == MB_COMPREAD)
963 wprintw(echoarea, "(%2zu) ",
964 cmplbuf->line_max);
966 wprintw(echoarea, "%s", ministate.prompt);
967 if (!ministate.editing)
968 wprintw(echoarea, "(%zu/%zu) ",
969 hist_off(ministate.hist) + 1,
970 hist_size(ministate.hist));
972 getyx(echoarea, off_y, off_x);
974 start = ministate.buf;
975 if (!ministate.editing)
976 start = hist_cur(ministate.hist);
977 line = buffer->current_line->parent->line + buffer->current_line->from;
978 c = line + buffer->point_offset;
979 while (start < c && utf8_swidth_between(start, c, off_x) > (size_t)COLS/2) {
980 start += grapheme_next_character_break_utf8(start, SIZE_MAX);
983 waddstr(echoarea, start);
985 if (ministate.curmesg != NULL)
986 wprintw(echoarea, " [%s]", ministate.curmesg);
988 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c, off_x));
991 static void
992 do_redraw_minibuffer_compl(void)
994 redraw_window(minibuffer, 0, 10, COLS, 1,
995 &ministate.compl.buffer);
998 /*
999 * Place the cursor in the right ncurses window. If soft is 1, use
1000 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
1001 * wrefresh.
1003 static void
1004 place_cursor(int soft)
1006 int (*touch)(WINDOW *);
1008 if (soft)
1009 touch = wnoutrefresh;
1010 else
1011 touch = wrefresh;
1013 if (in_minibuffer) {
1014 if (side_window & SIDE_WINDOW_LEFT)
1015 touch(help);
1016 if (side_window & SIDE_WINDOW_BOTTOM)
1017 touch(download);
1018 touch(body);
1019 touch(echoarea);
1020 } else if (in_side_window & SIDE_WINDOW_LEFT) {
1021 touch(body);
1022 touch(echoarea);
1023 if (in_side_window & SIDE_WINDOW_BOTTOM)
1024 touch(download);
1025 touch(help);
1026 } else if (in_side_window & SIDE_WINDOW_BOTTOM) {
1027 touch(body);
1028 touch(echoarea);
1029 if (in_side_window & SIDE_WINDOW_LEFT)
1030 touch(help);
1031 touch(download);
1032 } else {
1033 if (side_window & SIDE_WINDOW_LEFT)
1034 touch(help);
1035 if (side_window & SIDE_WINDOW_BOTTOM)
1036 touch(download);
1037 touch(echoarea);
1038 touch(body);
1042 static void
1043 redraw_tab(struct tab *tab)
1045 if (too_small)
1046 return;
1048 if (side_window & SIDE_WINDOW_LEFT) {
1049 redraw_help();
1050 wnoutrefresh(help);
1053 if (side_window & SIDE_WINDOW_BOTTOM) {
1054 redraw_download();
1055 wnoutrefresh(download);
1058 if (show_tab_bar)
1059 redraw_tabline();
1061 redraw_body(tab);
1062 redraw_modeline(tab);
1063 redraw_minibuffer();
1065 wnoutrefresh(tabline);
1066 wnoutrefresh(modeline);
1068 if (in_minibuffer == MB_COMPREAD)
1069 wnoutrefresh(minibuffer);
1071 place_cursor(1);
1073 doupdate();
1075 if (set_title)
1076 dprintf(1, "\033]2;%s - Telescope\a",
1077 current_tab->buffer.title);
1080 void
1081 start_loading_anim(struct tab *tab)
1083 if (tab->loading_anim)
1084 return;
1085 tab->loading_anim = 1;
1087 ev_timer_cancel(tab->loading_timer);
1088 tab->loading_timer = ev_timer(&loading_tv, update_loading_anim, tab);
1091 static void
1092 update_loading_anim(int fd, int ev, void *d)
1094 struct tab *tab = d;
1096 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1098 if (tab == current_tab) {
1099 redraw_modeline(tab);
1100 wrefresh(modeline);
1101 wrefresh(body);
1102 if (in_minibuffer)
1103 wrefresh(echoarea);
1106 tab->loading_timer = ev_timer(&loading_tv, update_loading_anim, tab);
1109 static void
1110 stop_loading_anim(struct tab *tab)
1112 if (!tab->loading_anim)
1113 return;
1115 ev_timer_cancel(tab->loading_timer);
1116 tab->loading_anim = 0;
1117 tab->loading_anim_step = 0;
1119 if (tab != current_tab)
1120 return;
1122 redraw_modeline(tab);
1124 wrefresh(modeline);
1125 wrefresh(body);
1126 if (in_minibuffer)
1127 wrefresh(echoarea);
1130 int
1131 ui_init(void)
1133 setlocale(LC_ALL, "");
1135 if (TAILQ_EMPTY(&global_map.m)) {
1136 fprintf(stderr, "no keys defined!\n");
1137 return 0;
1140 minibuffer_init();
1142 /* initialize download window */
1143 TAILQ_INIT(&downloadwin.head);
1144 TAILQ_INIT(&downloadwin.vhead);
1146 /* initialize help window */
1147 TAILQ_INIT(&helpwin.head);
1148 TAILQ_INIT(&helpwin.vhead);
1150 base_map = &global_map;
1151 current_map = &global_map;
1153 initscr();
1155 if (enable_colors) {
1156 if (has_colors()) {
1157 start_color();
1158 use_default_colors();
1159 } else
1160 enable_colors = 0;
1163 config_apply_style();
1165 raw();
1166 noecho();
1167 nonl();
1168 intrflush(stdscr, FALSE);
1170 if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1171 return 0;
1172 if ((body = newwin(1, 1, 0, 0)) == NULL)
1173 return 0;
1174 if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1175 return 0;
1176 if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1177 return 0;
1178 if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1179 return 0;
1180 if ((download = newwin(1, 1, 0, 0)) == NULL)
1181 return 0;
1182 if ((help = newwin(1, 1, 0, 0)) == NULL)
1183 return 0;
1185 wbkgd(body, body_face.body);
1186 wbkgd(download, download_face.background);
1187 wbkgd(echoarea, minibuffer_face.background);
1189 update_x_offset();
1191 keypad(body, TRUE);
1192 scrollok(body, FALSE);
1194 /* non-blocking input */
1195 wtimeout(body, 0);
1196 wtimeout(help, 0);
1198 wmove(body, 0, 0);
1200 return 1;
1203 void
1204 ui_main_loop(void)
1206 if (ev_signal(SIGWINCH, handle_signal, NULL) == -1 ||
1207 ev_signal(SIGCHLD, handle_signal, NULL) == -1 ||
1208 ev_add(0, EV_READ, dispatch_stdio, NULL) == -1)
1209 err(1, "ev_signal or ev_add failed");
1211 switch_to_tab(current_tab);
1212 rearrange_windows();
1214 ev_loop();
1217 void
1218 ui_on_tab_loaded(struct tab *tab)
1220 size_t line_off, curr_off;
1222 stop_loading_anim(tab);
1223 message("Loaded %s", hist_cur(tab->hist));
1225 hist_cur_offs(tab->hist, &line_off, &curr_off);
1226 if (curr_off != 0 &&
1227 tab->buffer.current_line == TAILQ_FIRST(&tab->buffer.vhead)) {
1228 set_scroll_position(tab, line_off, curr_off);
1229 redraw_tab(tab);
1230 return;
1233 if (show_tab_bar)
1234 redraw_tabline();
1236 wrefresh(tabline);
1237 place_cursor(0);
1240 void
1241 ui_on_tab_refresh(struct tab *tab)
1243 wrap_page(&tab->buffer, body_cols, x_offset);
1244 if (tab == current_tab)
1245 redraw_tab(tab);
1246 else
1247 tab->flags |= TAB_URGENT;
1250 void
1251 ui_force_tab_refresh(struct tab *tab)
1253 switch_to_tab(tab);
1254 rearrange_windows();
1255 redraw_tab(tab);
1258 void
1259 ui_on_download_refresh(void)
1261 if (ev_timer_pending(download_timer))
1262 return;
1264 download_timer = ev_timer(&download_refresh_timer,
1265 handle_download_refresh, NULL);
1268 static void
1269 open_download(int res, void *data)
1271 struct download *d = data;
1272 struct mailcap *mc = NULL;
1273 enum exec_mode mode = EXEC_BACKGROUND;
1275 if (!res)
1276 return;
1278 if ((mc = mailcap_cmd_from_mimetype(d->mime_type, d->path)) == NULL)
1279 return;
1281 if (mc->flags & MAILCAP_NEEDSTERMINAL)
1282 mode = EXEC_FOREGROUND;
1284 message("Loaded %s with %s", d->mime_type, mc->cmd_argv[0]);
1285 exec_cmd(mc->cmd_argv, mode);
1288 void
1289 ui_prompt_download_cmd(struct download *d)
1291 char prompt[64];
1293 snprintf(prompt, sizeof(prompt), "Open %s?", d->path);
1294 ui_yornp(prompt, open_download, d);
1297 void
1298 ui_remotely_open_link(const char *uri)
1300 new_tab(uri, NULL, NULL);
1301 ui_on_tab_refresh(current_tab);
1303 /* ring the bell */
1304 printf("\a");
1305 fflush(stdout);
1308 const char *
1309 ui_keyname(int k)
1311 return keyname(k);
1314 void
1315 ui_toggle_side_window(int kind)
1317 if (in_side_window & kind)
1318 ui_other_window();
1320 side_window ^= kind;
1321 if (side_window & SIDE_WINDOW_LEFT)
1322 recompute_help();
1323 if (side_window & SIDE_WINDOW_BOTTOM)
1324 recompute_downloads();
1327 * ugly hack, but otherwise the window doesn't get updated
1328 * until I call rearrange_windows a second time (e.g. via
1329 * C-l). I will be happy to know why something like this is
1330 * needed.
1332 rearrange_windows();
1333 rearrange_windows();
1336 void
1337 ui_show_downloads_pane(void)
1339 if (!(side_window & SIDE_WINDOW_BOTTOM))
1340 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1343 void
1344 ui_schedule_redraw(void)
1346 should_rearrange_windows = 1;
1349 void
1350 ui_require_input(struct tab *tab, int hide, void (*fn)(const char *))
1352 struct minibuffer m = {
1353 .self_insert = sensible_self_insert,
1354 .done = fn,
1355 .history = ir_history,
1358 /* TODO: hard-switching to another tab is ugly */
1359 switch_to_tab(tab);
1361 enter_minibuffer(&m, "Input required: ");
1362 redraw_tab(tab);
1365 void
1366 ui_after_message_hook(void)
1368 redraw_minibuffer();
1369 place_cursor(0);
1372 void
1373 ui_yornp(const char *prompt, void (*fn)(int, void *), void *data)
1375 yornp(prompt, fn, data);
1376 redraw_tab(current_tab);
1379 void
1380 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1381 struct tab *data, const char *input)
1383 minibuffer_read(prompt, fn, data, input);
1384 redraw_tab(current_tab);
1387 void
1388 ui_other_window(void)
1390 if (in_side_window & SIDE_WINDOW_LEFT &&
1391 side_window & SIDE_WINDOW_BOTTOM)
1392 in_side_window = SIDE_WINDOW_BOTTOM;
1393 else if (in_side_window)
1394 in_side_window = 0;
1395 else if (!in_side_window && side_window & SIDE_WINDOW_LEFT)
1396 in_side_window = SIDE_WINDOW_LEFT;
1397 else if (!in_side_window && side_window)
1398 in_side_window = SIDE_WINDOW_BOTTOM;
1399 else
1400 message("No other window to select");
1403 void
1404 ui_suspend(void)
1406 endwin();
1409 void
1410 ui_resume(void)
1412 refresh();
1413 clear();
1414 rearrange_windows();
1417 void
1418 ui_end(void)
1420 endwin();