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 too_small;
78 static int x_offset;
80 struct thiskey thiskey;
81 struct tab *current_tab;
83 static struct event resizeev;
84 static struct timeval resize_timer = { 0, 250000 };
86 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
88 int body_lines, body_cols;
90 static WINDOW *help;
91 /* not static so we can see them from help.c */
92 struct buffer helpwin;
93 int help_lines, help_cols;
95 static int side_window;
96 static int in_side_window;
98 static struct timeval loadingev_timer = { 0, 250000 };
100 static char keybuf[64];
102 /* XXX: don't forget to init these in main() */
103 struct kmap global_map,
104 minibuffer_map,
105 *current_map,
106 *base_map;
108 static inline void
109 update_x_offset(void)
111 if (olivetti_mode && fill_column < body_cols)
112 x_offset = (body_cols - fill_column)/2;
113 else
114 x_offset = 0;
117 void
118 save_excursion(struct excursion *place, struct buffer *buffer)
120 place->curs_x = buffer->curs_x;
121 place->curs_y = buffer->curs_y;
122 place->line_off = buffer->line_off;
123 place->top_line = buffer->top_line;
124 place->current_line = buffer->current_line;
125 place->cpoff = buffer->cpoff;
128 void
129 restore_excursion(struct excursion *place, struct buffer *buffer)
131 buffer->curs_x = place->curs_x;
132 buffer->curs_y = place->curs_y;
133 buffer->line_off = place->line_off;
134 buffer->top_line = place->top_line;
135 buffer->current_line = place->current_line;
136 buffer->cpoff = place->cpoff;
139 static void
140 restore_curs_x(struct buffer *buffer)
142 struct vline *vl;
143 const char *prfx, *text;
145 vl = buffer->current_line;
146 if (vl == NULL || vl->line == NULL)
147 buffer->curs_x = buffer->cpoff = 0;
148 else if (vl->parent->data != NULL) {
149 text = vl->parent->data;
150 buffer->curs_x = utf8_snwidth(text + 1, buffer->cpoff) + 1;
151 } else
152 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
154 buffer->curs_x += x_offset;
156 if (vl == NULL)
157 return;
159 if (vl->parent->data != NULL)
160 buffer->curs_x += utf8_swidth_between(vl->parent->line,
161 vl->parent->data);
162 else {
163 prfx = line_prefixes[vl->parent->type].prfx1;
164 buffer->curs_x += utf8_swidth(prfx);
168 void
169 global_key_unbound(void)
171 message("%s is undefined", keybuf);
174 struct buffer *
175 current_buffer(void)
177 if (in_minibuffer)
178 return &ministate.buffer;
179 if (in_side_window)
180 return &helpwin;
181 return &current_tab->buffer;
184 static int
185 readkey(void)
187 uint32_t state = 0;
189 if ((thiskey.key = wgetch(body)) == ERR)
190 return 0;
192 thiskey.meta = thiskey.key == 27;
193 if (thiskey.meta) {
194 thiskey.key = wgetch(body);
195 if (thiskey.key == ERR || thiskey.key == 27) {
196 thiskey.meta = 0;
197 thiskey.key = 27;
201 thiskey.cp = 0;
203 if ((unsigned int)thiskey.key >= UINT8_MAX)
204 return 1;
206 while (1) {
207 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
208 break;
209 if ((thiskey.key = wgetch(body)) == ERR) {
210 message("Error decoding user input");
211 return 0;
215 return 1;
218 static void
219 dispatch_stdio(int fd, short ev, void *d)
221 struct keymap *k;
222 const char *keyname;
223 char tmp[5] = {0};
225 /* TODO: schedule a redraw? */
226 if (too_small)
227 return;
229 if (!readkey())
230 return;
232 if (keybuf[0] != '\0')
233 strlcat(keybuf, " ", sizeof(keybuf));
234 if (thiskey.meta)
235 strlcat(keybuf, "M-", sizeof(keybuf));
236 if (thiskey.cp != 0) {
237 utf8_encode(thiskey.cp, tmp);
238 strlcat(keybuf, tmp, sizeof(keybuf));
239 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
240 strlcat(keybuf, keyname, sizeof(keybuf));
241 } else {
242 tmp[0] = thiskey.key;
243 strlcat(keybuf, tmp, sizeof(keybuf));
246 TAILQ_FOREACH(k, &current_map->m, keymaps) {
247 if (k->meta == thiskey.meta &&
248 k->key == thiskey.key) {
249 if (k->fn == NULL)
250 current_map = &k->map;
251 else {
252 current_map = base_map;
253 strlcpy(keybuf, "", sizeof(keybuf));
254 k->fn(current_buffer());
256 goto done;
260 if (current_map->unhandled_input != NULL)
261 current_map->unhandled_input();
262 else
263 global_key_unbound();
265 strlcpy(keybuf, "", sizeof(keybuf));
266 current_map = base_map;
268 done:
269 if (side_window)
270 recompute_help();
272 if (should_rearrange_windows)
273 rearrange_windows();
274 redraw_tab(current_tab);
277 static void
278 handle_resize(int sig, short ev, void *d)
280 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
281 event_del(&resizeev);
283 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
284 evtimer_add(&resizeev, &resize_timer);
287 static void
288 handle_resize_nodelay(int s, short ev, void *d)
290 endwin();
291 refresh();
292 clear();
294 rearrange_windows();
297 static void
298 rearrange_windows(void)
300 int lines;
302 should_rearrange_windows = 0;
304 lines = LINES;
306 if ((too_small = lines < 15)) {
307 erase();
308 printw("Window too small.");
309 refresh();
310 return;
313 /* move and resize the windows, in reverse order! */
315 if (in_minibuffer == MB_COMPREAD) {
316 mvwin(minibuffer, lines-10, 0);
317 wresize(minibuffer, 10, COLS);
318 lines -= 10;
320 wrap_page(&ministate.compl.buffer, COLS);
323 mvwin(echoarea, --lines, 0);
324 wresize(echoarea, 1, COLS);
326 mvwin(modeline, --lines, 0);
327 wresize(modeline, 1, COLS);
329 body_lines = --lines;
330 body_cols = COLS;
332 if (side_window) {
333 help_cols = 0.3 * COLS;
334 help_lines = lines;
335 mvwin(help, 1, 0);
336 wresize(help, help_lines, help_cols);
338 wrap_page(&helpwin, help_cols);
340 body_cols = COLS - help_cols - 1;
341 mvwin(body, 1, help_cols);
342 } else
343 mvwin(body, 1, 0);
345 update_x_offset();
346 wresize(body, body_lines, body_cols);
348 wresize(tabline, 1, COLS);
350 wrap_page(&current_tab->buffer, body_cols);
351 redraw_tab(current_tab);
354 static void
355 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
356 const char **prfx_ret, const char **text_ret)
358 int type, i, cont, width;
359 char *space, *t;
361 if ((*text_ret = vl->line) == NULL)
362 *text_ret = "";
364 cont = vl->flags & L_CONTINUATION;
365 type = vl->parent->type;
366 if (!cont)
367 *prfx_ret = line_prefixes[type].prfx1;
368 else
369 *prfx_ret = line_prefixes[type].prfx2;
371 space = vl->parent->data;
372 if (!emojify_link || type != LINE_LINK || space == NULL)
373 return;
375 if (cont) {
376 memset(buf, 0, len);
377 width = utf8_swidth_between(vl->parent->line, space);
378 for (i = 0; i < width + 1; ++i)
379 strlcat(buf, " ", len);
380 } else {
381 strlcpy(buf, *text_ret, len);
382 if ((t = strchr(buf, ' ')) != NULL)
383 *t = '\0';
384 strlcat(buf, " ", len);
386 /* skip the emoji */
387 *text_ret += (space - vl->parent->line) + 1;
390 *prfx_ret = buf;
393 static inline void
394 print_vline_descr(int width, WINDOW *window, struct vline *vl)
396 int x, y, goal;
398 if (vl->parent->type != LINE_COMPL &&
399 vl->parent->type != LINE_COMPL_CURRENT &&
400 vl->parent->type != LINE_HELP)
401 return;
403 if (vl->parent->alt == NULL)
404 return;
406 (void)y;
407 getyx(window, y, x);
409 if (vl->parent->type == LINE_HELP)
410 goal = 8;
411 else
412 goal = width/2;
414 if (goal <= x)
415 wprintw(window, " ");
416 for (; goal > x; ++x)
417 wprintw(window, " ");
419 wprintw(window, "%s", vl->parent->alt);
422 /*
423 * Core part of the rendering. It prints a vline starting from the
424 * current cursor position. Printing a vline consists of skipping
425 * `off' columns (for olivetti-mode), print the correct prefix (which
426 * may be the emoji in case of emojified links-lines), printing the
427 * text itself, filling until width - off and filling off columns
428 * again.
429 */
430 static void
431 print_vline(int off, int width, WINDOW *window, struct vline *vl)
433 /*
434 * Believe me or not, I've seen emoji ten code points long!
435 * That means, to stay large, 4*10 bytes + NUL.
436 */
437 char emojibuf[41] = {0};
438 const char *text, *prfx;
439 struct line_face *f;
440 int i, left, x, y;
442 f = &line_faces[vl->parent->type];
444 /* unused, set by getyx */
445 (void)y;
447 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
449 wattr_on(window, body_face.left, NULL);
450 for (i = 0; i < off; i++)
451 waddch(window, ' ');
452 wattr_off(window, body_face.left, NULL);
454 wattr_on(window, f->prefix, NULL);
455 wprintw(window, "%s", prfx);
456 wattr_off(window, f->prefix, NULL);
458 wattr_on(window, f->text, NULL);
459 wprintw(window, "%s", text);
460 print_vline_descr(width, window, vl);
461 wattr_off(window, f->text, NULL);
463 getyx(window, y, x);
465 left = width - x;
467 wattr_on(window, f->trail, NULL);
468 for (i = 0; i < left - off; ++i)
469 waddch(window, ' ');
470 wattr_off(window, f->trail, NULL);
472 wattr_on(window, body_face.right, NULL);
473 for (i = 0; i < off; i++)
474 waddch(window, ' ');
475 wattr_off(window, body_face.right, NULL);
479 static void
480 redraw_tabline(void)
482 struct tab *tab;
483 size_t toskip, ots, tabwidth, space, x;
484 int current, y, truncated, pair;
485 const char *title;
486 char buf[25];
488 x = 0;
490 /* unused, but setted by a getyx */
491 (void)y;
493 tabwidth = sizeof(buf)+1;
494 space = COLS-2;
496 toskip = 0;
497 TAILQ_FOREACH(tab, &tabshead, tabs) {
498 toskip++;
499 if (tab == current_tab)
500 break;
503 if (toskip * tabwidth < space)
504 toskip = 0;
505 else {
506 ots = toskip;
507 toskip--;
508 while (toskip != 0 &&
509 (ots - toskip+1) * tabwidth < space)
510 toskip--;
513 werase(tabline);
514 wattr_on(tabline, tab_face.background, NULL);
515 wprintw(tabline, toskip == 0 ? " " : "<");
516 wattr_off(tabline, tab_face.background, NULL);
518 truncated = 0;
519 TAILQ_FOREACH(tab, &tabshead, tabs) {
520 if (truncated)
521 break;
522 if (toskip != 0) {
523 toskip--;
524 continue;
527 getyx(tabline, y, x);
528 if (x + sizeof(buf)+2 >= (size_t)COLS)
529 truncated = 1;
531 current = tab == current_tab;
533 if (*(title = tab->buffer.page.title) == '\0')
534 title = tab->hist_cur->h;
536 if (tab->flags & TAB_URGENT)
537 strlcpy(buf, "!", sizeof(buf));
538 else
539 strlcpy(buf, " ", sizeof(buf));
541 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
542 /* truncation happens */
543 strlcpy(&buf[sizeof(buf)-4], "...", 4);
544 } else {
545 /* pad with spaces */
546 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
547 /* nop */ ;
550 pair = current ? tab_face.current : tab_face.tab;
551 wattr_on(tabline, pair, NULL);
552 wprintw(tabline, "%s", buf);
553 wattr_off(tabline, pair, NULL);
555 wattr_on(tabline, tab_face.background, NULL);
556 if (TAILQ_NEXT(tab, tabs) != NULL)
557 wprintw(tabline, "┃");
558 wattr_off(tabline, tab_face.background, NULL);
561 wattr_on(tabline, tab_face.background, NULL);
562 for (; x < (size_t)COLS; ++x)
563 waddch(tabline, ' ');
564 if (truncated)
565 mvwprintw(tabline, 0, COLS-1, ">");
566 wattr_off(tabline, tab_face.background, NULL);
569 /*
570 * Compute the first visible line around vl. Try to search forward
571 * until the end of the buffer; if a visible line is not found, search
572 * backward. Return NULL if no viable line was found.
573 */
574 struct vline *
575 adjust_line(struct vline *vl, struct buffer *buffer)
577 struct vline *t;
579 if (vl == NULL)
580 return NULL;
582 if (!(vl->parent->flags & L_HIDDEN))
583 return vl;
585 /* search forward */
586 for (t = vl;
587 t != NULL && t->parent->flags & L_HIDDEN;
588 t = TAILQ_NEXT(t, vlines))
589 ; /* nop */
591 if (t != NULL)
592 return t;
594 /* search backward */
595 for (t = vl;
596 t != NULL && t->parent->flags & L_HIDDEN;
597 t = TAILQ_PREV(t, vhead, vlines))
598 ; /* nop */
600 return t;
603 static void
604 redraw_window(WINDOW *win, int off, int height, int width,
605 struct buffer *buffer)
607 struct vline *vl;
608 int l, onscreen;
610 restore_curs_x(buffer);
612 /*
613 * TODO: ignoring buffer->force_update and always
614 * re-rendering. In theory we can recompute the y position
615 * without a re-render, and optimize here. It's not the only
616 * optimisation possible here, wscrl wolud also be an
617 * interesting one.
618 */
620 again:
621 werase(win);
622 buffer->curs_y = 0;
624 if (TAILQ_EMPTY(&buffer->head))
625 goto end;
627 if (buffer->top_line == NULL)
628 buffer->top_line = TAILQ_FIRST(&buffer->head);
630 buffer->top_line = adjust_line(buffer->top_line, buffer);
631 if (buffer->top_line == NULL)
632 goto end;
634 buffer->current_line = adjust_line(buffer->current_line, buffer);
636 l = 0;
637 onscreen = 0;
638 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
639 if (vl->parent->flags & L_HIDDEN)
640 continue;
642 wmove(win, l, 0);
643 print_vline(off, width, win, vl);
645 if (vl == buffer->current_line)
646 onscreen = 1;
648 if (!onscreen)
649 buffer->curs_y++;
651 l++;
652 if (l == height)
653 break;
656 if (!onscreen) {
657 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
658 if (vl == buffer->current_line)
659 break;
660 if (vl->parent->flags & L_HIDDEN)
661 continue;
662 buffer->line_off++;
663 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
666 if (vl != NULL)
667 goto again;
670 buffer->last_line_off = buffer->line_off;
671 buffer->force_redraw = 0;
672 end:
673 wmove(win, buffer->curs_y, buffer->curs_x);
676 static void
677 redraw_help(void)
679 redraw_window(help, 0, help_lines, help_cols, &helpwin);
682 static void
683 redraw_body(struct tab *tab)
685 static struct tab *last_tab;
687 if (last_tab != tab)
688 tab->buffer.force_redraw =1;
689 last_tab = tab;
691 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
694 static inline char
695 trust_status_char(enum trust_state ts)
697 switch (ts) {
698 case TS_UNKNOWN: return '-';
699 case TS_UNTRUSTED: return '!';
700 case TS_TEMP_TRUSTED: return '!';
701 case TS_TRUSTED: return 'v';
702 case TS_VERIFIED: return 'V';
703 default: return 'X';
707 static void
708 redraw_modeline(struct tab *tab)
710 struct buffer *buffer;
711 double pct;
712 int x, y, max_x, max_y;
713 const char *mode;
714 const char *spin = "-\\|/";
716 buffer = current_buffer();
717 mode = buffer->page.name;
719 werase(modeline);
720 wattr_on(modeline, modeline_face.background, NULL);
721 wmove(modeline, 0, 0);
723 wprintw(modeline, "-%c%c %s ",
724 spin[tab->loading_anim_step],
725 trust_status_char(tab->trust),
726 mode == NULL ? "(none)" : mode);
728 pct = (buffer->line_off + buffer->curs_y) * 100.0
729 / buffer->line_max;
731 if (buffer->line_max <= (size_t)body_lines)
732 wprintw(modeline, "All ");
733 else if (buffer->line_off == 0)
734 wprintw(modeline, "Top ");
735 else if (buffer->line_off + body_lines >= buffer->line_max)
736 wprintw(modeline, "Bottom ");
737 else
738 wprintw(modeline, "%.0f%% ", pct);
740 wprintw(modeline, "%d/%d %s ",
741 buffer->line_off + buffer->curs_y,
742 buffer->line_max,
743 tab->hist_cur->h);
745 getyx(modeline, y, x);
746 getmaxyx(modeline, max_y, max_x);
748 (void)y;
749 (void)max_y;
751 for (; x < max_x; ++x)
752 waddstr(modeline, "-");
754 wattr_off(modeline, modeline_face.background, NULL);
757 static void
758 redraw_minibuffer(void)
760 wattr_on(echoarea, minibuffer_face.background, NULL);
761 werase(echoarea);
763 if (in_minibuffer)
764 do_redraw_minibuffer();
765 else
766 do_redraw_echoarea();
768 if (in_minibuffer == MB_COMPREAD)
769 do_redraw_minibuffer_compl();
771 wattr_off(echoarea, minibuffer_face.background, NULL);
774 static void
775 do_redraw_echoarea(void)
777 struct vline *vl;
779 if (ministate.curmesg != NULL)
780 wprintw(echoarea, "%s", ministate.curmesg);
781 else if (*keybuf != '\0')
782 waddstr(echoarea, keybuf);
783 else {
784 /* If nothing else, show the URL at point */
785 vl = current_tab->buffer.current_line;
786 if (vl != NULL && vl->parent->type == LINE_LINK)
787 waddstr(echoarea, vl->parent->alt);
791 static void
792 do_redraw_minibuffer(void)
794 struct buffer *cmplbuf, *buffer;
795 size_t off_y, off_x = 0;
796 const char *start, *c;
798 cmplbuf = &ministate.compl.buffer;
799 buffer = &ministate.buffer;
800 (void)off_y; /* unused, set by getyx */
802 wmove(echoarea, 0, 0);
804 if (in_minibuffer == MB_COMPREAD)
805 wprintw(echoarea, "(%2d) ",
806 cmplbuf->line_max);
808 wprintw(echoarea, "%s", ministate.prompt);
809 if (ministate.hist_cur != NULL)
810 wprintw(echoarea, "(%zu/%zu) ",
811 ministate.hist_off + 1,
812 ministate.history->len);
814 getyx(echoarea, off_y, off_x);
816 start = ministate.hist_cur != NULL
817 ? ministate.hist_cur->h
818 : ministate.buf;
819 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
820 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
821 start = utf8_next_cp(start);
824 waddstr(echoarea, start);
826 if (ministate.curmesg != NULL)
827 wprintw(echoarea, " [%s]", ministate.curmesg);
829 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
832 static void
833 do_redraw_minibuffer_compl(void)
835 redraw_window(minibuffer, 0, 10, COLS,
836 &ministate.compl.buffer);
839 /*
840 * Place the cursor in the right ncurses window. If soft is 1, use
841 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
842 * wrefresh.
843 */
844 static void
845 place_cursor(int soft)
847 int (*touch)(WINDOW *);
849 if (soft)
850 touch = wnoutrefresh;
851 else
852 touch = wrefresh;
854 if (in_minibuffer) {
855 if (side_window)
856 touch(help);
857 touch(body);
858 touch(echoarea);
859 } else if (in_side_window) {
860 touch(body);
861 touch(echoarea);
862 touch(help);
863 } else {
864 if (side_window)
865 touch(help);
866 touch(echoarea);
867 touch(body);
871 static void
872 redraw_tab(struct tab *tab)
874 if (too_small)
875 return;
877 if (side_window) {
878 redraw_help();
879 wnoutrefresh(help);
882 redraw_tabline();
883 redraw_body(tab);
884 redraw_modeline(tab);
885 redraw_minibuffer();
887 wnoutrefresh(tabline);
888 wnoutrefresh(modeline);
890 if (in_minibuffer == MB_COMPREAD)
891 wnoutrefresh(minibuffer);
893 place_cursor(1);
895 doupdate();
897 if (set_title)
898 dprintf(1, "\033]2;%s - Telescope\a",
899 current_tab->buffer.page.title);
902 void
903 start_loading_anim(struct tab *tab)
905 if (tab->loading_anim)
906 return;
907 tab->loading_anim = 1;
908 evtimer_set(&tab->loadingev, update_loading_anim, tab);
909 evtimer_add(&tab->loadingev, &loadingev_timer);
912 static void
913 update_loading_anim(int fd, short ev, void *d)
915 struct tab *tab = d;
917 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
919 if (tab == current_tab) {
920 redraw_modeline(tab);
921 wrefresh(modeline);
922 wrefresh(body);
923 if (in_minibuffer)
924 wrefresh(echoarea);
927 evtimer_add(&tab->loadingev, &loadingev_timer);
930 static void
931 stop_loading_anim(struct tab *tab)
933 if (!tab->loading_anim)
934 return;
935 evtimer_del(&tab->loadingev);
936 tab->loading_anim = 0;
937 tab->loading_anim_step = 0;
939 if (tab != current_tab)
940 return;
942 redraw_modeline(tab);
944 wrefresh(modeline);
945 wrefresh(body);
946 if (in_minibuffer)
947 wrefresh(echoarea);
950 int
951 ui_print_colors(void)
953 int colors = 0, pairs = 0, can_change = 0;
954 int columns = 16, lines, color, i, j;
956 initscr();
957 if (has_colors()) {
958 start_color();
959 use_default_colors();
961 colors = COLORS;
962 pairs = COLOR_PAIRS;
963 can_change = can_change_color();
965 endwin();
967 printf("Term info:\n");
968 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
969 getenv("TERM"), colors, pairs, can_change);
970 printf("\n");
972 if (colors == 0) {
973 printf("No color support\n");
974 return 0;
977 printf("Available colors:\n\n");
978 lines = (colors - 1) / columns + 1;
979 color = 0;
980 for (i = 0; i < lines; ++i) {
981 for (j = 0; j < columns; ++j, ++color) {
982 printf("\033[0;38;5;%dm %03d", color, color);
984 printf("\n");
987 printf("\033[0m");
988 fflush(stdout);
989 return 0;
992 int
993 ui_init()
995 setlocale(LC_ALL, "");
997 if (TAILQ_EMPTY(&global_map.m)) {
998 fprintf(stderr, "no keys defined!\n");
999 return 0;
1002 minibuffer_init();
1004 /* initialize help window */
1005 TAILQ_INIT(&helpwin.head);
1006 TAILQ_INIT(&helpwin.page.head);
1008 base_map = &global_map;
1009 current_map = &global_map;
1011 initscr();
1013 if (enable_colors) {
1014 if (has_colors()) {
1015 start_color();
1016 use_default_colors();
1017 } else
1018 enable_colors = 0;
1021 config_apply_style();
1023 raw();
1024 noecho();
1025 nonl();
1026 intrflush(stdscr, FALSE);
1028 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1029 return 0;
1030 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1031 return 0;
1032 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1033 return 0;
1034 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1035 return 0;
1036 if ((minibuffer = newwin(1, COLS, LINES-1, 0)) == NULL)
1037 return 0;
1038 if ((help = newwin(1, 1, 1, 0)) == NULL)
1039 return 0;
1041 body_lines = LINES-3;
1042 body_cols = COLS;
1044 wbkgd(body, body_face.body);
1045 wbkgd(echoarea, minibuffer_face.background);
1047 update_x_offset();
1049 keypad(body, TRUE);
1050 scrollok(body, FALSE);
1052 /* non-blocking input */
1053 wtimeout(body, 0);
1054 wtimeout(help, 0);
1056 mvwprintw(body, 0, 0, "");
1058 evtimer_set(&resizeev, handle_resize, NULL);
1060 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1061 event_add(&stdioev, NULL);
1063 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1064 signal_add(&winchev, NULL);
1066 return 1;
1069 void
1070 ui_main_loop(void)
1072 switch_to_tab(current_tab);
1073 redraw_tab(current_tab);
1075 event_dispatch();
1078 void
1079 ui_on_tab_loaded(struct tab *tab)
1081 stop_loading_anim(tab);
1082 message("Loaded %s", tab->hist_cur->h);
1084 redraw_tabline();
1085 wrefresh(tabline);
1086 place_cursor(0);
1089 void
1090 ui_on_tab_refresh(struct tab *tab)
1092 wrap_page(&tab->buffer, body_cols);
1093 if (tab == current_tab)
1094 redraw_tab(tab);
1095 else
1096 tab->flags |= TAB_URGENT;
1099 const char *
1100 ui_keyname(int k)
1102 return keyname(k);
1105 void
1106 ui_toggle_side_window(void)
1108 side_window = !side_window;
1109 if (side_window)
1110 recompute_help();
1111 else
1112 in_side_window = 0;
1115 * ugly hack, but otherwise the window doesn't get updated
1116 * until I call rearrange_windows a second time (e.g. via
1117 * C-l). I will be happy to know why something like this is
1118 * needed.
1120 rearrange_windows();
1121 rearrange_windows();
1124 void
1125 ui_schedule_redraw(void)
1127 should_rearrange_windows = 1;
1130 void
1131 ui_require_input(struct tab *tab, int hide, int proto)
1133 void (*fn)(void);
1135 if (proto == PROTO_GEMINI)
1136 fn = ir_select_gemini;
1137 else if (proto == PROTO_GOPHER)
1138 fn = ir_select_gopher;
1139 else
1140 abort();
1142 /* TODO: hard-switching to another tab is ugly */
1143 switch_to_tab(tab);
1145 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1146 &ir_history, NULL, NULL);
1147 strlcpy(ministate.prompt, "Input required: ",
1148 sizeof(ministate.prompt));
1149 redraw_tab(tab);
1152 void
1153 ui_after_message_hook(void)
1155 redraw_minibuffer();
1156 place_cursor(0);
1159 void
1160 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1161 struct tab *data)
1163 yornp(prompt, fn, data);
1164 redraw_tab(current_tab);
1167 void
1168 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1169 struct tab *data)
1171 minibuffer_read(prompt, fn, data);
1172 redraw_tab(current_tab);
1175 void
1176 ui_other_window(void)
1178 if (side_window)
1179 in_side_window = !in_side_window;
1180 else
1181 message("No other window to select");
1184 void
1185 ui_suspend(void)
1187 endwin();
1189 kill(getpid(), SIGSTOP);
1191 refresh();
1192 clear();
1193 rearrange_windows();
1196 void
1197 ui_end(void)
1199 endwin();