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 /*
76 * Used to know when we're finished loading.
77 */
78 static int operating;
80 static int should_rearrange_windows;
81 static int too_small;
82 static int x_offset;
84 struct thiskey thiskey;
85 struct tab *current_tab;
87 static struct event resizeev;
88 static struct timeval resize_timer = { 0, 250000 };
90 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
92 int body_lines, body_cols;
94 static WINDOW *help;
95 /* not static so we can see them from help.c */
96 struct buffer helpwin;
97 int help_lines, help_cols;
99 static int side_window;
100 static int in_side_window;
102 static struct timeval loadingev_timer = { 0, 250000 };
104 static uint32_t tab_counter;
106 static char keybuf[64];
108 /* XXX: don't forget to init these in main() */
109 struct kmap global_map,
110 minibuffer_map,
111 *current_map,
112 *base_map;
114 static inline void
115 update_x_offset(void)
117 if (olivetti_mode && fill_column < body_cols)
118 x_offset = (body_cols - fill_column)/2;
119 else
120 x_offset = 0;
123 void
124 save_excursion(struct excursion *place, struct buffer *buffer)
126 place->curs_x = buffer->curs_x;
127 place->curs_y = buffer->curs_y;
128 place->line_off = buffer->line_off;
129 place->top_line = buffer->top_line;
130 place->current_line = buffer->current_line;
131 place->cpoff = buffer->cpoff;
134 void
135 restore_excursion(struct excursion *place, struct buffer *buffer)
137 buffer->curs_x = place->curs_x;
138 buffer->curs_y = place->curs_y;
139 buffer->line_off = place->line_off;
140 buffer->top_line = place->top_line;
141 buffer->current_line = place->current_line;
142 buffer->cpoff = place->cpoff;
145 static void
146 restore_curs_x(struct buffer *buffer)
148 struct vline *vl;
149 const char *prfx, *text;
151 vl = buffer->current_line;
152 if (vl == NULL || vl->line == NULL)
153 buffer->curs_x = buffer->cpoff = 0;
154 else if (vl->parent->data != NULL) {
155 text = vl->parent->data;
156 buffer->curs_x = utf8_snwidth(text + 1, buffer->cpoff) + 1;
157 } else
158 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
160 buffer->curs_x += x_offset;
162 if (vl == NULL)
163 return;
165 if (vl->parent->data != NULL)
166 buffer->curs_x += utf8_swidth_between(vl->parent->line, vl->parent->data);
167 else {
168 prfx = line_prefixes[vl->parent->type].prfx1;
169 buffer->curs_x += utf8_swidth(prfx);
173 void
174 global_key_unbound(void)
176 message("%s is undefined", keybuf);
179 struct buffer *
180 current_buffer(void)
182 if (in_minibuffer)
183 return &ministate.buffer;
184 if (in_side_window)
185 return &helpwin;
186 return &current_tab->buffer;
189 static int
190 readkey(void)
192 uint32_t state = 0;
194 if ((thiskey.key = wgetch(body)) == ERR)
195 return 0;
197 thiskey.meta = thiskey.key == 27;
198 if (thiskey.meta) {
199 thiskey.key = wgetch(body);
200 if (thiskey.key == ERR || thiskey.key == 27) {
201 thiskey.meta = 0;
202 thiskey.key = 27;
206 thiskey.cp = 0;
207 if ((unsigned int)thiskey.key < UINT8_MAX) {
208 while (1) {
209 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
210 break;
211 if ((thiskey.key = wgetch(body)) == ERR) {
212 message("Error decoding user input");
213 return 0;
218 return 1;
221 static void
222 dispatch_stdio(int fd, short ev, void *d)
224 struct keymap *k;
225 const char *keyname;
226 char tmp[5] = {0};
228 /* TODO: schedule a redraw? */
229 if (too_small)
230 return;
232 if (!readkey())
233 return;
235 if (keybuf[0] != '\0')
236 strlcat(keybuf, " ", sizeof(keybuf));
237 if (thiskey.meta)
238 strlcat(keybuf, "M-", sizeof(keybuf));
239 if (thiskey.cp != 0) {
240 utf8_encode(thiskey.cp, tmp);
241 strlcat(keybuf, tmp, sizeof(keybuf));
242 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
243 strlcat(keybuf, keyname, sizeof(keybuf));
244 } else {
245 tmp[0] = thiskey.key;
246 strlcat(keybuf, tmp, sizeof(keybuf));
249 TAILQ_FOREACH(k, &current_map->m, keymaps) {
250 if (k->meta == thiskey.meta &&
251 k->key == thiskey.key) {
252 if (k->fn == NULL)
253 current_map = &k->map;
254 else {
255 current_map = base_map;
256 strlcpy(keybuf, "", sizeof(keybuf));
257 k->fn(current_buffer());
259 goto done;
263 if (current_map->unhandled_input != NULL)
264 current_map->unhandled_input();
265 else
266 global_key_unbound();
268 strlcpy(keybuf, "", sizeof(keybuf));
269 current_map = base_map;
271 done:
272 if (side_window)
273 recompute_help();
275 if (should_rearrange_windows)
276 rearrange_windows();
277 redraw_tab(current_tab);
280 static void
281 handle_resize(int sig, short ev, void *d)
283 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
284 event_del(&resizeev);
286 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
287 evtimer_add(&resizeev, &resize_timer);
290 static void
291 handle_resize_nodelay(int s, short ev, void *d)
293 endwin();
294 refresh();
295 clear();
297 rearrange_windows();
300 static void
301 rearrange_windows(void)
303 int lines;
305 should_rearrange_windows = 0;
307 lines = LINES;
309 if ((too_small = lines < 15)) {
310 erase();
311 printw("Window too small.");
312 refresh();
313 return;
316 /* move and resize the windows, in reverse order! */
318 if (in_minibuffer == MB_COMPREAD) {
319 mvwin(minibuffer, lines-10, 0);
320 wresize(minibuffer, 10, COLS);
321 lines -= 10;
323 wrap_page(&ministate.compl.buffer, COLS);
326 mvwin(echoarea, --lines, 0);
327 wresize(echoarea, 1, COLS);
329 mvwin(modeline, --lines, 0);
330 wresize(modeline, 1, COLS);
332 body_lines = --lines;
333 body_cols = COLS;
335 if (side_window) {
336 help_cols = 0.3 * COLS;
337 help_lines = lines;
338 mvwin(help, 1, 0);
339 wresize(help, help_lines, help_cols);
341 wrap_page(&helpwin, help_cols);
343 body_cols = COLS - help_cols - 1;
344 mvwin(body, 1, help_cols);
345 } else
346 mvwin(body, 1, 0);
348 update_x_offset();
349 wresize(body, body_lines, body_cols);
351 wresize(tabline, 1, COLS);
353 wrap_page(&current_tab->buffer, body_cols);
354 redraw_tab(current_tab);
357 static void
358 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
359 const char **prfx_ret, const char **text_ret)
361 int type, i, cont, width;
362 char *space, *t;
364 if ((*text_ret = vl->line) == NULL)
365 *text_ret = "";
367 cont = vl->flags & L_CONTINUATION;
368 type = vl->parent->type;
369 if (!cont)
370 *prfx_ret = line_prefixes[type].prfx1;
371 else
372 *prfx_ret = line_prefixes[type].prfx2;
374 space = vl->parent->data;
375 if (!emojify_link || type != LINE_LINK || space == NULL)
376 return;
378 if (cont) {
379 memset(buf, 0, len);
380 width = utf8_swidth_between(vl->parent->line, space);
381 for (i = 0; i < width + 1; ++i)
382 strlcat(buf, " ", len);
383 } else {
384 strlcpy(buf, *text_ret, len);
385 if ((t = strchr(buf, ' ')) != NULL)
386 *t = '\0';
387 strlcat(buf, " ", len);
389 /* skip the emoji */
390 *text_ret += (space - vl->parent->line) + 1;
393 *prfx_ret = buf;
396 static inline void
397 print_vline_descr(int width, WINDOW *window, struct vline *vl)
399 int x, y, goal;
401 if (vl->parent->type != LINE_COMPL &&
402 vl->parent->type != LINE_COMPL_CURRENT &&
403 vl->parent->type != LINE_HELP)
404 return;
406 if (vl->parent->alt == NULL)
407 return;
409 (void)y;
410 getyx(window, y, x);
412 if (vl->parent->type == LINE_HELP)
413 goal = 8;
414 else
415 goal = width/2;
417 if (goal <= x)
418 wprintw(window, " ");
419 for (; goal > x; ++x)
420 wprintw(window, " ");
422 wprintw(window, "%s", vl->parent->alt);
425 /*
426 * Core part of the rendering. It prints a vline starting from the
427 * current cursor position. Printing a vline consists of skipping
428 * `off' columns (for olivetti-mode), print the correct prefix (which
429 * may be the emoji in case of emojified links-lines), printing the
430 * text itself, filling until width - off and filling off columns
431 * again.
432 */
433 static void
434 print_vline(int off, int width, WINDOW *window, struct vline *vl)
436 /*
437 * Believe me or not, I've seen emoji ten code points long!
438 * That means, to stay large, 4*10 bytes + NUL.
439 */
440 char emojibuf[41] = {0};
441 const char *text, *prfx;
442 struct line_face *f;
443 int i, left, x, y;
445 f = &line_faces[vl->parent->type];
447 /* unused, set by getyx */
448 (void)y;
450 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
452 wattr_on(window, body_face.left, NULL);
453 for (i = 0; i < off; i++)
454 waddch(window, ' ');
455 wattr_off(window, body_face.left, NULL);
457 wattr_on(window, f->prefix, NULL);
458 wprintw(window, "%s", prfx);
459 wattr_off(window, f->prefix, NULL);
461 wattr_on(window, f->text, NULL);
462 wprintw(window, "%s", text);
463 print_vline_descr(width, window, vl);
464 wattr_off(window, f->text, NULL);
466 getyx(window, y, x);
468 left = width - x;
470 wattr_on(window, f->trail, NULL);
471 for (i = 0; i < left - off; ++i)
472 waddch(window, ' ');
473 wattr_off(window, f->trail, NULL);
475 wattr_on(window, body_face.right, NULL);
476 for (i = 0; i < off; i++)
477 waddch(window, ' ');
478 wattr_off(window, body_face.right, NULL);
482 static void
483 redraw_tabline(void)
485 struct tab *tab;
486 size_t toskip, ots, tabwidth, space, x;
487 int current, y, truncated, pair;
488 const char *title;
489 char buf[25];
491 x = 0;
493 /* unused, but setted by a getyx */
494 (void)y;
496 tabwidth = sizeof(buf)+1;
497 space = COLS-2;
499 toskip = 0;
500 TAILQ_FOREACH(tab, &tabshead, tabs) {
501 toskip++;
502 if (tab == current_tab)
503 break;
506 if (toskip * tabwidth < space)
507 toskip = 0;
508 else {
509 ots = toskip;
510 toskip--;
511 while (toskip != 0 &&
512 (ots - toskip+1) * tabwidth < space)
513 toskip--;
516 werase(tabline);
517 wattr_on(tabline, tab_face.background, NULL);
518 wprintw(tabline, toskip == 0 ? " " : "<");
519 wattr_off(tabline, tab_face.background, NULL);
521 truncated = 0;
522 TAILQ_FOREACH(tab, &tabshead, tabs) {
523 if (truncated)
524 break;
525 if (toskip != 0) {
526 toskip--;
527 continue;
530 getyx(tabline, y, x);
531 if (x + sizeof(buf)+2 >= (size_t)COLS)
532 truncated = 1;
534 current = tab == current_tab;
536 if (*(title = tab->buffer.page.title) == '\0')
537 title = tab->hist_cur->h;
539 if (tab->flags & TAB_URGENT)
540 strlcpy(buf, "!", sizeof(buf));
541 else
542 strlcpy(buf, " ", sizeof(buf));
544 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
545 /* truncation happens */
546 strlcpy(&buf[sizeof(buf)-4], "...", 4);
547 } else {
548 /* pad with spaces */
549 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
550 /* nop */ ;
553 pair = current ? tab_face.current : tab_face.tab;
554 wattr_on(tabline, pair, NULL);
555 wprintw(tabline, "%s", buf);
556 wattr_off(tabline, pair, NULL);
558 wattr_on(tabline, tab_face.background, NULL);
559 if (TAILQ_NEXT(tab, tabs) != NULL)
560 wprintw(tabline, "┃");
561 wattr_off(tabline, tab_face.background, NULL);
564 wattr_on(tabline, tab_face.background, NULL);
565 for (; x < (size_t)COLS; ++x)
566 waddch(tabline, ' ');
567 if (truncated)
568 mvwprintw(tabline, 0, COLS-1, ">");
569 wattr_off(tabline, tab_face.background, NULL);
572 /*
573 * Compute the first visible line around vl. Try to search forward
574 * until the end of the buffer; if a visible line is not found, search
575 * backward. Return NULL if no viable line was found.
576 */
577 struct vline *
578 adjust_line(struct vline *vl, struct buffer *buffer)
580 struct vline *t;
582 if (vl == NULL)
583 return NULL;
585 if (!(vl->parent->flags & L_HIDDEN))
586 return vl;
588 /* search forward */
589 for (t = vl;
590 t != NULL && t->parent->flags & L_HIDDEN;
591 t = TAILQ_NEXT(t, vlines))
592 ; /* nop */
594 if (t != NULL)
595 return t;
597 /* search backward */
598 for (t = vl;
599 t != NULL && t->parent->flags & L_HIDDEN;
600 t = TAILQ_PREV(t, vhead, vlines))
601 ; /* nop */
603 return t;
606 static void
607 redraw_window(WINDOW *win, int off, int height, int width, struct buffer *buffer)
609 struct vline *vl;
610 int l, onscreen;
612 restore_curs_x(buffer);
614 /*
615 * TODO: ignoring buffer->force_update and always
616 * re-rendering. In theory we can recompute the y position
617 * without a re-render, and optimize here. It's not the only
618 * optimisation possible here, wscrl wolud also be an
619 * interesting one.
620 */
622 again:
623 werase(win);
624 buffer->curs_y = 0;
626 if (TAILQ_EMPTY(&buffer->head))
627 goto end;
629 if (buffer->top_line == NULL)
630 buffer->top_line = TAILQ_FIRST(&buffer->head);
632 buffer->top_line = adjust_line(buffer->top_line, buffer);
633 if (buffer->top_line == NULL)
634 goto end;
636 buffer->current_line = adjust_line(buffer->current_line, buffer);
638 l = 0;
639 onscreen = 0;
640 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
641 if (vl->parent->flags & L_HIDDEN)
642 continue;
644 wmove(win, l, 0);
645 print_vline(off, width, win, vl);
647 if (vl == buffer->current_line)
648 onscreen = 1;
650 if (!onscreen)
651 buffer->curs_y++;
653 l++;
654 if (l == height)
655 break;
658 if (!onscreen) {
659 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
660 if (vl == buffer->current_line)
661 break;
662 if (vl->parent->flags & L_HIDDEN)
663 continue;
664 buffer->line_off++;
665 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
668 if (vl != NULL)
669 goto again;
672 buffer->last_line_off = buffer->line_off;
673 buffer->force_redraw = 0;
674 end:
675 wmove(win, buffer->curs_y, buffer->curs_x);
678 static void
679 redraw_help(void)
681 redraw_window(help, 0, help_lines, help_cols, &helpwin);
684 static void
685 redraw_body(struct tab *tab)
687 static struct tab *last_tab;
689 if (last_tab != tab)
690 tab->buffer.force_redraw =1;
691 last_tab = tab;
693 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
696 static inline char
697 trust_status_char(enum trust_state ts)
699 switch (ts) {
700 case TS_UNKNOWN: return '-';
701 case TS_UNTRUSTED: return '!';
702 case TS_TEMP_TRUSTED: return '!';
703 case TS_TRUSTED: return 'v';
704 case TS_VERIFIED: return 'V';
705 default: return 'X';
709 static void
710 redraw_modeline(struct tab *tab)
712 struct buffer *buffer;
713 double pct;
714 int x, y, max_x, max_y;
715 const char *mode;
716 const char *spin = "-\\|/";
718 buffer = current_buffer();
719 mode = buffer->page.name;
721 werase(modeline);
722 wattr_on(modeline, modeline_face.background, NULL);
723 wmove(modeline, 0, 0);
725 wprintw(modeline, "-%c%c %s ",
726 spin[tab->loading_anim_step],
727 trust_status_char(tab->trust),
728 mode == NULL ? "(none)" : mode);
730 pct = (buffer->line_off + buffer->curs_y) * 100.0
731 / buffer->line_max;
733 if (buffer->line_max <= (size_t)body_lines)
734 wprintw(modeline, "All ");
735 else if (buffer->line_off == 0)
736 wprintw(modeline, "Top ");
737 else if (buffer->line_off + body_lines >= buffer->line_max)
738 wprintw(modeline, "Bottom ");
739 else
740 wprintw(modeline, "%.0f%% ", pct);
742 wprintw(modeline, "%d/%d %s ",
743 buffer->line_off + buffer->curs_y,
744 buffer->line_max,
745 tab->hist_cur->h);
747 getyx(modeline, y, x);
748 getmaxyx(modeline, max_y, max_x);
750 (void)y;
751 (void)max_y;
753 for (; x < max_x; ++x)
754 waddstr(modeline, "-");
756 wattr_off(modeline, modeline_face.background, NULL);
759 static void
760 redraw_minibuffer(void)
762 wattr_on(echoarea, minibuffer_face.background, NULL);
763 werase(echoarea);
765 if (in_minibuffer)
766 do_redraw_minibuffer();
767 else
768 do_redraw_echoarea();
770 if (in_minibuffer == MB_COMPREAD)
771 do_redraw_minibuffer_compl();
773 wattr_off(echoarea, minibuffer_face.background, NULL);
776 static void
777 do_redraw_echoarea(void)
779 struct vline *vl;
781 if (ministate.curmesg != NULL)
782 wprintw(echoarea, "%s", ministate.curmesg);
783 else if (*keybuf != '\0')
784 waddstr(echoarea, keybuf);
785 else {
786 /* If nothing else, show the URL at point */
787 vl = current_tab->buffer.current_line;
788 if (vl != NULL && vl->parent->type == LINE_LINK)
789 waddstr(echoarea, vl->parent->alt);
793 static void
794 do_redraw_minibuffer(void)
796 struct buffer *cmplbuf, *buffer;
797 size_t off_y, off_x = 0;
798 const char *start, *c;
800 cmplbuf = &ministate.compl.buffer;
801 buffer = &ministate.buffer;
802 (void)off_y; /* unused, set by getyx */
804 wmove(echoarea, 0, 0);
806 if (in_minibuffer == MB_COMPREAD)
807 wprintw(echoarea, "(%2d) ",
808 cmplbuf->line_max);
810 wprintw(echoarea, "%s", ministate.prompt);
811 if (ministate.hist_cur != NULL)
812 wprintw(echoarea, "(%zu/%zu) ",
813 ministate.hist_off + 1,
814 ministate.history->len);
816 getyx(echoarea, off_y, off_x);
818 start = ministate.hist_cur != NULL
819 ? ministate.hist_cur->h
820 : ministate.buf;
821 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
822 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
823 start = utf8_next_cp(start);
826 waddstr(echoarea, start);
828 if (ministate.curmesg != NULL)
829 wprintw(echoarea, " [%s]", ministate.curmesg);
831 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
834 static void
835 do_redraw_minibuffer_compl(void)
837 redraw_window(minibuffer, 0, 10, COLS,
838 &ministate.compl.buffer);
841 /*
842 * Place the cursor in the right ncurses window. If soft is 1, use
843 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
844 * wrefresh.
845 */
846 static void
847 place_cursor(int soft)
849 int (*touch)(WINDOW *);
851 if (soft)
852 touch = wnoutrefresh;
853 else
854 touch = wrefresh;
856 if (in_minibuffer) {
857 if (side_window)
858 touch(help);
859 touch(body);
860 touch(echoarea);
861 } else if (in_side_window) {
862 touch(body);
863 touch(echoarea);
864 touch(help);
865 } else {
866 if (side_window)
867 touch(help);
868 touch(echoarea);
869 touch(body);
873 static void
874 redraw_tab(struct tab *tab)
876 if (too_small)
877 return;
879 if (side_window) {
880 redraw_help();
881 wnoutrefresh(help);
884 redraw_tabline();
885 redraw_body(tab);
886 redraw_modeline(tab);
887 redraw_minibuffer();
889 wnoutrefresh(tabline);
890 wnoutrefresh(modeline);
892 if (in_minibuffer == MB_COMPREAD)
893 wnoutrefresh(minibuffer);
895 place_cursor(1);
897 doupdate();
899 if (set_title)
900 dprintf(1, "\033]0;%s - Telescope\a",
901 current_tab->buffer.page.title);
904 void
905 start_loading_anim(struct tab *tab)
907 if (tab->loading_anim)
908 return;
909 tab->loading_anim = 1;
910 evtimer_set(&tab->loadingev, update_loading_anim, tab);
911 evtimer_add(&tab->loadingev, &loadingev_timer);
914 static void
915 update_loading_anim(int fd, short ev, void *d)
917 struct tab *tab = d;
919 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
921 if (tab == current_tab) {
922 redraw_modeline(tab);
923 wrefresh(modeline);
924 wrefresh(body);
925 if (in_minibuffer)
926 wrefresh(echoarea);
929 evtimer_add(&tab->loadingev, &loadingev_timer);
932 static void
933 stop_loading_anim(struct tab *tab)
935 if (!tab->loading_anim)
936 return;
937 evtimer_del(&tab->loadingev);
938 tab->loading_anim = 0;
939 tab->loading_anim_step = 0;
941 if (tab != current_tab)
942 return;
944 redraw_modeline(tab);
946 wrefresh(modeline);
947 wrefresh(body);
948 if (in_minibuffer)
949 wrefresh(echoarea);
952 void
953 load_url_in_tab(struct tab *tab, const char *url, const char *base, int nohist)
955 if (!operating) {
956 load_url(tab, url, base, nohist);
957 return;
960 message("Loading %s...", url);
961 start_loading_anim(tab);
962 load_url(tab, url, base, nohist);
964 redraw_tab(tab);
967 void
968 switch_to_tab(struct tab *tab)
970 current_tab = tab;
971 tab->flags &= ~TAB_URGENT;
973 if (operating && tab->flags & TAB_LAZY)
974 load_url_in_tab(tab, tab->hist_cur->h, NULL, 0);
977 unsigned int
978 tab_new_id(void)
980 return tab_counter++;
983 struct tab *
984 new_tab(const char *url, const char *base, struct tab *after)
986 struct tab *tab;
988 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
989 event_loopbreak();
990 return NULL;
992 tab->fd = -1;
994 TAILQ_INIT(&tab->hist.head);
996 TAILQ_INIT(&tab->buffer.head);
997 TAILQ_INIT(&tab->buffer.page.head);
999 tab->id = tab_new_id();
1000 if (!operating)
1001 tab->flags |= TAB_LAZY;
1002 switch_to_tab(tab);
1004 if (after != NULL)
1005 TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
1006 else
1007 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1009 load_url_in_tab(tab, url, base, 0);
1010 return tab;
1013 int
1014 ui_print_colors(void)
1016 int colors = 0, pairs = 0, can_change = 0;
1017 int columns = 16, lines, color, i, j;
1019 initscr();
1020 if (has_colors()) {
1021 start_color();
1022 use_default_colors();
1024 colors = COLORS;
1025 pairs = COLOR_PAIRS;
1026 can_change = can_change_color();
1028 endwin();
1030 printf("Term info:\n");
1031 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1032 getenv("TERM"), colors, pairs, can_change);
1033 printf("\n");
1035 if (colors == 0) {
1036 printf("No color support\n");
1037 return 0;
1040 printf("Available colors:\n\n");
1041 lines = (colors - 1) / columns + 1;
1042 color = 0;
1043 for (i = 0; i < lines; ++i) {
1044 for (j = 0; j < columns; ++j, ++color) {
1045 printf("\033[0;38;5;%dm %03d", color, color);
1047 printf("\n");
1050 printf("\033[0m");
1051 fflush(stdout);
1052 return 0;
1055 int
1056 ui_init()
1058 setlocale(LC_ALL, "");
1060 minibuffer_init();
1062 /* initialize help window */
1063 TAILQ_INIT(&helpwin.head);
1064 TAILQ_INIT(&helpwin.page.head);
1066 base_map = &global_map;
1067 current_map = &global_map;
1069 initscr();
1071 if (enable_colors) {
1072 if (has_colors()) {
1073 start_color();
1074 use_default_colors();
1075 } else
1076 enable_colors = 0;
1079 config_apply_style();
1081 raw();
1082 noecho();
1083 nonl();
1084 intrflush(stdscr, FALSE);
1086 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1087 return 0;
1088 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1089 return 0;
1090 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1091 return 0;
1092 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1093 return 0;
1094 if ((minibuffer = newwin(1, COLS, LINES-1, 0)) == NULL)
1095 return 0;
1096 if ((help = newwin(1, 1, 1, 0)) == NULL)
1097 return 0;
1099 body_lines = LINES-3;
1100 body_cols = COLS;
1102 wbkgd(body, body_face.body);
1103 wbkgd(echoarea, minibuffer_face.background);
1105 update_x_offset();
1107 keypad(body, TRUE);
1108 scrollok(body, FALSE);
1110 /* non-blocking input */
1111 wtimeout(body, 0);
1112 wtimeout(help, 0);
1114 mvwprintw(body, 0, 0, "");
1116 evtimer_set(&resizeev, handle_resize, NULL);
1118 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1119 event_add(&stdioev, NULL);
1121 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1122 signal_add(&winchev, NULL);
1124 return 1;
1127 void
1128 ui_main_loop(void)
1130 operating = 1;
1131 switch_to_tab(current_tab);
1132 redraw_tab(current_tab);
1134 event_dispatch();
1137 void
1138 ui_on_tab_loaded(struct tab *tab)
1140 stop_loading_anim(tab);
1141 message("Loaded %s", tab->hist_cur->h);
1143 redraw_tabline();
1144 wrefresh(tabline);
1145 place_cursor(0);
1148 void
1149 ui_on_tab_refresh(struct tab *tab)
1151 wrap_page(&tab->buffer, body_cols);
1152 if (tab == current_tab)
1153 redraw_tab(tab);
1154 else
1155 tab->flags |= TAB_URGENT;
1158 const char *
1159 ui_keyname(int k)
1161 return keyname(k);
1164 void
1165 ui_toggle_side_window(void)
1167 side_window = !side_window;
1168 if (side_window)
1169 recompute_help();
1170 else
1171 in_side_window = 0;
1174 * ugly hack, but otherwise the window doesn't get updated
1175 * until I call rearrange_windows a second time (e.g. via
1176 * C-l). I will be happy to know why something like this is
1177 * needed.
1179 rearrange_windows();
1180 rearrange_windows();
1183 void
1184 ui_schedule_redraw(void)
1186 should_rearrange_windows = 1;
1189 void
1190 ui_require_input(struct tab *tab, int hide, int proto)
1192 void (*fn)(void);
1194 if (proto == PROTO_GEMINI)
1195 fn = ir_select_gemini;
1196 else if (proto == PROTO_GOPHER)
1197 fn = ir_select_gopher;
1198 else
1199 abort();
1201 /* TODO: hard-switching to another tab is ugly */
1202 switch_to_tab(tab);
1204 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1205 &ir_history, NULL, NULL);
1206 strlcpy(ministate.prompt, "Input required: ",
1207 sizeof(ministate.prompt));
1208 redraw_tab(tab);
1211 void
1212 ui_after_message_hook(void)
1214 redraw_minibuffer();
1215 place_cursor(0);
1218 void
1219 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1220 struct tab *data)
1222 yornp(prompt, fn, data);
1223 redraw_tab(current_tab);
1226 void
1227 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1228 struct tab *data)
1230 minibuffer_read(prompt, fn, data);
1231 redraw_tab(current_tab);
1234 void
1235 ui_other_window(void)
1237 if (side_window)
1238 in_side_window = !in_side_window;
1239 else
1240 message("No other window to select");
1243 void
1244 ui_suspend(void)
1246 endwin();
1248 kill(getpid(), SIGSTOP);
1250 refresh();
1251 clear();
1252 rearrange_windows();
1255 void
1256 ui_end(void)
1258 endwin();