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 rearrange_windows(void);
62 static void line_prefix_and_text(struct vline *, char *, size_t, const char **, const char **);
63 static void print_vline(int, int, WINDOW*, struct vline*);
64 static void redraw_tabline(void);
65 static void redraw_window(WINDOW *, int, int, int, int, struct buffer *);
66 static void redraw_download(void);
67 static void redraw_help(void);
68 static void redraw_body(struct tab*);
69 static void redraw_modeline(struct tab*);
70 static void redraw_minibuffer(void);
71 static void do_redraw_echoarea(void);
72 static void do_redraw_minibuffer(void);
73 static void do_redraw_minibuffer_compl(void);
74 static void place_cursor(int);
75 static void redraw_tab(struct tab*);
76 static void update_loading_anim(int, short, void*);
77 static void stop_loading_anim(struct tab*);
79 static int should_rearrange_windows;
80 static int show_tab_bar;
81 static int too_small;
82 static int x_offset;
84 struct thiskey thiskey;
85 struct tab *current_tab;
87 static struct event resizeev;
88 static struct timeval resize_timer = { 0, 250000 };
90 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
92 int body_lines, body_cols;
94 static WINDOW *help;
95 /* not static so we can see them from help.c */
96 struct buffer helpwin;
97 int help_lines, help_cols;
99 static WINDOW *download;
100 /* not static so we can see them from download.c */
101 struct buffer downloadwin;
102 int download_lines;
103 int download_cols;
105 static int side_window;
106 static int in_side_window;
108 static struct timeval loadingev_timer = { 0, 250000 };
110 static char keybuf[64];
112 /* XXX: don't forget to init these in main() */
113 struct kmap global_map,
114 minibuffer_map,
115 *current_map,
116 *base_map;
118 static inline void
119 update_x_offset(void)
121 if (olivetti_mode && fill_column < body_cols)
122 x_offset = (body_cols - fill_column)/2;
123 else
124 x_offset = 0;
127 static void
128 set_scroll_position(struct tab *tab, size_t top, size_t cur)
130 struct line *last;
131 struct vline *vl;
132 size_t i = 0;
133 int topfound = 0, curfound = 0;
135 last = TAILQ_FIRST(&tab->buffer.page.head);
136 TAILQ_FOREACH(vl, &tab->buffer.head, vlines) {
137 if (last != vl->parent) {
138 last = vl->parent;
139 i++;
142 if (!topfound && i == top) {
143 topfound = 1;
144 tab->buffer.top_line = vl;
147 if (!curfound && i == cur) {
148 curfound = 1;
149 tab->buffer.current_line = vl;
150 return;
154 if (!topfound) {
155 tab->buffer.top_line = TAILQ_FIRST(&tab->buffer.head);
156 tab->buffer.current_line = tab->buffer.top_line;
160 void
161 get_scroll_position(struct tab *tab, size_t *top, size_t *cur)
163 struct line *l;
164 int topfound = 0;
166 *top = 0;
167 *cur = 0;
169 if (tab->buffer.top_line == NULL ||
170 tab->buffer.current_line == NULL)
171 return;
173 TAILQ_FOREACH(l, &tab->buffer.page.head, lines) {
174 if (tab->buffer.top_line->parent == l)
175 topfound = 1;
176 if (tab->buffer.current_line->parent == l)
177 return;
179 if (!topfound)
180 (*top)++;
181 (*cur)++;
185 void
186 save_excursion(struct excursion *place, struct buffer *buffer)
188 place->curs_x = buffer->curs_x;
189 place->curs_y = buffer->curs_y;
190 place->line_off = buffer->line_off;
191 place->top_line = buffer->top_line;
192 place->current_line = buffer->current_line;
193 place->cpoff = buffer->cpoff;
196 void
197 restore_excursion(struct excursion *place, struct buffer *buffer)
199 buffer->curs_x = place->curs_x;
200 buffer->curs_y = place->curs_y;
201 buffer->line_off = place->line_off;
202 buffer->top_line = place->top_line;
203 buffer->current_line = place->current_line;
204 buffer->cpoff = place->cpoff;
207 static void
208 restore_curs_x(struct buffer *buffer)
210 struct vline *vl;
211 const char *prfx, *text;
213 vl = buffer->current_line;
214 if (vl == NULL || vl->line == NULL)
215 buffer->curs_x = buffer->cpoff = 0;
216 else if (vl->parent->data != NULL) {
217 text = vl->parent->data;
218 buffer->curs_x = utf8_snwidth(text + 1, buffer->cpoff) + 1;
219 } else
220 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
222 /* small hack: don't olivetti-mode the download pane */
223 if (buffer != &downloadwin)
224 buffer->curs_x += x_offset;
226 if (vl == NULL)
227 return;
229 if (vl->parent->data != NULL)
230 buffer->curs_x += utf8_swidth_between(vl->parent->line,
231 vl->parent->data);
232 else {
233 prfx = line_prefixes[vl->parent->type].prfx1;
234 buffer->curs_x += utf8_swidth(prfx);
238 void
239 global_key_unbound(void)
241 message("%s is undefined", keybuf);
244 struct buffer *
245 current_buffer(void)
247 if (in_minibuffer)
248 return &ministate.buffer;
249 if (in_side_window & SIDE_WINDOW_LEFT)
250 return &helpwin;
251 if (in_side_window & SIDE_WINDOW_BOTTOM)
252 return &downloadwin;
253 return &current_tab->buffer;
256 static int
257 readkey(void)
259 uint32_t state = 0;
261 if ((thiskey.key = wgetch(body)) == ERR)
262 return 0;
264 thiskey.meta = thiskey.key == '\e';
265 if (thiskey.meta) {
266 thiskey.key = wgetch(body);
267 if (thiskey.key == ERR || thiskey.key == '\e') {
268 thiskey.meta = 0;
269 thiskey.key = '\e';
273 thiskey.cp = 0;
275 if ((unsigned int)thiskey.key >= UINT8_MAX)
276 return 1;
278 while (1) {
279 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
280 break;
281 if ((thiskey.key = wgetch(body)) == ERR) {
282 message("Error decoding user input");
283 return 0;
287 return 1;
290 static void
291 dispatch_stdio(int fd, short ev, void *d)
293 int lk;
294 const char *keyname;
295 char tmp[5] = {0};
297 /* TODO: schedule a redraw? */
298 if (too_small)
299 return;
301 if (!readkey())
302 return;
304 if (keybuf[0] != '\0')
305 strlcat(keybuf, " ", sizeof(keybuf));
306 if (thiskey.meta)
307 strlcat(keybuf, "M-", sizeof(keybuf));
308 if (thiskey.cp != 0) {
309 utf8_encode(thiskey.cp, tmp);
310 strlcat(keybuf, tmp, sizeof(keybuf));
311 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
312 strlcat(keybuf, keyname, sizeof(keybuf));
313 } else {
314 tmp[0] = thiskey.key;
315 strlcat(keybuf, tmp, sizeof(keybuf));
318 lk = lookup_key(&current_map, &thiskey, current_buffer());
319 if (lk == LK_UNBOUND) {
320 if (current_map->unhandled_input != NULL)
321 current_map->unhandled_input();
322 else
323 global_key_unbound();
325 if (lk != LK_ADVANCED_MAP) {
326 current_map = base_map;
327 strlcpy(keybuf, "", sizeof(keybuf));
330 if (side_window & SIDE_WINDOW_LEFT)
331 recompute_help();
333 if (should_rearrange_windows)
334 rearrange_windows();
335 redraw_tab(current_tab);
338 static void
339 handle_resize(int sig, short ev, void *d)
341 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
342 event_del(&resizeev);
344 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
345 evtimer_add(&resizeev, &resize_timer);
348 static void
349 handle_resize_nodelay(int s, short ev, void *d)
351 endwin();
352 refresh();
353 clear();
355 rearrange_windows();
358 static inline int
359 should_show_tab_bar(void)
361 if (tab_bar_show == -1)
362 return 0;
363 if (tab_bar_show == 0)
364 return 1;
366 return TAILQ_NEXT(TAILQ_FIRST(&tabshead), tabs) != NULL;
369 static void
370 rearrange_windows(void)
372 int lines;
373 int minibuffer_lines;
375 should_rearrange_windows = 0;
376 show_tab_bar = should_show_tab_bar();
378 lines = LINES;
380 /* 3 lines for the ui and 12 for the */
381 if ((too_small = lines < 15)) {
382 erase();
383 printw("Window too small.");
384 refresh();
385 return;
388 /* move and resize the windows, in reverse order! */
390 if (in_minibuffer == MB_COMPREAD) {
391 minibuffer_lines = MIN(10, lines/2);
392 mvwin(minibuffer, lines - minibuffer_lines, 0);
393 wresize(minibuffer, minibuffer_lines, COLS);
394 lines -= minibuffer_lines;
396 wrap_page(&ministate.compl.buffer, COLS);
399 mvwin(echoarea, --lines, 0);
400 wresize(echoarea, 1, COLS);
402 mvwin(modeline, --lines, 0);
403 wresize(modeline, 1, COLS);
405 if (side_window & SIDE_WINDOW_BOTTOM) {
406 download_lines = MIN(5, lines/2);
407 download_cols = COLS;
408 mvwin(download, lines - download_lines, 0);
409 wresize(download, download_lines, download_cols);
410 lines -= download_lines;
412 wrap_page(&downloadwin, download_cols);
415 body_lines = show_tab_bar ? --lines : lines;
416 body_cols = COLS;
418 /*
419 * Here we make the assumption that show_tab_bar is either 0
420 * or 1, and reuse that as argument to mvwin.
421 */
422 if (side_window & SIDE_WINDOW_LEFT) {
423 help_cols = 0.3 * COLS;
424 help_lines = lines;
425 mvwin(help, show_tab_bar, 0);
426 wresize(help, help_lines, help_cols);
428 wrap_page(&helpwin, help_cols);
430 body_cols = COLS - help_cols - 1;
431 mvwin(body, show_tab_bar, help_cols);
432 } else
433 mvwin(body, show_tab_bar, 0);
435 update_x_offset();
436 wresize(body, body_lines, body_cols);
438 if (show_tab_bar)
439 wresize(tabline, 1, COLS);
441 wrap_page(&current_tab->buffer, body_cols);
442 redraw_tab(current_tab);
445 static void
446 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
447 const char **prfx_ret, const char **text_ret)
449 int type, i, cont, width;
450 char *space, *t;
452 if ((*text_ret = vl->line) == NULL)
453 *text_ret = "";
455 cont = vl->flags & L_CONTINUATION;
456 type = vl->parent->type;
457 if (!cont)
458 *prfx_ret = line_prefixes[type].prfx1;
459 else
460 *prfx_ret = line_prefixes[type].prfx2;
462 space = vl->parent->data;
463 if (!emojify_link || type != LINE_LINK || space == NULL)
464 return;
466 if (cont) {
467 memset(buf, 0, len);
468 width = utf8_swidth_between(vl->parent->line, space);
469 for (i = 0; i < width + 1; ++i)
470 strlcat(buf, " ", len);
471 } else {
472 strlcpy(buf, *text_ret, len);
473 if ((t = strchr(buf, ' ')) != NULL)
474 *t = '\0';
475 strlcat(buf, " ", len);
477 /* skip the emoji */
478 *text_ret += (space - vl->parent->line) + 1;
481 *prfx_ret = buf;
484 static inline void
485 print_vline_descr(int width, WINDOW *window, struct vline *vl)
487 int x, y, goal;
489 switch (vl->parent->type) {
490 case LINE_COMPL:
491 case LINE_COMPL_CURRENT:
492 goal = width/2;
493 break;
494 case LINE_DOWNLOAD:
495 case LINE_DOWNLOAD_DONE:
496 goal = width/4;
497 break;
498 case LINE_HELP:
499 goal = 8;
500 break;
501 default:
502 return;
505 if (vl->parent->alt == NULL)
506 return;
508 (void)y;
509 getyx(window, y, x);
511 if (goal <= x)
512 wprintw(window, " ");
513 for (; goal > x; ++x)
514 wprintw(window, " ");
516 wprintw(window, "%s", vl->parent->alt);
519 /*
520 * Core part of the rendering. It prints a vline starting from the
521 * current cursor position. Printing a vline consists of skipping
522 * `off' columns (for olivetti-mode), print the correct prefix (which
523 * may be the emoji in case of emojified links-lines), printing the
524 * text itself, filling until width - off and filling off columns
525 * again.
526 */
527 static void
528 print_vline(int off, int width, WINDOW *window, struct vline *vl)
530 /*
531 * Believe me or not, I've seen emoji ten code points long!
532 * That means, to stay large, 4*10 bytes + NUL.
533 */
534 char emojibuf[41] = {0};
535 const char *text, *prfx;
536 struct line_face *f;
537 int i, left, x, y;
539 f = &line_faces[vl->parent->type];
541 /* unused, set by getyx */
542 (void)y;
544 if (vl->parent->type == LINE_FRINGE && fringe_ignore_offset)
545 off = 0;
547 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
549 wattr_on(window, body_face.left, NULL);
550 for (i = 0; i < off; i++)
551 waddch(window, ' ');
552 wattr_off(window, body_face.left, NULL);
554 wattr_on(window, f->prefix, NULL);
555 wprintw(window, "%s", prfx);
556 wattr_off(window, f->prefix, NULL);
558 wattr_on(window, f->text, NULL);
559 wprintw(window, "%s", text);
560 print_vline_descr(width, window, vl);
561 wattr_off(window, f->text, NULL);
563 getyx(window, y, x);
565 left = width - x;
567 wattr_on(window, f->trail, NULL);
568 for (i = 0; i < left - off; ++i)
569 waddch(window, ' ');
570 wattr_off(window, f->trail, NULL);
572 wattr_on(window, body_face.right, NULL);
573 for (i = 0; i < off; i++)
574 waddch(window, ' ');
575 wattr_off(window, body_face.right, NULL);
579 static void
580 redraw_tabline(void)
582 struct tab *tab;
583 size_t toskip, ots, tabwidth, space, x;
584 int current, y, truncated, pair;
585 const char *title;
586 char buf[25];
588 x = 0;
590 /* unused, but setted by a getyx */
591 (void)y;
593 tabwidth = sizeof(buf)+1;
594 space = COLS-2;
596 toskip = 0;
597 TAILQ_FOREACH(tab, &tabshead, tabs) {
598 toskip++;
599 if (tab == current_tab)
600 break;
603 if (toskip * tabwidth <= space)
604 toskip = 0;
605 else {
606 ots = toskip;
607 toskip--;
608 while (toskip != 0 &&
609 (ots - toskip+1) * tabwidth < space)
610 toskip--;
613 werase(tabline);
614 wattr_on(tabline, tab_face.background, NULL);
615 wprintw(tabline, toskip == 0 ? " " : "<");
616 wattr_off(tabline, tab_face.background, NULL);
618 truncated = 0;
619 TAILQ_FOREACH(tab, &tabshead, tabs) {
620 if (truncated)
621 break;
622 if (toskip != 0) {
623 toskip--;
624 continue;
627 getyx(tabline, y, x);
628 if (x + sizeof(buf)+2 >= (size_t)COLS)
629 truncated = 1;
631 current = tab == current_tab;
633 if (*(title = tab->buffer.page.title) == '\0')
634 title = tab->hist_cur->h;
636 if (tab->flags & TAB_URGENT)
637 strlcpy(buf, "!", sizeof(buf));
638 else
639 strlcpy(buf, " ", sizeof(buf));
641 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
642 /* truncation happens */
643 strlcpy(&buf[sizeof(buf)-4], "...", 4);
644 } else {
645 /* pad with spaces */
646 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
647 /* nop */ ;
650 pair = current ? tab_face.current : tab_face.tab;
651 wattr_on(tabline, pair, NULL);
652 wprintw(tabline, "%s", buf);
653 wattr_off(tabline, pair, NULL);
655 wattr_on(tabline, tab_face.background, NULL);
656 if (TAILQ_NEXT(tab, tabs) != NULL)
657 wprintw(tabline, "┃");
658 wattr_off(tabline, tab_face.background, NULL);
661 wattr_on(tabline, tab_face.background, NULL);
662 for (; x < (size_t)COLS; ++x)
663 waddch(tabline, ' ');
664 if (truncated)
665 mvwprintw(tabline, 0, COLS-1, ">");
666 wattr_off(tabline, tab_face.background, NULL);
669 /*
670 * Compute the first visible line around vl. Try to search forward
671 * until the end of the buffer; if a visible line is not found, search
672 * backward. Return NULL if no viable line was found.
673 */
674 struct vline *
675 adjust_line(struct vline *vl, struct buffer *buffer)
677 struct vline *t;
679 if (vl == NULL)
680 return NULL;
682 if (!(vl->parent->flags & L_HIDDEN))
683 return vl;
685 /* search forward */
686 for (t = vl;
687 t != NULL && t->parent->flags & L_HIDDEN;
688 t = TAILQ_NEXT(t, vlines))
689 ; /* nop */
691 if (t != NULL)
692 return t;
694 /* search backward */
695 for (t = vl;
696 t != NULL && t->parent->flags & L_HIDDEN;
697 t = TAILQ_PREV(t, vhead, vlines))
698 ; /* nop */
700 return t;
703 static void
704 redraw_window(WINDOW *win, int off, int height, int width,
705 int show_fringe, struct buffer *buffer)
707 struct vline *vl;
708 int onscreen = 0, l = 0;
710 restore_curs_x(buffer);
712 /*
713 * TODO: ignoring buffer->force_update and always
714 * re-rendering. In theory we can recompute the y position
715 * without a re-render, and optimize here. It's not the only
716 * optimisation possible here, wscrl wolud also be an
717 * interesting one.
718 */
720 again:
721 werase(win);
722 buffer->curs_y = 0;
724 if (TAILQ_EMPTY(&buffer->head))
725 goto end;
727 if (buffer->top_line == NULL)
728 buffer->top_line = TAILQ_FIRST(&buffer->head);
730 buffer->top_line = adjust_line(buffer->top_line, buffer);
731 if (buffer->top_line == NULL)
732 goto end;
734 buffer->current_line = adjust_line(buffer->current_line, buffer);
736 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
737 if (vl->parent->flags & L_HIDDEN)
738 continue;
740 wmove(win, l, 0);
741 print_vline(off, width, win, vl);
743 if (vl == buffer->current_line)
744 onscreen = 1;
746 if (!onscreen)
747 buffer->curs_y++;
749 l++;
750 if (l == height)
751 break;
754 if (!onscreen) {
755 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
756 if (vl == buffer->current_line)
757 break;
758 if (vl->parent->flags & L_HIDDEN)
759 continue;
760 buffer->line_off++;
761 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
764 if (vl != NULL)
765 goto again;
768 buffer->last_line_off = buffer->line_off;
769 buffer->force_redraw = 0;
770 end:
771 for (; show_fringe && l < height; l++)
772 print_vline(off, width, win, &fringe);
774 wmove(win, buffer->curs_y, buffer->curs_x);
777 static void
778 redraw_download(void)
780 redraw_window(download, 0, download_lines, COLS, 0, &downloadwin);
783 static void
784 redraw_help(void)
786 redraw_window(help, 0, help_lines, help_cols, 1, &helpwin);
789 static void
790 redraw_body(struct tab *tab)
792 static struct tab *last_tab;
794 if (last_tab != tab)
795 tab->buffer.force_redraw =1;
796 last_tab = tab;
798 redraw_window(body, x_offset, body_lines, body_cols, 1, &tab->buffer);
801 static inline char
802 trust_status_char(enum trust_state ts)
804 switch (ts) {
805 case TS_UNKNOWN: return '-';
806 case TS_UNTRUSTED: return '!';
807 case TS_TEMP_TRUSTED: return '!';
808 case TS_TRUSTED: return 'v';
809 case TS_VERIFIED: return 'V';
810 default: return 'X';
814 static void
815 redraw_modeline(struct tab *tab)
817 struct buffer *buffer;
818 double pct;
819 int x, y, max_x, max_y;
820 const char *mode;
821 const char *spin = "-\\|/";
823 buffer = current_buffer();
824 mode = buffer->page.name;
826 werase(modeline);
827 wattr_on(modeline, modeline_face.background, NULL);
828 wmove(modeline, 0, 0);
830 wprintw(modeline, "-%c%c- %s ",
831 spin[tab->loading_anim_step],
832 trust_status_char(tab->trust),
833 mode == NULL ? "(none)" : mode);
835 pct = (buffer->line_off + buffer->curs_y) * 100.0
836 / buffer->line_max;
838 if (buffer->line_max <= (size_t)body_lines)
839 wprintw(modeline, "All ");
840 else if (buffer->line_off == 0)
841 wprintw(modeline, "Top ");
842 else if (buffer->line_off + body_lines >= buffer->line_max)
843 wprintw(modeline, "Bottom ");
844 else
845 wprintw(modeline, "%.0f%% ", pct);
847 wprintw(modeline, "%zu/%zu %s ",
848 buffer->line_off + buffer->curs_y,
849 buffer->line_max,
850 tab->hist_cur->h);
852 getyx(modeline, y, x);
853 getmaxyx(modeline, max_y, max_x);
855 (void)y;
856 (void)max_y;
858 for (; x < max_x; ++x)
859 waddstr(modeline, "-");
861 wattr_off(modeline, modeline_face.background, NULL);
864 static void
865 redraw_minibuffer(void)
867 wattr_on(echoarea, minibuffer_face.background, NULL);
868 werase(echoarea);
870 if (in_minibuffer)
871 do_redraw_minibuffer();
872 else
873 do_redraw_echoarea();
875 if (in_minibuffer == MB_COMPREAD)
876 do_redraw_minibuffer_compl();
878 wattr_off(echoarea, minibuffer_face.background, NULL);
881 static void
882 do_redraw_echoarea(void)
884 struct vline *vl;
886 if (ministate.curmesg != NULL)
887 wprintw(echoarea, "%s", ministate.curmesg);
888 else if (*keybuf != '\0')
889 waddstr(echoarea, keybuf);
890 else {
891 /* If nothing else, show the URL at point */
892 vl = current_tab->buffer.current_line;
893 if (vl != NULL && vl->parent->type == LINE_LINK)
894 waddstr(echoarea, vl->parent->alt);
898 static void
899 do_redraw_minibuffer(void)
901 struct buffer *cmplbuf, *buffer;
902 size_t off_y, off_x = 0;
903 const char *start, *c;
905 cmplbuf = &ministate.compl.buffer;
906 buffer = &ministate.buffer;
907 (void)off_y; /* unused, set by getyx */
909 wmove(echoarea, 0, 0);
911 if (in_minibuffer == MB_COMPREAD)
912 wprintw(echoarea, "(%2zu) ",
913 cmplbuf->line_max);
915 wprintw(echoarea, "%s", ministate.prompt);
916 if (ministate.hist_cur != NULL)
917 wprintw(echoarea, "(%zu/%zu) ",
918 ministate.hist_off + 1,
919 ministate.history->len);
921 getyx(echoarea, off_y, off_x);
923 start = ministate.hist_cur != NULL
924 ? ministate.hist_cur->h
925 : ministate.buf;
926 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
927 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
928 start = utf8_next_cp(start);
931 waddstr(echoarea, start);
933 if (ministate.curmesg != NULL)
934 wprintw(echoarea, " [%s]", ministate.curmesg);
936 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
939 static void
940 do_redraw_minibuffer_compl(void)
942 redraw_window(minibuffer, 0, 10, COLS, 1,
943 &ministate.compl.buffer);
946 /*
947 * Place the cursor in the right ncurses window. If soft is 1, use
948 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
949 * wrefresh.
950 */
951 static void
952 place_cursor(int soft)
954 int (*touch)(WINDOW *);
956 if (soft)
957 touch = wnoutrefresh;
958 else
959 touch = wrefresh;
961 if (in_minibuffer) {
962 if (side_window & SIDE_WINDOW_LEFT)
963 touch(help);
964 if (side_window & SIDE_WINDOW_BOTTOM)
965 touch(download);
966 touch(body);
967 touch(echoarea);
968 } else if (in_side_window & SIDE_WINDOW_LEFT) {
969 touch(body);
970 touch(echoarea);
971 if (in_side_window & SIDE_WINDOW_BOTTOM)
972 touch(download);
973 touch(help);
974 } else if (in_side_window & SIDE_WINDOW_BOTTOM) {
975 touch(body);
976 touch(echoarea);
977 if (in_side_window & SIDE_WINDOW_LEFT)
978 touch(help);
979 touch(download);
980 } else {
981 if (side_window & SIDE_WINDOW_LEFT)
982 touch(help);
983 if (side_window & SIDE_WINDOW_BOTTOM)
984 touch(download);
985 touch(echoarea);
986 touch(body);
990 static void
991 redraw_tab(struct tab *tab)
993 if (too_small)
994 return;
996 if (side_window & SIDE_WINDOW_LEFT) {
997 redraw_help();
998 wnoutrefresh(help);
1001 if (side_window & SIDE_WINDOW_BOTTOM) {
1002 redraw_download();
1003 wnoutrefresh(download);
1006 if (show_tab_bar)
1007 redraw_tabline();
1009 redraw_body(tab);
1010 redraw_modeline(tab);
1011 redraw_minibuffer();
1013 wnoutrefresh(tabline);
1014 wnoutrefresh(modeline);
1016 if (in_minibuffer == MB_COMPREAD)
1017 wnoutrefresh(minibuffer);
1019 place_cursor(1);
1021 doupdate();
1023 if (set_title)
1024 dprintf(1, "\033]2;%s - Telescope\a",
1025 current_tab->buffer.page.title);
1028 void
1029 start_loading_anim(struct tab *tab)
1031 if (tab->loading_anim)
1032 return;
1033 tab->loading_anim = 1;
1034 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1035 evtimer_add(&tab->loadingev, &loadingev_timer);
1038 static void
1039 update_loading_anim(int fd, short ev, void *d)
1041 struct tab *tab = d;
1043 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1045 if (tab == current_tab) {
1046 redraw_modeline(tab);
1047 wrefresh(modeline);
1048 wrefresh(body);
1049 if (in_minibuffer)
1050 wrefresh(echoarea);
1053 evtimer_add(&tab->loadingev, &loadingev_timer);
1056 static void
1057 stop_loading_anim(struct tab *tab)
1059 if (!tab->loading_anim)
1060 return;
1061 evtimer_del(&tab->loadingev);
1062 tab->loading_anim = 0;
1063 tab->loading_anim_step = 0;
1065 if (tab != current_tab)
1066 return;
1068 redraw_modeline(tab);
1070 wrefresh(modeline);
1071 wrefresh(body);
1072 if (in_minibuffer)
1073 wrefresh(echoarea);
1076 int
1077 ui_print_colors(void)
1079 int colors = 0, pairs = 0, can_change = 0;
1080 int columns = 16, lines, color, i, j;
1082 initscr();
1083 if (has_colors()) {
1084 start_color();
1085 use_default_colors();
1087 colors = COLORS;
1088 pairs = COLOR_PAIRS;
1089 can_change = can_change_color();
1091 endwin();
1093 printf("Term info:\n");
1094 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1095 getenv("TERM"), colors, pairs, can_change);
1096 printf("\n");
1098 if (colors == 0) {
1099 printf("No color support\n");
1100 return 0;
1103 printf("Available colors:\n\n");
1104 lines = (colors - 1) / columns + 1;
1105 color = 0;
1106 for (i = 0; i < lines; ++i) {
1107 for (j = 0; j < columns; ++j, ++color) {
1108 printf("\033[0;38;5;%dm %03d", color, color);
1110 printf("\n");
1113 printf("\033[0m");
1114 fflush(stdout);
1115 return 0;
1118 int
1119 ui_init()
1121 setlocale(LC_ALL, "");
1123 if (TAILQ_EMPTY(&global_map.m)) {
1124 fprintf(stderr, "no keys defined!\n");
1125 return 0;
1128 minibuffer_init();
1130 /* initialize download window */
1131 TAILQ_INIT(&downloadwin.head);
1132 TAILQ_INIT(&downloadwin.page.head);
1134 /* initialize help window */
1135 TAILQ_INIT(&helpwin.head);
1136 TAILQ_INIT(&helpwin.page.head);
1138 base_map = &global_map;
1139 current_map = &global_map;
1141 initscr();
1143 if (enable_colors) {
1144 if (has_colors()) {
1145 start_color();
1146 use_default_colors();
1147 } else
1148 enable_colors = 0;
1151 config_apply_style();
1153 raw();
1154 noecho();
1155 nonl();
1156 intrflush(stdscr, FALSE);
1158 if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1159 return 0;
1160 if ((body = newwin(1, 1, 0, 0)) == NULL)
1161 return 0;
1162 if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1163 return 0;
1164 if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1165 return 0;
1166 if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1167 return 0;
1168 if ((download = newwin(1, 1, 0, 0)) == NULL)
1169 return 0;
1170 if ((help = newwin(1, 1, 0, 0)) == NULL)
1171 return 0;
1173 wbkgd(body, body_face.body);
1174 wbkgd(download, download_face.background);
1175 wbkgd(echoarea, minibuffer_face.background);
1177 update_x_offset();
1179 keypad(body, TRUE);
1180 scrollok(body, FALSE);
1182 /* non-blocking input */
1183 wtimeout(body, 0);
1184 wtimeout(help, 0);
1186 mvwprintw(body, 0, 0, "");
1188 return 1;
1191 void
1192 ui_main_loop(void)
1194 evtimer_set(&resizeev, handle_resize, NULL);
1196 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1197 event_add(&stdioev, NULL);
1199 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1200 signal_add(&winchev, NULL);
1202 switch_to_tab(current_tab);
1203 rearrange_windows();
1206 void
1207 ui_on_tab_loaded(struct tab *tab)
1209 stop_loading_anim(tab);
1210 message("Loaded %s", tab->hist_cur->h);
1212 if (tab->hist_cur->current_off != 0 &&
1213 tab->buffer.current_line == TAILQ_FIRST(&tab->buffer.head)) {
1214 set_scroll_position(tab, tab->hist_cur->line_off,
1215 tab->hist_cur->current_off);
1216 redraw_tab(tab);
1217 return;
1220 if (show_tab_bar)
1221 redraw_tabline();
1223 wrefresh(tabline);
1224 place_cursor(0);
1227 void
1228 ui_on_tab_refresh(struct tab *tab)
1230 wrap_page(&tab->buffer, body_cols);
1231 if (tab == current_tab)
1232 redraw_tab(tab);
1233 else
1234 tab->flags |= TAB_URGENT;
1237 void
1238 ui_on_download_refresh(void)
1240 if (side_window & SIDE_WINDOW_BOTTOM) {
1241 recompute_downloads();
1242 redraw_tab(current_tab);
1246 const char *
1247 ui_keyname(int k)
1249 return keyname(k);
1252 void
1253 ui_toggle_side_window(int kind)
1255 if (in_side_window & kind)
1256 ui_other_window();
1258 side_window ^= kind;
1259 if (side_window & SIDE_WINDOW_LEFT)
1260 recompute_help();
1261 if (side_window & SIDE_WINDOW_BOTTOM)
1262 recompute_downloads();
1265 * ugly hack, but otherwise the window doesn't get updated
1266 * until I call rearrange_windows a second time (e.g. via
1267 * C-l). I will be happy to know why something like this is
1268 * needed.
1270 rearrange_windows();
1271 rearrange_windows();
1274 void
1275 ui_show_downloads_pane(void)
1277 if (!(side_window & SIDE_WINDOW_BOTTOM))
1278 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1281 void
1282 ui_schedule_redraw(void)
1284 should_rearrange_windows = 1;
1287 void
1288 ui_require_input(struct tab *tab, int hide, int proto)
1290 void (*fn)(void);
1292 if (proto == PROTO_GEMINI)
1293 fn = ir_select_gemini;
1294 else if (proto == PROTO_GOPHER)
1295 fn = ir_select_gopher;
1296 else
1297 abort();
1299 /* TODO: hard-switching to another tab is ugly */
1300 switch_to_tab(tab);
1302 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1303 &ir_history, NULL, NULL);
1304 strlcpy(ministate.prompt, "Input required: ",
1305 sizeof(ministate.prompt));
1306 redraw_tab(tab);
1309 void
1310 ui_after_message_hook(void)
1312 redraw_minibuffer();
1313 place_cursor(0);
1316 void
1317 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1318 struct tab *data)
1320 yornp(prompt, fn, data);
1321 redraw_tab(current_tab);
1324 void
1325 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1326 struct tab *data, const char *input)
1328 minibuffer_read(prompt, fn, data);
1330 if (input != NULL) {
1331 strlcpy(ministate.buf, input, sizeof(ministate.buf));
1332 cmd_move_end_of_line(&ministate.buffer);
1335 redraw_tab(current_tab);
1338 void
1339 ui_other_window(void)
1341 if (in_side_window & SIDE_WINDOW_LEFT &&
1342 side_window & SIDE_WINDOW_BOTTOM)
1343 in_side_window = SIDE_WINDOW_BOTTOM;
1344 else if (in_side_window)
1345 in_side_window = 0;
1346 else if (!in_side_window && side_window & SIDE_WINDOW_LEFT)
1347 in_side_window = SIDE_WINDOW_LEFT;
1348 else if (!in_side_window && side_window)
1349 in_side_window = SIDE_WINDOW_BOTTOM;
1350 else
1351 message("No other window to select");
1354 void
1355 ui_suspend(void)
1357 endwin();
1359 kill(getpid(), SIGSTOP);
1361 refresh();
1362 clear();
1363 rearrange_windows();
1366 void
1367 ui_end(void)
1369 endwin();