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 tab->buffer.current_line = vl;
149 return;
153 if (!topfound) {
154 tab->buffer.top_line = TAILQ_FIRST(&tab->buffer.head);
155 tab->buffer.current_line = tab->buffer.top_line;
158 if (!curfound)
159 tab->buffer.current_line = tab->buffer.top_line;
162 void
163 get_scroll_position(struct tab *tab, size_t *top, size_t *cur)
165 struct line *l;
166 int topfound = 0;
168 *top = 0;
169 *cur = 0;
171 if (tab->buffer.top_line == NULL ||
172 tab->buffer.current_line == NULL)
173 return;
175 TAILQ_FOREACH(l, &tab->buffer.page.head, lines) {
176 if (tab->buffer.top_line->parent == l)
177 topfound = 1;
178 if (tab->buffer.current_line->parent == l)
179 return;
181 if (!topfound)
182 (*top)++;
183 (*cur)++;
187 void
188 save_excursion(struct excursion *place, struct buffer *buffer)
190 place->curs_x = buffer->curs_x;
191 place->curs_y = buffer->curs_y;
192 place->line_off = buffer->line_off;
193 place->top_line = buffer->top_line;
194 place->current_line = buffer->current_line;
195 place->cpoff = buffer->cpoff;
198 void
199 restore_excursion(struct excursion *place, struct buffer *buffer)
201 buffer->curs_x = place->curs_x;
202 buffer->curs_y = place->curs_y;
203 buffer->line_off = place->line_off;
204 buffer->top_line = place->top_line;
205 buffer->current_line = place->current_line;
206 buffer->cpoff = place->cpoff;
209 static void
210 restore_curs_x(struct buffer *buffer)
212 struct vline *vl;
213 const char *prfx, *text;
215 vl = buffer->current_line;
216 if (vl == NULL || vl->line == NULL)
217 buffer->curs_x = buffer->cpoff = 0;
218 else if (vl->parent->data != NULL) {
219 text = vl->parent->data;
220 buffer->curs_x = utf8_snwidth(text + 1, buffer->cpoff) + 1;
221 } else
222 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
224 /* small hack: don't olivetti-mode the download pane */
225 if (buffer != &downloadwin)
226 buffer->curs_x += x_offset;
228 if (vl == NULL)
229 return;
231 if (vl->parent->data != NULL)
232 buffer->curs_x += utf8_swidth_between(vl->parent->line,
233 vl->parent->data);
234 else {
235 prfx = line_prefixes[vl->parent->type].prfx1;
236 buffer->curs_x += utf8_swidth(prfx);
240 void
241 global_key_unbound(void)
243 message("%s is undefined", keybuf);
246 struct buffer *
247 current_buffer(void)
249 if (in_minibuffer)
250 return &ministate.buffer;
251 if (in_side_window & SIDE_WINDOW_LEFT)
252 return &helpwin;
253 if (in_side_window & SIDE_WINDOW_BOTTOM)
254 return &downloadwin;
255 return &current_tab->buffer;
258 static int
259 readkey(void)
261 uint32_t state = 0;
263 if ((thiskey.key = wgetch(body)) == ERR)
264 return 0;
266 thiskey.meta = thiskey.key == '\e';
267 if (thiskey.meta) {
268 thiskey.key = wgetch(body);
269 if (thiskey.key == ERR || thiskey.key == '\e') {
270 thiskey.meta = 0;
271 thiskey.key = '\e';
275 thiskey.cp = 0;
277 if ((unsigned int)thiskey.key >= UINT8_MAX)
278 return 1;
280 while (1) {
281 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
282 break;
283 if ((thiskey.key = wgetch(body)) == ERR) {
284 message("Error decoding user input");
285 return 0;
289 return 1;
292 static void
293 dispatch_stdio(int fd, short ev, void *d)
295 int lk;
296 const char *keyname;
297 char tmp[5] = {0};
299 /* TODO: schedule a redraw? */
300 if (too_small)
301 return;
303 if (!readkey())
304 return;
306 if (keybuf[0] != '\0')
307 strlcat(keybuf, " ", sizeof(keybuf));
308 if (thiskey.meta)
309 strlcat(keybuf, "M-", sizeof(keybuf));
310 if (thiskey.cp != 0) {
311 utf8_encode(thiskey.cp, tmp);
312 strlcat(keybuf, tmp, sizeof(keybuf));
313 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
314 strlcat(keybuf, keyname, sizeof(keybuf));
315 } else {
316 tmp[0] = thiskey.key;
317 strlcat(keybuf, tmp, sizeof(keybuf));
320 lk = lookup_key(&current_map, &thiskey, current_buffer());
321 if (lk == LK_UNBOUND) {
322 if (current_map->unhandled_input != NULL)
323 current_map->unhandled_input();
324 else
325 global_key_unbound();
327 if (lk != LK_ADVANCED_MAP) {
328 current_map = base_map;
329 strlcpy(keybuf, "", sizeof(keybuf));
332 if (side_window & SIDE_WINDOW_LEFT)
333 recompute_help();
335 if (should_rearrange_windows)
336 rearrange_windows();
337 redraw_tab(current_tab);
340 static void
341 handle_resize(int sig, short ev, void *d)
343 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
344 event_del(&resizeev);
346 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
347 evtimer_add(&resizeev, &resize_timer);
350 static void
351 handle_resize_nodelay(int s, short ev, void *d)
353 endwin();
354 refresh();
355 clear();
357 rearrange_windows();
360 static inline int
361 should_show_tab_bar(void)
363 if (tab_bar_show == -1)
364 return 0;
365 if (tab_bar_show == 0)
366 return 1;
368 return TAILQ_NEXT(TAILQ_FIRST(&tabshead), tabs) != NULL;
371 static void
372 rearrange_windows(void)
374 int lines;
375 int minibuffer_lines;
377 should_rearrange_windows = 0;
378 show_tab_bar = should_show_tab_bar();
380 lines = LINES;
382 /* 3 lines for the ui and 12 for the */
383 if ((too_small = lines < 15)) {
384 erase();
385 printw("Window too small.");
386 refresh();
387 return;
390 /* move and resize the windows, in reverse order! */
392 if (in_minibuffer == MB_COMPREAD) {
393 minibuffer_lines = MIN(10, lines/2);
394 mvwin(minibuffer, lines - minibuffer_lines, 0);
395 wresize(minibuffer, minibuffer_lines, COLS);
396 lines -= minibuffer_lines;
398 wrap_page(&ministate.compl.buffer, COLS);
401 mvwin(echoarea, --lines, 0);
402 wresize(echoarea, 1, COLS);
404 mvwin(modeline, --lines, 0);
405 wresize(modeline, 1, COLS);
407 if (side_window & SIDE_WINDOW_BOTTOM) {
408 download_lines = MIN(5, lines/2);
409 download_cols = COLS;
410 mvwin(download, lines - download_lines, 0);
411 wresize(download, download_lines, download_cols);
412 lines -= download_lines;
414 wrap_page(&downloadwin, download_cols);
417 body_lines = show_tab_bar ? --lines : lines;
418 body_cols = COLS;
420 /*
421 * Here we make the assumption that show_tab_bar is either 0
422 * or 1, and reuse that as argument to mvwin.
423 */
424 if (side_window & SIDE_WINDOW_LEFT) {
425 help_cols = 0.3 * COLS;
426 help_lines = lines;
427 mvwin(help, show_tab_bar, 0);
428 wresize(help, help_lines, help_cols);
430 wrap_page(&helpwin, help_cols);
432 body_cols = COLS - help_cols - 1;
433 mvwin(body, show_tab_bar, help_cols);
434 } else
435 mvwin(body, show_tab_bar, 0);
437 update_x_offset();
438 wresize(body, body_lines, body_cols);
440 if (show_tab_bar)
441 wresize(tabline, 1, COLS);
443 wrap_page(&current_tab->buffer, body_cols);
444 redraw_tab(current_tab);
447 static void
448 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
449 const char **prfx_ret, const char **text_ret)
451 int type, i, cont, width;
452 char *space, *t;
454 if ((*text_ret = vl->line) == NULL)
455 *text_ret = "";
457 cont = vl->flags & L_CONTINUATION;
458 type = vl->parent->type;
459 if (!cont)
460 *prfx_ret = line_prefixes[type].prfx1;
461 else
462 *prfx_ret = line_prefixes[type].prfx2;
464 space = vl->parent->data;
465 if (!emojify_link || type != LINE_LINK || space == NULL)
466 return;
468 if (cont) {
469 memset(buf, 0, len);
470 width = utf8_swidth_between(vl->parent->line, space);
471 for (i = 0; i < width + 1; ++i)
472 strlcat(buf, " ", len);
473 } else {
474 strlcpy(buf, *text_ret, len);
475 if ((t = strchr(buf, ' ')) != NULL)
476 *t = '\0';
477 strlcat(buf, " ", len);
479 /* skip the emoji */
480 *text_ret += (space - vl->parent->line) + 1;
483 *prfx_ret = buf;
486 static inline void
487 print_vline_descr(int width, WINDOW *window, struct vline *vl)
489 int x, y, goal;
491 switch (vl->parent->type) {
492 case LINE_COMPL:
493 case LINE_COMPL_CURRENT:
494 goal = width/2;
495 break;
496 case LINE_DOWNLOAD:
497 case LINE_DOWNLOAD_DONE:
498 goal = width/4;
499 break;
500 case LINE_HELP:
501 goal = 8;
502 break;
503 default:
504 return;
507 if (vl->parent->alt == NULL)
508 return;
510 (void)y;
511 getyx(window, y, x);
513 if (goal <= x)
514 wprintw(window, " ");
515 for (; goal > x; ++x)
516 wprintw(window, " ");
518 wprintw(window, "%s", vl->parent->alt);
521 /*
522 * Core part of the rendering. It prints a vline starting from the
523 * current cursor position. Printing a vline consists of skipping
524 * `off' columns (for olivetti-mode), print the correct prefix (which
525 * may be the emoji in case of emojified links-lines), printing the
526 * text itself, filling until width - off and filling off columns
527 * again.
528 */
529 static void
530 print_vline(int off, int width, WINDOW *window, struct vline *vl)
532 /*
533 * Believe me or not, I've seen emoji ten code points long!
534 * That means, to stay large, 4*10 bytes + NUL.
535 */
536 char emojibuf[41] = {0};
537 const char *text, *prfx;
538 struct line_face *f;
539 int i, left, x, y;
541 f = &line_faces[vl->parent->type];
543 /* unused, set by getyx */
544 (void)y;
546 if (vl->parent->type == LINE_FRINGE && fringe_ignore_offset)
547 off = 0;
549 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
551 wattr_on(window, body_face.left, NULL);
552 for (i = 0; i < off; i++)
553 waddch(window, ' ');
554 wattr_off(window, body_face.left, NULL);
556 wattr_on(window, f->prefix, NULL);
557 wprintw(window, "%s", prfx);
558 wattr_off(window, f->prefix, NULL);
560 wattr_on(window, f->text, NULL);
561 wprintw(window, "%s", text);
562 print_vline_descr(width, window, vl);
563 wattr_off(window, f->text, NULL);
565 getyx(window, y, x);
567 left = width - x;
569 wattr_on(window, f->trail, NULL);
570 for (i = 0; i < left - off; ++i)
571 waddch(window, ' ');
572 wattr_off(window, f->trail, NULL);
574 wattr_on(window, body_face.right, NULL);
575 for (i = 0; i < off; i++)
576 waddch(window, ' ');
577 wattr_off(window, body_face.right, NULL);
581 static void
582 redraw_tabline(void)
584 struct tab *tab;
585 size_t toskip, ots, tabwidth, space, x;
586 int current, y, truncated, pair;
587 const char *title;
588 char buf[25];
590 x = 0;
592 /* unused, but setted by a getyx */
593 (void)y;
595 tabwidth = sizeof(buf)+1;
596 space = COLS-2;
598 toskip = 0;
599 TAILQ_FOREACH(tab, &tabshead, tabs) {
600 toskip++;
601 if (tab == current_tab)
602 break;
605 if (toskip * tabwidth <= space)
606 toskip = 0;
607 else {
608 ots = toskip;
609 toskip--;
610 while (toskip != 0 &&
611 (ots - toskip+1) * tabwidth < space)
612 toskip--;
615 werase(tabline);
616 wattr_on(tabline, tab_face.background, NULL);
617 wprintw(tabline, toskip == 0 ? " " : "<");
618 wattr_off(tabline, tab_face.background, NULL);
620 truncated = 0;
621 TAILQ_FOREACH(tab, &tabshead, tabs) {
622 if (truncated)
623 break;
624 if (toskip != 0) {
625 toskip--;
626 continue;
629 getyx(tabline, y, x);
630 if (x + sizeof(buf)+2 >= (size_t)COLS)
631 truncated = 1;
633 current = tab == current_tab;
635 if (*(title = tab->buffer.page.title) == '\0')
636 title = tab->hist_cur->h;
638 if (tab->flags & TAB_URGENT)
639 strlcpy(buf, "!", sizeof(buf));
640 else
641 strlcpy(buf, " ", sizeof(buf));
643 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
644 /* truncation happens */
645 strlcpy(&buf[sizeof(buf)-4], "...", 4);
646 } else {
647 /* pad with spaces */
648 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
649 /* nop */ ;
652 pair = current ? tab_face.current : tab_face.tab;
653 wattr_on(tabline, pair, NULL);
654 wprintw(tabline, "%s", buf);
655 wattr_off(tabline, pair, NULL);
657 wattr_on(tabline, tab_face.background, NULL);
658 if (TAILQ_NEXT(tab, tabs) != NULL)
659 wprintw(tabline, "┃");
660 wattr_off(tabline, tab_face.background, NULL);
663 wattr_on(tabline, tab_face.background, NULL);
664 for (; x < (size_t)COLS; ++x)
665 waddch(tabline, ' ');
666 if (truncated)
667 mvwprintw(tabline, 0, COLS-1, ">");
668 wattr_off(tabline, tab_face.background, NULL);
671 /*
672 * Compute the first visible line around vl. Try to search forward
673 * until the end of the buffer; if a visible line is not found, search
674 * backward. Return NULL if no viable line was found.
675 */
676 struct vline *
677 adjust_line(struct vline *vl, struct buffer *buffer)
679 struct vline *t;
681 if (vl == NULL)
682 return NULL;
684 if (!(vl->parent->flags & L_HIDDEN))
685 return vl;
687 /* search forward */
688 for (t = vl;
689 t != NULL && t->parent->flags & L_HIDDEN;
690 t = TAILQ_NEXT(t, vlines))
691 ; /* nop */
693 if (t != NULL)
694 return t;
696 /* search backward */
697 for (t = vl;
698 t != NULL && t->parent->flags & L_HIDDEN;
699 t = TAILQ_PREV(t, vhead, vlines))
700 ; /* nop */
702 return t;
705 static void
706 redraw_window(WINDOW *win, int off, int height, int width,
707 int show_fringe, struct buffer *buffer)
709 struct vline *vl;
710 int onscreen = 0, l = 0;
712 restore_curs_x(buffer);
714 /*
715 * TODO: ignoring buffer->force_update and always
716 * re-rendering. In theory we can recompute the y position
717 * without a re-render, and optimize here. It's not the only
718 * optimisation possible here, wscrl wolud also be an
719 * interesting one.
720 */
722 again:
723 werase(win);
724 buffer->curs_y = 0;
726 if (TAILQ_EMPTY(&buffer->head))
727 goto end;
729 if (buffer->top_line == NULL)
730 buffer->top_line = TAILQ_FIRST(&buffer->head);
732 buffer->top_line = adjust_line(buffer->top_line, buffer);
733 if (buffer->top_line == NULL)
734 goto end;
736 buffer->current_line = adjust_line(buffer->current_line, buffer);
738 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
739 if (vl->parent->flags & L_HIDDEN)
740 continue;
742 wmove(win, l, 0);
743 print_vline(off, width, win, vl);
745 if (vl == buffer->current_line)
746 onscreen = 1;
748 if (!onscreen)
749 buffer->curs_y++;
751 l++;
752 if (l == height)
753 break;
756 if (!onscreen) {
757 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
758 if (vl == buffer->current_line)
759 break;
760 if (vl->parent->flags & L_HIDDEN)
761 continue;
762 buffer->line_off++;
763 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
766 if (vl != NULL)
767 goto again;
770 buffer->last_line_off = buffer->line_off;
771 buffer->force_redraw = 0;
772 end:
773 for (; show_fringe && l < height; l++)
774 print_vline(off, width, win, &fringe);
776 wmove(win, buffer->curs_y, buffer->curs_x);
779 static void
780 redraw_download(void)
782 redraw_window(download, 0, download_lines, COLS, 0, &downloadwin);
785 static void
786 redraw_help(void)
788 redraw_window(help, 0, help_lines, help_cols, 1, &helpwin);
791 static void
792 redraw_body(struct tab *tab)
794 static struct tab *last_tab;
796 if (last_tab != tab)
797 tab->buffer.force_redraw =1;
798 last_tab = tab;
800 redraw_window(body, x_offset, body_lines, body_cols, 1, &tab->buffer);
803 static inline char
804 trust_status_char(enum trust_state ts)
806 switch (ts) {
807 case TS_UNKNOWN: return '-';
808 case TS_UNTRUSTED: return '!';
809 case TS_TEMP_TRUSTED: return '!';
810 case TS_TRUSTED: return 'v';
811 case TS_VERIFIED: return 'V';
812 default: return 'X';
816 static void
817 redraw_modeline(struct tab *tab)
819 struct buffer *buffer;
820 double pct;
821 int x, y, max_x, max_y;
822 const char *mode;
823 const char *spin = "-\\|/";
825 buffer = current_buffer();
826 mode = buffer->page.name;
828 werase(modeline);
829 wattr_on(modeline, modeline_face.background, NULL);
830 wmove(modeline, 0, 0);
832 wprintw(modeline, "-%c%c- %s ",
833 spin[tab->loading_anim_step],
834 trust_status_char(tab->trust),
835 mode == NULL ? "(none)" : mode);
837 pct = (buffer->line_off + buffer->curs_y) * 100.0
838 / buffer->line_max;
840 if (buffer->line_max <= (size_t)body_lines)
841 wprintw(modeline, "All ");
842 else if (buffer->line_off == 0)
843 wprintw(modeline, "Top ");
844 else if (buffer->line_off + body_lines >= buffer->line_max)
845 wprintw(modeline, "Bottom ");
846 else
847 wprintw(modeline, "%.0f%% ", pct);
849 wprintw(modeline, "%zu/%zu %s ",
850 buffer->line_off + buffer->curs_y,
851 buffer->line_max,
852 tab->hist_cur->h);
854 getyx(modeline, y, x);
855 getmaxyx(modeline, max_y, max_x);
857 (void)y;
858 (void)max_y;
860 for (; x < max_x; ++x)
861 waddstr(modeline, "-");
863 wattr_off(modeline, modeline_face.background, NULL);
866 static void
867 redraw_minibuffer(void)
869 wattr_on(echoarea, minibuffer_face.background, NULL);
870 werase(echoarea);
872 if (in_minibuffer)
873 do_redraw_minibuffer();
874 else
875 do_redraw_echoarea();
877 if (in_minibuffer == MB_COMPREAD)
878 do_redraw_minibuffer_compl();
880 wattr_off(echoarea, minibuffer_face.background, NULL);
883 static void
884 do_redraw_echoarea(void)
886 struct vline *vl;
888 if (ministate.curmesg != NULL)
889 wprintw(echoarea, "%s", ministate.curmesg);
890 else if (*keybuf != '\0')
891 waddstr(echoarea, keybuf);
892 else {
893 /* If nothing else, show the URL at point */
894 vl = current_tab->buffer.current_line;
895 if (vl != NULL && vl->parent->type == LINE_LINK)
896 waddstr(echoarea, vl->parent->alt);
900 static void
901 do_redraw_minibuffer(void)
903 struct buffer *cmplbuf, *buffer;
904 size_t off_y, off_x = 0;
905 const char *start, *c;
907 cmplbuf = &ministate.compl.buffer;
908 buffer = &ministate.buffer;
909 (void)off_y; /* unused, set by getyx */
911 wmove(echoarea, 0, 0);
913 if (in_minibuffer == MB_COMPREAD)
914 wprintw(echoarea, "(%2zu) ",
915 cmplbuf->line_max);
917 wprintw(echoarea, "%s", ministate.prompt);
918 if (ministate.hist_cur != NULL)
919 wprintw(echoarea, "(%zu/%zu) ",
920 ministate.hist_off + 1,
921 ministate.history->len);
923 getyx(echoarea, off_y, off_x);
925 start = ministate.hist_cur != NULL
926 ? ministate.hist_cur->h
927 : ministate.buf;
928 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
929 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
930 start = utf8_next_cp(start);
933 waddstr(echoarea, start);
935 if (ministate.curmesg != NULL)
936 wprintw(echoarea, " [%s]", ministate.curmesg);
938 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
941 static void
942 do_redraw_minibuffer_compl(void)
944 redraw_window(minibuffer, 0, 10, COLS, 1,
945 &ministate.compl.buffer);
948 /*
949 * Place the cursor in the right ncurses window. If soft is 1, use
950 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
951 * wrefresh.
952 */
953 static void
954 place_cursor(int soft)
956 int (*touch)(WINDOW *);
958 if (soft)
959 touch = wnoutrefresh;
960 else
961 touch = wrefresh;
963 if (in_minibuffer) {
964 if (side_window & SIDE_WINDOW_LEFT)
965 touch(help);
966 if (side_window & SIDE_WINDOW_BOTTOM)
967 touch(download);
968 touch(body);
969 touch(echoarea);
970 } else if (in_side_window & SIDE_WINDOW_LEFT) {
971 touch(body);
972 touch(echoarea);
973 if (in_side_window & SIDE_WINDOW_BOTTOM)
974 touch(download);
975 touch(help);
976 } else if (in_side_window & SIDE_WINDOW_BOTTOM) {
977 touch(body);
978 touch(echoarea);
979 if (in_side_window & SIDE_WINDOW_LEFT)
980 touch(help);
981 touch(download);
982 } else {
983 if (side_window & SIDE_WINDOW_LEFT)
984 touch(help);
985 if (side_window & SIDE_WINDOW_BOTTOM)
986 touch(download);
987 touch(echoarea);
988 touch(body);
992 static void
993 redraw_tab(struct tab *tab)
995 if (too_small)
996 return;
998 if (side_window & SIDE_WINDOW_LEFT) {
999 redraw_help();
1000 wnoutrefresh(help);
1003 if (side_window & SIDE_WINDOW_BOTTOM) {
1004 redraw_download();
1005 wnoutrefresh(download);
1008 if (show_tab_bar)
1009 redraw_tabline();
1011 redraw_body(tab);
1012 redraw_modeline(tab);
1013 redraw_minibuffer();
1015 wnoutrefresh(tabline);
1016 wnoutrefresh(modeline);
1018 if (in_minibuffer == MB_COMPREAD)
1019 wnoutrefresh(minibuffer);
1021 place_cursor(1);
1023 doupdate();
1025 if (set_title)
1026 dprintf(1, "\033]2;%s - Telescope\a",
1027 current_tab->buffer.page.title);
1030 void
1031 start_loading_anim(struct tab *tab)
1033 if (tab->loading_anim)
1034 return;
1035 tab->loading_anim = 1;
1036 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1037 evtimer_add(&tab->loadingev, &loadingev_timer);
1040 static void
1041 update_loading_anim(int fd, short ev, void *d)
1043 struct tab *tab = d;
1045 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1047 if (tab == current_tab) {
1048 redraw_modeline(tab);
1049 wrefresh(modeline);
1050 wrefresh(body);
1051 if (in_minibuffer)
1052 wrefresh(echoarea);
1055 evtimer_add(&tab->loadingev, &loadingev_timer);
1058 static void
1059 stop_loading_anim(struct tab *tab)
1061 if (!tab->loading_anim)
1062 return;
1063 evtimer_del(&tab->loadingev);
1064 tab->loading_anim = 0;
1065 tab->loading_anim_step = 0;
1067 if (tab != current_tab)
1068 return;
1070 redraw_modeline(tab);
1072 wrefresh(modeline);
1073 wrefresh(body);
1074 if (in_minibuffer)
1075 wrefresh(echoarea);
1078 int
1079 ui_print_colors(void)
1081 int colors = 0, pairs = 0, can_change = 0;
1082 int columns = 16, lines, color, i, j;
1084 initscr();
1085 if (has_colors()) {
1086 start_color();
1087 use_default_colors();
1089 colors = COLORS;
1090 pairs = COLOR_PAIRS;
1091 can_change = can_change_color();
1093 endwin();
1095 printf("Term info:\n");
1096 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1097 getenv("TERM"), colors, pairs, can_change);
1098 printf("\n");
1100 if (colors == 0) {
1101 printf("No color support\n");
1102 return 0;
1105 printf("Available colors:\n\n");
1106 lines = (colors - 1) / columns + 1;
1107 color = 0;
1108 for (i = 0; i < lines; ++i) {
1109 for (j = 0; j < columns; ++j, ++color) {
1110 printf("\033[0;38;5;%dm %03d", color, color);
1112 printf("\n");
1115 printf("\033[0m");
1116 fflush(stdout);
1117 return 0;
1120 int
1121 ui_init()
1123 setlocale(LC_ALL, "");
1125 if (TAILQ_EMPTY(&global_map.m)) {
1126 fprintf(stderr, "no keys defined!\n");
1127 return 0;
1130 minibuffer_init();
1132 /* initialize download window */
1133 TAILQ_INIT(&downloadwin.head);
1134 TAILQ_INIT(&downloadwin.page.head);
1136 /* initialize help window */
1137 TAILQ_INIT(&helpwin.head);
1138 TAILQ_INIT(&helpwin.page.head);
1140 base_map = &global_map;
1141 current_map = &global_map;
1143 initscr();
1145 if (enable_colors) {
1146 if (has_colors()) {
1147 start_color();
1148 use_default_colors();
1149 } else
1150 enable_colors = 0;
1153 config_apply_style();
1155 raw();
1156 noecho();
1157 nonl();
1158 intrflush(stdscr, FALSE);
1160 if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1161 return 0;
1162 if ((body = newwin(1, 1, 0, 0)) == NULL)
1163 return 0;
1164 if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1165 return 0;
1166 if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1167 return 0;
1168 if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1169 return 0;
1170 if ((download = newwin(1, 1, 0, 0)) == NULL)
1171 return 0;
1172 if ((help = newwin(1, 1, 0, 0)) == NULL)
1173 return 0;
1175 wbkgd(body, body_face.body);
1176 wbkgd(download, download_face.background);
1177 wbkgd(echoarea, minibuffer_face.background);
1179 update_x_offset();
1181 keypad(body, TRUE);
1182 scrollok(body, FALSE);
1184 /* non-blocking input */
1185 wtimeout(body, 0);
1186 wtimeout(help, 0);
1188 mvwprintw(body, 0, 0, "");
1190 return 1;
1193 void
1194 ui_main_loop(void)
1196 evtimer_set(&resizeev, handle_resize, NULL);
1198 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1199 event_add(&stdioev, NULL);
1201 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1202 signal_add(&winchev, NULL);
1204 switch_to_tab(current_tab);
1205 rearrange_windows();
1208 void
1209 ui_on_tab_loaded(struct tab *tab)
1211 stop_loading_anim(tab);
1212 message("Loaded %s", tab->hist_cur->h);
1214 if (tab->hist_cur->current_off != 0 &&
1215 tab->buffer.current_line == TAILQ_FIRST(&tab->buffer.head)) {
1216 set_scroll_position(tab, tab->hist_cur->line_off,
1217 tab->hist_cur->current_off);
1218 redraw_tab(tab);
1219 return;
1222 if (show_tab_bar)
1223 redraw_tabline();
1225 wrefresh(tabline);
1226 place_cursor(0);
1229 void
1230 ui_on_tab_refresh(struct tab *tab)
1232 wrap_page(&tab->buffer, body_cols);
1233 if (tab == current_tab)
1234 redraw_tab(tab);
1235 else
1236 tab->flags |= TAB_URGENT;
1239 void
1240 ui_on_download_refresh(void)
1242 if (side_window & SIDE_WINDOW_BOTTOM) {
1243 recompute_downloads();
1244 redraw_tab(current_tab);
1248 const char *
1249 ui_keyname(int k)
1251 return keyname(k);
1254 void
1255 ui_toggle_side_window(int kind)
1257 if (in_side_window & kind)
1258 ui_other_window();
1260 side_window ^= kind;
1261 if (side_window & SIDE_WINDOW_LEFT)
1262 recompute_help();
1263 if (side_window & SIDE_WINDOW_BOTTOM)
1264 recompute_downloads();
1267 * ugly hack, but otherwise the window doesn't get updated
1268 * until I call rearrange_windows a second time (e.g. via
1269 * C-l). I will be happy to know why something like this is
1270 * needed.
1272 rearrange_windows();
1273 rearrange_windows();
1276 void
1277 ui_show_downloads_pane(void)
1279 if (!(side_window & SIDE_WINDOW_BOTTOM))
1280 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1283 void
1284 ui_schedule_redraw(void)
1286 should_rearrange_windows = 1;
1289 void
1290 ui_require_input(struct tab *tab, int hide, int proto)
1292 void (*fn)(void);
1294 if (proto == PROTO_GEMINI)
1295 fn = ir_select_gemini;
1296 else if (proto == PROTO_GOPHER)
1297 fn = ir_select_gopher;
1298 else
1299 abort();
1301 /* TODO: hard-switching to another tab is ugly */
1302 switch_to_tab(tab);
1304 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1305 &ir_history, NULL, NULL);
1306 strlcpy(ministate.prompt, "Input required: ",
1307 sizeof(ministate.prompt));
1308 redraw_tab(tab);
1311 void
1312 ui_after_message_hook(void)
1314 redraw_minibuffer();
1315 place_cursor(0);
1318 void
1319 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1320 struct tab *data)
1322 yornp(prompt, fn, data);
1323 redraw_tab(current_tab);
1326 void
1327 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1328 struct tab *data, const char *input)
1330 minibuffer_read(prompt, fn, data);
1332 if (input != NULL) {
1333 strlcpy(ministate.buf, input, sizeof(ministate.buf));
1334 cmd_move_end_of_line(&ministate.buffer);
1337 redraw_tab(current_tab);
1340 void
1341 ui_other_window(void)
1343 if (in_side_window & SIDE_WINDOW_LEFT &&
1344 side_window & SIDE_WINDOW_BOTTOM)
1345 in_side_window = SIDE_WINDOW_BOTTOM;
1346 else if (in_side_window)
1347 in_side_window = 0;
1348 else if (!in_side_window && side_window & SIDE_WINDOW_LEFT)
1349 in_side_window = SIDE_WINDOW_LEFT;
1350 else if (!in_side_window && side_window)
1351 in_side_window = SIDE_WINDOW_BOTTOM;
1352 else
1353 message("No other window to select");
1356 void
1357 ui_suspend(void)
1359 endwin();
1361 kill(getpid(), SIGSTOP);
1363 refresh();
1364 clear();
1365 rearrange_windows();
1368 void
1369 ui_end(void)
1371 endwin();