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 /*
441 * Core part of the rendering. It prints a vline starting from the
442 * current cursor position. Printing a vline consists of skipping
443 * `off' columns (for olivetti-mode), print the correct prefix (which
444 * may be the emoji in case of emojified links-lines), printing the
445 * text itself, filling until width - off and filling off columns
446 * again.
447 */
448 static void
449 print_vline(int off, int width, WINDOW *window, struct vline *vl)
451 /*
452 * Believe me or not, I've seen emoji ten code points long!
453 * That means, to stay large, 4*10 bytes + NUL.
454 */
455 char emojibuf[41] = {0};
456 const char *text, *prfx;
457 struct line_face *f;
458 int i, left, x, y;
460 f = &line_faces[vl->parent->type];
462 /* unused, set by getyx */
463 (void)y;
465 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
467 wattr_on(window, body_face.left, NULL);
468 for (i = 0; i < off; i++)
469 waddch(window, ' ');
470 wattr_off(window, body_face.left, NULL);
472 wattr_on(window, f->prefix, NULL);
473 wprintw(window, "%s", prfx);
474 wattr_off(window, f->prefix, NULL);
476 wattr_on(window, f->text, NULL);
477 wprintw(window, "%s", text);
478 print_vline_descr(width, window, vl);
479 wattr_off(window, f->text, NULL);
481 getyx(window, y, x);
483 left = width - x;
485 wattr_on(window, f->trail, NULL);
486 for (i = 0; i < left - off; ++i)
487 waddch(window, ' ');
488 wattr_off(window, f->trail, NULL);
490 wattr_on(window, body_face.right, NULL);
491 for (i = 0; i < off; i++)
492 waddch(window, ' ');
493 wattr_off(window, body_face.right, NULL);
497 static void
498 redraw_tabline(void)
500 struct tab *tab;
501 size_t toskip, ots, tabwidth, space, x;
502 int current, y, truncated, pair;
503 const char *title;
504 char buf[25];
506 x = 0;
508 /* unused, but setted by a getyx */
509 (void)y;
511 tabwidth = sizeof(buf)+1;
512 space = COLS-2;
514 toskip = 0;
515 TAILQ_FOREACH(tab, &tabshead, tabs) {
516 toskip++;
517 if (tab == current_tab)
518 break;
521 if (toskip * tabwidth < space)
522 toskip = 0;
523 else {
524 ots = toskip;
525 toskip--;
526 while (toskip != 0 &&
527 (ots - toskip+1) * tabwidth < space)
528 toskip--;
531 werase(tabline);
532 wattr_on(tabline, tab_face.background, NULL);
533 wprintw(tabline, toskip == 0 ? " " : "<");
534 wattr_off(tabline, tab_face.background, NULL);
536 truncated = 0;
537 TAILQ_FOREACH(tab, &tabshead, tabs) {
538 if (truncated)
539 break;
540 if (toskip != 0) {
541 toskip--;
542 continue;
545 getyx(tabline, y, x);
546 if (x + sizeof(buf)+2 >= (size_t)COLS)
547 truncated = 1;
549 current = tab == current_tab;
551 if (*(title = tab->buffer.page.title) == '\0')
552 title = tab->hist_cur->h;
554 if (tab->flags & TAB_URGENT)
555 strlcpy(buf, "!", sizeof(buf));
556 else
557 strlcpy(buf, " ", sizeof(buf));
559 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
560 /* truncation happens */
561 strlcpy(&buf[sizeof(buf)-4], "...", 4);
562 } else {
563 /* pad with spaces */
564 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
565 /* nop */ ;
568 pair = current ? tab_face.current : tab_face.tab;
569 wattr_on(tabline, pair, NULL);
570 wprintw(tabline, "%s", buf);
571 wattr_off(tabline, pair, NULL);
573 wattr_on(tabline, tab_face.background, NULL);
574 if (TAILQ_NEXT(tab, tabs) != NULL)
575 wprintw(tabline, "┃");
576 wattr_off(tabline, tab_face.background, NULL);
579 wattr_on(tabline, tab_face.background, NULL);
580 for (; x < (size_t)COLS; ++x)
581 waddch(tabline, ' ');
582 if (truncated)
583 mvwprintw(tabline, 0, COLS-1, ">");
584 wattr_off(tabline, tab_face.background, NULL);
587 /*
588 * Compute the first visible line around vl. Try to search forward
589 * until the end of the buffer; if a visible line is not found, search
590 * backward. Return NULL if no viable line was found.
591 */
592 struct vline *
593 adjust_line(struct vline *vl, struct buffer *buffer)
595 struct vline *t;
597 if (vl == NULL)
598 return NULL;
600 if (!(vl->parent->flags & L_HIDDEN))
601 return vl;
603 /* search forward */
604 for (t = vl;
605 t != NULL && t->parent->flags & L_HIDDEN;
606 t = TAILQ_NEXT(t, vlines))
607 ; /* nop */
609 if (t != NULL)
610 return t;
612 /* search backward */
613 for (t = vl;
614 t != NULL && t->parent->flags & L_HIDDEN;
615 t = TAILQ_PREV(t, vhead, vlines))
616 ; /* nop */
618 return t;
621 static void
622 redraw_window(WINDOW *win, int off, int height, int width,
623 struct buffer *buffer)
625 struct vline *vl;
626 int l, onscreen;
628 restore_curs_x(buffer);
630 /*
631 * TODO: ignoring buffer->force_update and always
632 * re-rendering. In theory we can recompute the y position
633 * without a re-render, and optimize here. It's not the only
634 * optimisation possible here, wscrl wolud also be an
635 * interesting one.
636 */
638 again:
639 werase(win);
640 buffer->curs_y = 0;
642 if (TAILQ_EMPTY(&buffer->head))
643 goto end;
645 if (buffer->top_line == NULL)
646 buffer->top_line = TAILQ_FIRST(&buffer->head);
648 buffer->top_line = adjust_line(buffer->top_line, buffer);
649 if (buffer->top_line == NULL)
650 goto end;
652 buffer->current_line = adjust_line(buffer->current_line, buffer);
654 l = 0;
655 onscreen = 0;
656 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
657 if (vl->parent->flags & L_HIDDEN)
658 continue;
660 wmove(win, l, 0);
661 print_vline(off, width, win, vl);
663 if (vl == buffer->current_line)
664 onscreen = 1;
666 if (!onscreen)
667 buffer->curs_y++;
669 l++;
670 if (l == height)
671 break;
674 if (!onscreen) {
675 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
676 if (vl == buffer->current_line)
677 break;
678 if (vl->parent->flags & L_HIDDEN)
679 continue;
680 buffer->line_off++;
681 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
684 if (vl != NULL)
685 goto again;
688 buffer->last_line_off = buffer->line_off;
689 buffer->force_redraw = 0;
690 end:
691 wmove(win, buffer->curs_y, buffer->curs_x);
694 static void
695 redraw_help(void)
697 redraw_window(help, 0, help_lines, help_cols, &helpwin);
700 static void
701 redraw_body(struct tab *tab)
703 static struct tab *last_tab;
705 if (last_tab != tab)
706 tab->buffer.force_redraw =1;
707 last_tab = tab;
709 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
712 static inline char
713 trust_status_char(enum trust_state ts)
715 switch (ts) {
716 case TS_UNKNOWN: return '-';
717 case TS_UNTRUSTED: return '!';
718 case TS_TEMP_TRUSTED: return '!';
719 case TS_TRUSTED: return 'v';
720 case TS_VERIFIED: return 'V';
721 default: return 'X';
725 static void
726 redraw_modeline(struct tab *tab)
728 struct buffer *buffer;
729 double pct;
730 int x, y, max_x, max_y;
731 const char *mode;
732 const char *spin = "-\\|/";
734 buffer = current_buffer();
735 mode = buffer->page.name;
737 werase(modeline);
738 wattr_on(modeline, modeline_face.background, NULL);
739 wmove(modeline, 0, 0);
741 wprintw(modeline, "-%c%c %s ",
742 spin[tab->loading_anim_step],
743 trust_status_char(tab->trust),
744 mode == NULL ? "(none)" : mode);
746 pct = (buffer->line_off + buffer->curs_y) * 100.0
747 / buffer->line_max;
749 if (buffer->line_max <= (size_t)body_lines)
750 wprintw(modeline, "All ");
751 else if (buffer->line_off == 0)
752 wprintw(modeline, "Top ");
753 else if (buffer->line_off + body_lines >= buffer->line_max)
754 wprintw(modeline, "Bottom ");
755 else
756 wprintw(modeline, "%.0f%% ", pct);
758 wprintw(modeline, "%d/%d %s ",
759 buffer->line_off + buffer->curs_y,
760 buffer->line_max,
761 tab->hist_cur->h);
763 getyx(modeline, y, x);
764 getmaxyx(modeline, max_y, max_x);
766 (void)y;
767 (void)max_y;
769 for (; x < max_x; ++x)
770 waddstr(modeline, "-");
772 wattr_off(modeline, modeline_face.background, NULL);
775 static void
776 redraw_minibuffer(void)
778 wattr_on(echoarea, minibuffer_face.background, NULL);
779 werase(echoarea);
781 if (in_minibuffer)
782 do_redraw_minibuffer();
783 else
784 do_redraw_echoarea();
786 if (in_minibuffer == MB_COMPREAD)
787 do_redraw_minibuffer_compl();
789 wattr_off(echoarea, minibuffer_face.background, NULL);
792 static void
793 do_redraw_echoarea(void)
795 struct vline *vl;
797 if (ministate.curmesg != NULL)
798 wprintw(echoarea, "%s", ministate.curmesg);
799 else if (*keybuf != '\0')
800 waddstr(echoarea, keybuf);
801 else {
802 /* If nothing else, show the URL at point */
803 vl = current_tab->buffer.current_line;
804 if (vl != NULL && vl->parent->type == LINE_LINK)
805 waddstr(echoarea, vl->parent->alt);
809 static void
810 do_redraw_minibuffer(void)
812 struct buffer *cmplbuf, *buffer;
813 size_t off_y, off_x = 0;
814 const char *start, *c;
816 cmplbuf = &ministate.compl.buffer;
817 buffer = &ministate.buffer;
818 (void)off_y; /* unused, set by getyx */
820 wmove(echoarea, 0, 0);
822 if (in_minibuffer == MB_COMPREAD)
823 wprintw(echoarea, "(%2d) ",
824 cmplbuf->line_max);
826 wprintw(echoarea, "%s", ministate.prompt);
827 if (ministate.hist_cur != NULL)
828 wprintw(echoarea, "(%zu/%zu) ",
829 ministate.hist_off + 1,
830 ministate.history->len);
832 getyx(echoarea, off_y, off_x);
834 start = ministate.hist_cur != NULL
835 ? ministate.hist_cur->h
836 : ministate.buf;
837 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
838 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
839 start = utf8_next_cp(start);
842 waddstr(echoarea, start);
844 if (ministate.curmesg != NULL)
845 wprintw(echoarea, " [%s]", ministate.curmesg);
847 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
850 static void
851 do_redraw_minibuffer_compl(void)
853 redraw_window(minibuffer, 0, 10, COLS,
854 &ministate.compl.buffer);
857 /*
858 * Place the cursor in the right ncurses window. If soft is 1, use
859 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
860 * wrefresh.
861 */
862 static void
863 place_cursor(int soft)
865 int (*touch)(WINDOW *);
867 if (soft)
868 touch = wnoutrefresh;
869 else
870 touch = wrefresh;
872 if (in_minibuffer) {
873 if (side_window)
874 touch(help);
875 touch(body);
876 touch(echoarea);
877 } else if (in_side_window) {
878 touch(body);
879 touch(echoarea);
880 touch(help);
881 } else {
882 if (side_window)
883 touch(help);
884 touch(echoarea);
885 touch(body);
889 static void
890 redraw_tab(struct tab *tab)
892 if (too_small)
893 return;
895 if (side_window) {
896 redraw_help();
897 wnoutrefresh(help);
900 if (show_tab_bar)
901 redraw_tabline();
903 redraw_body(tab);
904 redraw_modeline(tab);
905 redraw_minibuffer();
907 wnoutrefresh(tabline);
908 wnoutrefresh(modeline);
910 if (in_minibuffer == MB_COMPREAD)
911 wnoutrefresh(minibuffer);
913 place_cursor(1);
915 doupdate();
917 if (set_title)
918 dprintf(1, "\033]2;%s - Telescope\a",
919 current_tab->buffer.page.title);
922 void
923 start_loading_anim(struct tab *tab)
925 if (tab->loading_anim)
926 return;
927 tab->loading_anim = 1;
928 evtimer_set(&tab->loadingev, update_loading_anim, tab);
929 evtimer_add(&tab->loadingev, &loadingev_timer);
932 static void
933 update_loading_anim(int fd, short ev, void *d)
935 struct tab *tab = d;
937 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
939 if (tab == current_tab) {
940 redraw_modeline(tab);
941 wrefresh(modeline);
942 wrefresh(body);
943 if (in_minibuffer)
944 wrefresh(echoarea);
947 evtimer_add(&tab->loadingev, &loadingev_timer);
950 static void
951 stop_loading_anim(struct tab *tab)
953 if (!tab->loading_anim)
954 return;
955 evtimer_del(&tab->loadingev);
956 tab->loading_anim = 0;
957 tab->loading_anim_step = 0;
959 if (tab != current_tab)
960 return;
962 redraw_modeline(tab);
964 wrefresh(modeline);
965 wrefresh(body);
966 if (in_minibuffer)
967 wrefresh(echoarea);
970 int
971 ui_print_colors(void)
973 int colors = 0, pairs = 0, can_change = 0;
974 int columns = 16, lines, color, i, j;
976 initscr();
977 if (has_colors()) {
978 start_color();
979 use_default_colors();
981 colors = COLORS;
982 pairs = COLOR_PAIRS;
983 can_change = can_change_color();
985 endwin();
987 printf("Term info:\n");
988 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
989 getenv("TERM"), colors, pairs, can_change);
990 printf("\n");
992 if (colors == 0) {
993 printf("No color support\n");
994 return 0;
997 printf("Available colors:\n\n");
998 lines = (colors - 1) / columns + 1;
999 color = 0;
1000 for (i = 0; i < lines; ++i) {
1001 for (j = 0; j < columns; ++j, ++color) {
1002 printf("\033[0;38;5;%dm %03d", color, color);
1004 printf("\n");
1007 printf("\033[0m");
1008 fflush(stdout);
1009 return 0;
1012 int
1013 ui_init()
1015 setlocale(LC_ALL, "");
1017 if (TAILQ_EMPTY(&global_map.m)) {
1018 fprintf(stderr, "no keys defined!\n");
1019 return 0;
1022 minibuffer_init();
1024 /* initialize help window */
1025 TAILQ_INIT(&helpwin.head);
1026 TAILQ_INIT(&helpwin.page.head);
1028 base_map = &global_map;
1029 current_map = &global_map;
1031 initscr();
1033 if (enable_colors) {
1034 if (has_colors()) {
1035 start_color();
1036 use_default_colors();
1037 } else
1038 enable_colors = 0;
1041 config_apply_style();
1043 raw();
1044 noecho();
1045 nonl();
1046 intrflush(stdscr, FALSE);
1048 if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1049 return 0;
1050 if ((body = newwin(1, 1, 0, 0)) == NULL)
1051 return 0;
1052 if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1053 return 0;
1054 if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1055 return 0;
1056 if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1057 return 0;
1058 if ((help = newwin(1, 1, 0, 0)) == NULL)
1059 return 0;
1061 wbkgd(body, body_face.body);
1062 wbkgd(echoarea, minibuffer_face.background);
1064 update_x_offset();
1066 keypad(body, TRUE);
1067 scrollok(body, FALSE);
1069 /* non-blocking input */
1070 wtimeout(body, 0);
1071 wtimeout(help, 0);
1073 mvwprintw(body, 0, 0, "");
1075 evtimer_set(&resizeev, handle_resize, NULL);
1077 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1078 event_add(&stdioev, NULL);
1080 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1081 signal_add(&winchev, NULL);
1083 return 1;
1086 void
1087 ui_main_loop(void)
1089 switch_to_tab(current_tab);
1090 rearrange_windows();
1092 event_dispatch();
1095 void
1096 ui_on_tab_loaded(struct tab *tab)
1098 stop_loading_anim(tab);
1099 message("Loaded %s", tab->hist_cur->h);
1101 if (show_tab_bar)
1102 redraw_tabline();
1104 wrefresh(tabline);
1105 place_cursor(0);
1108 void
1109 ui_on_tab_refresh(struct tab *tab)
1111 wrap_page(&tab->buffer, body_cols);
1112 if (tab == current_tab)
1113 redraw_tab(tab);
1114 else
1115 tab->flags |= TAB_URGENT;
1118 const char *
1119 ui_keyname(int k)
1121 return keyname(k);
1124 void
1125 ui_toggle_side_window(void)
1127 side_window = !side_window;
1128 if (side_window)
1129 recompute_help();
1130 else
1131 in_side_window = 0;
1134 * ugly hack, but otherwise the window doesn't get updated
1135 * until I call rearrange_windows a second time (e.g. via
1136 * C-l). I will be happy to know why something like this is
1137 * needed.
1139 rearrange_windows();
1140 rearrange_windows();
1143 void
1144 ui_schedule_redraw(void)
1146 should_rearrange_windows = 1;
1149 void
1150 ui_require_input(struct tab *tab, int hide, int proto)
1152 void (*fn)(void);
1154 if (proto == PROTO_GEMINI)
1155 fn = ir_select_gemini;
1156 else if (proto == PROTO_GOPHER)
1157 fn = ir_select_gopher;
1158 else
1159 abort();
1161 /* TODO: hard-switching to another tab is ugly */
1162 switch_to_tab(tab);
1164 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1165 &ir_history, NULL, NULL);
1166 strlcpy(ministate.prompt, "Input required: ",
1167 sizeof(ministate.prompt));
1168 redraw_tab(tab);
1171 void
1172 ui_after_message_hook(void)
1174 redraw_minibuffer();
1175 place_cursor(0);
1178 void
1179 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1180 struct tab *data)
1182 yornp(prompt, fn, data);
1183 redraw_tab(current_tab);
1186 void
1187 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1188 struct tab *data)
1190 minibuffer_read(prompt, fn, data);
1191 redraw_tab(current_tab);
1194 void
1195 ui_other_window(void)
1197 if (side_window)
1198 in_side_window = !in_side_window;
1199 else
1200 message("No other window to select");
1203 void
1204 ui_suspend(void)
1206 endwin();
1208 kill(getpid(), SIGSTOP);
1210 refresh();
1211 clear();
1212 rearrange_windows();
1215 void
1216 ui_end(void)
1218 endwin();