Blob


1 /*
2 * Copyright (c) 2021 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 <assert.h>
36 #include <curses.h>
37 #include <locale.h>
38 #include <signal.h>
39 #include <stdarg.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
44 #include "defaults.h"
45 #include "minibuffer.h"
46 #include "session.h"
47 #include "telescope.h"
48 #include "ui.h"
49 #include "utf8.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;
82 static int too_small;
83 static int x_offset;
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;
98 static WINDOW *help;
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;
106 int download_lines;
107 int download_cols;
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,
118 minibuffer_map,
119 *current_map,
120 *base_map;
122 static inline void
123 update_x_offset(void)
125 if (olivetti_mode && fill_column < body_cols)
126 x_offset = (body_cols - fill_column)/2;
127 else
128 x_offset = 0;
131 static void
132 set_scroll_position(struct tab *tab, size_t top, size_t cur)
134 struct line *last;
135 struct vline *vl;
136 size_t i = 0;
137 int topfound = 0;
139 last = TAILQ_FIRST(&tab->buffer.page.head);
140 TAILQ_FOREACH(vl, &tab->buffer.head, vlines) {
141 if (last != vl->parent) {
142 last = vl->parent;
143 i++;
146 if (!topfound && i == top) {
147 topfound = 1;
148 tab->buffer.top_line = vl;
151 if (i == cur) {
152 tab->buffer.current_line = vl;
153 return;
157 if (!topfound)
158 tab->buffer.top_line = TAILQ_FIRST(&tab->buffer.head);
160 tab->buffer.current_line = tab->buffer.top_line;
163 void
164 get_scroll_position(struct tab *tab, size_t *top, size_t *cur)
166 struct line *l;
167 int topfound = 0;
169 *top = 0;
170 *cur = 0;
172 if (tab->buffer.top_line == NULL ||
173 tab->buffer.current_line == NULL)
174 return;
176 TAILQ_FOREACH(l, &tab->buffer.page.head, lines) {
177 if (tab->buffer.top_line->parent == l)
178 topfound = 1;
179 if (tab->buffer.current_line->parent == l)
180 return;
182 if (!topfound)
183 (*top)++;
184 (*cur)++;
188 void
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;
199 void
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;
210 static void
211 restore_curs_x(struct buffer *buffer)
213 struct vline *vl;
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;
222 } else {
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;
231 if (vl == NULL)
232 return;
234 if (vl->parent->data != NULL)
235 buffer->curs_x += utf8_swidth_between(vl->parent->line,
236 vl->parent->data);
237 else {
238 prfx = line_prefixes[vl->parent->type].prfx1;
239 buffer->curs_x += utf8_swidth(prfx);
243 void
244 global_key_unbound(void)
246 message("%s is undefined", keybuf);
249 struct buffer *
250 current_buffer(void)
252 if (in_minibuffer)
253 return &ministate.buffer;
254 if (in_side_window & SIDE_WINDOW_LEFT)
255 return &helpwin;
256 if (in_side_window & SIDE_WINDOW_BOTTOM)
257 return &downloadwin;
258 return &current_tab->buffer;
261 static int
262 readkey(void)
264 uint32_t state = 0;
266 if ((thiskey.key = wgetch(body)) == ERR)
267 return 0;
269 thiskey.meta = thiskey.key == '\e';
270 if (thiskey.meta) {
271 thiskey.key = wgetch(body);
272 if (thiskey.key == ERR || thiskey.key == '\e') {
273 thiskey.meta = 0;
274 thiskey.key = '\e';
278 thiskey.cp = 0;
280 if ((unsigned int)thiskey.key >= UINT8_MAX)
281 return 1;
283 while (1) {
284 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
285 break;
286 if ((thiskey.key = wgetch(body)) == ERR) {
287 message("Error decoding user input");
288 return 0;
292 return 1;
295 static void
296 dispatch_stdio(int fd, short ev, void *d)
298 int lk;
299 const char *keyname;
300 char tmp[5] = {0};
302 /* TODO: schedule a redraw? */
303 if (too_small)
304 return;
306 if (!readkey())
307 return;
309 if (keybuf[0] != '\0')
310 strlcat(keybuf, " ", sizeof(keybuf));
311 if (thiskey.meta)
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));
318 } else {
319 tmp[0] = thiskey.key;
320 strlcat(keybuf, tmp, sizeof(keybuf));
323 lk = lookup_key(&current_map, &thiskey, current_buffer());
324 if (lk == LK_UNBOUND) {
325 if (current_map->unhandled_input != NULL)
326 current_map->unhandled_input();
327 else
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)
336 recompute_help();
338 if (should_rearrange_windows)
339 rearrange_windows();
340 redraw_tab(current_tab);
343 static void
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);
353 static void
354 handle_resize_nodelay(int s, short ev, void *d)
356 endwin();
357 refresh();
358 clear();
360 rearrange_windows();
363 static void
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);
372 static inline int
373 should_show_tab_bar(void)
375 if (tab_bar_show == -1)
376 return 0;
377 if (tab_bar_show == 0)
378 return 1;
380 return TAILQ_NEXT(TAILQ_FIRST(&tabshead), tabs) != NULL;
383 static void
384 rearrange_windows(void)
386 int lines;
387 int minibuffer_lines;
389 should_rearrange_windows = 0;
390 show_tab_bar = should_show_tab_bar();
392 lines = LINES;
394 /* 3 lines for the ui and 12 for the */
395 if ((too_small = lines < 15)) {
396 erase();
397 printw("Window too small.");
398 refresh();
399 return;
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;
430 body_cols = COLS;
432 /*
433 * Here we make the assumption that show_tab_bar is either 0
434 * or 1, and reuse that as argument to mvwin.
435 */
436 if (side_window & SIDE_WINDOW_LEFT) {
437 help_cols = 0.3 * COLS;
438 help_lines = lines;
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);
446 } else
447 mvwin(body, show_tab_bar, 0);
449 update_x_offset();
450 wresize(body, body_lines, body_cols);
452 if (show_tab_bar)
453 wresize(tabline, 1, COLS);
455 wrap_page(&current_tab->buffer, body_cols);
456 redraw_tab(current_tab);
459 static void
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;
464 char *space, *t;
466 if (vl->len == 0) {
467 *text_ret = "";
468 *text_len = 0;
471 cont = vl->flags & L_CONTINUATION;
472 type = vl->parent->type;
473 if (!cont)
474 *prfx_ret = line_prefixes[type].prfx1;
475 else
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);
482 return;
485 if (cont) {
486 memset(buf, 0, len);
487 width = utf8_swidth_between(vl->parent->line, space);
488 for (i = 0; i < width + 1; ++i)
489 strlcat(buf, " ", len);
490 } else {
491 strlcpy(buf, *text_ret, len);
492 if ((t = strchr(buf, ' ')) != NULL)
493 *t = '\0';
494 strlcat(buf, " ", len);
496 /* skip the emoji */
497 *text_ret += (space - vl->parent->line) + 1;
500 *prfx_ret = buf;
501 *text_len = INT_MAX;
504 static inline void
505 print_vline_descr(int width, WINDOW *window, struct vline *vl)
507 int x, y, goal;
509 switch (vl->parent->type) {
510 case LINE_COMPL:
511 case LINE_COMPL_CURRENT:
512 goal = width/2;
513 break;
514 case LINE_DOWNLOAD:
515 case LINE_DOWNLOAD_DONE:
516 goal = width/4;
517 break;
518 case LINE_HELP:
519 goal = 8;
520 break;
521 default:
522 return;
525 if (vl->parent->alt == NULL)
526 return;
528 (void)y;
529 getyx(window, y, x);
531 if (goal <= x)
532 wprintw(window, " ");
533 for (; goal > x; ++x)
534 wprintw(window, " ");
536 wprintw(window, "%s", vl->parent->alt);
539 /*
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
545 * again.
546 */
547 static void
548 print_vline(int off, int width, WINDOW *window, struct vline *vl)
550 /*
551 * Believe me or not, I've seen emoji ten code points long!
552 * That means, to stay large, 4*10 bytes + NUL.
553 */
554 char emojibuf[41] = {0};
555 const char *text, *prfx;
556 struct line_face *f;
557 int i, left, x, y, textlen;
559 f = &line_faces[vl->parent->type];
561 /* unused, set by getyx */
562 (void)y;
564 if (vl->parent->type == LINE_FRINGE && fringe_ignore_offset)
565 off = 0;
567 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx,
568 &text, &textlen);
570 wattr_on(window, body_face.left, NULL);
571 for (i = 0; i < off; i++)
572 waddch(window, ' ');
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);
584 getyx(window, y, x);
586 left = width - x;
588 wattr_on(window, f->trail, NULL);
589 for (i = 0; i < left - off; ++i)
590 waddch(window, ' ');
591 wattr_off(window, f->trail, NULL);
593 wattr_on(window, body_face.right, NULL);
594 for (i = 0; i < off; i++)
595 waddch(window, ' ');
596 wattr_off(window, body_face.right, NULL);
600 static void
601 redraw_tabline(void)
603 struct tab *tab;
604 size_t toskip, ots, tabwidth, space, x;
605 int current, y, truncated, pair;
606 const char *title;
607 char buf[25];
609 x = 0;
611 /* unused, but setted by a getyx */
612 (void)y;
614 tabwidth = sizeof(buf)+1;
615 space = COLS-2;
617 toskip = 0;
618 TAILQ_FOREACH(tab, &tabshead, tabs) {
619 toskip++;
620 if (tab == current_tab)
621 break;
624 if (toskip * tabwidth <= space)
625 toskip = 0;
626 else {
627 ots = toskip;
628 toskip--;
629 while (toskip != 0 &&
630 (ots - toskip+1) * tabwidth < space)
631 toskip--;
634 werase(tabline);
635 wattr_on(tabline, tab_face.background, NULL);
636 wprintw(tabline, toskip == 0 ? " " : "<");
637 wattr_off(tabline, tab_face.background, NULL);
639 truncated = 0;
640 TAILQ_FOREACH(tab, &tabshead, tabs) {
641 if (truncated)
642 break;
643 if (toskip != 0) {
644 toskip--;
645 continue;
648 getyx(tabline, y, x);
649 if (x + sizeof(buf)+2 >= (size_t)COLS)
650 truncated = 1;
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));
659 else
660 strlcpy(buf, " ", sizeof(buf));
662 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
663 /* truncation happens */
664 strlcpy(&buf[sizeof(buf)-4], "...", 4);
665 } else {
666 /* pad with spaces */
667 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
668 /* nop */ ;
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, ' ');
685 if (truncated)
686 mvwprintw(tabline, 0, COLS-1, ">");
687 wattr_off(tabline, tab_face.background, NULL);
690 /*
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.
694 */
695 struct vline *
696 adjust_line(struct vline *vl, struct buffer *buffer)
698 struct vline *t;
700 if (vl == NULL)
701 return NULL;
703 if (!(vl->parent->flags & L_HIDDEN))
704 return vl;
706 /* search forward */
707 for (t = vl;
708 t != NULL && t->parent->flags & L_HIDDEN;
709 t = TAILQ_NEXT(t, vlines))
710 ; /* nop */
712 if (t != NULL)
713 return t;
715 /* search backward */
716 for (t = vl;
717 t != NULL && t->parent->flags & L_HIDDEN;
718 t = TAILQ_PREV(t, vhead, vlines))
719 ; /* nop */
721 return t;
724 static void
725 redraw_window(WINDOW *win, int off, int height, int width,
726 int show_fringe, struct buffer *buffer)
728 struct vline *vl;
729 int onscreen = 0, l = 0;
731 restore_curs_x(buffer);
733 /*
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
738 * interesting one.
739 */
741 again:
742 werase(win);
743 buffer->curs_y = 0;
745 if (TAILQ_EMPTY(&buffer->head))
746 goto end;
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)
753 goto end;
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)
759 continue;
761 wmove(win, l, 0);
762 print_vline(off, width, win, vl);
764 if (vl == buffer->current_line)
765 onscreen = 1;
767 if (!onscreen)
768 buffer->curs_y++;
770 l++;
771 if (l == height)
772 break;
775 if (!onscreen) {
776 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
777 if (vl == buffer->current_line)
778 break;
779 if (vl->parent->flags & L_HIDDEN)
780 continue;
781 buffer->line_off++;
782 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
785 if (vl != NULL)
786 goto again;
789 buffer->last_line_off = buffer->line_off;
790 buffer->force_redraw = 0;
791 end:
792 for (; show_fringe && l < height; l++)
793 print_vline(off, width, win, &fringe);
795 wmove(win, buffer->curs_y, buffer->curs_x);
798 static void
799 redraw_download(void)
801 redraw_window(download, 0, download_lines, COLS, 0, &downloadwin);
804 static void
805 redraw_help(void)
807 redraw_window(help, 0, help_lines, help_cols, 1, &helpwin);
810 static void
811 redraw_body(struct tab *tab)
813 static struct tab *last_tab;
815 if (last_tab != tab)
816 tab->buffer.force_redraw =1;
817 last_tab = tab;
819 redraw_window(body, x_offset, body_lines, body_cols, 1, &tab->buffer);
822 static inline char
823 trust_status_char(enum trust_state ts)
825 switch (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';
831 default: return 'X';
835 static void
836 redraw_modeline(struct tab *tab)
838 struct buffer *buffer;
839 double pct;
840 int x, y, max_x, max_y;
841 const char *mode;
842 const char *spin = "-\\|/";
844 buffer = current_buffer();
845 mode = buffer->page.name;
847 werase(modeline);
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
857 / buffer->line_max;
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 ");
865 else
866 wprintw(modeline, "%.0f%% ", pct);
868 wprintw(modeline, "%zu/%zu %s ",
869 buffer->line_off + buffer->curs_y,
870 buffer->line_max,
871 tab->hist_cur->h);
873 getyx(modeline, y, x);
874 getmaxyx(modeline, max_y, max_x);
876 (void)y;
877 (void)max_y;
879 for (; x < max_x; ++x)
880 waddstr(modeline, "-");
882 wattr_off(modeline, modeline_face.background, NULL);
885 static void
886 redraw_minibuffer(void)
888 wattr_on(echoarea, minibuffer_face.background, NULL);
889 werase(echoarea);
891 if (in_minibuffer)
892 do_redraw_minibuffer();
893 else
894 do_redraw_echoarea();
896 if (in_minibuffer == MB_COMPREAD)
897 do_redraw_minibuffer_compl();
899 wattr_off(echoarea, minibuffer_face.background, NULL);
902 static void
903 do_redraw_echoarea(void)
905 struct vline *vl;
907 if (ministate.curmesg != NULL)
908 wprintw(echoarea, "%s", ministate.curmesg);
909 else if (*keybuf != '\0')
910 waddstr(echoarea, keybuf);
911 else {
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);
919 static void
920 do_redraw_minibuffer(void)
922 struct buffer *cmplbuf, *buffer;
923 size_t off_y, off_x = 0;
924 const char *start, *c;
925 char *line;
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) ",
935 cmplbuf->line_max);
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
947 : ministate.buf;
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));
962 static void
963 do_redraw_minibuffer_compl(void)
965 redraw_window(minibuffer, 0, 10, COLS, 1,
966 &ministate.compl.buffer);
969 /*
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
972 * wrefresh.
973 */
974 static void
975 place_cursor(int soft)
977 int (*touch)(WINDOW *);
979 if (soft)
980 touch = wnoutrefresh;
981 else
982 touch = wrefresh;
984 if (in_minibuffer) {
985 if (side_window & SIDE_WINDOW_LEFT)
986 touch(help);
987 if (side_window & SIDE_WINDOW_BOTTOM)
988 touch(download);
989 touch(body);
990 touch(echoarea);
991 } else if (in_side_window & SIDE_WINDOW_LEFT) {
992 touch(body);
993 touch(echoarea);
994 if (in_side_window & SIDE_WINDOW_BOTTOM)
995 touch(download);
996 touch(help);
997 } else if (in_side_window & SIDE_WINDOW_BOTTOM) {
998 touch(body);
999 touch(echoarea);
1000 if (in_side_window & SIDE_WINDOW_LEFT)
1001 touch(help);
1002 touch(download);
1003 } else {
1004 if (side_window & SIDE_WINDOW_LEFT)
1005 touch(help);
1006 if (side_window & SIDE_WINDOW_BOTTOM)
1007 touch(download);
1008 touch(echoarea);
1009 touch(body);
1013 static void
1014 redraw_tab(struct tab *tab)
1016 if (too_small)
1017 return;
1019 if (side_window & SIDE_WINDOW_LEFT) {
1020 redraw_help();
1021 wnoutrefresh(help);
1024 if (side_window & SIDE_WINDOW_BOTTOM) {
1025 redraw_download();
1026 wnoutrefresh(download);
1029 if (show_tab_bar)
1030 redraw_tabline();
1032 redraw_body(tab);
1033 redraw_modeline(tab);
1034 redraw_minibuffer();
1036 wnoutrefresh(tabline);
1037 wnoutrefresh(modeline);
1039 if (in_minibuffer == MB_COMPREAD)
1040 wnoutrefresh(minibuffer);
1042 place_cursor(1);
1044 doupdate();
1046 if (set_title)
1047 dprintf(1, "\033]2;%s - Telescope\a",
1048 current_tab->buffer.page.title);
1051 void
1052 start_loading_anim(struct tab *tab)
1054 if (tab->loading_anim)
1055 return;
1056 tab->loading_anim = 1;
1057 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1058 evtimer_add(&tab->loadingev, &loadingev_timer);
1061 static void
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);
1070 wrefresh(modeline);
1071 wrefresh(body);
1072 if (in_minibuffer)
1073 wrefresh(echoarea);
1076 evtimer_add(&tab->loadingev, &loadingev_timer);
1079 static void
1080 stop_loading_anim(struct tab *tab)
1082 if (!tab->loading_anim)
1083 return;
1084 evtimer_del(&tab->loadingev);
1085 tab->loading_anim = 0;
1086 tab->loading_anim_step = 0;
1088 if (tab != current_tab)
1089 return;
1091 redraw_modeline(tab);
1093 wrefresh(modeline);
1094 wrefresh(body);
1095 if (in_minibuffer)
1096 wrefresh(echoarea);
1099 int
1100 ui_print_colors(void)
1102 int colors = 0, pairs = 0, can_change = 0;
1103 int columns = 16, lines, color, i, j;
1105 initscr();
1106 if (has_colors()) {
1107 start_color();
1108 use_default_colors();
1110 colors = COLORS;
1111 pairs = COLOR_PAIRS;
1112 can_change = can_change_color();
1114 endwin();
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);
1119 printf("\n");
1121 if (colors == 0) {
1122 printf("No color support\n");
1123 return 0;
1126 printf("Available colors:\n\n");
1127 lines = (colors - 1) / columns + 1;
1128 color = 0;
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);
1133 printf("\n");
1136 printf("\033[0m");
1137 fflush(stdout);
1138 return 0;
1141 int
1142 ui_init()
1144 setlocale(LC_ALL, "");
1146 if (TAILQ_EMPTY(&global_map.m)) {
1147 fprintf(stderr, "no keys defined!\n");
1148 return 0;
1151 minibuffer_init();
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;
1164 initscr();
1166 if (enable_colors) {
1167 if (has_colors()) {
1168 start_color();
1169 use_default_colors();
1170 } else
1171 enable_colors = 0;
1174 config_apply_style();
1176 raw();
1177 noecho();
1178 nonl();
1179 intrflush(stdscr, FALSE);
1181 if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1182 return 0;
1183 if ((body = newwin(1, 1, 0, 0)) == NULL)
1184 return 0;
1185 if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1186 return 0;
1187 if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1188 return 0;
1189 if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1190 return 0;
1191 if ((download = newwin(1, 1, 0, 0)) == NULL)
1192 return 0;
1193 if ((help = newwin(1, 1, 0, 0)) == NULL)
1194 return 0;
1196 wbkgd(body, body_face.body);
1197 wbkgd(download, download_face.background);
1198 wbkgd(echoarea, minibuffer_face.background);
1200 update_x_offset();
1202 keypad(body, TRUE);
1203 scrollok(body, FALSE);
1205 /* non-blocking input */
1206 wtimeout(body, 0);
1207 wtimeout(help, 0);
1209 mvwprintw(body, 0, 0, "");
1211 return 1;
1214 void
1215 ui_main_loop(void)
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();
1229 event_dispatch();
1232 void
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);
1242 redraw_tab(tab);
1243 return;
1246 if (show_tab_bar)
1247 redraw_tabline();
1249 wrefresh(tabline);
1250 place_cursor(0);
1253 void
1254 ui_on_tab_refresh(struct tab *tab)
1256 wrap_page(&tab->buffer, body_cols);
1257 if (tab == current_tab)
1258 redraw_tab(tab);
1259 else
1260 tab->flags |= TAB_URGENT;
1263 void
1264 ui_on_download_refresh(void)
1266 if (event_pending(&download_refreshev, EV_TIMEOUT, NULL))
1267 return;
1269 evtimer_set(&download_refreshev, handle_download_refresh, NULL);
1270 evtimer_add(&download_refreshev, &download_refresh_timer);
1273 void
1274 ui_remotely_open_link(const char *uri)
1276 new_tab(uri, NULL, NULL);
1277 ui_on_tab_refresh(current_tab);
1279 /* ring the bell */
1280 printf("\a");
1281 fflush(stdout);
1284 const char *
1285 ui_keyname(int k)
1287 return keyname(k);
1290 void
1291 ui_toggle_side_window(int kind)
1293 if (in_side_window & kind)
1294 ui_other_window();
1296 side_window ^= kind;
1297 if (side_window & SIDE_WINDOW_LEFT)
1298 recompute_help();
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
1306 * needed.
1308 rearrange_windows();
1309 rearrange_windows();
1312 void
1313 ui_show_downloads_pane(void)
1315 if (!(side_window & SIDE_WINDOW_BOTTOM))
1316 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1319 void
1320 ui_schedule_redraw(void)
1322 should_rearrange_windows = 1;
1325 void
1326 ui_require_input(struct tab *tab, int hide, void (*fn)(void))
1328 /* TODO: hard-switching to another tab is ugly */
1329 switch_to_tab(tab);
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));
1335 redraw_tab(tab);
1338 void
1339 ui_after_message_hook(void)
1341 redraw_minibuffer();
1342 place_cursor(0);
1345 void
1346 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1347 struct tab *data)
1349 yornp(prompt, fn, data);
1350 redraw_tab(current_tab);
1353 void
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);
1367 void
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)
1374 in_side_window = 0;
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;
1379 else
1380 message("No other window to select");
1383 void
1384 ui_suspend(void)
1386 endwin();
1388 kill(getpid(), SIGSTOP);
1390 refresh();
1391 clear();
1392 rearrange_windows();
1395 void
1396 ui_end(void)
1398 endwin();