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 "telescope.h"
47 #include "ui.h"
48 #include "utf8.h"
50 static struct event stdioev, winchev;
52 static void restore_curs_x(struct buffer *);
54 static int readkey(void);
55 static void dispatch_stdio(int, short, void*);
56 static void handle_resize(int, short, void*);
57 static void handle_resize_nodelay(int, short, void*);
58 static void rearrange_windows(void);
59 static void line_prefix_and_text(struct vline *, char *, size_t, const char **, const char **);
60 static void print_vline(int, int, WINDOW*, struct vline*);
61 static void redraw_tabline(void);
62 static void redraw_window(WINDOW*, int, int, int, struct buffer*);
63 static void redraw_help(void);
64 static void redraw_body(struct tab*);
65 static void redraw_modeline(struct tab*);
66 static void redraw_minibuffer(void);
67 static void do_redraw_echoarea(void);
68 static void do_redraw_minibuffer(void);
69 static void do_redraw_minibuffer_compl(void);
70 static void place_cursor(int);
71 static void redraw_tab(struct tab*);
72 static void update_loading_anim(int, short, void*);
73 static void stop_loading_anim(struct tab*);
75 static int should_rearrange_windows;
76 static int too_small;
77 static int x_offset;
79 struct thiskey thiskey;
80 struct tab *current_tab;
82 static struct event resizeev;
83 static struct timeval resize_timer = { 0, 250000 };
85 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
87 int body_lines, body_cols;
89 static WINDOW *help;
90 /* not static so we can see them from help.c */
91 struct buffer helpwin;
92 int help_lines, help_cols;
94 static int side_window;
95 static int in_side_window;
97 static struct timeval loadingev_timer = { 0, 250000 };
99 static char keybuf[64];
101 /* XXX: don't forget to init these in main() */
102 struct kmap global_map,
103 minibuffer_map,
104 *current_map,
105 *base_map;
107 static inline void
108 update_x_offset(void)
110 if (olivetti_mode && fill_column < body_cols)
111 x_offset = (body_cols - fill_column)/2;
112 else
113 x_offset = 0;
116 void
117 save_excursion(struct excursion *place, struct buffer *buffer)
119 place->curs_x = buffer->curs_x;
120 place->curs_y = buffer->curs_y;
121 place->line_off = buffer->line_off;
122 place->top_line = buffer->top_line;
123 place->current_line = buffer->current_line;
124 place->cpoff = buffer->cpoff;
127 void
128 restore_excursion(struct excursion *place, struct buffer *buffer)
130 buffer->curs_x = place->curs_x;
131 buffer->curs_y = place->curs_y;
132 buffer->line_off = place->line_off;
133 buffer->top_line = place->top_line;
134 buffer->current_line = place->current_line;
135 buffer->cpoff = place->cpoff;
138 static void
139 restore_curs_x(struct buffer *buffer)
141 struct vline *vl;
142 const char *prfx, *text;
144 vl = buffer->current_line;
145 if (vl == NULL || vl->line == NULL)
146 buffer->curs_x = buffer->cpoff = 0;
147 else if (vl->parent->data != NULL) {
148 text = vl->parent->data;
149 buffer->curs_x = utf8_snwidth(text + 1, buffer->cpoff) + 1;
150 } else
151 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
153 buffer->curs_x += x_offset;
155 if (vl == NULL)
156 return;
158 if (vl->parent->data != NULL)
159 buffer->curs_x += utf8_swidth_between(vl->parent->line,
160 vl->parent->data);
161 else {
162 prfx = line_prefixes[vl->parent->type].prfx1;
163 buffer->curs_x += utf8_swidth(prfx);
167 void
168 global_key_unbound(void)
170 message("%s is undefined", keybuf);
173 struct buffer *
174 current_buffer(void)
176 if (in_minibuffer)
177 return &ministate.buffer;
178 if (in_side_window)
179 return &helpwin;
180 return &current_tab->buffer;
183 static int
184 readkey(void)
186 uint32_t state = 0;
188 if ((thiskey.key = wgetch(body)) == ERR)
189 return 0;
191 thiskey.meta = thiskey.key == 27;
192 if (thiskey.meta) {
193 thiskey.key = wgetch(body);
194 if (thiskey.key == ERR || thiskey.key == 27) {
195 thiskey.meta = 0;
196 thiskey.key = 27;
200 thiskey.cp = 0;
202 if ((unsigned int)thiskey.key >= UINT8_MAX)
203 return 1;
205 while (1) {
206 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
207 break;
208 if ((thiskey.key = wgetch(body)) == ERR) {
209 message("Error decoding user input");
210 return 0;
214 return 1;
217 static void
218 dispatch_stdio(int fd, short ev, void *d)
220 struct keymap *k;
221 const char *keyname;
222 char tmp[5] = {0};
224 /* TODO: schedule a redraw? */
225 if (too_small)
226 return;
228 if (!readkey())
229 return;
231 if (keybuf[0] != '\0')
232 strlcat(keybuf, " ", sizeof(keybuf));
233 if (thiskey.meta)
234 strlcat(keybuf, "M-", sizeof(keybuf));
235 if (thiskey.cp != 0) {
236 utf8_encode(thiskey.cp, tmp);
237 strlcat(keybuf, tmp, sizeof(keybuf));
238 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
239 strlcat(keybuf, keyname, sizeof(keybuf));
240 } else {
241 tmp[0] = thiskey.key;
242 strlcat(keybuf, tmp, sizeof(keybuf));
245 TAILQ_FOREACH(k, &current_map->m, keymaps) {
246 if (k->meta == thiskey.meta &&
247 k->key == thiskey.key) {
248 if (k->fn == NULL)
249 current_map = &k->map;
250 else {
251 current_map = base_map;
252 strlcpy(keybuf, "", sizeof(keybuf));
253 k->fn(current_buffer());
255 goto done;
259 if (current_map->unhandled_input != NULL)
260 current_map->unhandled_input();
261 else
262 global_key_unbound();
264 strlcpy(keybuf, "", sizeof(keybuf));
265 current_map = base_map;
267 done:
268 if (side_window)
269 recompute_help();
271 if (should_rearrange_windows)
272 rearrange_windows();
273 redraw_tab(current_tab);
276 static void
277 handle_resize(int sig, short ev, void *d)
279 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
280 event_del(&resizeev);
282 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
283 evtimer_add(&resizeev, &resize_timer);
286 static void
287 handle_resize_nodelay(int s, short ev, void *d)
289 endwin();
290 refresh();
291 clear();
293 rearrange_windows();
296 static void
297 rearrange_windows(void)
299 int lines;
301 should_rearrange_windows = 0;
303 lines = LINES;
305 if ((too_small = lines < 15)) {
306 erase();
307 printw("Window too small.");
308 refresh();
309 return;
312 /* move and resize the windows, in reverse order! */
314 if (in_minibuffer == MB_COMPREAD) {
315 mvwin(minibuffer, lines-10, 0);
316 wresize(minibuffer, 10, COLS);
317 lines -= 10;
319 wrap_page(&ministate.compl.buffer, COLS);
322 mvwin(echoarea, --lines, 0);
323 wresize(echoarea, 1, COLS);
325 mvwin(modeline, --lines, 0);
326 wresize(modeline, 1, COLS);
328 body_lines = --lines;
329 body_cols = COLS;
331 if (side_window) {
332 help_cols = 0.3 * COLS;
333 help_lines = lines;
334 mvwin(help, 1, 0);
335 wresize(help, help_lines, help_cols);
337 wrap_page(&helpwin, help_cols);
339 body_cols = COLS - help_cols - 1;
340 mvwin(body, 1, help_cols);
341 } else
342 mvwin(body, 1, 0);
344 update_x_offset();
345 wresize(body, body_lines, body_cols);
347 wresize(tabline, 1, COLS);
349 wrap_page(&current_tab->buffer, body_cols);
350 redraw_tab(current_tab);
353 static void
354 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
355 const char **prfx_ret, const char **text_ret)
357 int type, i, cont, width;
358 char *space, *t;
360 if ((*text_ret = vl->line) == NULL)
361 *text_ret = "";
363 cont = vl->flags & L_CONTINUATION;
364 type = vl->parent->type;
365 if (!cont)
366 *prfx_ret = line_prefixes[type].prfx1;
367 else
368 *prfx_ret = line_prefixes[type].prfx2;
370 space = vl->parent->data;
371 if (!emojify_link || type != LINE_LINK || space == NULL)
372 return;
374 if (cont) {
375 memset(buf, 0, len);
376 width = utf8_swidth_between(vl->parent->line, space);
377 for (i = 0; i < width + 1; ++i)
378 strlcat(buf, " ", len);
379 } else {
380 strlcpy(buf, *text_ret, len);
381 if ((t = strchr(buf, ' ')) != NULL)
382 *t = '\0';
383 strlcat(buf, " ", len);
385 /* skip the emoji */
386 *text_ret += (space - vl->parent->line) + 1;
389 *prfx_ret = buf;
392 static inline void
393 print_vline_descr(int width, WINDOW *window, struct vline *vl)
395 int x, y, goal;
397 if (vl->parent->type != LINE_COMPL &&
398 vl->parent->type != LINE_COMPL_CURRENT &&
399 vl->parent->type != LINE_HELP)
400 return;
402 if (vl->parent->alt == NULL)
403 return;
405 (void)y;
406 getyx(window, y, x);
408 if (vl->parent->type == LINE_HELP)
409 goal = 8;
410 else
411 goal = width/2;
413 if (goal <= x)
414 wprintw(window, " ");
415 for (; goal > x; ++x)
416 wprintw(window, " ");
418 wprintw(window, "%s", vl->parent->alt);
421 /*
422 * Core part of the rendering. It prints a vline starting from the
423 * current cursor position. Printing a vline consists of skipping
424 * `off' columns (for olivetti-mode), print the correct prefix (which
425 * may be the emoji in case of emojified links-lines), printing the
426 * text itself, filling until width - off and filling off columns
427 * again.
428 */
429 static void
430 print_vline(int off, int width, WINDOW *window, struct vline *vl)
432 /*
433 * Believe me or not, I've seen emoji ten code points long!
434 * That means, to stay large, 4*10 bytes + NUL.
435 */
436 char emojibuf[41] = {0};
437 const char *text, *prfx;
438 struct line_face *f;
439 int i, left, x, y;
441 f = &line_faces[vl->parent->type];
443 /* unused, set by getyx */
444 (void)y;
446 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
448 wattr_on(window, body_face.left, NULL);
449 for (i = 0; i < off; i++)
450 waddch(window, ' ');
451 wattr_off(window, body_face.left, NULL);
453 wattr_on(window, f->prefix, NULL);
454 wprintw(window, "%s", prfx);
455 wattr_off(window, f->prefix, NULL);
457 wattr_on(window, f->text, NULL);
458 wprintw(window, "%s", text);
459 print_vline_descr(width, window, vl);
460 wattr_off(window, f->text, NULL);
462 getyx(window, y, x);
464 left = width - x;
466 wattr_on(window, f->trail, NULL);
467 for (i = 0; i < left - off; ++i)
468 waddch(window, ' ');
469 wattr_off(window, f->trail, NULL);
471 wattr_on(window, body_face.right, NULL);
472 for (i = 0; i < off; i++)
473 waddch(window, ' ');
474 wattr_off(window, body_face.right, NULL);
478 static void
479 redraw_tabline(void)
481 struct tab *tab;
482 size_t toskip, ots, tabwidth, space, x;
483 int current, y, truncated, pair;
484 const char *title;
485 char buf[25];
487 x = 0;
489 /* unused, but setted by a getyx */
490 (void)y;
492 tabwidth = sizeof(buf)+1;
493 space = COLS-2;
495 toskip = 0;
496 TAILQ_FOREACH(tab, &tabshead, tabs) {
497 toskip++;
498 if (tab == current_tab)
499 break;
502 if (toskip * tabwidth < space)
503 toskip = 0;
504 else {
505 ots = toskip;
506 toskip--;
507 while (toskip != 0 &&
508 (ots - toskip+1) * tabwidth < space)
509 toskip--;
512 werase(tabline);
513 wattr_on(tabline, tab_face.background, NULL);
514 wprintw(tabline, toskip == 0 ? " " : "<");
515 wattr_off(tabline, tab_face.background, NULL);
517 truncated = 0;
518 TAILQ_FOREACH(tab, &tabshead, tabs) {
519 if (truncated)
520 break;
521 if (toskip != 0) {
522 toskip--;
523 continue;
526 getyx(tabline, y, x);
527 if (x + sizeof(buf)+2 >= (size_t)COLS)
528 truncated = 1;
530 current = tab == current_tab;
532 if (*(title = tab->buffer.page.title) == '\0')
533 title = tab->hist_cur->h;
535 if (tab->flags & TAB_URGENT)
536 strlcpy(buf, "!", sizeof(buf));
537 else
538 strlcpy(buf, " ", sizeof(buf));
540 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
541 /* truncation happens */
542 strlcpy(&buf[sizeof(buf)-4], "...", 4);
543 } else {
544 /* pad with spaces */
545 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
546 /* nop */ ;
549 pair = current ? tab_face.current : tab_face.tab;
550 wattr_on(tabline, pair, NULL);
551 wprintw(tabline, "%s", buf);
552 wattr_off(tabline, pair, NULL);
554 wattr_on(tabline, tab_face.background, NULL);
555 if (TAILQ_NEXT(tab, tabs) != NULL)
556 wprintw(tabline, "┃");
557 wattr_off(tabline, tab_face.background, NULL);
560 wattr_on(tabline, tab_face.background, NULL);
561 for (; x < (size_t)COLS; ++x)
562 waddch(tabline, ' ');
563 if (truncated)
564 mvwprintw(tabline, 0, COLS-1, ">");
565 wattr_off(tabline, tab_face.background, NULL);
568 /*
569 * Compute the first visible line around vl. Try to search forward
570 * until the end of the buffer; if a visible line is not found, search
571 * backward. Return NULL if no viable line was found.
572 */
573 struct vline *
574 adjust_line(struct vline *vl, struct buffer *buffer)
576 struct vline *t;
578 if (vl == NULL)
579 return NULL;
581 if (!(vl->parent->flags & L_HIDDEN))
582 return vl;
584 /* search forward */
585 for (t = vl;
586 t != NULL && t->parent->flags & L_HIDDEN;
587 t = TAILQ_NEXT(t, vlines))
588 ; /* nop */
590 if (t != NULL)
591 return t;
593 /* search backward */
594 for (t = vl;
595 t != NULL && t->parent->flags & L_HIDDEN;
596 t = TAILQ_PREV(t, vhead, vlines))
597 ; /* nop */
599 return t;
602 static void
603 redraw_window(WINDOW *win, int off, int height, int width,
604 struct buffer *buffer)
606 struct vline *vl;
607 int l, onscreen;
609 restore_curs_x(buffer);
611 /*
612 * TODO: ignoring buffer->force_update and always
613 * re-rendering. In theory we can recompute the y position
614 * without a re-render, and optimize here. It's not the only
615 * optimisation possible here, wscrl wolud also be an
616 * interesting one.
617 */
619 again:
620 werase(win);
621 buffer->curs_y = 0;
623 if (TAILQ_EMPTY(&buffer->head))
624 goto end;
626 if (buffer->top_line == NULL)
627 buffer->top_line = TAILQ_FIRST(&buffer->head);
629 buffer->top_line = adjust_line(buffer->top_line, buffer);
630 if (buffer->top_line == NULL)
631 goto end;
633 buffer->current_line = adjust_line(buffer->current_line, buffer);
635 l = 0;
636 onscreen = 0;
637 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
638 if (vl->parent->flags & L_HIDDEN)
639 continue;
641 wmove(win, l, 0);
642 print_vline(off, width, win, vl);
644 if (vl == buffer->current_line)
645 onscreen = 1;
647 if (!onscreen)
648 buffer->curs_y++;
650 l++;
651 if (l == height)
652 break;
655 if (!onscreen) {
656 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
657 if (vl == buffer->current_line)
658 break;
659 if (vl->parent->flags & L_HIDDEN)
660 continue;
661 buffer->line_off++;
662 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
665 if (vl != NULL)
666 goto again;
669 buffer->last_line_off = buffer->line_off;
670 buffer->force_redraw = 0;
671 end:
672 wmove(win, buffer->curs_y, buffer->curs_x);
675 static void
676 redraw_help(void)
678 redraw_window(help, 0, help_lines, help_cols, &helpwin);
681 static void
682 redraw_body(struct tab *tab)
684 static struct tab *last_tab;
686 if (last_tab != tab)
687 tab->buffer.force_redraw =1;
688 last_tab = tab;
690 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
693 static inline char
694 trust_status_char(enum trust_state ts)
696 switch (ts) {
697 case TS_UNKNOWN: return '-';
698 case TS_UNTRUSTED: return '!';
699 case TS_TEMP_TRUSTED: return '!';
700 case TS_TRUSTED: return 'v';
701 case TS_VERIFIED: return 'V';
702 default: return 'X';
706 static void
707 redraw_modeline(struct tab *tab)
709 struct buffer *buffer;
710 double pct;
711 int x, y, max_x, max_y;
712 const char *mode;
713 const char *spin = "-\\|/";
715 buffer = current_buffer();
716 mode = buffer->page.name;
718 werase(modeline);
719 wattr_on(modeline, modeline_face.background, NULL);
720 wmove(modeline, 0, 0);
722 wprintw(modeline, "-%c%c %s ",
723 spin[tab->loading_anim_step],
724 trust_status_char(tab->trust),
725 mode == NULL ? "(none)" : mode);
727 pct = (buffer->line_off + buffer->curs_y) * 100.0
728 / buffer->line_max;
730 if (buffer->line_max <= (size_t)body_lines)
731 wprintw(modeline, "All ");
732 else if (buffer->line_off == 0)
733 wprintw(modeline, "Top ");
734 else if (buffer->line_off + body_lines >= buffer->line_max)
735 wprintw(modeline, "Bottom ");
736 else
737 wprintw(modeline, "%.0f%% ", pct);
739 wprintw(modeline, "%d/%d %s ",
740 buffer->line_off + buffer->curs_y,
741 buffer->line_max,
742 tab->hist_cur->h);
744 getyx(modeline, y, x);
745 getmaxyx(modeline, max_y, max_x);
747 (void)y;
748 (void)max_y;
750 for (; x < max_x; ++x)
751 waddstr(modeline, "-");
753 wattr_off(modeline, modeline_face.background, NULL);
756 static void
757 redraw_minibuffer(void)
759 wattr_on(echoarea, minibuffer_face.background, NULL);
760 werase(echoarea);
762 if (in_minibuffer)
763 do_redraw_minibuffer();
764 else
765 do_redraw_echoarea();
767 if (in_minibuffer == MB_COMPREAD)
768 do_redraw_minibuffer_compl();
770 wattr_off(echoarea, minibuffer_face.background, NULL);
773 static void
774 do_redraw_echoarea(void)
776 struct vline *vl;
778 if (ministate.curmesg != NULL)
779 wprintw(echoarea, "%s", ministate.curmesg);
780 else if (*keybuf != '\0')
781 waddstr(echoarea, keybuf);
782 else {
783 /* If nothing else, show the URL at point */
784 vl = current_tab->buffer.current_line;
785 if (vl != NULL && vl->parent->type == LINE_LINK)
786 waddstr(echoarea, vl->parent->alt);
790 static void
791 do_redraw_minibuffer(void)
793 struct buffer *cmplbuf, *buffer;
794 size_t off_y, off_x = 0;
795 const char *start, *c;
797 cmplbuf = &ministate.compl.buffer;
798 buffer = &ministate.buffer;
799 (void)off_y; /* unused, set by getyx */
801 wmove(echoarea, 0, 0);
803 if (in_minibuffer == MB_COMPREAD)
804 wprintw(echoarea, "(%2d) ",
805 cmplbuf->line_max);
807 wprintw(echoarea, "%s", ministate.prompt);
808 if (ministate.hist_cur != NULL)
809 wprintw(echoarea, "(%zu/%zu) ",
810 ministate.hist_off + 1,
811 ministate.history->len);
813 getyx(echoarea, off_y, off_x);
815 start = ministate.hist_cur != NULL
816 ? ministate.hist_cur->h
817 : ministate.buf;
818 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
819 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
820 start = utf8_next_cp(start);
823 waddstr(echoarea, start);
825 if (ministate.curmesg != NULL)
826 wprintw(echoarea, " [%s]", ministate.curmesg);
828 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
831 static void
832 do_redraw_minibuffer_compl(void)
834 redraw_window(minibuffer, 0, 10, COLS,
835 &ministate.compl.buffer);
838 /*
839 * Place the cursor in the right ncurses window. If soft is 1, use
840 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
841 * wrefresh.
842 */
843 static void
844 place_cursor(int soft)
846 int (*touch)(WINDOW *);
848 if (soft)
849 touch = wnoutrefresh;
850 else
851 touch = wrefresh;
853 if (in_minibuffer) {
854 if (side_window)
855 touch(help);
856 touch(body);
857 touch(echoarea);
858 } else if (in_side_window) {
859 touch(body);
860 touch(echoarea);
861 touch(help);
862 } else {
863 if (side_window)
864 touch(help);
865 touch(echoarea);
866 touch(body);
870 static void
871 redraw_tab(struct tab *tab)
873 if (too_small)
874 return;
876 if (side_window) {
877 redraw_help();
878 wnoutrefresh(help);
881 redraw_tabline();
882 redraw_body(tab);
883 redraw_modeline(tab);
884 redraw_minibuffer();
886 wnoutrefresh(tabline);
887 wnoutrefresh(modeline);
889 if (in_minibuffer == MB_COMPREAD)
890 wnoutrefresh(minibuffer);
892 place_cursor(1);
894 doupdate();
896 if (set_title)
897 dprintf(1, "\033]0;%s - Telescope\a",
898 current_tab->buffer.page.title);
901 void
902 start_loading_anim(struct tab *tab)
904 if (tab->loading_anim)
905 return;
906 tab->loading_anim = 1;
907 evtimer_set(&tab->loadingev, update_loading_anim, tab);
908 evtimer_add(&tab->loadingev, &loadingev_timer);
911 static void
912 update_loading_anim(int fd, short ev, void *d)
914 struct tab *tab = d;
916 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
918 if (tab == current_tab) {
919 redraw_modeline(tab);
920 wrefresh(modeline);
921 wrefresh(body);
922 if (in_minibuffer)
923 wrefresh(echoarea);
926 evtimer_add(&tab->loadingev, &loadingev_timer);
929 static void
930 stop_loading_anim(struct tab *tab)
932 if (!tab->loading_anim)
933 return;
934 evtimer_del(&tab->loadingev);
935 tab->loading_anim = 0;
936 tab->loading_anim_step = 0;
938 if (tab != current_tab)
939 return;
941 redraw_modeline(tab);
943 wrefresh(modeline);
944 wrefresh(body);
945 if (in_minibuffer)
946 wrefresh(echoarea);
949 int
950 ui_print_colors(void)
952 int colors = 0, pairs = 0, can_change = 0;
953 int columns = 16, lines, color, i, j;
955 initscr();
956 if (has_colors()) {
957 start_color();
958 use_default_colors();
960 colors = COLORS;
961 pairs = COLOR_PAIRS;
962 can_change = can_change_color();
964 endwin();
966 printf("Term info:\n");
967 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
968 getenv("TERM"), colors, pairs, can_change);
969 printf("\n");
971 if (colors == 0) {
972 printf("No color support\n");
973 return 0;
976 printf("Available colors:\n\n");
977 lines = (colors - 1) / columns + 1;
978 color = 0;
979 for (i = 0; i < lines; ++i) {
980 for (j = 0; j < columns; ++j, ++color) {
981 printf("\033[0;38;5;%dm %03d", color, color);
983 printf("\n");
986 printf("\033[0m");
987 fflush(stdout);
988 return 0;
991 int
992 ui_init()
994 setlocale(LC_ALL, "");
996 if (TAILQ_EMPTY(&global_map.m)) {
997 fprintf(stderr, "no keys defined!\n");
998 return 0;
1001 minibuffer_init();
1003 /* initialize help window */
1004 TAILQ_INIT(&helpwin.head);
1005 TAILQ_INIT(&helpwin.page.head);
1007 base_map = &global_map;
1008 current_map = &global_map;
1010 initscr();
1012 if (enable_colors) {
1013 if (has_colors()) {
1014 start_color();
1015 use_default_colors();
1016 } else
1017 enable_colors = 0;
1020 config_apply_style();
1022 raw();
1023 noecho();
1024 nonl();
1025 intrflush(stdscr, FALSE);
1027 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1028 return 0;
1029 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1030 return 0;
1031 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1032 return 0;
1033 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1034 return 0;
1035 if ((minibuffer = newwin(1, COLS, LINES-1, 0)) == NULL)
1036 return 0;
1037 if ((help = newwin(1, 1, 1, 0)) == NULL)
1038 return 0;
1040 body_lines = LINES-3;
1041 body_cols = COLS;
1043 wbkgd(body, body_face.body);
1044 wbkgd(echoarea, minibuffer_face.background);
1046 update_x_offset();
1048 keypad(body, TRUE);
1049 scrollok(body, FALSE);
1051 /* non-blocking input */
1052 wtimeout(body, 0);
1053 wtimeout(help, 0);
1055 mvwprintw(body, 0, 0, "");
1057 evtimer_set(&resizeev, handle_resize, NULL);
1059 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1060 event_add(&stdioev, NULL);
1062 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1063 signal_add(&winchev, NULL);
1065 return 1;
1068 void
1069 ui_main_loop(void)
1071 switch_to_tab(current_tab);
1072 redraw_tab(current_tab);
1074 event_dispatch();
1077 void
1078 ui_on_tab_loaded(struct tab *tab)
1080 stop_loading_anim(tab);
1081 message("Loaded %s", tab->hist_cur->h);
1083 redraw_tabline();
1084 wrefresh(tabline);
1085 place_cursor(0);
1088 void
1089 ui_on_tab_refresh(struct tab *tab)
1091 wrap_page(&tab->buffer, body_cols);
1092 if (tab == current_tab)
1093 redraw_tab(tab);
1094 else
1095 tab->flags |= TAB_URGENT;
1098 const char *
1099 ui_keyname(int k)
1101 return keyname(k);
1104 void
1105 ui_toggle_side_window(void)
1107 side_window = !side_window;
1108 if (side_window)
1109 recompute_help();
1110 else
1111 in_side_window = 0;
1114 * ugly hack, but otherwise the window doesn't get updated
1115 * until I call rearrange_windows a second time (e.g. via
1116 * C-l). I will be happy to know why something like this is
1117 * needed.
1119 rearrange_windows();
1120 rearrange_windows();
1123 void
1124 ui_schedule_redraw(void)
1126 should_rearrange_windows = 1;
1129 void
1130 ui_require_input(struct tab *tab, int hide, int proto)
1132 void (*fn)(void);
1134 if (proto == PROTO_GEMINI)
1135 fn = ir_select_gemini;
1136 else if (proto == PROTO_GOPHER)
1137 fn = ir_select_gopher;
1138 else
1139 abort();
1141 /* TODO: hard-switching to another tab is ugly */
1142 switch_to_tab(tab);
1144 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1145 &ir_history, NULL, NULL);
1146 strlcpy(ministate.prompt, "Input required: ",
1147 sizeof(ministate.prompt));
1148 redraw_tab(tab);
1151 void
1152 ui_after_message_hook(void)
1154 redraw_minibuffer();
1155 place_cursor(0);
1158 void
1159 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1160 struct tab *data)
1162 yornp(prompt, fn, data);
1163 redraw_tab(current_tab);
1166 void
1167 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1168 struct tab *data)
1170 minibuffer_read(prompt, fn, data);
1171 redraw_tab(current_tab);
1174 void
1175 ui_other_window(void)
1177 if (side_window)
1178 in_side_window = !in_side_window;
1179 else
1180 message("No other window to select");
1183 void
1184 ui_suspend(void)
1186 endwin();
1188 kill(getpid(), SIGSTOP);
1190 refresh();
1191 clear();
1192 rearrange_windows();
1195 void
1196 ui_end(void)
1198 endwin();