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 restore_curs_x(struct buffer *);
55 static int readkey(void);
56 static void dispatch_stdio(int, short, void*);
57 static void handle_resize(int, short, void*);
58 static void handle_resize_nodelay(int, short, void*);
59 static void rearrange_windows(void);
60 static void line_prefix_and_text(struct vline *, char *, size_t, const char **, const char **);
61 static void print_vline(int, int, WINDOW*, struct vline*);
62 static void redraw_tabline(void);
63 static void redraw_window(WINDOW*, int, int, int, struct buffer*);
64 static void redraw_help(void);
65 static void redraw_body(struct tab*);
66 static void redraw_modeline(struct tab*);
67 static void redraw_minibuffer(void);
68 static void do_redraw_echoarea(void);
69 static void do_redraw_minibuffer(void);
70 static void do_redraw_minibuffer_compl(void);
71 static void place_cursor(int);
72 static void redraw_tab(struct tab*);
73 static void update_loading_anim(int, short, void*);
74 static void stop_loading_anim(struct tab*);
76 static int should_rearrange_windows;
77 static int show_tab_bar;
78 static int too_small;
79 static int x_offset;
81 struct thiskey thiskey;
82 struct tab *current_tab;
84 static struct event resizeev;
85 static struct timeval resize_timer = { 0, 250000 };
87 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
89 int body_lines, body_cols;
91 static WINDOW *help;
92 /* not static so we can see them from help.c */
93 struct buffer helpwin;
94 int help_lines, help_cols;
96 static int side_window;
97 static int in_side_window;
99 static struct timeval loadingev_timer = { 0, 250000 };
101 static char keybuf[64];
103 /* XXX: don't forget to init these in main() */
104 struct kmap global_map,
105 minibuffer_map,
106 *current_map,
107 *base_map;
109 static inline void
110 update_x_offset(void)
112 if (olivetti_mode && fill_column < body_cols)
113 x_offset = (body_cols - fill_column)/2;
114 else
115 x_offset = 0;
118 void
119 save_excursion(struct excursion *place, struct buffer *buffer)
121 place->curs_x = buffer->curs_x;
122 place->curs_y = buffer->curs_y;
123 place->line_off = buffer->line_off;
124 place->top_line = buffer->top_line;
125 place->current_line = buffer->current_line;
126 place->cpoff = buffer->cpoff;
129 void
130 restore_excursion(struct excursion *place, struct buffer *buffer)
132 buffer->curs_x = place->curs_x;
133 buffer->curs_y = place->curs_y;
134 buffer->line_off = place->line_off;
135 buffer->top_line = place->top_line;
136 buffer->current_line = place->current_line;
137 buffer->cpoff = place->cpoff;
140 static void
141 restore_curs_x(struct buffer *buffer)
143 struct vline *vl;
144 const char *prfx, *text;
146 vl = buffer->current_line;
147 if (vl == NULL || vl->line == NULL)
148 buffer->curs_x = buffer->cpoff = 0;
149 else if (vl->parent->data != NULL) {
150 text = vl->parent->data;
151 buffer->curs_x = utf8_snwidth(text + 1, buffer->cpoff) + 1;
152 } else
153 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
155 buffer->curs_x += x_offset;
157 if (vl == NULL)
158 return;
160 if (vl->parent->data != NULL)
161 buffer->curs_x += utf8_swidth_between(vl->parent->line,
162 vl->parent->data);
163 else {
164 prfx = line_prefixes[vl->parent->type].prfx1;
165 buffer->curs_x += utf8_swidth(prfx);
169 void
170 global_key_unbound(void)
172 message("%s is undefined", keybuf);
175 struct buffer *
176 current_buffer(void)
178 if (in_minibuffer)
179 return &ministate.buffer;
180 if (in_side_window)
181 return &helpwin;
182 return &current_tab->buffer;
185 static int
186 readkey(void)
188 uint32_t state = 0;
190 if ((thiskey.key = wgetch(body)) == ERR)
191 return 0;
193 thiskey.meta = thiskey.key == 27;
194 if (thiskey.meta) {
195 thiskey.key = wgetch(body);
196 if (thiskey.key == ERR || thiskey.key == 27) {
197 thiskey.meta = 0;
198 thiskey.key = 27;
202 thiskey.cp = 0;
204 if ((unsigned int)thiskey.key >= UINT8_MAX)
205 return 1;
207 while (1) {
208 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
209 break;
210 if ((thiskey.key = wgetch(body)) == ERR) {
211 message("Error decoding user input");
212 return 0;
216 return 1;
219 static void
220 dispatch_stdio(int fd, short ev, void *d)
222 struct keymap *k;
223 const char *keyname;
224 char tmp[5] = {0};
226 /* TODO: schedule a redraw? */
227 if (too_small)
228 return;
230 if (!readkey())
231 return;
233 if (keybuf[0] != '\0')
234 strlcat(keybuf, " ", sizeof(keybuf));
235 if (thiskey.meta)
236 strlcat(keybuf, "M-", sizeof(keybuf));
237 if (thiskey.cp != 0) {
238 utf8_encode(thiskey.cp, tmp);
239 strlcat(keybuf, tmp, sizeof(keybuf));
240 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
241 strlcat(keybuf, keyname, sizeof(keybuf));
242 } else {
243 tmp[0] = thiskey.key;
244 strlcat(keybuf, tmp, sizeof(keybuf));
247 TAILQ_FOREACH(k, &current_map->m, keymaps) {
248 if (k->meta == thiskey.meta &&
249 k->key == thiskey.key) {
250 if (k->fn == NULL)
251 current_map = &k->map;
252 else {
253 current_map = base_map;
254 strlcpy(keybuf, "", sizeof(keybuf));
255 k->fn(current_buffer());
257 goto done;
261 if (current_map->unhandled_input != NULL)
262 current_map->unhandled_input();
263 else
264 global_key_unbound();
266 strlcpy(keybuf, "", sizeof(keybuf));
267 current_map = base_map;
269 done:
270 if (side_window)
271 recompute_help();
273 if (should_rearrange_windows)
274 rearrange_windows();
275 redraw_tab(current_tab);
278 static void
279 handle_resize(int sig, short ev, void *d)
281 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
282 event_del(&resizeev);
284 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
285 evtimer_add(&resizeev, &resize_timer);
288 static void
289 handle_resize_nodelay(int s, short ev, void *d)
291 endwin();
292 refresh();
293 clear();
295 rearrange_windows();
298 static inline int
299 should_show_tab_bar(void)
301 if (tab_bar_show == -1)
302 return 0;
303 if (tab_bar_show == 0)
304 return 1;
306 return TAILQ_NEXT(TAILQ_FIRST(&tabshead), tabs) != NULL;
309 static void
310 rearrange_windows(void)
312 int lines;
314 should_rearrange_windows = 0;
315 show_tab_bar = should_show_tab_bar();
317 lines = LINES;
319 if ((too_small = lines < 15)) {
320 erase();
321 printw("Window too small.");
322 refresh();
323 return;
326 /* move and resize the windows, in reverse order! */
328 if (in_minibuffer == MB_COMPREAD) {
329 mvwin(minibuffer, lines-10, 0);
330 wresize(minibuffer, 10, COLS);
331 lines -= 10;
333 wrap_page(&ministate.compl.buffer, COLS);
336 mvwin(echoarea, --lines, 0);
337 wresize(echoarea, 1, COLS);
339 mvwin(modeline, --lines, 0);
340 wresize(modeline, 1, COLS);
342 body_lines = show_tab_bar ? --lines : lines;
343 body_cols = COLS;
345 /*
346 * Here we make the assumption that show_tab_bar is either 0
347 * or 1, and reuse that as argument to mvwin.
348 */
349 if (side_window) {
350 help_cols = 0.3 * COLS;
351 help_lines = lines;
352 mvwin(help, show_tab_bar, 0);
353 wresize(help, help_lines, help_cols);
355 wrap_page(&helpwin, help_cols);
357 body_cols = COLS - help_cols - 1;
358 mvwin(body, show_tab_bar, help_cols);
359 } else
360 mvwin(body, show_tab_bar, 0);
362 update_x_offset();
363 wresize(body, body_lines, body_cols);
365 if (show_tab_bar)
366 wresize(tabline, 1, COLS);
368 wrap_page(&current_tab->buffer, body_cols);
369 redraw_tab(current_tab);
372 static void
373 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
374 const char **prfx_ret, const char **text_ret)
376 int type, i, cont, width;
377 char *space, *t;
379 if ((*text_ret = vl->line) == NULL)
380 *text_ret = "";
382 cont = vl->flags & L_CONTINUATION;
383 type = vl->parent->type;
384 if (!cont)
385 *prfx_ret = line_prefixes[type].prfx1;
386 else
387 *prfx_ret = line_prefixes[type].prfx2;
389 space = vl->parent->data;
390 if (!emojify_link || type != LINE_LINK || space == NULL)
391 return;
393 if (cont) {
394 memset(buf, 0, len);
395 width = utf8_swidth_between(vl->parent->line, space);
396 for (i = 0; i < width + 1; ++i)
397 strlcat(buf, " ", len);
398 } else {
399 strlcpy(buf, *text_ret, len);
400 if ((t = strchr(buf, ' ')) != NULL)
401 *t = '\0';
402 strlcat(buf, " ", len);
404 /* skip the emoji */
405 *text_ret += (space - vl->parent->line) + 1;
408 *prfx_ret = buf;
411 static inline void
412 print_vline_descr(int width, WINDOW *window, struct vline *vl)
414 int x, y, goal;
416 if (vl->parent->type != LINE_COMPL &&
417 vl->parent->type != LINE_COMPL_CURRENT &&
418 vl->parent->type != LINE_HELP)
419 return;
421 if (vl->parent->alt == NULL)
422 return;
424 (void)y;
425 getyx(window, y, x);
427 if (vl->parent->type == LINE_HELP)
428 goal = 8;
429 else
430 goal = width/2;
432 if (goal <= x)
433 wprintw(window, " ");
434 for (; goal > x; ++x)
435 wprintw(window, " ");
437 wprintw(window, "%s", vl->parent->alt);
440 static inline void
441 print_line_break(int width, WINDOW *window)
443 int x, y;
445 getyx(window, y, x);
446 mvwprintw(window, y, width/2 - 4, "-*-*-");
449 /*
450 * Core part of the rendering. It prints a vline starting from the
451 * current cursor position. Printing a vline consists of skipping
452 * `off' columns (for olivetti-mode), print the correct prefix (which
453 * may be the emoji in case of emojified links-lines), printing the
454 * text itself, filling until width - off and filling off columns
455 * again.
456 */
457 static void
458 print_vline(int off, int width, WINDOW *window, struct vline *vl)
460 /*
461 * Believe me or not, I've seen emoji ten code points long!
462 * That means, to stay large, 4*10 bytes + NUL.
463 */
464 char emojibuf[41] = {0};
465 const char *text, *prfx;
466 struct line_face *f;
467 int i, left, x, y;
469 f = &line_faces[vl->parent->type];
471 /* unused, set by getyx */
472 (void)y;
474 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
476 wattr_on(window, body_face.left, NULL);
477 for (i = 0; i < off; i++)
478 waddch(window, ' ');
479 wattr_off(window, body_face.left, NULL);
481 if (vl->parent->type == LINE_BREAK) {
482 print_line_break(width, window);
483 } else {
484 wattr_on(window, f->prefix, NULL);
485 wprintw(window, "%s", prfx);
486 wattr_off(window, f->prefix, NULL);
489 wattr_on(window, f->text, NULL);
490 wprintw(window, "%s", text);
491 print_vline_descr(width, window, vl);
492 wattr_off(window, f->text, NULL);
494 getyx(window, y, x);
496 left = width - x;
498 wattr_on(window, f->trail, NULL);
499 for (i = 0; i < left - off; ++i)
500 waddch(window, ' ');
501 wattr_off(window, f->trail, NULL);
503 wattr_on(window, body_face.right, NULL);
504 for (i = 0; i < off; i++)
505 waddch(window, ' ');
506 wattr_off(window, body_face.right, NULL);
510 static void
511 redraw_tabline(void)
513 struct tab *tab;
514 size_t toskip, ots, tabwidth, space, x;
515 int current, y, truncated, pair;
516 const char *title;
517 char buf[25];
519 x = 0;
521 /* unused, but setted by a getyx */
522 (void)y;
524 tabwidth = sizeof(buf)+1;
525 space = COLS-2;
527 toskip = 0;
528 TAILQ_FOREACH(tab, &tabshead, tabs) {
529 toskip++;
530 if (tab == current_tab)
531 break;
534 if (toskip * tabwidth < space)
535 toskip = 0;
536 else {
537 ots = toskip;
538 toskip--;
539 while (toskip != 0 &&
540 (ots - toskip+1) * tabwidth < space)
541 toskip--;
544 werase(tabline);
545 wattr_on(tabline, tab_face.background, NULL);
546 wprintw(tabline, toskip == 0 ? " " : "<");
547 wattr_off(tabline, tab_face.background, NULL);
549 truncated = 0;
550 TAILQ_FOREACH(tab, &tabshead, tabs) {
551 if (truncated)
552 break;
553 if (toskip != 0) {
554 toskip--;
555 continue;
558 getyx(tabline, y, x);
559 if (x + sizeof(buf)+2 >= (size_t)COLS)
560 truncated = 1;
562 current = tab == current_tab;
564 if (*(title = tab->buffer.page.title) == '\0')
565 title = tab->hist_cur->h;
567 if (tab->flags & TAB_URGENT)
568 strlcpy(buf, "!", sizeof(buf));
569 else
570 strlcpy(buf, " ", sizeof(buf));
572 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
573 /* truncation happens */
574 strlcpy(&buf[sizeof(buf)-4], "...", 4);
575 } else {
576 /* pad with spaces */
577 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
578 /* nop */ ;
581 pair = current ? tab_face.current : tab_face.tab;
582 wattr_on(tabline, pair, NULL);
583 wprintw(tabline, "%s", buf);
584 wattr_off(tabline, pair, NULL);
586 wattr_on(tabline, tab_face.background, NULL);
587 if (TAILQ_NEXT(tab, tabs) != NULL)
588 wprintw(tabline, "┃");
589 wattr_off(tabline, tab_face.background, NULL);
592 wattr_on(tabline, tab_face.background, NULL);
593 for (; x < (size_t)COLS; ++x)
594 waddch(tabline, ' ');
595 if (truncated)
596 mvwprintw(tabline, 0, COLS-1, ">");
597 wattr_off(tabline, tab_face.background, NULL);
600 /*
601 * Compute the first visible line around vl. Try to search forward
602 * until the end of the buffer; if a visible line is not found, search
603 * backward. Return NULL if no viable line was found.
604 */
605 struct vline *
606 adjust_line(struct vline *vl, struct buffer *buffer)
608 struct vline *t;
610 if (vl == NULL)
611 return NULL;
613 if (!(vl->parent->flags & L_HIDDEN))
614 return vl;
616 /* search forward */
617 for (t = vl;
618 t != NULL && t->parent->flags & L_HIDDEN;
619 t = TAILQ_NEXT(t, vlines))
620 ; /* nop */
622 if (t != NULL)
623 return t;
625 /* search backward */
626 for (t = vl;
627 t != NULL && t->parent->flags & L_HIDDEN;
628 t = TAILQ_PREV(t, vhead, vlines))
629 ; /* nop */
631 return t;
634 static void
635 redraw_window(WINDOW *win, int off, int height, int width,
636 struct buffer *buffer)
638 struct vline *vl;
639 int l, onscreen;
641 restore_curs_x(buffer);
643 /*
644 * TODO: ignoring buffer->force_update and always
645 * re-rendering. In theory we can recompute the y position
646 * without a re-render, and optimize here. It's not the only
647 * optimisation possible here, wscrl wolud also be an
648 * interesting one.
649 */
651 again:
652 werase(win);
653 buffer->curs_y = 0;
655 if (TAILQ_EMPTY(&buffer->head))
656 goto end;
658 if (buffer->top_line == NULL)
659 buffer->top_line = TAILQ_FIRST(&buffer->head);
661 buffer->top_line = adjust_line(buffer->top_line, buffer);
662 if (buffer->top_line == NULL)
663 goto end;
665 buffer->current_line = adjust_line(buffer->current_line, buffer);
667 l = 0;
668 onscreen = 0;
669 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
670 if (vl->parent->flags & L_HIDDEN)
671 continue;
673 wmove(win, l, 0);
674 print_vline(off, width, win, vl);
676 if (vl == buffer->current_line)
677 onscreen = 1;
679 if (!onscreen)
680 buffer->curs_y++;
682 l++;
683 if (l == height)
684 break;
687 if (!onscreen) {
688 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
689 if (vl == buffer->current_line)
690 break;
691 if (vl->parent->flags & L_HIDDEN)
692 continue;
693 buffer->line_off++;
694 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
697 if (vl != NULL)
698 goto again;
701 buffer->last_line_off = buffer->line_off;
702 buffer->force_redraw = 0;
703 end:
704 wmove(win, buffer->curs_y, buffer->curs_x);
707 static void
708 redraw_help(void)
710 redraw_window(help, 0, help_lines, help_cols, &helpwin);
713 static void
714 redraw_body(struct tab *tab)
716 static struct tab *last_tab;
718 if (last_tab != tab)
719 tab->buffer.force_redraw =1;
720 last_tab = tab;
722 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
725 static inline char
726 trust_status_char(enum trust_state ts)
728 switch (ts) {
729 case TS_UNKNOWN: return '-';
730 case TS_UNTRUSTED: return '!';
731 case TS_TEMP_TRUSTED: return '!';
732 case TS_TRUSTED: return 'v';
733 case TS_VERIFIED: return 'V';
734 default: return 'X';
738 static void
739 redraw_modeline(struct tab *tab)
741 struct buffer *buffer;
742 double pct;
743 int x, y, max_x, max_y;
744 const char *mode;
745 const char *spin = "-\\|/";
747 buffer = current_buffer();
748 mode = buffer->page.name;
750 werase(modeline);
751 wattr_on(modeline, modeline_face.background, NULL);
752 wmove(modeline, 0, 0);
754 wprintw(modeline, "-%c%c %s ",
755 spin[tab->loading_anim_step],
756 trust_status_char(tab->trust),
757 mode == NULL ? "(none)" : mode);
759 pct = (buffer->line_off + buffer->curs_y) * 100.0
760 / buffer->line_max;
762 if (buffer->line_max <= (size_t)body_lines)
763 wprintw(modeline, "All ");
764 else if (buffer->line_off == 0)
765 wprintw(modeline, "Top ");
766 else if (buffer->line_off + body_lines >= buffer->line_max)
767 wprintw(modeline, "Bottom ");
768 else
769 wprintw(modeline, "%.0f%% ", pct);
771 wprintw(modeline, "%d/%d %s ",
772 buffer->line_off + buffer->curs_y,
773 buffer->line_max,
774 tab->hist_cur->h);
776 getyx(modeline, y, x);
777 getmaxyx(modeline, max_y, max_x);
779 (void)y;
780 (void)max_y;
782 for (; x < max_x; ++x)
783 waddstr(modeline, "-");
785 wattr_off(modeline, modeline_face.background, NULL);
788 static void
789 redraw_minibuffer(void)
791 wattr_on(echoarea, minibuffer_face.background, NULL);
792 werase(echoarea);
794 if (in_minibuffer)
795 do_redraw_minibuffer();
796 else
797 do_redraw_echoarea();
799 if (in_minibuffer == MB_COMPREAD)
800 do_redraw_minibuffer_compl();
802 wattr_off(echoarea, minibuffer_face.background, NULL);
805 static void
806 do_redraw_echoarea(void)
808 struct vline *vl;
810 if (ministate.curmesg != NULL)
811 wprintw(echoarea, "%s", ministate.curmesg);
812 else if (*keybuf != '\0')
813 waddstr(echoarea, keybuf);
814 else {
815 /* If nothing else, show the URL at point */
816 vl = current_tab->buffer.current_line;
817 if (vl != NULL && vl->parent->type == LINE_LINK)
818 waddstr(echoarea, vl->parent->alt);
822 static void
823 do_redraw_minibuffer(void)
825 struct buffer *cmplbuf, *buffer;
826 size_t off_y, off_x = 0;
827 const char *start, *c;
829 cmplbuf = &ministate.compl.buffer;
830 buffer = &ministate.buffer;
831 (void)off_y; /* unused, set by getyx */
833 wmove(echoarea, 0, 0);
835 if (in_minibuffer == MB_COMPREAD)
836 wprintw(echoarea, "(%2d) ",
837 cmplbuf->line_max);
839 wprintw(echoarea, "%s", ministate.prompt);
840 if (ministate.hist_cur != NULL)
841 wprintw(echoarea, "(%zu/%zu) ",
842 ministate.hist_off + 1,
843 ministate.history->len);
845 getyx(echoarea, off_y, off_x);
847 start = ministate.hist_cur != NULL
848 ? ministate.hist_cur->h
849 : ministate.buf;
850 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
851 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
852 start = utf8_next_cp(start);
855 waddstr(echoarea, start);
857 if (ministate.curmesg != NULL)
858 wprintw(echoarea, " [%s]", ministate.curmesg);
860 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
863 static void
864 do_redraw_minibuffer_compl(void)
866 redraw_window(minibuffer, 0, 10, COLS,
867 &ministate.compl.buffer);
870 /*
871 * Place the cursor in the right ncurses window. If soft is 1, use
872 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
873 * wrefresh.
874 */
875 static void
876 place_cursor(int soft)
878 int (*touch)(WINDOW *);
880 if (soft)
881 touch = wnoutrefresh;
882 else
883 touch = wrefresh;
885 if (in_minibuffer) {
886 if (side_window)
887 touch(help);
888 touch(body);
889 touch(echoarea);
890 } else if (in_side_window) {
891 touch(body);
892 touch(echoarea);
893 touch(help);
894 } else {
895 if (side_window)
896 touch(help);
897 touch(echoarea);
898 touch(body);
902 static void
903 redraw_tab(struct tab *tab)
905 if (too_small)
906 return;
908 if (side_window) {
909 redraw_help();
910 wnoutrefresh(help);
913 if (show_tab_bar)
914 redraw_tabline();
916 redraw_body(tab);
917 redraw_modeline(tab);
918 redraw_minibuffer();
920 wnoutrefresh(tabline);
921 wnoutrefresh(modeline);
923 if (in_minibuffer == MB_COMPREAD)
924 wnoutrefresh(minibuffer);
926 place_cursor(1);
928 doupdate();
930 if (set_title)
931 dprintf(1, "\033]2;%s - Telescope\a",
932 current_tab->buffer.page.title);
935 void
936 start_loading_anim(struct tab *tab)
938 if (tab->loading_anim)
939 return;
940 tab->loading_anim = 1;
941 evtimer_set(&tab->loadingev, update_loading_anim, tab);
942 evtimer_add(&tab->loadingev, &loadingev_timer);
945 static void
946 update_loading_anim(int fd, short ev, void *d)
948 struct tab *tab = d;
950 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
952 if (tab == current_tab) {
953 redraw_modeline(tab);
954 wrefresh(modeline);
955 wrefresh(body);
956 if (in_minibuffer)
957 wrefresh(echoarea);
960 evtimer_add(&tab->loadingev, &loadingev_timer);
963 static void
964 stop_loading_anim(struct tab *tab)
966 if (!tab->loading_anim)
967 return;
968 evtimer_del(&tab->loadingev);
969 tab->loading_anim = 0;
970 tab->loading_anim_step = 0;
972 if (tab != current_tab)
973 return;
975 redraw_modeline(tab);
977 wrefresh(modeline);
978 wrefresh(body);
979 if (in_minibuffer)
980 wrefresh(echoarea);
983 int
984 ui_print_colors(void)
986 int colors = 0, pairs = 0, can_change = 0;
987 int columns = 16, lines, color, i, j;
989 initscr();
990 if (has_colors()) {
991 start_color();
992 use_default_colors();
994 colors = COLORS;
995 pairs = COLOR_PAIRS;
996 can_change = can_change_color();
998 endwin();
1000 printf("Term info:\n");
1001 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1002 getenv("TERM"), colors, pairs, can_change);
1003 printf("\n");
1005 if (colors == 0) {
1006 printf("No color support\n");
1007 return 0;
1010 printf("Available colors:\n\n");
1011 lines = (colors - 1) / columns + 1;
1012 color = 0;
1013 for (i = 0; i < lines; ++i) {
1014 for (j = 0; j < columns; ++j, ++color) {
1015 printf("\033[0;38;5;%dm %03d", color, color);
1017 printf("\n");
1020 printf("\033[0m");
1021 fflush(stdout);
1022 return 0;
1025 int
1026 ui_init()
1028 setlocale(LC_ALL, "");
1030 if (TAILQ_EMPTY(&global_map.m)) {
1031 fprintf(stderr, "no keys defined!\n");
1032 return 0;
1035 minibuffer_init();
1037 /* initialize help window */
1038 TAILQ_INIT(&helpwin.head);
1039 TAILQ_INIT(&helpwin.page.head);
1041 base_map = &global_map;
1042 current_map = &global_map;
1044 initscr();
1046 if (enable_colors) {
1047 if (has_colors()) {
1048 start_color();
1049 use_default_colors();
1050 } else
1051 enable_colors = 0;
1054 config_apply_style();
1056 raw();
1057 noecho();
1058 nonl();
1059 intrflush(stdscr, FALSE);
1061 if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1062 return 0;
1063 if ((body = newwin(1, 1, 0, 0)) == NULL)
1064 return 0;
1065 if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1066 return 0;
1067 if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1068 return 0;
1069 if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1070 return 0;
1071 if ((help = newwin(1, 1, 0, 0)) == NULL)
1072 return 0;
1074 wbkgd(body, body_face.body);
1075 wbkgd(echoarea, minibuffer_face.background);
1077 update_x_offset();
1079 keypad(body, TRUE);
1080 scrollok(body, FALSE);
1082 /* non-blocking input */
1083 wtimeout(body, 0);
1084 wtimeout(help, 0);
1086 mvwprintw(body, 0, 0, "");
1088 evtimer_set(&resizeev, handle_resize, NULL);
1090 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1091 event_add(&stdioev, NULL);
1093 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1094 signal_add(&winchev, NULL);
1096 return 1;
1099 void
1100 ui_main_loop(void)
1102 switch_to_tab(current_tab);
1103 rearrange_windows();
1105 event_dispatch();
1108 void
1109 ui_on_tab_loaded(struct tab *tab)
1111 stop_loading_anim(tab);
1112 message("Loaded %s", tab->hist_cur->h);
1114 if (show_tab_bar)
1115 redraw_tabline();
1117 wrefresh(tabline);
1118 place_cursor(0);
1121 void
1122 ui_on_tab_refresh(struct tab *tab)
1124 wrap_page(&tab->buffer, body_cols);
1125 if (tab == current_tab)
1126 redraw_tab(tab);
1127 else
1128 tab->flags |= TAB_URGENT;
1131 const char *
1132 ui_keyname(int k)
1134 return keyname(k);
1137 void
1138 ui_toggle_side_window(void)
1140 side_window = !side_window;
1141 if (side_window)
1142 recompute_help();
1143 else
1144 in_side_window = 0;
1147 * ugly hack, but otherwise the window doesn't get updated
1148 * until I call rearrange_windows a second time (e.g. via
1149 * C-l). I will be happy to know why something like this is
1150 * needed.
1152 rearrange_windows();
1153 rearrange_windows();
1156 void
1157 ui_schedule_redraw(void)
1159 should_rearrange_windows = 1;
1162 void
1163 ui_require_input(struct tab *tab, int hide, int proto)
1165 void (*fn)(void);
1167 if (proto == PROTO_GEMINI)
1168 fn = ir_select_gemini;
1169 else if (proto == PROTO_GOPHER)
1170 fn = ir_select_gopher;
1171 else
1172 abort();
1174 /* TODO: hard-switching to another tab is ugly */
1175 switch_to_tab(tab);
1177 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1178 &ir_history, NULL, NULL);
1179 strlcpy(ministate.prompt, "Input required: ",
1180 sizeof(ministate.prompt));
1181 redraw_tab(tab);
1184 void
1185 ui_after_message_hook(void)
1187 redraw_minibuffer();
1188 place_cursor(0);
1191 void
1192 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1193 struct tab *data)
1195 yornp(prompt, fn, data);
1196 redraw_tab(current_tab);
1199 void
1200 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1201 struct tab *data, const char *input)
1203 minibuffer_read(prompt, fn, data);
1205 if (input != NULL) {
1206 strlcpy(ministate.buf, input, sizeof(ministate.buf));
1207 cmd_move_end_of_line(&ministate.buffer);
1210 redraw_tab(current_tab);
1213 void
1214 ui_other_window(void)
1216 if (side_window)
1217 in_side_window = !in_side_window;
1218 else
1219 message("No other window to select");
1222 void
1223 ui_suspend(void)
1225 endwin();
1227 kill(getpid(), SIGSTOP);
1229 refresh();
1230 clear();
1231 rearrange_windows();
1234 void
1235 ui_end(void)
1237 endwin();