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 /* 3 lines for the ui and 12 for the */
320 if ((too_small = lines < 15)) {
321 erase();
322 printw("Window too small.");
323 refresh();
324 return;
327 /* move and resize the windows, in reverse order! */
329 if (in_minibuffer == MB_COMPREAD) {
330 mvwin(minibuffer, lines-10, 0);
331 wresize(minibuffer, 10, COLS);
332 lines -= 10;
334 wrap_page(&ministate.compl.buffer, COLS);
337 mvwin(echoarea, --lines, 0);
338 wresize(echoarea, 1, COLS);
340 mvwin(modeline, --lines, 0);
341 wresize(modeline, 1, COLS);
343 body_lines = show_tab_bar ? --lines : lines;
344 body_cols = COLS;
346 /*
347 * Here we make the assumption that show_tab_bar is either 0
348 * or 1, and reuse that as argument to mvwin.
349 */
350 if (side_window) {
351 help_cols = 0.3 * COLS;
352 help_lines = lines;
353 mvwin(help, show_tab_bar, 0);
354 wresize(help, help_lines, help_cols);
356 wrap_page(&helpwin, help_cols);
358 body_cols = COLS - help_cols - 1;
359 mvwin(body, show_tab_bar, help_cols);
360 } else
361 mvwin(body, show_tab_bar, 0);
363 update_x_offset();
364 wresize(body, body_lines, body_cols);
366 if (show_tab_bar)
367 wresize(tabline, 1, COLS);
369 wrap_page(&current_tab->buffer, body_cols);
370 redraw_tab(current_tab);
373 static void
374 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
375 const char **prfx_ret, const char **text_ret)
377 int type, i, cont, width;
378 char *space, *t;
380 if ((*text_ret = vl->line) == NULL)
381 *text_ret = "";
383 cont = vl->flags & L_CONTINUATION;
384 type = vl->parent->type;
385 if (!cont)
386 *prfx_ret = line_prefixes[type].prfx1;
387 else
388 *prfx_ret = line_prefixes[type].prfx2;
390 space = vl->parent->data;
391 if (!emojify_link || type != LINE_LINK || space == NULL)
392 return;
394 if (cont) {
395 memset(buf, 0, len);
396 width = utf8_swidth_between(vl->parent->line, space);
397 for (i = 0; i < width + 1; ++i)
398 strlcat(buf, " ", len);
399 } else {
400 strlcpy(buf, *text_ret, len);
401 if ((t = strchr(buf, ' ')) != NULL)
402 *t = '\0';
403 strlcat(buf, " ", len);
405 /* skip the emoji */
406 *text_ret += (space - vl->parent->line) + 1;
409 *prfx_ret = buf;
412 static inline void
413 print_vline_descr(int width, WINDOW *window, struct vline *vl)
415 int x, y, goal;
417 if (vl->parent->type != LINE_COMPL &&
418 vl->parent->type != LINE_COMPL_CURRENT &&
419 vl->parent->type != LINE_HELP)
420 return;
422 if (vl->parent->alt == NULL)
423 return;
425 (void)y;
426 getyx(window, y, x);
428 if (vl->parent->type == LINE_HELP)
429 goal = 8;
430 else
431 goal = width/2;
433 if (goal <= x)
434 wprintw(window, " ");
435 for (; goal > x; ++x)
436 wprintw(window, " ");
438 wprintw(window, "%s", vl->parent->alt);
441 /*
442 * Core part of the rendering. It prints a vline starting from the
443 * current cursor position. Printing a vline consists of skipping
444 * `off' columns (for olivetti-mode), print the correct prefix (which
445 * may be the emoji in case of emojified links-lines), printing the
446 * text itself, filling until width - off and filling off columns
447 * again.
448 */
449 static void
450 print_vline(int off, int width, WINDOW *window, struct vline *vl)
452 /*
453 * Believe me or not, I've seen emoji ten code points long!
454 * That means, to stay large, 4*10 bytes + NUL.
455 */
456 char emojibuf[41] = {0};
457 const char *text, *prfx;
458 struct line_face *f;
459 int i, left, x, y;
461 f = &line_faces[vl->parent->type];
463 /* unused, set by getyx */
464 (void)y;
466 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
468 wattr_on(window, body_face.left, NULL);
469 for (i = 0; i < off; i++)
470 waddch(window, ' ');
471 wattr_off(window, body_face.left, NULL);
473 wattr_on(window, f->prefix, NULL);
474 wprintw(window, "%s", prfx);
475 wattr_off(window, f->prefix, NULL);
477 wattr_on(window, f->text, NULL);
478 wprintw(window, "%s", text);
479 print_vline_descr(width, window, vl);
480 wattr_off(window, f->text, NULL);
482 getyx(window, y, x);
484 left = width - x;
486 wattr_on(window, f->trail, NULL);
487 for (i = 0; i < left - off; ++i)
488 waddch(window, ' ');
489 wattr_off(window, f->trail, NULL);
491 wattr_on(window, body_face.right, NULL);
492 for (i = 0; i < off; i++)
493 waddch(window, ' ');
494 wattr_off(window, body_face.right, NULL);
498 static void
499 redraw_tabline(void)
501 struct tab *tab;
502 size_t toskip, ots, tabwidth, space, x;
503 int current, y, truncated, pair;
504 const char *title;
505 char buf[25];
507 x = 0;
509 /* unused, but setted by a getyx */
510 (void)y;
512 tabwidth = sizeof(buf)+1;
513 space = COLS-2;
515 toskip = 0;
516 TAILQ_FOREACH(tab, &tabshead, tabs) {
517 toskip++;
518 if (tab == current_tab)
519 break;
522 if (toskip * tabwidth < space)
523 toskip = 0;
524 else {
525 ots = toskip;
526 toskip--;
527 while (toskip != 0 &&
528 (ots - toskip+1) * tabwidth < space)
529 toskip--;
532 werase(tabline);
533 wattr_on(tabline, tab_face.background, NULL);
534 wprintw(tabline, toskip == 0 ? " " : "<");
535 wattr_off(tabline, tab_face.background, NULL);
537 truncated = 0;
538 TAILQ_FOREACH(tab, &tabshead, tabs) {
539 if (truncated)
540 break;
541 if (toskip != 0) {
542 toskip--;
543 continue;
546 getyx(tabline, y, x);
547 if (x + sizeof(buf)+2 >= (size_t)COLS)
548 truncated = 1;
550 current = tab == current_tab;
552 if (*(title = tab->buffer.page.title) == '\0')
553 title = tab->hist_cur->h;
555 if (tab->flags & TAB_URGENT)
556 strlcpy(buf, "!", sizeof(buf));
557 else
558 strlcpy(buf, " ", sizeof(buf));
560 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
561 /* truncation happens */
562 strlcpy(&buf[sizeof(buf)-4], "...", 4);
563 } else {
564 /* pad with spaces */
565 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
566 /* nop */ ;
569 pair = current ? tab_face.current : tab_face.tab;
570 wattr_on(tabline, pair, NULL);
571 wprintw(tabline, "%s", buf);
572 wattr_off(tabline, pair, NULL);
574 wattr_on(tabline, tab_face.background, NULL);
575 if (TAILQ_NEXT(tab, tabs) != NULL)
576 wprintw(tabline, "┃");
577 wattr_off(tabline, tab_face.background, NULL);
580 wattr_on(tabline, tab_face.background, NULL);
581 for (; x < (size_t)COLS; ++x)
582 waddch(tabline, ' ');
583 if (truncated)
584 mvwprintw(tabline, 0, COLS-1, ">");
585 wattr_off(tabline, tab_face.background, NULL);
588 /*
589 * Compute the first visible line around vl. Try to search forward
590 * until the end of the buffer; if a visible line is not found, search
591 * backward. Return NULL if no viable line was found.
592 */
593 struct vline *
594 adjust_line(struct vline *vl, struct buffer *buffer)
596 struct vline *t;
598 if (vl == NULL)
599 return NULL;
601 if (!(vl->parent->flags & L_HIDDEN))
602 return vl;
604 /* search forward */
605 for (t = vl;
606 t != NULL && t->parent->flags & L_HIDDEN;
607 t = TAILQ_NEXT(t, vlines))
608 ; /* nop */
610 if (t != NULL)
611 return t;
613 /* search backward */
614 for (t = vl;
615 t != NULL && t->parent->flags & L_HIDDEN;
616 t = TAILQ_PREV(t, vhead, vlines))
617 ; /* nop */
619 return t;
622 static void
623 redraw_window(WINDOW *win, int off, int height, int width,
624 struct buffer *buffer)
626 struct vline *vl;
627 int l, onscreen;
629 restore_curs_x(buffer);
631 /*
632 * TODO: ignoring buffer->force_update and always
633 * re-rendering. In theory we can recompute the y position
634 * without a re-render, and optimize here. It's not the only
635 * optimisation possible here, wscrl wolud also be an
636 * interesting one.
637 */
639 again:
640 werase(win);
641 buffer->curs_y = 0;
643 if (TAILQ_EMPTY(&buffer->head))
644 goto end;
646 if (buffer->top_line == NULL)
647 buffer->top_line = TAILQ_FIRST(&buffer->head);
649 buffer->top_line = adjust_line(buffer->top_line, buffer);
650 if (buffer->top_line == NULL)
651 goto end;
653 buffer->current_line = adjust_line(buffer->current_line, buffer);
655 l = 0;
656 onscreen = 0;
657 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
658 if (vl->parent->flags & L_HIDDEN)
659 continue;
661 wmove(win, l, 0);
662 print_vline(off, width, win, vl);
664 if (vl == buffer->current_line)
665 onscreen = 1;
667 if (!onscreen)
668 buffer->curs_y++;
670 l++;
671 if (l == height)
672 break;
675 if (!onscreen) {
676 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
677 if (vl == buffer->current_line)
678 break;
679 if (vl->parent->flags & L_HIDDEN)
680 continue;
681 buffer->line_off++;
682 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
685 if (vl != NULL)
686 goto again;
689 buffer->last_line_off = buffer->line_off;
690 buffer->force_redraw = 0;
691 end:
692 wmove(win, buffer->curs_y, buffer->curs_x);
695 static void
696 redraw_help(void)
698 redraw_window(help, 0, help_lines, help_cols, &helpwin);
701 static void
702 redraw_body(struct tab *tab)
704 static struct tab *last_tab;
706 if (last_tab != tab)
707 tab->buffer.force_redraw =1;
708 last_tab = tab;
710 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
713 static inline char
714 trust_status_char(enum trust_state ts)
716 switch (ts) {
717 case TS_UNKNOWN: return '-';
718 case TS_UNTRUSTED: return '!';
719 case TS_TEMP_TRUSTED: return '!';
720 case TS_TRUSTED: return 'v';
721 case TS_VERIFIED: return 'V';
722 default: return 'X';
726 static void
727 redraw_modeline(struct tab *tab)
729 struct buffer *buffer;
730 double pct;
731 int x, y, max_x, max_y;
732 const char *mode;
733 const char *spin = "-\\|/";
735 buffer = current_buffer();
736 mode = buffer->page.name;
738 werase(modeline);
739 wattr_on(modeline, modeline_face.background, NULL);
740 wmove(modeline, 0, 0);
742 wprintw(modeline, "-%c%c %s ",
743 spin[tab->loading_anim_step],
744 trust_status_char(tab->trust),
745 mode == NULL ? "(none)" : mode);
747 pct = (buffer->line_off + buffer->curs_y) * 100.0
748 / buffer->line_max;
750 if (buffer->line_max <= (size_t)body_lines)
751 wprintw(modeline, "All ");
752 else if (buffer->line_off == 0)
753 wprintw(modeline, "Top ");
754 else if (buffer->line_off + body_lines >= buffer->line_max)
755 wprintw(modeline, "Bottom ");
756 else
757 wprintw(modeline, "%.0f%% ", pct);
759 wprintw(modeline, "%d/%d %s ",
760 buffer->line_off + buffer->curs_y,
761 buffer->line_max,
762 tab->hist_cur->h);
764 getyx(modeline, y, x);
765 getmaxyx(modeline, max_y, max_x);
767 (void)y;
768 (void)max_y;
770 for (; x < max_x; ++x)
771 waddstr(modeline, "-");
773 wattr_off(modeline, modeline_face.background, NULL);
776 static void
777 redraw_minibuffer(void)
779 wattr_on(echoarea, minibuffer_face.background, NULL);
780 werase(echoarea);
782 if (in_minibuffer)
783 do_redraw_minibuffer();
784 else
785 do_redraw_echoarea();
787 if (in_minibuffer == MB_COMPREAD)
788 do_redraw_minibuffer_compl();
790 wattr_off(echoarea, minibuffer_face.background, NULL);
793 static void
794 do_redraw_echoarea(void)
796 struct vline *vl;
798 if (ministate.curmesg != NULL)
799 wprintw(echoarea, "%s", ministate.curmesg);
800 else if (*keybuf != '\0')
801 waddstr(echoarea, keybuf);
802 else {
803 /* If nothing else, show the URL at point */
804 vl = current_tab->buffer.current_line;
805 if (vl != NULL && vl->parent->type == LINE_LINK)
806 waddstr(echoarea, vl->parent->alt);
810 static void
811 do_redraw_minibuffer(void)
813 struct buffer *cmplbuf, *buffer;
814 size_t off_y, off_x = 0;
815 const char *start, *c;
817 cmplbuf = &ministate.compl.buffer;
818 buffer = &ministate.buffer;
819 (void)off_y; /* unused, set by getyx */
821 wmove(echoarea, 0, 0);
823 if (in_minibuffer == MB_COMPREAD)
824 wprintw(echoarea, "(%2d) ",
825 cmplbuf->line_max);
827 wprintw(echoarea, "%s", ministate.prompt);
828 if (ministate.hist_cur != NULL)
829 wprintw(echoarea, "(%zu/%zu) ",
830 ministate.hist_off + 1,
831 ministate.history->len);
833 getyx(echoarea, off_y, off_x);
835 start = ministate.hist_cur != NULL
836 ? ministate.hist_cur->h
837 : ministate.buf;
838 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
839 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
840 start = utf8_next_cp(start);
843 waddstr(echoarea, start);
845 if (ministate.curmesg != NULL)
846 wprintw(echoarea, " [%s]", ministate.curmesg);
848 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
851 static void
852 do_redraw_minibuffer_compl(void)
854 redraw_window(minibuffer, 0, 10, COLS,
855 &ministate.compl.buffer);
858 /*
859 * Place the cursor in the right ncurses window. If soft is 1, use
860 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
861 * wrefresh.
862 */
863 static void
864 place_cursor(int soft)
866 int (*touch)(WINDOW *);
868 if (soft)
869 touch = wnoutrefresh;
870 else
871 touch = wrefresh;
873 if (in_minibuffer) {
874 if (side_window)
875 touch(help);
876 touch(body);
877 touch(echoarea);
878 } else if (in_side_window) {
879 touch(body);
880 touch(echoarea);
881 touch(help);
882 } else {
883 if (side_window)
884 touch(help);
885 touch(echoarea);
886 touch(body);
890 static void
891 redraw_tab(struct tab *tab)
893 if (too_small)
894 return;
896 if (side_window) {
897 redraw_help();
898 wnoutrefresh(help);
901 if (show_tab_bar)
902 redraw_tabline();
904 redraw_body(tab);
905 redraw_modeline(tab);
906 redraw_minibuffer();
908 wnoutrefresh(tabline);
909 wnoutrefresh(modeline);
911 if (in_minibuffer == MB_COMPREAD)
912 wnoutrefresh(minibuffer);
914 place_cursor(1);
916 doupdate();
918 if (set_title)
919 dprintf(1, "\033]2;%s - Telescope\a",
920 current_tab->buffer.page.title);
923 void
924 start_loading_anim(struct tab *tab)
926 if (tab->loading_anim)
927 return;
928 tab->loading_anim = 1;
929 evtimer_set(&tab->loadingev, update_loading_anim, tab);
930 evtimer_add(&tab->loadingev, &loadingev_timer);
933 static void
934 update_loading_anim(int fd, short ev, void *d)
936 struct tab *tab = d;
938 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
940 if (tab == current_tab) {
941 redraw_modeline(tab);
942 wrefresh(modeline);
943 wrefresh(body);
944 if (in_minibuffer)
945 wrefresh(echoarea);
948 evtimer_add(&tab->loadingev, &loadingev_timer);
951 static void
952 stop_loading_anim(struct tab *tab)
954 if (!tab->loading_anim)
955 return;
956 evtimer_del(&tab->loadingev);
957 tab->loading_anim = 0;
958 tab->loading_anim_step = 0;
960 if (tab != current_tab)
961 return;
963 redraw_modeline(tab);
965 wrefresh(modeline);
966 wrefresh(body);
967 if (in_minibuffer)
968 wrefresh(echoarea);
971 int
972 ui_print_colors(void)
974 int colors = 0, pairs = 0, can_change = 0;
975 int columns = 16, lines, color, i, j;
977 initscr();
978 if (has_colors()) {
979 start_color();
980 use_default_colors();
982 colors = COLORS;
983 pairs = COLOR_PAIRS;
984 can_change = can_change_color();
986 endwin();
988 printf("Term info:\n");
989 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
990 getenv("TERM"), colors, pairs, can_change);
991 printf("\n");
993 if (colors == 0) {
994 printf("No color support\n");
995 return 0;
998 printf("Available colors:\n\n");
999 lines = (colors - 1) / columns + 1;
1000 color = 0;
1001 for (i = 0; i < lines; ++i) {
1002 for (j = 0; j < columns; ++j, ++color) {
1003 printf("\033[0;38;5;%dm %03d", color, color);
1005 printf("\n");
1008 printf("\033[0m");
1009 fflush(stdout);
1010 return 0;
1013 int
1014 ui_init()
1016 setlocale(LC_ALL, "");
1018 if (TAILQ_EMPTY(&global_map.m)) {
1019 fprintf(stderr, "no keys defined!\n");
1020 return 0;
1023 minibuffer_init();
1025 /* initialize help window */
1026 TAILQ_INIT(&helpwin.head);
1027 TAILQ_INIT(&helpwin.page.head);
1029 base_map = &global_map;
1030 current_map = &global_map;
1032 initscr();
1034 if (enable_colors) {
1035 if (has_colors()) {
1036 start_color();
1037 use_default_colors();
1038 } else
1039 enable_colors = 0;
1042 config_apply_style();
1044 raw();
1045 noecho();
1046 nonl();
1047 intrflush(stdscr, FALSE);
1049 if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1050 return 0;
1051 if ((body = newwin(1, 1, 0, 0)) == NULL)
1052 return 0;
1053 if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1054 return 0;
1055 if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1056 return 0;
1057 if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1058 return 0;
1059 if ((help = newwin(1, 1, 0, 0)) == NULL)
1060 return 0;
1062 wbkgd(body, body_face.body);
1063 wbkgd(echoarea, minibuffer_face.background);
1065 update_x_offset();
1067 keypad(body, TRUE);
1068 scrollok(body, FALSE);
1070 /* non-blocking input */
1071 wtimeout(body, 0);
1072 wtimeout(help, 0);
1074 mvwprintw(body, 0, 0, "");
1076 evtimer_set(&resizeev, handle_resize, NULL);
1078 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1079 event_add(&stdioev, NULL);
1081 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1082 signal_add(&winchev, NULL);
1084 return 1;
1087 void
1088 ui_main_loop(void)
1090 switch_to_tab(current_tab);
1091 rearrange_windows();
1093 event_dispatch();
1096 void
1097 ui_on_tab_loaded(struct tab *tab)
1099 stop_loading_anim(tab);
1100 message("Loaded %s", tab->hist_cur->h);
1102 if (show_tab_bar)
1103 redraw_tabline();
1105 wrefresh(tabline);
1106 place_cursor(0);
1109 void
1110 ui_on_tab_refresh(struct tab *tab)
1112 wrap_page(&tab->buffer, body_cols);
1113 if (tab == current_tab)
1114 redraw_tab(tab);
1115 else
1116 tab->flags |= TAB_URGENT;
1119 const char *
1120 ui_keyname(int k)
1122 return keyname(k);
1125 void
1126 ui_toggle_side_window(void)
1128 side_window = !side_window;
1129 if (side_window)
1130 recompute_help();
1131 else
1132 in_side_window = 0;
1135 * ugly hack, but otherwise the window doesn't get updated
1136 * until I call rearrange_windows a second time (e.g. via
1137 * C-l). I will be happy to know why something like this is
1138 * needed.
1140 rearrange_windows();
1141 rearrange_windows();
1144 void
1145 ui_schedule_redraw(void)
1147 should_rearrange_windows = 1;
1150 void
1151 ui_require_input(struct tab *tab, int hide, int proto)
1153 void (*fn)(void);
1155 if (proto == PROTO_GEMINI)
1156 fn = ir_select_gemini;
1157 else if (proto == PROTO_GOPHER)
1158 fn = ir_select_gopher;
1159 else
1160 abort();
1162 /* TODO: hard-switching to another tab is ugly */
1163 switch_to_tab(tab);
1165 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1166 &ir_history, NULL, NULL);
1167 strlcpy(ministate.prompt, "Input required: ",
1168 sizeof(ministate.prompt));
1169 redraw_tab(tab);
1172 void
1173 ui_after_message_hook(void)
1175 redraw_minibuffer();
1176 place_cursor(0);
1179 void
1180 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1181 struct tab *data)
1183 yornp(prompt, fn, data);
1184 redraw_tab(current_tab);
1187 void
1188 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1189 struct tab *data, const char *input)
1191 minibuffer_read(prompt, fn, data);
1193 if (input != NULL) {
1194 strlcpy(ministate.buf, input, sizeof(ministate.buf));
1195 cmd_move_end_of_line(&ministate.buffer);
1198 redraw_tab(current_tab);
1201 void
1202 ui_other_window(void)
1204 if (side_window)
1205 in_side_window = !in_side_window;
1206 else
1207 message("No other window to select");
1210 void
1211 ui_suspend(void)
1213 endwin();
1215 kill(getpid(), SIGSTOP);
1217 refresh();
1218 clear();
1219 rearrange_windows();
1222 void
1223 ui_end(void)
1225 endwin();