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>
37 #include <assert.h>
38 #include <curses.h>
39 #include <locale.h>
40 #include <signal.h>
41 #include <stdarg.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
46 #include "cmd.h"
47 #include "defaults.h"
48 #include "ev.h"
49 #include "hist.h"
50 #include "keymap.h"
51 #include "minibuffer.h"
52 #include "session.h"
53 #include "telescope.h"
54 #include "ui.h"
55 #include "utf8.h"
57 static void set_scroll_position(struct tab *, size_t, size_t);
59 static void restore_curs_x(struct buffer *);
61 static int readkey(void);
62 static void dispatch_stdio(int, int, void*);
63 static void handle_resize(int, int, void*);
64 static void handle_resize_nodelay(int, int, void*);
65 static void handle_download_refresh(int, int, void *);
66 static void rearrange_windows(void);
67 static void line_prefix_and_text(struct vline *, char *, size_t, const char **, const char **, int *);
68 static void print_vline(int, int, WINDOW*, struct vline*);
69 static void redraw_tabline(void);
70 static void redraw_window(WINDOW *, int, int, int, int, struct buffer *);
71 static void redraw_download(void);
72 static void redraw_help(void);
73 static void redraw_body(struct tab*);
74 static void redraw_modeline(struct tab*);
75 static void redraw_minibuffer(void);
76 static void do_redraw_echoarea(void);
77 static void do_redraw_minibuffer(void);
78 static void do_redraw_minibuffer_compl(void);
79 static void place_cursor(int);
80 static void redraw_tab(struct tab*);
81 static void update_loading_anim(int, int, void*);
82 static void stop_loading_anim(struct tab*);
84 static int should_rearrange_windows;
85 static int show_tab_bar;
86 static int too_small;
87 static int x_offset;
89 struct thiskey thiskey;
90 struct tab *current_tab;
92 static unsigned int resize_timer;
93 static struct timeval resize_tv = { 0, 250000 };
95 static unsigned int download_timer;
96 static struct timeval download_refresh_timer = { 0, 250000 };
98 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
100 int body_lines, body_cols;
102 static WINDOW *help;
103 /* not static so we can see them from help.c */
104 struct buffer helpwin;
105 int help_lines, help_cols;
107 static WINDOW *download;
108 /* not static so we can see them from download.c */
109 struct buffer downloadwin;
110 int download_lines;
111 int download_cols;
113 static int side_window;
114 static int in_side_window;
116 static struct timeval loading_tv = { 0, 250000 };
118 static char keybuf[64];
120 /* XXX: don't forget to init these in main() */
121 struct kmap global_map,
122 minibuffer_map,
123 *current_map,
124 *base_map;
126 static inline void
127 update_x_offset(void)
129 if (olivetti_mode && fill_column < body_cols)
130 x_offset = (body_cols - fill_column)/2;
131 else
132 x_offset = 0;
135 static void
136 set_scroll_position(struct tab *tab, size_t top, size_t cur)
138 struct line *last;
139 struct vline *vl;
140 size_t i = 0;
141 int topfound = 0;
143 last = TAILQ_FIRST(&tab->buffer.page.head);
144 TAILQ_FOREACH(vl, &tab->buffer.head, vlines) {
145 if (last != vl->parent) {
146 last = vl->parent;
147 i++;
150 if (!topfound && i == top) {
151 topfound = 1;
152 tab->buffer.top_line = vl;
155 if (i == cur) {
156 tab->buffer.current_line = vl;
157 return;
161 if (!topfound)
162 tab->buffer.top_line = TAILQ_FIRST(&tab->buffer.head);
164 tab->buffer.current_line = tab->buffer.top_line;
167 void
168 get_scroll_position(struct tab *tab, size_t *top, size_t *cur)
170 struct line *l;
171 int topfound = 0;
173 *top = 0;
174 *cur = 0;
176 if (tab->buffer.top_line == NULL ||
177 tab->buffer.current_line == NULL)
178 return;
180 TAILQ_FOREACH(l, &tab->buffer.page.head, lines) {
181 if (tab->buffer.top_line->parent == l)
182 topfound = 1;
183 if (tab->buffer.current_line->parent == l)
184 return;
186 if (!topfound)
187 (*top)++;
188 (*cur)++;
192 void
193 save_excursion(struct excursion *place, struct buffer *buffer)
195 place->curs_x = buffer->curs_x;
196 place->curs_y = buffer->curs_y;
197 place->line_off = buffer->line_off;
198 place->top_line = buffer->top_line;
199 place->current_line = buffer->current_line;
200 place->cpoff = buffer->cpoff;
203 void
204 restore_excursion(struct excursion *place, struct buffer *buffer)
206 buffer->curs_x = place->curs_x;
207 buffer->curs_y = place->curs_y;
208 buffer->line_off = place->line_off;
209 buffer->top_line = place->top_line;
210 buffer->current_line = place->current_line;
211 buffer->cpoff = place->cpoff;
214 static void
215 restore_curs_x(struct buffer *buffer)
217 struct vline *vl;
218 const char *prfx, *text;
220 vl = buffer->current_line;
221 if (vl == NULL || vl->len == 0)
222 buffer->curs_x = buffer->cpoff = 0;
223 else if (vl->parent->data != NULL) {
224 text = vl->parent->data;
225 buffer->curs_x = utf8_snwidth(text + 1, buffer->cpoff) + 1;
226 } else {
227 text = vl->parent->line + vl->from;
228 buffer->curs_x = utf8_snwidth(text, buffer->cpoff);
231 /* small hack: don't olivetti-mode the download pane */
232 if (buffer != &downloadwin)
233 buffer->curs_x += x_offset;
235 if (vl == NULL)
236 return;
238 if (vl->parent->data != NULL)
239 buffer->curs_x += utf8_swidth_between(vl->parent->line,
240 vl->parent->data);
241 else {
242 prfx = line_prefixes[vl->parent->type].prfx1;
243 buffer->curs_x += utf8_swidth(prfx);
247 void
248 global_key_unbound(void)
250 message("%s is undefined", keybuf);
253 struct buffer *
254 current_buffer(void)
256 if (in_minibuffer)
257 return &ministate.buffer;
258 if (in_side_window & SIDE_WINDOW_LEFT)
259 return &helpwin;
260 if (in_side_window & SIDE_WINDOW_BOTTOM)
261 return &downloadwin;
262 return &current_tab->buffer;
265 static int
266 readkey(void)
268 uint32_t state = 0;
270 if ((thiskey.key = wgetch(body)) == ERR)
271 return 0;
273 thiskey.meta = thiskey.key == '\e';
274 if (thiskey.meta) {
275 thiskey.key = wgetch(body);
276 if (thiskey.key == ERR || thiskey.key == '\e') {
277 thiskey.meta = 0;
278 thiskey.key = '\e';
282 thiskey.cp = 0;
284 if ((unsigned int)thiskey.key >= UINT8_MAX)
285 return 1;
287 while (1) {
288 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
289 break;
290 if ((thiskey.key = wgetch(body)) == ERR) {
291 message("Error decoding user input");
292 return 0;
296 return 1;
299 static void
300 dispatch_stdio(int fd, int ev, void *d)
302 int lk;
303 const char *keyname;
304 char tmp[5] = {0};
306 /* TODO: schedule a redraw? */
307 if (too_small)
308 return;
310 if (!readkey())
311 return;
313 if (keybuf[0] != '\0')
314 strlcat(keybuf, " ", sizeof(keybuf));
315 if (thiskey.meta)
316 strlcat(keybuf, "M-", sizeof(keybuf));
317 if (thiskey.cp != 0) {
318 utf8_encode(thiskey.cp, tmp);
319 strlcat(keybuf, tmp, sizeof(keybuf));
320 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
321 strlcat(keybuf, keyname, sizeof(keybuf));
322 } else {
323 tmp[0] = thiskey.key;
324 strlcat(keybuf, tmp, sizeof(keybuf));
327 lk = lookup_key(&current_map, &thiskey, current_buffer());
328 if (lk == LK_UNBOUND) {
329 if (current_map->unhandled_input != NULL)
330 current_map->unhandled_input();
331 else
332 global_key_unbound();
334 if (lk != LK_ADVANCED_MAP) {
335 current_map = base_map;
336 strlcpy(keybuf, "", sizeof(keybuf));
339 if (side_window & SIDE_WINDOW_LEFT)
340 recompute_help();
342 if (should_rearrange_windows)
343 rearrange_windows();
344 redraw_tab(current_tab);
347 static void
348 handle_resize(int sig, int ev, void *d)
350 ev_timer_cancel(resize_timer);
351 resize_timer = ev_timer(&resize_tv, handle_resize_nodelay, NULL);
354 static void
355 handle_resize_nodelay(int s, int ev, void *d)
357 endwin();
358 refresh();
359 clear();
361 rearrange_windows();
364 static void
365 handle_download_refresh(int s, int v, void *d)
367 if (side_window & SIDE_WINDOW_BOTTOM) {
368 recompute_downloads();
369 redraw_tab(current_tab);
373 static inline int
374 should_show_tab_bar(void)
376 if (tab_bar_show == -1)
377 return 0;
378 if (tab_bar_show == 0)
379 return 1;
381 return TAILQ_NEXT(TAILQ_FIRST(&tabshead), tabs) != NULL;
384 static void
385 rearrange_windows(void)
387 int lines;
388 int minibuffer_lines;
390 should_rearrange_windows = 0;
391 show_tab_bar = should_show_tab_bar();
393 lines = LINES;
395 /* 3 lines for the ui and 12 for the body and minibuffer */
396 if ((too_small = lines < 15)) {
397 erase();
398 printw("Window too small.");
399 refresh();
400 return;
403 /* move and resize the windows, in reverse order! */
405 if (in_minibuffer == MB_COMPREAD) {
406 minibuffer_lines = MIN(10, lines/2);
407 mvwin(minibuffer, lines - minibuffer_lines, 0);
408 wresize(minibuffer, minibuffer_lines, COLS);
409 lines -= minibuffer_lines;
411 wrap_page(&ministate.compl.buffer, COLS);
414 mvwin(echoarea, --lines, 0);
415 wresize(echoarea, 1, COLS);
417 mvwin(modeline, --lines, 0);
418 wresize(modeline, 1, COLS);
420 if (side_window & SIDE_WINDOW_BOTTOM) {
421 download_lines = MIN(5, lines/2);
422 download_cols = COLS;
423 mvwin(download, lines - download_lines, 0);
424 wresize(download, download_lines, download_cols);
425 lines -= download_lines;
427 wrap_page(&downloadwin, download_cols);
430 body_lines = show_tab_bar ? --lines : lines;
431 body_cols = COLS;
433 /*
434 * Here we make the assumption that show_tab_bar is either 0
435 * or 1, and reuse that as argument to mvwin.
436 */
437 if (side_window & SIDE_WINDOW_LEFT) {
438 help_cols = 0.3 * COLS;
439 help_lines = lines;
440 mvwin(help, show_tab_bar, 0);
441 wresize(help, help_lines, help_cols);
443 wrap_page(&helpwin, help_cols);
445 body_cols = COLS - help_cols - 1;
446 mvwin(body, show_tab_bar, help_cols);
447 } else
448 mvwin(body, show_tab_bar, 0);
450 update_x_offset();
451 wresize(body, body_lines, body_cols);
453 if (show_tab_bar)
454 wresize(tabline, 1, COLS);
456 wrap_page(&current_tab->buffer, body_cols);
457 redraw_tab(current_tab);
460 static void
461 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
462 const char **prfx_ret, const char **text_ret, int *text_len)
464 int type, cont;
465 size_t i, width;
466 char *space, *t;
468 if (vl->len == 0) {
469 *text_ret = "";
470 *text_len = 0;
473 cont = vl->flags & L_CONTINUATION;
474 type = vl->parent->type;
475 if (!cont)
476 *prfx_ret = line_prefixes[type].prfx1;
477 else
478 *prfx_ret = line_prefixes[type].prfx2;
480 space = vl->parent->data;
481 *text_ret = vl->parent->line + vl->from;
482 *text_len = MIN(INT_MAX, vl->len);
483 if (!emojify_link || type != LINE_LINK || space == NULL) {
484 return;
487 if (cont) {
488 memset(buf, 0, len);
489 width = utf8_swidth_between(vl->parent->line, space);
490 for (i = 0; i < width + 1 && i < len - 1; ++i)
491 buf[i] = ' ';
492 } else {
493 strlcpy(buf, vl->parent->line, len);
494 if ((t = strchr(buf, ' ')) != NULL)
495 *t = '\0';
496 strlcat(buf, " ", len);
499 *prfx_ret = buf;
502 static inline void
503 print_vline_descr(int width, WINDOW *window, struct vline *vl)
505 int x, y, goal;
507 switch (vl->parent->type) {
508 case LINE_COMPL:
509 case LINE_COMPL_CURRENT:
510 goal = width/2;
511 break;
512 case LINE_DOWNLOAD:
513 case LINE_DOWNLOAD_DONE:
514 goal = width/4;
515 break;
516 case LINE_HELP:
517 goal = 8;
518 break;
519 default:
520 return;
523 if (vl->parent->alt == NULL)
524 return;
526 (void)y;
527 getyx(window, y, x);
529 if (goal <= x)
530 wprintw(window, " ");
531 for (; goal > x; ++x)
532 wprintw(window, " ");
534 wprintw(window, "%s", vl->parent->alt);
537 /*
538 * Core part of the rendering. It prints a vline starting from the
539 * current cursor position. Printing a vline consists of skipping
540 * `off' columns (for olivetti-mode), print the correct prefix (which
541 * may be the emoji in case of emojified links-lines), printing the
542 * text itself, filling until width - off and filling off columns
543 * again.
544 */
545 static void
546 print_vline(int off, int width, WINDOW *window, struct vline *vl)
548 /*
549 * Believe me or not, I've seen emoji ten code points long!
550 * That means, to stay large, 4*10 bytes + NUL.
551 */
552 char emojibuf[41] = {0};
553 const char *text, *prfx;
554 struct line_face *f;
555 int i, left, x, y, textlen;
557 f = &line_faces[vl->parent->type];
559 /* unused, set by getyx */
560 (void)y;
562 if (vl->parent->type == LINE_FRINGE && fringe_ignore_offset)
563 off = 0;
565 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx,
566 &text, &textlen);
568 wattr_on(window, body_face.left, NULL);
569 for (i = 0; i < off; i++)
570 waddch(window, ' ');
571 wattr_off(window, body_face.left, NULL);
573 wattr_on(window, f->prefix, NULL);
574 wprintw(window, "%s", prfx);
575 wattr_off(window, f->prefix, NULL);
577 wattr_on(window, f->text, NULL);
578 if (text)
579 wprintw(window, "%.*s", textlen, text);
580 print_vline_descr(width, window, vl);
581 wattr_off(window, f->text, NULL);
583 getyx(window, y, x);
585 left = width - x;
587 wattr_on(window, f->trail, NULL);
588 for (i = 0; i < left - off; ++i)
589 waddch(window, ' ');
590 wattr_off(window, f->trail, NULL);
592 wattr_on(window, body_face.right, NULL);
593 for (i = 0; i < off; i++)
594 waddch(window, ' ');
595 wattr_off(window, body_face.right, NULL);
598 static void
599 redraw_tabline(void)
601 struct tab *tab;
602 size_t toskip, ots, tabwidth, space, x;
603 int current, y, truncated, pair;
604 const char *title;
605 char buf[25];
607 x = 0;
609 /* unused, but setted by a getyx */
610 (void)y;
612 tabwidth = sizeof(buf)+1;
613 space = COLS-2;
615 toskip = 0;
616 TAILQ_FOREACH(tab, &tabshead, tabs) {
617 toskip++;
618 if (tab == current_tab)
619 break;
622 if (toskip * tabwidth <= space)
623 toskip = 0;
624 else {
625 ots = toskip;
626 toskip--;
627 while (toskip != 0 &&
628 (ots - toskip+1) * tabwidth < space)
629 toskip--;
632 werase(tabline);
633 wattr_on(tabline, tab_face.background, NULL);
634 wprintw(tabline, toskip == 0 ? " " : "<");
635 wattr_off(tabline, tab_face.background, NULL);
637 truncated = 0;
638 TAILQ_FOREACH(tab, &tabshead, tabs) {
639 if (truncated)
640 break;
641 if (toskip != 0) {
642 toskip--;
643 continue;
646 getyx(tabline, y, x);
647 if (x + sizeof(buf)+2 >= (size_t)COLS)
648 truncated = 1;
650 current = tab == current_tab;
652 if (*(title = tab->buffer.page.title) == '\0')
653 title = hist_cur(tab->hist);
655 if (tab->flags & TAB_URGENT)
656 strlcpy(buf, "!", sizeof(buf));
657 else
658 strlcpy(buf, " ", sizeof(buf));
660 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
661 /* truncation happens */
662 strlcpy(&buf[sizeof(buf)-4], "...", 4);
663 } else {
664 /* pad with spaces */
665 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
666 /* nop */ ;
669 pair = current ? tab_face.current : tab_face.tab;
670 wattr_on(tabline, pair, NULL);
671 wprintw(tabline, "%s", buf);
672 wattr_off(tabline, pair, NULL);
674 wattr_on(tabline, tab_face.background, NULL);
675 if (TAILQ_NEXT(tab, tabs) != NULL)
676 wprintw(tabline, "┃");
677 wattr_off(tabline, tab_face.background, NULL);
680 wattr_on(tabline, tab_face.background, NULL);
681 for (; x < (size_t)COLS; ++x)
682 waddch(tabline, ' ');
683 if (truncated)
684 mvwprintw(tabline, 0, COLS-1, ">");
685 wattr_off(tabline, tab_face.background, NULL);
688 /*
689 * Compute the first visible line around vl. Try to search forward
690 * until the end of the buffer; if a visible line is not found, search
691 * backward. Return NULL if no viable line was found.
692 */
693 struct vline *
694 adjust_line(struct vline *vl, struct buffer *buffer)
696 struct vline *t;
698 if (vl == NULL)
699 return NULL;
701 if (!(vl->parent->flags & L_HIDDEN))
702 return vl;
704 /* search forward */
705 for (t = vl;
706 t != NULL && t->parent->flags & L_HIDDEN;
707 t = TAILQ_NEXT(t, vlines))
708 ; /* nop */
710 if (t != NULL)
711 return t;
713 /* search backward */
714 for (t = vl;
715 t != NULL && t->parent->flags & L_HIDDEN;
716 t = TAILQ_PREV(t, vhead, vlines))
717 ; /* nop */
719 return t;
722 static void
723 redraw_window(WINDOW *win, int off, int height, int width,
724 int show_fringe, struct buffer *buffer)
726 struct vline *vl;
727 int onscreen = 0, l = 0;
729 restore_curs_x(buffer);
731 /*
732 * TODO: ignoring buffer->force_update and always
733 * re-rendering. In theory we can recompute the y position
734 * without a re-render, and optimize here. It's not the only
735 * optimisation possible here, wscrl wolud also be an
736 * interesting one.
737 */
739 again:
740 werase(win);
741 buffer->curs_y = 0;
743 if (TAILQ_EMPTY(&buffer->head))
744 goto end;
746 if (buffer->top_line == NULL)
747 buffer->top_line = TAILQ_FIRST(&buffer->head);
749 buffer->top_line = adjust_line(buffer->top_line, buffer);
750 if (buffer->top_line == NULL)
751 goto end;
753 buffer->current_line = adjust_line(buffer->current_line, buffer);
755 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
756 if (vl->parent->flags & L_HIDDEN)
757 continue;
759 wmove(win, l, 0);
760 print_vline(off, width, win, vl);
762 if (vl == buffer->current_line)
763 onscreen = 1;
765 if (!onscreen)
766 buffer->curs_y++;
768 l++;
769 if (l == height)
770 break;
773 if (!onscreen) {
774 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
775 if (vl == buffer->current_line)
776 break;
777 if (vl->parent->flags & L_HIDDEN)
778 continue;
779 buffer->line_off++;
780 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
783 if (vl != NULL)
784 goto again;
787 buffer->last_line_off = buffer->line_off;
788 buffer->force_redraw = 0;
789 end:
790 for (; show_fringe && l < height; l++)
791 print_vline(off, width, win, &fringe);
793 wmove(win, buffer->curs_y, buffer->curs_x);
796 static void
797 redraw_download(void)
799 redraw_window(download, 0, download_lines, COLS, 0, &downloadwin);
802 static void
803 redraw_help(void)
805 redraw_window(help, 0, help_lines, help_cols, 1, &helpwin);
808 static void
809 redraw_body(struct tab *tab)
811 static struct tab *last_tab;
813 if (last_tab != tab)
814 tab->buffer.force_redraw =1;
815 last_tab = tab;
817 redraw_window(body, x_offset, body_lines, body_cols, 1, &tab->buffer);
820 static inline char
821 trust_status_char(enum trust_state ts)
823 switch (ts) {
824 case TS_UNKNOWN: return '-';
825 case TS_UNTRUSTED: return '!';
826 case TS_TEMP_TRUSTED: return '!';
827 case TS_TRUSTED: return 'v';
828 case TS_VERIFIED: return 'V';
829 default: return 'X';
833 static void
834 redraw_modeline(struct tab *tab)
836 struct buffer *buffer;
837 double pct;
838 int x, y, max_x, max_y;
839 const char *mode;
840 const char *spin = "-\\|/";
842 buffer = current_buffer();
843 mode = buffer->page.name;
845 werase(modeline);
846 wattr_on(modeline, modeline_face.background, NULL);
847 wmove(modeline, 0, 0);
849 wprintw(modeline, "-%c%c%c %s ",
850 spin[tab->loading_anim_step],
851 trust_status_char(tab->trust),
852 tab->client_cert ? 'C' : '-',
853 mode == NULL ? "(none)" : mode);
855 pct = (buffer->line_off + buffer->curs_y) * 100.0
856 / buffer->line_max;
858 if (buffer->line_max <= (size_t)body_lines)
859 wprintw(modeline, "All ");
860 else if (buffer->line_off == 0)
861 wprintw(modeline, "Top ");
862 else if (buffer->line_off + body_lines >= buffer->line_max)
863 wprintw(modeline, "Bottom ");
864 else
865 wprintw(modeline, "%.0f%% ", pct);
867 wprintw(modeline, "%zu/%zu %s ",
868 buffer->line_off + buffer->curs_y,
869 buffer->line_max,
870 hist_cur(tab->hist));
872 getyx(modeline, y, x);
873 getmaxyx(modeline, max_y, max_x);
875 (void)y;
876 (void)max_y;
878 for (; x < max_x; ++x)
879 waddstr(modeline, "-");
881 wattr_off(modeline, modeline_face.background, NULL);
884 static void
885 redraw_minibuffer(void)
887 wattr_on(echoarea, minibuffer_face.background, NULL);
888 werase(echoarea);
890 if (in_minibuffer)
891 do_redraw_minibuffer();
892 else
893 do_redraw_echoarea();
895 if (in_minibuffer == MB_COMPREAD)
896 do_redraw_minibuffer_compl();
898 wattr_off(echoarea, minibuffer_face.background, NULL);
901 static void
902 do_redraw_echoarea(void)
904 struct vline *vl;
906 if (ministate.curmesg != NULL)
907 wprintw(echoarea, "%s", ministate.curmesg);
908 else if (*keybuf != '\0')
909 waddstr(echoarea, keybuf);
910 else {
911 /* If nothing else, show the URL at point */
912 vl = current_tab->buffer.current_line;
913 if (vl != NULL && vl->parent->type == LINE_LINK)
914 waddstr(echoarea, vl->parent->alt);
918 static void
919 do_redraw_minibuffer(void)
921 struct buffer *cmplbuf, *buffer;
922 size_t off_y, off_x = 0;
923 const char *start, *c;
924 char *line;
926 cmplbuf = &ministate.compl.buffer;
927 buffer = &ministate.buffer;
928 (void)off_y; /* unused, set by getyx */
930 wmove(echoarea, 0, 0);
932 if (in_minibuffer == MB_COMPREAD)
933 wprintw(echoarea, "(%2zu) ",
934 cmplbuf->line_max);
936 wprintw(echoarea, "%s", ministate.prompt);
937 if (!ministate.editing)
938 wprintw(echoarea, "(%zu/%zu) ",
939 hist_off(ministate.hist) + 1,
940 hist_size(ministate.hist));
942 getyx(echoarea, off_y, off_x);
944 start = ministate.buf;
945 if (!ministate.editing)
946 start = hist_cur(ministate.hist);
947 line = buffer->current_line->parent->line + buffer->current_line->from;
948 c = utf8_nth(line, buffer->cpoff);
949 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
950 start = utf8_next_cp(start);
953 waddstr(echoarea, start);
955 if (ministate.curmesg != NULL)
956 wprintw(echoarea, " [%s]", ministate.curmesg);
958 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
961 static void
962 do_redraw_minibuffer_compl(void)
964 redraw_window(minibuffer, 0, 10, COLS, 1,
965 &ministate.compl.buffer);
968 /*
969 * Place the cursor in the right ncurses window. If soft is 1, use
970 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
971 * wrefresh.
972 */
973 static void
974 place_cursor(int soft)
976 int (*touch)(WINDOW *);
978 if (soft)
979 touch = wnoutrefresh;
980 else
981 touch = wrefresh;
983 if (in_minibuffer) {
984 if (side_window & SIDE_WINDOW_LEFT)
985 touch(help);
986 if (side_window & SIDE_WINDOW_BOTTOM)
987 touch(download);
988 touch(body);
989 touch(echoarea);
990 } else if (in_side_window & SIDE_WINDOW_LEFT) {
991 touch(body);
992 touch(echoarea);
993 if (in_side_window & SIDE_WINDOW_BOTTOM)
994 touch(download);
995 touch(help);
996 } else if (in_side_window & SIDE_WINDOW_BOTTOM) {
997 touch(body);
998 touch(echoarea);
999 if (in_side_window & SIDE_WINDOW_LEFT)
1000 touch(help);
1001 touch(download);
1002 } else {
1003 if (side_window & SIDE_WINDOW_LEFT)
1004 touch(help);
1005 if (side_window & SIDE_WINDOW_BOTTOM)
1006 touch(download);
1007 touch(echoarea);
1008 touch(body);
1012 static void
1013 redraw_tab(struct tab *tab)
1015 if (too_small)
1016 return;
1018 if (side_window & SIDE_WINDOW_LEFT) {
1019 redraw_help();
1020 wnoutrefresh(help);
1023 if (side_window & SIDE_WINDOW_BOTTOM) {
1024 redraw_download();
1025 wnoutrefresh(download);
1028 if (show_tab_bar)
1029 redraw_tabline();
1031 redraw_body(tab);
1032 redraw_modeline(tab);
1033 redraw_minibuffer();
1035 wnoutrefresh(tabline);
1036 wnoutrefresh(modeline);
1038 if (in_minibuffer == MB_COMPREAD)
1039 wnoutrefresh(minibuffer);
1041 place_cursor(1);
1043 doupdate();
1045 if (set_title)
1046 dprintf(1, "\033]2;%s - Telescope\a",
1047 current_tab->buffer.page.title);
1050 void
1051 start_loading_anim(struct tab *tab)
1053 if (tab->loading_anim)
1054 return;
1055 tab->loading_anim = 1;
1057 ev_timer_cancel(tab->loading_timer);
1058 tab->loading_timer = ev_timer(&loading_tv, update_loading_anim, tab);
1061 static void
1062 update_loading_anim(int fd, int 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);
1070 wrefresh(modeline);
1071 wrefresh(body);
1072 if (in_minibuffer)
1073 wrefresh(echoarea);
1076 tab->loading_timer = ev_timer(&loading_tv, update_loading_anim, tab);
1079 static void
1080 stop_loading_anim(struct tab *tab)
1082 if (!tab->loading_anim)
1083 return;
1085 ev_timer_cancel(tab->loading_timer);
1086 tab->loading_anim = 0;
1087 tab->loading_anim_step = 0;
1089 if (tab != current_tab)
1090 return;
1092 redraw_modeline(tab);
1094 wrefresh(modeline);
1095 wrefresh(body);
1096 if (in_minibuffer)
1097 wrefresh(echoarea);
1100 int
1101 ui_init(void)
1103 setlocale(LC_ALL, "");
1105 if (TAILQ_EMPTY(&global_map.m)) {
1106 fprintf(stderr, "no keys defined!\n");
1107 return 0;
1110 minibuffer_init();
1112 /* initialize download window */
1113 TAILQ_INIT(&downloadwin.head);
1114 TAILQ_INIT(&downloadwin.page.head);
1116 /* initialize help window */
1117 TAILQ_INIT(&helpwin.head);
1118 TAILQ_INIT(&helpwin.page.head);
1120 base_map = &global_map;
1121 current_map = &global_map;
1123 initscr();
1125 if (enable_colors) {
1126 if (has_colors()) {
1127 start_color();
1128 use_default_colors();
1129 } else
1130 enable_colors = 0;
1133 config_apply_style();
1135 raw();
1136 noecho();
1137 nonl();
1138 intrflush(stdscr, FALSE);
1140 if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1141 return 0;
1142 if ((body = newwin(1, 1, 0, 0)) == NULL)
1143 return 0;
1144 if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1145 return 0;
1146 if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1147 return 0;
1148 if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1149 return 0;
1150 if ((download = newwin(1, 1, 0, 0)) == NULL)
1151 return 0;
1152 if ((help = newwin(1, 1, 0, 0)) == NULL)
1153 return 0;
1155 wbkgd(body, body_face.body);
1156 wbkgd(download, download_face.background);
1157 wbkgd(echoarea, minibuffer_face.background);
1159 update_x_offset();
1161 keypad(body, TRUE);
1162 scrollok(body, FALSE);
1164 /* non-blocking input */
1165 wtimeout(body, 0);
1166 wtimeout(help, 0);
1168 wmove(body, 0, 0);
1170 return 1;
1173 void
1174 ui_main_loop(void)
1176 if (ev_signal(SIGWINCH, handle_resize, NULL) == -1 ||
1177 ev_add(0, EV_READ, dispatch_stdio, NULL) == -1)
1178 err(1, "ev_signal or ev_add failed");
1180 switch_to_tab(current_tab);
1181 rearrange_windows();
1183 ev_loop();
1186 void
1187 ui_on_tab_loaded(struct tab *tab)
1189 size_t line_off, curr_off;
1191 stop_loading_anim(tab);
1192 message("Loaded %s", hist_cur(tab->hist));
1194 hist_cur_offs(tab->hist, &line_off, &curr_off);
1195 if (curr_off != 0 &&
1196 tab->buffer.current_line == TAILQ_FIRST(&tab->buffer.head)) {
1197 set_scroll_position(tab, line_off, curr_off);
1198 redraw_tab(tab);
1199 return;
1202 if (show_tab_bar)
1203 redraw_tabline();
1205 wrefresh(tabline);
1206 place_cursor(0);
1209 void
1210 ui_on_tab_refresh(struct tab *tab)
1212 wrap_page(&tab->buffer, body_cols);
1213 if (tab == current_tab)
1214 redraw_tab(tab);
1215 else
1216 tab->flags |= TAB_URGENT;
1219 void
1220 ui_on_download_refresh(void)
1222 if (ev_timer_pending(download_timer))
1223 return;
1225 download_timer = ev_timer(&download_refresh_timer,
1226 handle_download_refresh, NULL);
1229 void
1230 ui_remotely_open_link(const char *uri)
1232 new_tab(uri, NULL, NULL);
1233 ui_on_tab_refresh(current_tab);
1235 /* ring the bell */
1236 printf("\a");
1237 fflush(stdout);
1240 const char *
1241 ui_keyname(int k)
1243 return keyname(k);
1246 void
1247 ui_toggle_side_window(int kind)
1249 if (in_side_window & kind)
1250 ui_other_window();
1252 side_window ^= kind;
1253 if (side_window & SIDE_WINDOW_LEFT)
1254 recompute_help();
1255 if (side_window & SIDE_WINDOW_BOTTOM)
1256 recompute_downloads();
1259 * ugly hack, but otherwise the window doesn't get updated
1260 * until I call rearrange_windows a second time (e.g. via
1261 * C-l). I will be happy to know why something like this is
1262 * needed.
1264 rearrange_windows();
1265 rearrange_windows();
1268 void
1269 ui_show_downloads_pane(void)
1271 if (!(side_window & SIDE_WINDOW_BOTTOM))
1272 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1275 void
1276 ui_schedule_redraw(void)
1278 should_rearrange_windows = 1;
1281 void
1282 ui_require_input(struct tab *tab, int hide, void (*fn)(void))
1284 /* TODO: hard-switching to another tab is ugly */
1285 switch_to_tab(tab);
1287 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1288 ir_history, NULL, NULL, 0);
1289 strlcpy(ministate.prompt, "Input required: ",
1290 sizeof(ministate.prompt));
1291 redraw_tab(tab);
1294 void
1295 ui_after_message_hook(void)
1297 redraw_minibuffer();
1298 place_cursor(0);
1301 void
1302 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1303 struct tab *data)
1305 yornp(prompt, fn, data);
1306 redraw_tab(current_tab);
1309 void
1310 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1311 struct tab *data, const char *input)
1313 minibuffer_read(prompt, fn, data);
1315 if (input != NULL) {
1316 strlcpy(ministate.buf, input, sizeof(ministate.buf));
1317 cmd_move_end_of_line(&ministate.buffer);
1320 redraw_tab(current_tab);
1323 void
1324 ui_other_window(void)
1326 if (in_side_window & SIDE_WINDOW_LEFT &&
1327 side_window & SIDE_WINDOW_BOTTOM)
1328 in_side_window = SIDE_WINDOW_BOTTOM;
1329 else if (in_side_window)
1330 in_side_window = 0;
1331 else if (!in_side_window && side_window & SIDE_WINDOW_LEFT)
1332 in_side_window = SIDE_WINDOW_LEFT;
1333 else if (!in_side_window && side_window)
1334 in_side_window = SIDE_WINDOW_BOTTOM;
1335 else
1336 message("No other window to select");
1339 void
1340 ui_suspend(void)
1342 endwin();
1344 kill(getpid(), SIGSTOP);
1346 refresh();
1347 clear();
1348 rearrange_windows();
1351 void
1352 ui_end(void)
1354 endwin();