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;
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 (i == cur) {
148 tab->buffer.current_line = vl;
149 return;
153 if (!topfound)
154 tab->buffer.top_line = TAILQ_FIRST(&tab->buffer.head);
156 tab->buffer.current_line = tab->buffer.top_line;
159 void
160 get_scroll_position(struct tab *tab, size_t *top, size_t *cur)
162 struct line *l;
163 int topfound = 0;
165 *top = 0;
166 *cur = 0;
168 if (tab->buffer.top_line == NULL ||
169 tab->buffer.current_line == NULL)
170 return;
172 TAILQ_FOREACH(l, &tab->buffer.page.head, lines) {
173 if (tab->buffer.top_line->parent == l)
174 topfound = 1;
175 if (tab->buffer.current_line->parent == l)
176 return;
178 if (!topfound)
179 (*top)++;
180 (*cur)++;
184 void
185 save_excursion(struct excursion *place, struct buffer *buffer)
187 place->curs_x = buffer->curs_x;
188 place->curs_y = buffer->curs_y;
189 place->line_off = buffer->line_off;
190 place->top_line = buffer->top_line;
191 place->current_line = buffer->current_line;
192 place->cpoff = buffer->cpoff;
195 void
196 restore_excursion(struct excursion *place, struct buffer *buffer)
198 buffer->curs_x = place->curs_x;
199 buffer->curs_y = place->curs_y;
200 buffer->line_off = place->line_off;
201 buffer->top_line = place->top_line;
202 buffer->current_line = place->current_line;
203 buffer->cpoff = place->cpoff;
206 static void
207 restore_curs_x(struct buffer *buffer)
209 struct vline *vl;
210 const char *prfx, *text;
212 vl = buffer->current_line;
213 if (vl == NULL || vl->line == NULL)
214 buffer->curs_x = buffer->cpoff = 0;
215 else if (vl->parent->data != NULL) {
216 text = vl->parent->data;
217 buffer->curs_x = utf8_snwidth(text + 1, buffer->cpoff) + 1;
218 } else
219 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
221 /* small hack: don't olivetti-mode the download pane */
222 if (buffer != &downloadwin)
223 buffer->curs_x += x_offset;
225 if (vl == NULL)
226 return;
228 if (vl->parent->data != NULL)
229 buffer->curs_x += utf8_swidth_between(vl->parent->line,
230 vl->parent->data);
231 else {
232 prfx = line_prefixes[vl->parent->type].prfx1;
233 buffer->curs_x += utf8_swidth(prfx);
237 void
238 global_key_unbound(void)
240 message("%s is undefined", keybuf);
243 struct buffer *
244 current_buffer(void)
246 if (in_minibuffer)
247 return &ministate.buffer;
248 if (in_side_window & SIDE_WINDOW_LEFT)
249 return &helpwin;
250 if (in_side_window & SIDE_WINDOW_BOTTOM)
251 return &downloadwin;
252 return &current_tab->buffer;
255 static int
256 readkey(void)
258 uint32_t state = 0;
260 if ((thiskey.key = wgetch(body)) == ERR)
261 return 0;
263 thiskey.meta = thiskey.key == '\e';
264 if (thiskey.meta) {
265 thiskey.key = wgetch(body);
266 if (thiskey.key == ERR || thiskey.key == '\e') {
267 thiskey.meta = 0;
268 thiskey.key = '\e';
272 thiskey.cp = 0;
274 if ((unsigned int)thiskey.key >= UINT8_MAX)
275 return 1;
277 while (1) {
278 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
279 break;
280 if ((thiskey.key = wgetch(body)) == ERR) {
281 message("Error decoding user input");
282 return 0;
286 return 1;
289 static void
290 dispatch_stdio(int fd, short ev, void *d)
292 int lk;
293 const char *keyname;
294 char tmp[5] = {0};
296 /* TODO: schedule a redraw? */
297 if (too_small)
298 return;
300 if (!readkey())
301 return;
303 if (keybuf[0] != '\0')
304 strlcat(keybuf, " ", sizeof(keybuf));
305 if (thiskey.meta)
306 strlcat(keybuf, "M-", sizeof(keybuf));
307 if (thiskey.cp != 0) {
308 utf8_encode(thiskey.cp, tmp);
309 strlcat(keybuf, tmp, sizeof(keybuf));
310 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
311 strlcat(keybuf, keyname, sizeof(keybuf));
312 } else {
313 tmp[0] = thiskey.key;
314 strlcat(keybuf, tmp, sizeof(keybuf));
317 lk = lookup_key(&current_map, &thiskey, current_buffer());
318 if (lk == LK_UNBOUND) {
319 if (current_map->unhandled_input != NULL)
320 current_map->unhandled_input();
321 else
322 global_key_unbound();
324 if (lk != LK_ADVANCED_MAP) {
325 current_map = base_map;
326 strlcpy(keybuf, "", sizeof(keybuf));
329 if (side_window & SIDE_WINDOW_LEFT)
330 recompute_help();
332 if (should_rearrange_windows)
333 rearrange_windows();
334 redraw_tab(current_tab);
337 static void
338 handle_resize(int sig, short ev, void *d)
340 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
341 event_del(&resizeev);
343 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
344 evtimer_add(&resizeev, &resize_timer);
347 static void
348 handle_resize_nodelay(int s, short ev, void *d)
350 endwin();
351 refresh();
352 clear();
354 rearrange_windows();
357 static inline int
358 should_show_tab_bar(void)
360 if (tab_bar_show == -1)
361 return 0;
362 if (tab_bar_show == 0)
363 return 1;
365 return TAILQ_NEXT(TAILQ_FIRST(&tabshead), tabs) != NULL;
368 static void
369 rearrange_windows(void)
371 int lines;
372 int minibuffer_lines;
374 should_rearrange_windows = 0;
375 show_tab_bar = should_show_tab_bar();
377 lines = LINES;
379 /* 3 lines for the ui and 12 for the */
380 if ((too_small = lines < 15)) {
381 erase();
382 printw("Window too small.");
383 refresh();
384 return;
387 /* move and resize the windows, in reverse order! */
389 if (in_minibuffer == MB_COMPREAD) {
390 minibuffer_lines = MIN(10, lines/2);
391 mvwin(minibuffer, lines - minibuffer_lines, 0);
392 wresize(minibuffer, minibuffer_lines, COLS);
393 lines -= minibuffer_lines;
395 wrap_page(&ministate.compl.buffer, COLS);
398 mvwin(echoarea, --lines, 0);
399 wresize(echoarea, 1, COLS);
401 mvwin(modeline, --lines, 0);
402 wresize(modeline, 1, COLS);
404 if (side_window & SIDE_WINDOW_BOTTOM) {
405 download_lines = MIN(5, lines/2);
406 download_cols = COLS;
407 mvwin(download, lines - download_lines, 0);
408 wresize(download, download_lines, download_cols);
409 lines -= download_lines;
411 wrap_page(&downloadwin, download_cols);
414 body_lines = show_tab_bar ? --lines : lines;
415 body_cols = COLS;
417 /*
418 * Here we make the assumption that show_tab_bar is either 0
419 * or 1, and reuse that as argument to mvwin.
420 */
421 if (side_window & SIDE_WINDOW_LEFT) {
422 help_cols = 0.3 * COLS;
423 help_lines = lines;
424 mvwin(help, show_tab_bar, 0);
425 wresize(help, help_lines, help_cols);
427 wrap_page(&helpwin, help_cols);
429 body_cols = COLS - help_cols - 1;
430 mvwin(body, show_tab_bar, help_cols);
431 } else
432 mvwin(body, show_tab_bar, 0);
434 update_x_offset();
435 wresize(body, body_lines, body_cols);
437 if (show_tab_bar)
438 wresize(tabline, 1, COLS);
440 wrap_page(&current_tab->buffer, body_cols);
441 redraw_tab(current_tab);
444 static void
445 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
446 const char **prfx_ret, const char **text_ret)
448 int type, i, cont, width;
449 char *space, *t;
451 if ((*text_ret = vl->line) == NULL)
452 *text_ret = "";
454 cont = vl->flags & L_CONTINUATION;
455 type = vl->parent->type;
456 if (!cont)
457 *prfx_ret = line_prefixes[type].prfx1;
458 else
459 *prfx_ret = line_prefixes[type].prfx2;
461 space = vl->parent->data;
462 if (!emojify_link || type != LINE_LINK || space == NULL)
463 return;
465 if (cont) {
466 memset(buf, 0, len);
467 width = utf8_swidth_between(vl->parent->line, space);
468 for (i = 0; i < width + 1; ++i)
469 strlcat(buf, " ", len);
470 } else {
471 strlcpy(buf, *text_ret, len);
472 if ((t = strchr(buf, ' ')) != NULL)
473 *t = '\0';
474 strlcat(buf, " ", len);
476 /* skip the emoji */
477 *text_ret += (space - vl->parent->line) + 1;
480 *prfx_ret = buf;
483 static inline void
484 print_vline_descr(int width, WINDOW *window, struct vline *vl)
486 int x, y, goal;
488 switch (vl->parent->type) {
489 case LINE_COMPL:
490 case LINE_COMPL_CURRENT:
491 goal = width/2;
492 break;
493 case LINE_DOWNLOAD:
494 case LINE_DOWNLOAD_DONE:
495 goal = width/4;
496 break;
497 case LINE_HELP:
498 goal = 8;
499 break;
500 default:
501 return;
504 if (vl->parent->alt == NULL)
505 return;
507 (void)y;
508 getyx(window, y, x);
510 if (goal <= x)
511 wprintw(window, " ");
512 for (; goal > x; ++x)
513 wprintw(window, " ");
515 wprintw(window, "%s", vl->parent->alt);
518 /*
519 * Core part of the rendering. It prints a vline starting from the
520 * current cursor position. Printing a vline consists of skipping
521 * `off' columns (for olivetti-mode), print the correct prefix (which
522 * may be the emoji in case of emojified links-lines), printing the
523 * text itself, filling until width - off and filling off columns
524 * again.
525 */
526 static void
527 print_vline(int off, int width, WINDOW *window, struct vline *vl)
529 /*
530 * Believe me or not, I've seen emoji ten code points long!
531 * That means, to stay large, 4*10 bytes + NUL.
532 */
533 char emojibuf[41] = {0};
534 const char *text, *prfx;
535 struct line_face *f;
536 int i, left, x, y;
538 f = &line_faces[vl->parent->type];
540 /* unused, set by getyx */
541 (void)y;
543 if (vl->parent->type == LINE_FRINGE && fringe_ignore_offset)
544 off = 0;
546 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
548 wattr_on(window, body_face.left, NULL);
549 for (i = 0; i < off; i++)
550 waddch(window, ' ');
551 wattr_off(window, body_face.left, NULL);
553 wattr_on(window, f->prefix, NULL);
554 wprintw(window, "%s", prfx);
555 wattr_off(window, f->prefix, NULL);
557 wattr_on(window, f->text, NULL);
558 wprintw(window, "%s", text);
559 print_vline_descr(width, window, vl);
560 wattr_off(window, f->text, NULL);
562 getyx(window, y, x);
564 left = width - x;
566 wattr_on(window, f->trail, NULL);
567 for (i = 0; i < left - off; ++i)
568 waddch(window, ' ');
569 wattr_off(window, f->trail, NULL);
571 wattr_on(window, body_face.right, NULL);
572 for (i = 0; i < off; i++)
573 waddch(window, ' ');
574 wattr_off(window, body_face.right, NULL);
578 static void
579 redraw_tabline(void)
581 struct tab *tab;
582 size_t toskip, ots, tabwidth, space, x;
583 int current, y, truncated, pair;
584 const char *title;
585 char buf[25];
587 x = 0;
589 /* unused, but setted by a getyx */
590 (void)y;
592 tabwidth = sizeof(buf)+1;
593 space = COLS-2;
595 toskip = 0;
596 TAILQ_FOREACH(tab, &tabshead, tabs) {
597 toskip++;
598 if (tab == current_tab)
599 break;
602 if (toskip * tabwidth <= space)
603 toskip = 0;
604 else {
605 ots = toskip;
606 toskip--;
607 while (toskip != 0 &&
608 (ots - toskip+1) * tabwidth < space)
609 toskip--;
612 werase(tabline);
613 wattr_on(tabline, tab_face.background, NULL);
614 wprintw(tabline, toskip == 0 ? " " : "<");
615 wattr_off(tabline, tab_face.background, NULL);
617 truncated = 0;
618 TAILQ_FOREACH(tab, &tabshead, tabs) {
619 if (truncated)
620 break;
621 if (toskip != 0) {
622 toskip--;
623 continue;
626 getyx(tabline, y, x);
627 if (x + sizeof(buf)+2 >= (size_t)COLS)
628 truncated = 1;
630 current = tab == current_tab;
632 if (*(title = tab->buffer.page.title) == '\0')
633 title = tab->hist_cur->h;
635 if (tab->flags & TAB_URGENT)
636 strlcpy(buf, "!", sizeof(buf));
637 else
638 strlcpy(buf, " ", sizeof(buf));
640 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
641 /* truncation happens */
642 strlcpy(&buf[sizeof(buf)-4], "...", 4);
643 } else {
644 /* pad with spaces */
645 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
646 /* nop */ ;
649 pair = current ? tab_face.current : tab_face.tab;
650 wattr_on(tabline, pair, NULL);
651 wprintw(tabline, "%s", buf);
652 wattr_off(tabline, pair, NULL);
654 wattr_on(tabline, tab_face.background, NULL);
655 if (TAILQ_NEXT(tab, tabs) != NULL)
656 wprintw(tabline, "┃");
657 wattr_off(tabline, tab_face.background, NULL);
660 wattr_on(tabline, tab_face.background, NULL);
661 for (; x < (size_t)COLS; ++x)
662 waddch(tabline, ' ');
663 if (truncated)
664 mvwprintw(tabline, 0, COLS-1, ">");
665 wattr_off(tabline, tab_face.background, NULL);
668 /*
669 * Compute the first visible line around vl. Try to search forward
670 * until the end of the buffer; if a visible line is not found, search
671 * backward. Return NULL if no viable line was found.
672 */
673 struct vline *
674 adjust_line(struct vline *vl, struct buffer *buffer)
676 struct vline *t;
678 if (vl == NULL)
679 return NULL;
681 if (!(vl->parent->flags & L_HIDDEN))
682 return vl;
684 /* search forward */
685 for (t = vl;
686 t != NULL && t->parent->flags & L_HIDDEN;
687 t = TAILQ_NEXT(t, vlines))
688 ; /* nop */
690 if (t != NULL)
691 return t;
693 /* search backward */
694 for (t = vl;
695 t != NULL && t->parent->flags & L_HIDDEN;
696 t = TAILQ_PREV(t, vhead, vlines))
697 ; /* nop */
699 return t;
702 static void
703 redraw_window(WINDOW *win, int off, int height, int width,
704 int show_fringe, struct buffer *buffer)
706 struct vline *vl;
707 int onscreen = 0, l = 0;
709 restore_curs_x(buffer);
711 /*
712 * TODO: ignoring buffer->force_update and always
713 * re-rendering. In theory we can recompute the y position
714 * without a re-render, and optimize here. It's not the only
715 * optimisation possible here, wscrl wolud also be an
716 * interesting one.
717 */
719 again:
720 werase(win);
721 buffer->curs_y = 0;
723 if (TAILQ_EMPTY(&buffer->head))
724 goto end;
726 if (buffer->top_line == NULL)
727 buffer->top_line = TAILQ_FIRST(&buffer->head);
729 buffer->top_line = adjust_line(buffer->top_line, buffer);
730 if (buffer->top_line == NULL)
731 goto end;
733 buffer->current_line = adjust_line(buffer->current_line, buffer);
735 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
736 if (vl->parent->flags & L_HIDDEN)
737 continue;
739 wmove(win, l, 0);
740 print_vline(off, width, win, vl);
742 if (vl == buffer->current_line)
743 onscreen = 1;
745 if (!onscreen)
746 buffer->curs_y++;
748 l++;
749 if (l == height)
750 break;
753 if (!onscreen) {
754 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
755 if (vl == buffer->current_line)
756 break;
757 if (vl->parent->flags & L_HIDDEN)
758 continue;
759 buffer->line_off++;
760 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
763 if (vl != NULL)
764 goto again;
767 buffer->last_line_off = buffer->line_off;
768 buffer->force_redraw = 0;
769 end:
770 for (; show_fringe && l < height; l++)
771 print_vline(off, width, win, &fringe);
773 wmove(win, buffer->curs_y, buffer->curs_x);
776 static void
777 redraw_download(void)
779 redraw_window(download, 0, download_lines, COLS, 0, &downloadwin);
782 static void
783 redraw_help(void)
785 redraw_window(help, 0, help_lines, help_cols, 1, &helpwin);
788 static void
789 redraw_body(struct tab *tab)
791 static struct tab *last_tab;
793 if (last_tab != tab)
794 tab->buffer.force_redraw =1;
795 last_tab = tab;
797 redraw_window(body, x_offset, body_lines, body_cols, 1, &tab->buffer);
800 static inline char
801 trust_status_char(enum trust_state ts)
803 switch (ts) {
804 case TS_UNKNOWN: return '-';
805 case TS_UNTRUSTED: return '!';
806 case TS_TEMP_TRUSTED: return '!';
807 case TS_TRUSTED: return 'v';
808 case TS_VERIFIED: return 'V';
809 default: return 'X';
813 static void
814 redraw_modeline(struct tab *tab)
816 struct buffer *buffer;
817 double pct;
818 int x, y, max_x, max_y;
819 const char *mode;
820 const char *spin = "-\\|/";
822 buffer = current_buffer();
823 mode = buffer->page.name;
825 werase(modeline);
826 wattr_on(modeline, modeline_face.background, NULL);
827 wmove(modeline, 0, 0);
829 wprintw(modeline, "-%c%c- %s ",
830 spin[tab->loading_anim_step],
831 trust_status_char(tab->trust),
832 mode == NULL ? "(none)" : mode);
834 pct = (buffer->line_off + buffer->curs_y) * 100.0
835 / buffer->line_max;
837 if (buffer->line_max <= (size_t)body_lines)
838 wprintw(modeline, "All ");
839 else if (buffer->line_off == 0)
840 wprintw(modeline, "Top ");
841 else if (buffer->line_off + body_lines >= buffer->line_max)
842 wprintw(modeline, "Bottom ");
843 else
844 wprintw(modeline, "%.0f%% ", pct);
846 wprintw(modeline, "%zu/%zu %s ",
847 buffer->line_off + buffer->curs_y,
848 buffer->line_max,
849 tab->hist_cur->h);
851 getyx(modeline, y, x);
852 getmaxyx(modeline, max_y, max_x);
854 (void)y;
855 (void)max_y;
857 for (; x < max_x; ++x)
858 waddstr(modeline, "-");
860 wattr_off(modeline, modeline_face.background, NULL);
863 static void
864 redraw_minibuffer(void)
866 wattr_on(echoarea, minibuffer_face.background, NULL);
867 werase(echoarea);
869 if (in_minibuffer)
870 do_redraw_minibuffer();
871 else
872 do_redraw_echoarea();
874 if (in_minibuffer == MB_COMPREAD)
875 do_redraw_minibuffer_compl();
877 wattr_off(echoarea, minibuffer_face.background, NULL);
880 static void
881 do_redraw_echoarea(void)
883 struct vline *vl;
885 if (ministate.curmesg != NULL)
886 wprintw(echoarea, "%s", ministate.curmesg);
887 else if (*keybuf != '\0')
888 waddstr(echoarea, keybuf);
889 else {
890 /* If nothing else, show the URL at point */
891 vl = current_tab->buffer.current_line;
892 if (vl != NULL && vl->parent->type == LINE_LINK)
893 waddstr(echoarea, vl->parent->alt);
897 static void
898 do_redraw_minibuffer(void)
900 struct buffer *cmplbuf, *buffer;
901 size_t off_y, off_x = 0;
902 const char *start, *c;
904 cmplbuf = &ministate.compl.buffer;
905 buffer = &ministate.buffer;
906 (void)off_y; /* unused, set by getyx */
908 wmove(echoarea, 0, 0);
910 if (in_minibuffer == MB_COMPREAD)
911 wprintw(echoarea, "(%2zu) ",
912 cmplbuf->line_max);
914 wprintw(echoarea, "%s", ministate.prompt);
915 if (ministate.hist_cur != NULL)
916 wprintw(echoarea, "(%zu/%zu) ",
917 ministate.hist_off + 1,
918 ministate.history->len);
920 getyx(echoarea, off_y, off_x);
922 start = ministate.hist_cur != NULL
923 ? ministate.hist_cur->h
924 : ministate.buf;
925 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
926 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
927 start = utf8_next_cp(start);
930 waddstr(echoarea, start);
932 if (ministate.curmesg != NULL)
933 wprintw(echoarea, " [%s]", ministate.curmesg);
935 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
938 static void
939 do_redraw_minibuffer_compl(void)
941 redraw_window(minibuffer, 0, 10, COLS, 1,
942 &ministate.compl.buffer);
945 /*
946 * Place the cursor in the right ncurses window. If soft is 1, use
947 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
948 * wrefresh.
949 */
950 static void
951 place_cursor(int soft)
953 int (*touch)(WINDOW *);
955 if (soft)
956 touch = wnoutrefresh;
957 else
958 touch = wrefresh;
960 if (in_minibuffer) {
961 if (side_window & SIDE_WINDOW_LEFT)
962 touch(help);
963 if (side_window & SIDE_WINDOW_BOTTOM)
964 touch(download);
965 touch(body);
966 touch(echoarea);
967 } else if (in_side_window & SIDE_WINDOW_LEFT) {
968 touch(body);
969 touch(echoarea);
970 if (in_side_window & SIDE_WINDOW_BOTTOM)
971 touch(download);
972 touch(help);
973 } else if (in_side_window & SIDE_WINDOW_BOTTOM) {
974 touch(body);
975 touch(echoarea);
976 if (in_side_window & SIDE_WINDOW_LEFT)
977 touch(help);
978 touch(download);
979 } else {
980 if (side_window & SIDE_WINDOW_LEFT)
981 touch(help);
982 if (side_window & SIDE_WINDOW_BOTTOM)
983 touch(download);
984 touch(echoarea);
985 touch(body);
989 static void
990 redraw_tab(struct tab *tab)
992 if (too_small)
993 return;
995 if (side_window & SIDE_WINDOW_LEFT) {
996 redraw_help();
997 wnoutrefresh(help);
1000 if (side_window & SIDE_WINDOW_BOTTOM) {
1001 redraw_download();
1002 wnoutrefresh(download);
1005 if (show_tab_bar)
1006 redraw_tabline();
1008 redraw_body(tab);
1009 redraw_modeline(tab);
1010 redraw_minibuffer();
1012 wnoutrefresh(tabline);
1013 wnoutrefresh(modeline);
1015 if (in_minibuffer == MB_COMPREAD)
1016 wnoutrefresh(minibuffer);
1018 place_cursor(1);
1020 doupdate();
1022 if (set_title)
1023 dprintf(1, "\033]2;%s - Telescope\a",
1024 current_tab->buffer.page.title);
1027 void
1028 start_loading_anim(struct tab *tab)
1030 if (tab->loading_anim)
1031 return;
1032 tab->loading_anim = 1;
1033 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1034 evtimer_add(&tab->loadingev, &loadingev_timer);
1037 static void
1038 update_loading_anim(int fd, short ev, void *d)
1040 struct tab *tab = d;
1042 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1044 if (tab == current_tab) {
1045 redraw_modeline(tab);
1046 wrefresh(modeline);
1047 wrefresh(body);
1048 if (in_minibuffer)
1049 wrefresh(echoarea);
1052 evtimer_add(&tab->loadingev, &loadingev_timer);
1055 static void
1056 stop_loading_anim(struct tab *tab)
1058 if (!tab->loading_anim)
1059 return;
1060 evtimer_del(&tab->loadingev);
1061 tab->loading_anim = 0;
1062 tab->loading_anim_step = 0;
1064 if (tab != current_tab)
1065 return;
1067 redraw_modeline(tab);
1069 wrefresh(modeline);
1070 wrefresh(body);
1071 if (in_minibuffer)
1072 wrefresh(echoarea);
1075 int
1076 ui_print_colors(void)
1078 int colors = 0, pairs = 0, can_change = 0;
1079 int columns = 16, lines, color, i, j;
1081 initscr();
1082 if (has_colors()) {
1083 start_color();
1084 use_default_colors();
1086 colors = COLORS;
1087 pairs = COLOR_PAIRS;
1088 can_change = can_change_color();
1090 endwin();
1092 printf("Term info:\n");
1093 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1094 getenv("TERM"), colors, pairs, can_change);
1095 printf("\n");
1097 if (colors == 0) {
1098 printf("No color support\n");
1099 return 0;
1102 printf("Available colors:\n\n");
1103 lines = (colors - 1) / columns + 1;
1104 color = 0;
1105 for (i = 0; i < lines; ++i) {
1106 for (j = 0; j < columns; ++j, ++color) {
1107 printf("\033[0;38;5;%dm %03d", color, color);
1109 printf("\n");
1112 printf("\033[0m");
1113 fflush(stdout);
1114 return 0;
1117 int
1118 ui_init()
1120 setlocale(LC_ALL, "");
1122 if (TAILQ_EMPTY(&global_map.m)) {
1123 fprintf(stderr, "no keys defined!\n");
1124 return 0;
1127 minibuffer_init();
1129 /* initialize download window */
1130 TAILQ_INIT(&downloadwin.head);
1131 TAILQ_INIT(&downloadwin.page.head);
1133 /* initialize help window */
1134 TAILQ_INIT(&helpwin.head);
1135 TAILQ_INIT(&helpwin.page.head);
1137 base_map = &global_map;
1138 current_map = &global_map;
1140 initscr();
1142 if (enable_colors) {
1143 if (has_colors()) {
1144 start_color();
1145 use_default_colors();
1146 } else
1147 enable_colors = 0;
1150 config_apply_style();
1152 raw();
1153 noecho();
1154 nonl();
1155 intrflush(stdscr, FALSE);
1157 if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1158 return 0;
1159 if ((body = newwin(1, 1, 0, 0)) == NULL)
1160 return 0;
1161 if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1162 return 0;
1163 if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1164 return 0;
1165 if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1166 return 0;
1167 if ((download = newwin(1, 1, 0, 0)) == NULL)
1168 return 0;
1169 if ((help = newwin(1, 1, 0, 0)) == NULL)
1170 return 0;
1172 wbkgd(body, body_face.body);
1173 wbkgd(download, download_face.background);
1174 wbkgd(echoarea, minibuffer_face.background);
1176 update_x_offset();
1178 keypad(body, TRUE);
1179 scrollok(body, FALSE);
1181 /* non-blocking input */
1182 wtimeout(body, 0);
1183 wtimeout(help, 0);
1185 mvwprintw(body, 0, 0, "");
1187 return 1;
1190 void
1191 ui_main_loop(void)
1193 evtimer_set(&resizeev, handle_resize, NULL);
1195 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1196 event_add(&stdioev, NULL);
1198 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1199 signal_add(&winchev, NULL);
1201 switch_to_tab(current_tab);
1202 rearrange_windows();
1205 void
1206 ui_on_tab_loaded(struct tab *tab)
1208 stop_loading_anim(tab);
1209 message("Loaded %s", tab->hist_cur->h);
1211 if (tab->hist_cur->current_off != 0 &&
1212 tab->buffer.current_line == TAILQ_FIRST(&tab->buffer.head)) {
1213 set_scroll_position(tab, tab->hist_cur->line_off,
1214 tab->hist_cur->current_off);
1215 redraw_tab(tab);
1216 return;
1219 if (show_tab_bar)
1220 redraw_tabline();
1222 wrefresh(tabline);
1223 place_cursor(0);
1226 void
1227 ui_on_tab_refresh(struct tab *tab)
1229 wrap_page(&tab->buffer, body_cols);
1230 if (tab == current_tab)
1231 redraw_tab(tab);
1232 else
1233 tab->flags |= TAB_URGENT;
1236 void
1237 ui_on_download_refresh(void)
1239 if (side_window & SIDE_WINDOW_BOTTOM) {
1240 recompute_downloads();
1241 redraw_tab(current_tab);
1245 const char *
1246 ui_keyname(int k)
1248 return keyname(k);
1251 void
1252 ui_toggle_side_window(int kind)
1254 if (in_side_window & kind)
1255 ui_other_window();
1257 side_window ^= kind;
1258 if (side_window & SIDE_WINDOW_LEFT)
1259 recompute_help();
1260 if (side_window & SIDE_WINDOW_BOTTOM)
1261 recompute_downloads();
1264 * ugly hack, but otherwise the window doesn't get updated
1265 * until I call rearrange_windows a second time (e.g. via
1266 * C-l). I will be happy to know why something like this is
1267 * needed.
1269 rearrange_windows();
1270 rearrange_windows();
1273 void
1274 ui_show_downloads_pane(void)
1276 if (!(side_window & SIDE_WINDOW_BOTTOM))
1277 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1280 void
1281 ui_schedule_redraw(void)
1283 should_rearrange_windows = 1;
1286 void
1287 ui_require_input(struct tab *tab, int hide, void (*fn)(void))
1289 /* TODO: hard-switching to another tab is ugly */
1290 switch_to_tab(tab);
1292 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1293 &ir_history, NULL, NULL);
1294 strlcpy(ministate.prompt, "Input required: ",
1295 sizeof(ministate.prompt));
1296 redraw_tab(tab);
1299 void
1300 ui_after_message_hook(void)
1302 redraw_minibuffer();
1303 place_cursor(0);
1306 void
1307 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1308 struct tab *data)
1310 yornp(prompt, fn, data);
1311 redraw_tab(current_tab);
1314 void
1315 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1316 struct tab *data, const char *input)
1318 minibuffer_read(prompt, fn, data);
1320 if (input != NULL) {
1321 strlcpy(ministate.buf, input, sizeof(ministate.buf));
1322 cmd_move_end_of_line(&ministate.buffer);
1325 redraw_tab(current_tab);
1328 void
1329 ui_other_window(void)
1331 if (in_side_window & SIDE_WINDOW_LEFT &&
1332 side_window & SIDE_WINDOW_BOTTOM)
1333 in_side_window = SIDE_WINDOW_BOTTOM;
1334 else if (in_side_window)
1335 in_side_window = 0;
1336 else if (!in_side_window && side_window & SIDE_WINDOW_LEFT)
1337 in_side_window = SIDE_WINDOW_LEFT;
1338 else if (!in_side_window && side_window)
1339 in_side_window = SIDE_WINDOW_BOTTOM;
1340 else
1341 message("No other window to select");
1344 void
1345 ui_suspend(void)
1347 endwin();
1349 kill(getpid(), SIGSTOP);
1351 refresh();
1352 clear();
1353 rearrange_windows();
1356 void
1357 ui_end(void)
1359 endwin();