Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 /*
18 * Ncurses UI for telescope.
19 *
20 * Text scrolling
21 * ==============
22 *
23 * ncurses allows you to scroll a window, but when a line goes out of
24 * the visible area it's forgotten. We keep a list of formatted lines
25 * (``visual lines'') that we know fits in the window, and draw them.
26 *
27 * This means that on every resize we have to clear our list of lines
28 * and re-render everything. A clever approach would be to do this
29 * ``on-demand'', but it's still missing.
30 *
31 */
33 #include "compat.h"
35 #include <assert.h>
36 #include <curses.h>
37 #include <locale.h>
38 #include <signal.h>
39 #include <stdarg.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
44 #include "defaults.h"
45 #include "minibuffer.h"
46 #include "session.h"
47 #include "telescope.h"
48 #include "ui.h"
49 #include "utf8.h"
51 static struct event stdioev, winchev;
53 static void set_scroll_position(struct tab *, size_t, size_t);
55 static void restore_curs_x(struct buffer *);
57 static int readkey(void);
58 static void dispatch_stdio(int, short, void*);
59 static void handle_resize(int, short, void*);
60 static void handle_resize_nodelay(int, short, void*);
61 static void handle_download_refresh(int, short, void *);
62 static void rearrange_windows(void);
63 static void line_prefix_and_text(struct vline *, char *, size_t, const char **, const char **);
64 static void print_vline(int, int, WINDOW*, struct vline*);
65 static void redraw_tabline(void);
66 static void redraw_window(WINDOW *, int, int, int, int, struct buffer *);
67 static void redraw_download(void);
68 static void redraw_help(void);
69 static void redraw_body(struct tab*);
70 static void redraw_modeline(struct tab*);
71 static void redraw_minibuffer(void);
72 static void do_redraw_echoarea(void);
73 static void do_redraw_minibuffer(void);
74 static void do_redraw_minibuffer_compl(void);
75 static void place_cursor(int);
76 static void redraw_tab(struct tab*);
77 static void update_loading_anim(int, short, void*);
78 static void stop_loading_anim(struct tab*);
80 static int should_rearrange_windows;
81 static int show_tab_bar;
82 static int too_small;
83 static int x_offset;
85 struct thiskey thiskey;
86 struct tab *current_tab;
88 static struct event resizeev;
89 static struct timeval resize_timer = { 0, 250000 };
91 static struct event download_refreshev;
92 static struct timeval download_refresh_timer = { 0, 250000 };
94 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
96 int body_lines, body_cols;
98 static WINDOW *help;
99 /* not static so we can see them from help.c */
100 struct buffer helpwin;
101 int help_lines, help_cols;
103 static WINDOW *download;
104 /* not static so we can see them from download.c */
105 struct buffer downloadwin;
106 int download_lines;
107 int download_cols;
109 static int side_window;
110 static int in_side_window;
112 static struct timeval loadingev_timer = { 0, 250000 };
114 static char keybuf[64];
116 /* XXX: don't forget to init these in main() */
117 struct kmap global_map,
118 minibuffer_map,
119 *current_map,
120 *base_map;
122 static inline void
123 update_x_offset(void)
125 if (olivetti_mode && fill_column < body_cols)
126 x_offset = (body_cols - fill_column)/2;
127 else
128 x_offset = 0;
131 static void
132 set_scroll_position(struct tab *tab, size_t top, size_t cur)
134 struct line *last;
135 struct vline *vl;
136 size_t i = 0;
137 int topfound = 0;
139 last = TAILQ_FIRST(&tab->buffer.page.head);
140 TAILQ_FOREACH(vl, &tab->buffer.head, vlines) {
141 if (last != vl->parent) {
142 last = vl->parent;
143 i++;
146 if (!topfound && i == top) {
147 topfound = 1;
148 tab->buffer.top_line = vl;
151 if (i == cur) {
152 tab->buffer.current_line = vl;
153 return;
157 if (!topfound)
158 tab->buffer.top_line = TAILQ_FIRST(&tab->buffer.head);
160 tab->buffer.current_line = tab->buffer.top_line;
163 void
164 get_scroll_position(struct tab *tab, size_t *top, size_t *cur)
166 struct line *l;
167 int topfound = 0;
169 *top = 0;
170 *cur = 0;
172 if (tab->buffer.top_line == NULL ||
173 tab->buffer.current_line == NULL)
174 return;
176 TAILQ_FOREACH(l, &tab->buffer.page.head, lines) {
177 if (tab->buffer.top_line->parent == l)
178 topfound = 1;
179 if (tab->buffer.current_line->parent == l)
180 return;
182 if (!topfound)
183 (*top)++;
184 (*cur)++;
188 void
189 save_excursion(struct excursion *place, struct buffer *buffer)
191 place->curs_x = buffer->curs_x;
192 place->curs_y = buffer->curs_y;
193 place->line_off = buffer->line_off;
194 place->top_line = buffer->top_line;
195 place->current_line = buffer->current_line;
196 place->cpoff = buffer->cpoff;
199 void
200 restore_excursion(struct excursion *place, struct buffer *buffer)
202 buffer->curs_x = place->curs_x;
203 buffer->curs_y = place->curs_y;
204 buffer->line_off = place->line_off;
205 buffer->top_line = place->top_line;
206 buffer->current_line = place->current_line;
207 buffer->cpoff = place->cpoff;
210 static void
211 restore_curs_x(struct buffer *buffer)
213 struct vline *vl;
214 const char *prfx, *text;
216 vl = buffer->current_line;
217 if (vl == NULL || vl->line == NULL)
218 buffer->curs_x = buffer->cpoff = 0;
219 else if (vl->parent->data != NULL) {
220 text = vl->parent->data;
221 buffer->curs_x = utf8_snwidth(text + 1, buffer->cpoff) + 1;
222 } else
223 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
225 /* small hack: don't olivetti-mode the download pane */
226 if (buffer != &downloadwin)
227 buffer->curs_x += x_offset;
229 if (vl == NULL)
230 return;
232 if (vl->parent->data != NULL)
233 buffer->curs_x += utf8_swidth_between(vl->parent->line,
234 vl->parent->data);
235 else {
236 prfx = line_prefixes[vl->parent->type].prfx1;
237 buffer->curs_x += utf8_swidth(prfx);
241 void
242 global_key_unbound(void)
244 message("%s is undefined", keybuf);
247 struct buffer *
248 current_buffer(void)
250 if (in_minibuffer)
251 return &ministate.buffer;
252 if (in_side_window & SIDE_WINDOW_LEFT)
253 return &helpwin;
254 if (in_side_window & SIDE_WINDOW_BOTTOM)
255 return &downloadwin;
256 return &current_tab->buffer;
259 static int
260 readkey(void)
262 uint32_t state = 0;
264 if ((thiskey.key = wgetch(body)) == ERR)
265 return 0;
267 thiskey.meta = thiskey.key == '\e';
268 if (thiskey.meta) {
269 thiskey.key = wgetch(body);
270 if (thiskey.key == ERR || thiskey.key == '\e') {
271 thiskey.meta = 0;
272 thiskey.key = '\e';
276 thiskey.cp = 0;
278 if ((unsigned int)thiskey.key >= UINT8_MAX)
279 return 1;
281 while (1) {
282 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
283 break;
284 if ((thiskey.key = wgetch(body)) == ERR) {
285 message("Error decoding user input");
286 return 0;
290 return 1;
293 static void
294 dispatch_stdio(int fd, short ev, void *d)
296 int lk;
297 const char *keyname;
298 char tmp[5] = {0};
300 /* TODO: schedule a redraw? */
301 if (too_small)
302 return;
304 if (!readkey())
305 return;
307 if (keybuf[0] != '\0')
308 strlcat(keybuf, " ", sizeof(keybuf));
309 if (thiskey.meta)
310 strlcat(keybuf, "M-", sizeof(keybuf));
311 if (thiskey.cp != 0) {
312 utf8_encode(thiskey.cp, tmp);
313 strlcat(keybuf, tmp, sizeof(keybuf));
314 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
315 strlcat(keybuf, keyname, sizeof(keybuf));
316 } else {
317 tmp[0] = thiskey.key;
318 strlcat(keybuf, tmp, sizeof(keybuf));
321 lk = lookup_key(&current_map, &thiskey, current_buffer());
322 if (lk == LK_UNBOUND) {
323 if (current_map->unhandled_input != NULL)
324 current_map->unhandled_input();
325 else
326 global_key_unbound();
328 if (lk != LK_ADVANCED_MAP) {
329 current_map = base_map;
330 strlcpy(keybuf, "", sizeof(keybuf));
333 if (side_window & SIDE_WINDOW_LEFT)
334 recompute_help();
336 if (should_rearrange_windows)
337 rearrange_windows();
338 redraw_tab(current_tab);
341 static void
342 handle_resize(int sig, short ev, void *d)
344 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
345 event_del(&resizeev);
347 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
348 evtimer_add(&resizeev, &resize_timer);
351 static void
352 handle_resize_nodelay(int s, short ev, void *d)
354 endwin();
355 refresh();
356 clear();
358 rearrange_windows();
361 static void
362 handle_download_refresh(int s, short v, void *d)
364 if (side_window & SIDE_WINDOW_BOTTOM) {
365 recompute_downloads();
366 redraw_tab(current_tab);
370 static inline int
371 should_show_tab_bar(void)
373 if (tab_bar_show == -1)
374 return 0;
375 if (tab_bar_show == 0)
376 return 1;
378 return TAILQ_NEXT(TAILQ_FIRST(&tabshead), tabs) != NULL;
381 static void
382 rearrange_windows(void)
384 int lines;
385 int minibuffer_lines;
387 should_rearrange_windows = 0;
388 show_tab_bar = should_show_tab_bar();
390 lines = LINES;
392 /* 3 lines for the ui and 12 for the */
393 if ((too_small = lines < 15)) {
394 erase();
395 printw("Window too small.");
396 refresh();
397 return;
400 /* move and resize the windows, in reverse order! */
402 if (in_minibuffer == MB_COMPREAD) {
403 minibuffer_lines = MIN(10, lines/2);
404 mvwin(minibuffer, lines - minibuffer_lines, 0);
405 wresize(minibuffer, minibuffer_lines, COLS);
406 lines -= minibuffer_lines;
408 wrap_page(&ministate.compl.buffer, COLS);
411 mvwin(echoarea, --lines, 0);
412 wresize(echoarea, 1, COLS);
414 mvwin(modeline, --lines, 0);
415 wresize(modeline, 1, COLS);
417 if (side_window & SIDE_WINDOW_BOTTOM) {
418 download_lines = MIN(5, lines/2);
419 download_cols = COLS;
420 mvwin(download, lines - download_lines, 0);
421 wresize(download, download_lines, download_cols);
422 lines -= download_lines;
424 wrap_page(&downloadwin, download_cols);
427 body_lines = show_tab_bar ? --lines : lines;
428 body_cols = COLS;
430 /*
431 * Here we make the assumption that show_tab_bar is either 0
432 * or 1, and reuse that as argument to mvwin.
433 */
434 if (side_window & SIDE_WINDOW_LEFT) {
435 help_cols = 0.3 * COLS;
436 help_lines = lines;
437 mvwin(help, show_tab_bar, 0);
438 wresize(help, help_lines, help_cols);
440 wrap_page(&helpwin, help_cols);
442 body_cols = COLS - help_cols - 1;
443 mvwin(body, show_tab_bar, help_cols);
444 } else
445 mvwin(body, show_tab_bar, 0);
447 update_x_offset();
448 wresize(body, body_lines, body_cols);
450 if (show_tab_bar)
451 wresize(tabline, 1, COLS);
453 wrap_page(&current_tab->buffer, body_cols);
454 redraw_tab(current_tab);
457 static void
458 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
459 const char **prfx_ret, const char **text_ret)
461 int type, i, cont, width;
462 char *space, *t;
464 if ((*text_ret = vl->line) == NULL)
465 *text_ret = "";
467 cont = vl->flags & L_CONTINUATION;
468 type = vl->parent->type;
469 if (!cont)
470 *prfx_ret = line_prefixes[type].prfx1;
471 else
472 *prfx_ret = line_prefixes[type].prfx2;
474 space = vl->parent->data;
475 if (!emojify_link || type != LINE_LINK || space == NULL)
476 return;
478 if (cont) {
479 memset(buf, 0, len);
480 width = utf8_swidth_between(vl->parent->line, space);
481 for (i = 0; i < width + 1; ++i)
482 strlcat(buf, " ", len);
483 } else {
484 strlcpy(buf, *text_ret, len);
485 if ((t = strchr(buf, ' ')) != NULL)
486 *t = '\0';
487 strlcat(buf, " ", len);
489 /* skip the emoji */
490 *text_ret += (space - vl->parent->line) + 1;
493 *prfx_ret = buf;
496 static inline void
497 print_vline_descr(int width, WINDOW *window, struct vline *vl)
499 int x, y, goal;
501 switch (vl->parent->type) {
502 case LINE_COMPL:
503 case LINE_COMPL_CURRENT:
504 goal = width/2;
505 break;
506 case LINE_DOWNLOAD:
507 case LINE_DOWNLOAD_DONE:
508 goal = width/4;
509 break;
510 case LINE_HELP:
511 goal = 8;
512 break;
513 default:
514 return;
517 if (vl->parent->alt == NULL)
518 return;
520 (void)y;
521 getyx(window, y, x);
523 if (goal <= x)
524 wprintw(window, " ");
525 for (; goal > x; ++x)
526 wprintw(window, " ");
528 wprintw(window, "%s", vl->parent->alt);
531 /*
532 * Core part of the rendering. It prints a vline starting from the
533 * current cursor position. Printing a vline consists of skipping
534 * `off' columns (for olivetti-mode), print the correct prefix (which
535 * may be the emoji in case of emojified links-lines), printing the
536 * text itself, filling until width - off and filling off columns
537 * again.
538 */
539 static void
540 print_vline(int off, int width, WINDOW *window, struct vline *vl)
542 /*
543 * Believe me or not, I've seen emoji ten code points long!
544 * That means, to stay large, 4*10 bytes + NUL.
545 */
546 char emojibuf[41] = {0};
547 const char *text, *prfx;
548 struct line_face *f;
549 int i, left, x, y;
551 f = &line_faces[vl->parent->type];
553 /* unused, set by getyx */
554 (void)y;
556 if (vl->parent->type == LINE_FRINGE && fringe_ignore_offset)
557 off = 0;
559 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
561 wattr_on(window, body_face.left, NULL);
562 for (i = 0; i < off; i++)
563 waddch(window, ' ');
564 wattr_off(window, body_face.left, NULL);
566 wattr_on(window, f->prefix, NULL);
567 wprintw(window, "%s", prfx);
568 wattr_off(window, f->prefix, NULL);
570 wattr_on(window, f->text, NULL);
571 wprintw(window, "%s", text);
572 print_vline_descr(width, window, vl);
573 wattr_off(window, f->text, NULL);
575 getyx(window, y, x);
577 left = width - x;
579 wattr_on(window, f->trail, NULL);
580 for (i = 0; i < left - off; ++i)
581 waddch(window, ' ');
582 wattr_off(window, f->trail, NULL);
584 wattr_on(window, body_face.right, NULL);
585 for (i = 0; i < off; i++)
586 waddch(window, ' ');
587 wattr_off(window, body_face.right, NULL);
591 static void
592 redraw_tabline(void)
594 struct tab *tab;
595 size_t toskip, ots, tabwidth, space, x;
596 int current, y, truncated, pair;
597 const char *title;
598 char buf[25];
600 x = 0;
602 /* unused, but setted by a getyx */
603 (void)y;
605 tabwidth = sizeof(buf)+1;
606 space = COLS-2;
608 toskip = 0;
609 TAILQ_FOREACH(tab, &tabshead, tabs) {
610 toskip++;
611 if (tab == current_tab)
612 break;
615 if (toskip * tabwidth <= space)
616 toskip = 0;
617 else {
618 ots = toskip;
619 toskip--;
620 while (toskip != 0 &&
621 (ots - toskip+1) * tabwidth < space)
622 toskip--;
625 werase(tabline);
626 wattr_on(tabline, tab_face.background, NULL);
627 wprintw(tabline, toskip == 0 ? " " : "<");
628 wattr_off(tabline, tab_face.background, NULL);
630 truncated = 0;
631 TAILQ_FOREACH(tab, &tabshead, tabs) {
632 if (truncated)
633 break;
634 if (toskip != 0) {
635 toskip--;
636 continue;
639 getyx(tabline, y, x);
640 if (x + sizeof(buf)+2 >= (size_t)COLS)
641 truncated = 1;
643 current = tab == current_tab;
645 if (*(title = tab->buffer.page.title) == '\0')
646 title = tab->hist_cur->h;
648 if (tab->flags & TAB_URGENT)
649 strlcpy(buf, "!", sizeof(buf));
650 else
651 strlcpy(buf, " ", sizeof(buf));
653 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
654 /* truncation happens */
655 strlcpy(&buf[sizeof(buf)-4], "...", 4);
656 } else {
657 /* pad with spaces */
658 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
659 /* nop */ ;
662 pair = current ? tab_face.current : tab_face.tab;
663 wattr_on(tabline, pair, NULL);
664 wprintw(tabline, "%s", buf);
665 wattr_off(tabline, pair, NULL);
667 wattr_on(tabline, tab_face.background, NULL);
668 if (TAILQ_NEXT(tab, tabs) != NULL)
669 wprintw(tabline, "┃");
670 wattr_off(tabline, tab_face.background, NULL);
673 wattr_on(tabline, tab_face.background, NULL);
674 for (; x < (size_t)COLS; ++x)
675 waddch(tabline, ' ');
676 if (truncated)
677 mvwprintw(tabline, 0, COLS-1, ">");
678 wattr_off(tabline, tab_face.background, NULL);
681 /*
682 * Compute the first visible line around vl. Try to search forward
683 * until the end of the buffer; if a visible line is not found, search
684 * backward. Return NULL if no viable line was found.
685 */
686 struct vline *
687 adjust_line(struct vline *vl, struct buffer *buffer)
689 struct vline *t;
691 if (vl == NULL)
692 return NULL;
694 if (!(vl->parent->flags & L_HIDDEN))
695 return vl;
697 /* search forward */
698 for (t = vl;
699 t != NULL && t->parent->flags & L_HIDDEN;
700 t = TAILQ_NEXT(t, vlines))
701 ; /* nop */
703 if (t != NULL)
704 return t;
706 /* search backward */
707 for (t = vl;
708 t != NULL && t->parent->flags & L_HIDDEN;
709 t = TAILQ_PREV(t, vhead, vlines))
710 ; /* nop */
712 return t;
715 static void
716 redraw_window(WINDOW *win, int off, int height, int width,
717 int show_fringe, struct buffer *buffer)
719 struct vline *vl;
720 int onscreen = 0, l = 0;
722 restore_curs_x(buffer);
724 /*
725 * TODO: ignoring buffer->force_update and always
726 * re-rendering. In theory we can recompute the y position
727 * without a re-render, and optimize here. It's not the only
728 * optimisation possible here, wscrl wolud also be an
729 * interesting one.
730 */
732 again:
733 werase(win);
734 buffer->curs_y = 0;
736 if (TAILQ_EMPTY(&buffer->head))
737 goto end;
739 if (buffer->top_line == NULL)
740 buffer->top_line = TAILQ_FIRST(&buffer->head);
742 buffer->top_line = adjust_line(buffer->top_line, buffer);
743 if (buffer->top_line == NULL)
744 goto end;
746 buffer->current_line = adjust_line(buffer->current_line, buffer);
748 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
749 if (vl->parent->flags & L_HIDDEN)
750 continue;
752 wmove(win, l, 0);
753 print_vline(off, width, win, vl);
755 if (vl == buffer->current_line)
756 onscreen = 1;
758 if (!onscreen)
759 buffer->curs_y++;
761 l++;
762 if (l == height)
763 break;
766 if (!onscreen) {
767 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
768 if (vl == buffer->current_line)
769 break;
770 if (vl->parent->flags & L_HIDDEN)
771 continue;
772 buffer->line_off++;
773 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
776 if (vl != NULL)
777 goto again;
780 buffer->last_line_off = buffer->line_off;
781 buffer->force_redraw = 0;
782 end:
783 for (; show_fringe && l < height; l++)
784 print_vline(off, width, win, &fringe);
786 wmove(win, buffer->curs_y, buffer->curs_x);
789 static void
790 redraw_download(void)
792 redraw_window(download, 0, download_lines, COLS, 0, &downloadwin);
795 static void
796 redraw_help(void)
798 redraw_window(help, 0, help_lines, help_cols, 1, &helpwin);
801 static void
802 redraw_body(struct tab *tab)
804 static struct tab *last_tab;
806 if (last_tab != tab)
807 tab->buffer.force_redraw =1;
808 last_tab = tab;
810 redraw_window(body, x_offset, body_lines, body_cols, 1, &tab->buffer);
813 static inline char
814 trust_status_char(enum trust_state ts)
816 switch (ts) {
817 case TS_UNKNOWN: return '-';
818 case TS_UNTRUSTED: return '!';
819 case TS_TEMP_TRUSTED: return '!';
820 case TS_TRUSTED: return 'v';
821 case TS_VERIFIED: return 'V';
822 default: return 'X';
826 static void
827 redraw_modeline(struct tab *tab)
829 struct buffer *buffer;
830 double pct;
831 int x, y, max_x, max_y;
832 const char *mode;
833 const char *spin = "-\\|/";
835 buffer = current_buffer();
836 mode = buffer->page.name;
838 werase(modeline);
839 wattr_on(modeline, modeline_face.background, NULL);
840 wmove(modeline, 0, 0);
842 wprintw(modeline, "-%c%c- %s ",
843 spin[tab->loading_anim_step],
844 trust_status_char(tab->trust),
845 mode == NULL ? "(none)" : mode);
847 pct = (buffer->line_off + buffer->curs_y) * 100.0
848 / buffer->line_max;
850 if (buffer->line_max <= (size_t)body_lines)
851 wprintw(modeline, "All ");
852 else if (buffer->line_off == 0)
853 wprintw(modeline, "Top ");
854 else if (buffer->line_off + body_lines >= buffer->line_max)
855 wprintw(modeline, "Bottom ");
856 else
857 wprintw(modeline, "%.0f%% ", pct);
859 wprintw(modeline, "%zu/%zu %s ",
860 buffer->line_off + buffer->curs_y,
861 buffer->line_max,
862 tab->hist_cur->h);
864 getyx(modeline, y, x);
865 getmaxyx(modeline, max_y, max_x);
867 (void)y;
868 (void)max_y;
870 for (; x < max_x; ++x)
871 waddstr(modeline, "-");
873 wattr_off(modeline, modeline_face.background, NULL);
876 static void
877 redraw_minibuffer(void)
879 wattr_on(echoarea, minibuffer_face.background, NULL);
880 werase(echoarea);
882 if (in_minibuffer)
883 do_redraw_minibuffer();
884 else
885 do_redraw_echoarea();
887 if (in_minibuffer == MB_COMPREAD)
888 do_redraw_minibuffer_compl();
890 wattr_off(echoarea, minibuffer_face.background, NULL);
893 static void
894 do_redraw_echoarea(void)
896 struct vline *vl;
898 if (ministate.curmesg != NULL)
899 wprintw(echoarea, "%s", ministate.curmesg);
900 else if (*keybuf != '\0')
901 waddstr(echoarea, keybuf);
902 else {
903 /* If nothing else, show the URL at point */
904 vl = current_tab->buffer.current_line;
905 if (vl != NULL && vl->parent->type == LINE_LINK)
906 waddstr(echoarea, vl->parent->alt);
910 static void
911 do_redraw_minibuffer(void)
913 struct buffer *cmplbuf, *buffer;
914 size_t off_y, off_x = 0;
915 const char *start, *c;
917 cmplbuf = &ministate.compl.buffer;
918 buffer = &ministate.buffer;
919 (void)off_y; /* unused, set by getyx */
921 wmove(echoarea, 0, 0);
923 if (in_minibuffer == MB_COMPREAD)
924 wprintw(echoarea, "(%2zu) ",
925 cmplbuf->line_max);
927 wprintw(echoarea, "%s", ministate.prompt);
928 if (ministate.hist_cur != NULL)
929 wprintw(echoarea, "(%zu/%zu) ",
930 ministate.hist_off + 1,
931 ministate.history->len);
933 getyx(echoarea, off_y, off_x);
935 start = ministate.hist_cur != NULL
936 ? ministate.hist_cur->h
937 : ministate.buf;
938 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
939 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
940 start = utf8_next_cp(start);
943 waddstr(echoarea, start);
945 if (ministate.curmesg != NULL)
946 wprintw(echoarea, " [%s]", ministate.curmesg);
948 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
951 static void
952 do_redraw_minibuffer_compl(void)
954 redraw_window(minibuffer, 0, 10, COLS, 1,
955 &ministate.compl.buffer);
958 /*
959 * Place the cursor in the right ncurses window. If soft is 1, use
960 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
961 * wrefresh.
962 */
963 static void
964 place_cursor(int soft)
966 int (*touch)(WINDOW *);
968 if (soft)
969 touch = wnoutrefresh;
970 else
971 touch = wrefresh;
973 if (in_minibuffer) {
974 if (side_window & SIDE_WINDOW_LEFT)
975 touch(help);
976 if (side_window & SIDE_WINDOW_BOTTOM)
977 touch(download);
978 touch(body);
979 touch(echoarea);
980 } else if (in_side_window & SIDE_WINDOW_LEFT) {
981 touch(body);
982 touch(echoarea);
983 if (in_side_window & SIDE_WINDOW_BOTTOM)
984 touch(download);
985 touch(help);
986 } else if (in_side_window & SIDE_WINDOW_BOTTOM) {
987 touch(body);
988 touch(echoarea);
989 if (in_side_window & SIDE_WINDOW_LEFT)
990 touch(help);
991 touch(download);
992 } else {
993 if (side_window & SIDE_WINDOW_LEFT)
994 touch(help);
995 if (side_window & SIDE_WINDOW_BOTTOM)
996 touch(download);
997 touch(echoarea);
998 touch(body);
1002 static void
1003 redraw_tab(struct tab *tab)
1005 if (too_small)
1006 return;
1008 if (side_window & SIDE_WINDOW_LEFT) {
1009 redraw_help();
1010 wnoutrefresh(help);
1013 if (side_window & SIDE_WINDOW_BOTTOM) {
1014 redraw_download();
1015 wnoutrefresh(download);
1018 if (show_tab_bar)
1019 redraw_tabline();
1021 redraw_body(tab);
1022 redraw_modeline(tab);
1023 redraw_minibuffer();
1025 wnoutrefresh(tabline);
1026 wnoutrefresh(modeline);
1028 if (in_minibuffer == MB_COMPREAD)
1029 wnoutrefresh(minibuffer);
1031 place_cursor(1);
1033 doupdate();
1035 if (set_title)
1036 dprintf(1, "\033]2;%s - Telescope\a",
1037 current_tab->buffer.page.title);
1040 void
1041 start_loading_anim(struct tab *tab)
1043 if (tab->loading_anim)
1044 return;
1045 tab->loading_anim = 1;
1046 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1047 evtimer_add(&tab->loadingev, &loadingev_timer);
1050 static void
1051 update_loading_anim(int fd, short ev, void *d)
1053 struct tab *tab = d;
1055 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1057 if (tab == current_tab) {
1058 redraw_modeline(tab);
1059 wrefresh(modeline);
1060 wrefresh(body);
1061 if (in_minibuffer)
1062 wrefresh(echoarea);
1065 evtimer_add(&tab->loadingev, &loadingev_timer);
1068 static void
1069 stop_loading_anim(struct tab *tab)
1071 if (!tab->loading_anim)
1072 return;
1073 evtimer_del(&tab->loadingev);
1074 tab->loading_anim = 0;
1075 tab->loading_anim_step = 0;
1077 if (tab != current_tab)
1078 return;
1080 redraw_modeline(tab);
1082 wrefresh(modeline);
1083 wrefresh(body);
1084 if (in_minibuffer)
1085 wrefresh(echoarea);
1088 int
1089 ui_print_colors(void)
1091 int colors = 0, pairs = 0, can_change = 0;
1092 int columns = 16, lines, color, i, j;
1094 initscr();
1095 if (has_colors()) {
1096 start_color();
1097 use_default_colors();
1099 colors = COLORS;
1100 pairs = COLOR_PAIRS;
1101 can_change = can_change_color();
1103 endwin();
1105 printf("Term info:\n");
1106 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1107 getenv("TERM"), colors, pairs, can_change);
1108 printf("\n");
1110 if (colors == 0) {
1111 printf("No color support\n");
1112 return 0;
1115 printf("Available colors:\n\n");
1116 lines = (colors - 1) / columns + 1;
1117 color = 0;
1118 for (i = 0; i < lines; ++i) {
1119 for (j = 0; j < columns; ++j, ++color) {
1120 printf("\033[0;38;5;%dm %03d", color, color);
1122 printf("\n");
1125 printf("\033[0m");
1126 fflush(stdout);
1127 return 0;
1130 int
1131 ui_init()
1133 setlocale(LC_ALL, "");
1135 if (TAILQ_EMPTY(&global_map.m)) {
1136 fprintf(stderr, "no keys defined!\n");
1137 return 0;
1140 minibuffer_init();
1142 /* initialize download window */
1143 TAILQ_INIT(&downloadwin.head);
1144 TAILQ_INIT(&downloadwin.page.head);
1146 /* initialize help window */
1147 TAILQ_INIT(&helpwin.head);
1148 TAILQ_INIT(&helpwin.page.head);
1150 base_map = &global_map;
1151 current_map = &global_map;
1153 initscr();
1155 if (enable_colors) {
1156 if (has_colors()) {
1157 start_color();
1158 use_default_colors();
1159 } else
1160 enable_colors = 0;
1163 config_apply_style();
1165 raw();
1166 noecho();
1167 nonl();
1168 intrflush(stdscr, FALSE);
1170 if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1171 return 0;
1172 if ((body = newwin(1, 1, 0, 0)) == NULL)
1173 return 0;
1174 if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1175 return 0;
1176 if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1177 return 0;
1178 if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1179 return 0;
1180 if ((download = newwin(1, 1, 0, 0)) == NULL)
1181 return 0;
1182 if ((help = newwin(1, 1, 0, 0)) == NULL)
1183 return 0;
1185 wbkgd(body, body_face.body);
1186 wbkgd(download, download_face.background);
1187 wbkgd(echoarea, minibuffer_face.background);
1189 update_x_offset();
1191 keypad(body, TRUE);
1192 scrollok(body, FALSE);
1194 /* non-blocking input */
1195 wtimeout(body, 0);
1196 wtimeout(help, 0);
1198 mvwprintw(body, 0, 0, "");
1200 return 1;
1203 void
1204 ui_main_loop(void)
1206 evtimer_set(&resizeev, handle_resize, NULL);
1207 evtimer_set(&download_refreshev, handle_download_refresh, NULL);
1209 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1210 event_add(&stdioev, NULL);
1212 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1213 signal_add(&winchev, NULL);
1215 switch_to_tab(current_tab);
1216 rearrange_windows();
1218 event_dispatch();
1221 void
1222 ui_on_tab_loaded(struct tab *tab)
1224 stop_loading_anim(tab);
1225 message("Loaded %s", tab->hist_cur->h);
1227 if (tab->hist_cur->current_off != 0 &&
1228 tab->buffer.current_line == TAILQ_FIRST(&tab->buffer.head)) {
1229 set_scroll_position(tab, tab->hist_cur->line_off,
1230 tab->hist_cur->current_off);
1231 redraw_tab(tab);
1232 return;
1235 if (show_tab_bar)
1236 redraw_tabline();
1238 wrefresh(tabline);
1239 place_cursor(0);
1242 void
1243 ui_on_tab_refresh(struct tab *tab)
1245 wrap_page(&tab->buffer, body_cols);
1246 if (tab == current_tab)
1247 redraw_tab(tab);
1248 else
1249 tab->flags |= TAB_URGENT;
1252 void
1253 ui_on_download_refresh(void)
1255 if (event_pending(&download_refreshev, EV_TIMEOUT, NULL))
1256 return;
1258 evtimer_set(&download_refreshev, handle_download_refresh, NULL);
1259 evtimer_add(&download_refreshev, &download_refresh_timer);
1262 void
1263 ui_remotely_open_link(const char *uri)
1265 new_tab(uri, NULL, NULL);
1266 ui_on_tab_refresh(current_tab);
1268 /* ring the bell */
1269 printf("\a");
1270 fflush(stdout);
1273 const char *
1274 ui_keyname(int k)
1276 return keyname(k);
1279 void
1280 ui_toggle_side_window(int kind)
1282 if (in_side_window & kind)
1283 ui_other_window();
1285 side_window ^= kind;
1286 if (side_window & SIDE_WINDOW_LEFT)
1287 recompute_help();
1288 if (side_window & SIDE_WINDOW_BOTTOM)
1289 recompute_downloads();
1292 * ugly hack, but otherwise the window doesn't get updated
1293 * until I call rearrange_windows a second time (e.g. via
1294 * C-l). I will be happy to know why something like this is
1295 * needed.
1297 rearrange_windows();
1298 rearrange_windows();
1301 void
1302 ui_show_downloads_pane(void)
1304 if (!(side_window & SIDE_WINDOW_BOTTOM))
1305 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1308 void
1309 ui_schedule_redraw(void)
1311 should_rearrange_windows = 1;
1314 void
1315 ui_require_input(struct tab *tab, int hide, void (*fn)(void))
1317 /* TODO: hard-switching to another tab is ugly */
1318 switch_to_tab(tab);
1320 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1321 &ir_history, NULL, NULL, 0);
1322 strlcpy(ministate.prompt, "Input required: ",
1323 sizeof(ministate.prompt));
1324 redraw_tab(tab);
1327 void
1328 ui_after_message_hook(void)
1330 redraw_minibuffer();
1331 place_cursor(0);
1334 void
1335 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1336 struct tab *data)
1338 yornp(prompt, fn, data);
1339 redraw_tab(current_tab);
1342 void
1343 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1344 struct tab *data, const char *input)
1346 minibuffer_read(prompt, fn, data);
1348 if (input != NULL) {
1349 strlcpy(ministate.buf, input, sizeof(ministate.buf));
1350 cmd_move_end_of_line(&ministate.buffer);
1353 redraw_tab(current_tab);
1356 void
1357 ui_other_window(void)
1359 if (in_side_window & SIDE_WINDOW_LEFT &&
1360 side_window & SIDE_WINDOW_BOTTOM)
1361 in_side_window = SIDE_WINDOW_BOTTOM;
1362 else if (in_side_window)
1363 in_side_window = 0;
1364 else if (!in_side_window && side_window & SIDE_WINDOW_LEFT)
1365 in_side_window = SIDE_WINDOW_LEFT;
1366 else if (!in_side_window && side_window)
1367 in_side_window = SIDE_WINDOW_BOTTOM;
1368 else
1369 message("No other window to select");
1372 void
1373 ui_suspend(void)
1375 endwin();
1377 kill(getpid(), SIGSTOP);
1379 refresh();
1380 clear();
1381 rearrange_windows();
1384 void
1385 ui_end(void)
1387 endwin();