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,
167 vl->parent->data);
168 else {
169 prfx = line_prefixes[vl->parent->type].prfx1;
170 buffer->curs_x += utf8_swidth(prfx);
174 void
175 global_key_unbound(void)
177 message("%s is undefined", keybuf);
180 struct buffer *
181 current_buffer(void)
183 if (in_minibuffer)
184 return &ministate.buffer;
185 if (in_side_window)
186 return &helpwin;
187 return &current_tab->buffer;
190 static int
191 readkey(void)
193 uint32_t state = 0;
195 if ((thiskey.key = wgetch(body)) == ERR)
196 return 0;
198 thiskey.meta = thiskey.key == 27;
199 if (thiskey.meta) {
200 thiskey.key = wgetch(body);
201 if (thiskey.key == ERR || thiskey.key == 27) {
202 thiskey.meta = 0;
203 thiskey.key = 27;
207 thiskey.cp = 0;
209 if ((unsigned int)thiskey.key >= UINT8_MAX)
210 return 1;
212 while (1) {
213 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
214 break;
215 if ((thiskey.key = wgetch(body)) == ERR) {
216 message("Error decoding user input");
217 return 0;
221 return 1;
224 static void
225 dispatch_stdio(int fd, short ev, void *d)
227 struct keymap *k;
228 const char *keyname;
229 char tmp[5] = {0};
231 /* TODO: schedule a redraw? */
232 if (too_small)
233 return;
235 if (!readkey())
236 return;
238 if (keybuf[0] != '\0')
239 strlcat(keybuf, " ", sizeof(keybuf));
240 if (thiskey.meta)
241 strlcat(keybuf, "M-", sizeof(keybuf));
242 if (thiskey.cp != 0) {
243 utf8_encode(thiskey.cp, tmp);
244 strlcat(keybuf, tmp, sizeof(keybuf));
245 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
246 strlcat(keybuf, keyname, sizeof(keybuf));
247 } else {
248 tmp[0] = thiskey.key;
249 strlcat(keybuf, tmp, sizeof(keybuf));
252 TAILQ_FOREACH(k, &current_map->m, keymaps) {
253 if (k->meta == thiskey.meta &&
254 k->key == thiskey.key) {
255 if (k->fn == NULL)
256 current_map = &k->map;
257 else {
258 current_map = base_map;
259 strlcpy(keybuf, "", sizeof(keybuf));
260 k->fn(current_buffer());
262 goto done;
266 if (current_map->unhandled_input != NULL)
267 current_map->unhandled_input();
268 else
269 global_key_unbound();
271 strlcpy(keybuf, "", sizeof(keybuf));
272 current_map = base_map;
274 done:
275 if (side_window)
276 recompute_help();
278 if (should_rearrange_windows)
279 rearrange_windows();
280 redraw_tab(current_tab);
283 static void
284 handle_resize(int sig, short ev, void *d)
286 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
287 event_del(&resizeev);
289 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
290 evtimer_add(&resizeev, &resize_timer);
293 static void
294 handle_resize_nodelay(int s, short ev, void *d)
296 endwin();
297 refresh();
298 clear();
300 rearrange_windows();
303 static void
304 rearrange_windows(void)
306 int lines;
308 should_rearrange_windows = 0;
310 lines = LINES;
312 if ((too_small = lines < 15)) {
313 erase();
314 printw("Window too small.");
315 refresh();
316 return;
319 /* move and resize the windows, in reverse order! */
321 if (in_minibuffer == MB_COMPREAD) {
322 mvwin(minibuffer, lines-10, 0);
323 wresize(minibuffer, 10, COLS);
324 lines -= 10;
326 wrap_page(&ministate.compl.buffer, COLS);
329 mvwin(echoarea, --lines, 0);
330 wresize(echoarea, 1, COLS);
332 mvwin(modeline, --lines, 0);
333 wresize(modeline, 1, COLS);
335 body_lines = --lines;
336 body_cols = COLS;
338 if (side_window) {
339 help_cols = 0.3 * COLS;
340 help_lines = lines;
341 mvwin(help, 1, 0);
342 wresize(help, help_lines, help_cols);
344 wrap_page(&helpwin, help_cols);
346 body_cols = COLS - help_cols - 1;
347 mvwin(body, 1, help_cols);
348 } else
349 mvwin(body, 1, 0);
351 update_x_offset();
352 wresize(body, body_lines, body_cols);
354 wresize(tabline, 1, COLS);
356 wrap_page(&current_tab->buffer, body_cols);
357 redraw_tab(current_tab);
360 static void
361 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
362 const char **prfx_ret, const char **text_ret)
364 int type, i, cont, width;
365 char *space, *t;
367 if ((*text_ret = vl->line) == NULL)
368 *text_ret = "";
370 cont = vl->flags & L_CONTINUATION;
371 type = vl->parent->type;
372 if (!cont)
373 *prfx_ret = line_prefixes[type].prfx1;
374 else
375 *prfx_ret = line_prefixes[type].prfx2;
377 space = vl->parent->data;
378 if (!emojify_link || type != LINE_LINK || space == NULL)
379 return;
381 if (cont) {
382 memset(buf, 0, len);
383 width = utf8_swidth_between(vl->parent->line, space);
384 for (i = 0; i < width + 1; ++i)
385 strlcat(buf, " ", len);
386 } else {
387 strlcpy(buf, *text_ret, len);
388 if ((t = strchr(buf, ' ')) != NULL)
389 *t = '\0';
390 strlcat(buf, " ", len);
392 /* skip the emoji */
393 *text_ret += (space - vl->parent->line) + 1;
396 *prfx_ret = buf;
399 static inline void
400 print_vline_descr(int width, WINDOW *window, struct vline *vl)
402 int x, y, goal;
404 if (vl->parent->type != LINE_COMPL &&
405 vl->parent->type != LINE_COMPL_CURRENT &&
406 vl->parent->type != LINE_HELP)
407 return;
409 if (vl->parent->alt == NULL)
410 return;
412 (void)y;
413 getyx(window, y, x);
415 if (vl->parent->type == LINE_HELP)
416 goal = 8;
417 else
418 goal = width/2;
420 if (goal <= x)
421 wprintw(window, " ");
422 for (; goal > x; ++x)
423 wprintw(window, " ");
425 wprintw(window, "%s", vl->parent->alt);
428 /*
429 * Core part of the rendering. It prints a vline starting from the
430 * current cursor position. Printing a vline consists of skipping
431 * `off' columns (for olivetti-mode), print the correct prefix (which
432 * may be the emoji in case of emojified links-lines), printing the
433 * text itself, filling until width - off and filling off columns
434 * again.
435 */
436 static void
437 print_vline(int off, int width, WINDOW *window, struct vline *vl)
439 /*
440 * Believe me or not, I've seen emoji ten code points long!
441 * That means, to stay large, 4*10 bytes + NUL.
442 */
443 char emojibuf[41] = {0};
444 const char *text, *prfx;
445 struct line_face *f;
446 int i, left, x, y;
448 f = &line_faces[vl->parent->type];
450 /* unused, set by getyx */
451 (void)y;
453 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
455 wattr_on(window, body_face.left, NULL);
456 for (i = 0; i < off; i++)
457 waddch(window, ' ');
458 wattr_off(window, body_face.left, NULL);
460 wattr_on(window, f->prefix, NULL);
461 wprintw(window, "%s", prfx);
462 wattr_off(window, f->prefix, NULL);
464 wattr_on(window, f->text, NULL);
465 wprintw(window, "%s", text);
466 print_vline_descr(width, window, vl);
467 wattr_off(window, f->text, NULL);
469 getyx(window, y, x);
471 left = width - x;
473 wattr_on(window, f->trail, NULL);
474 for (i = 0; i < left - off; ++i)
475 waddch(window, ' ');
476 wattr_off(window, f->trail, NULL);
478 wattr_on(window, body_face.right, NULL);
479 for (i = 0; i < off; i++)
480 waddch(window, ' ');
481 wattr_off(window, body_face.right, NULL);
485 static void
486 redraw_tabline(void)
488 struct tab *tab;
489 size_t toskip, ots, tabwidth, space, x;
490 int current, y, truncated, pair;
491 const char *title;
492 char buf[25];
494 x = 0;
496 /* unused, but setted by a getyx */
497 (void)y;
499 tabwidth = sizeof(buf)+1;
500 space = COLS-2;
502 toskip = 0;
503 TAILQ_FOREACH(tab, &tabshead, tabs) {
504 toskip++;
505 if (tab == current_tab)
506 break;
509 if (toskip * tabwidth < space)
510 toskip = 0;
511 else {
512 ots = toskip;
513 toskip--;
514 while (toskip != 0 &&
515 (ots - toskip+1) * tabwidth < space)
516 toskip--;
519 werase(tabline);
520 wattr_on(tabline, tab_face.background, NULL);
521 wprintw(tabline, toskip == 0 ? " " : "<");
522 wattr_off(tabline, tab_face.background, NULL);
524 truncated = 0;
525 TAILQ_FOREACH(tab, &tabshead, tabs) {
526 if (truncated)
527 break;
528 if (toskip != 0) {
529 toskip--;
530 continue;
533 getyx(tabline, y, x);
534 if (x + sizeof(buf)+2 >= (size_t)COLS)
535 truncated = 1;
537 current = tab == current_tab;
539 if (*(title = tab->buffer.page.title) == '\0')
540 title = tab->hist_cur->h;
542 if (tab->flags & TAB_URGENT)
543 strlcpy(buf, "!", sizeof(buf));
544 else
545 strlcpy(buf, " ", sizeof(buf));
547 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
548 /* truncation happens */
549 strlcpy(&buf[sizeof(buf)-4], "...", 4);
550 } else {
551 /* pad with spaces */
552 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
553 /* nop */ ;
556 pair = current ? tab_face.current : tab_face.tab;
557 wattr_on(tabline, pair, NULL);
558 wprintw(tabline, "%s", buf);
559 wattr_off(tabline, pair, NULL);
561 wattr_on(tabline, tab_face.background, NULL);
562 if (TAILQ_NEXT(tab, tabs) != NULL)
563 wprintw(tabline, "┃");
564 wattr_off(tabline, tab_face.background, NULL);
567 wattr_on(tabline, tab_face.background, NULL);
568 for (; x < (size_t)COLS; ++x)
569 waddch(tabline, ' ');
570 if (truncated)
571 mvwprintw(tabline, 0, COLS-1, ">");
572 wattr_off(tabline, tab_face.background, NULL);
575 /*
576 * Compute the first visible line around vl. Try to search forward
577 * until the end of the buffer; if a visible line is not found, search
578 * backward. Return NULL if no viable line was found.
579 */
580 struct vline *
581 adjust_line(struct vline *vl, struct buffer *buffer)
583 struct vline *t;
585 if (vl == NULL)
586 return NULL;
588 if (!(vl->parent->flags & L_HIDDEN))
589 return vl;
591 /* search forward */
592 for (t = vl;
593 t != NULL && t->parent->flags & L_HIDDEN;
594 t = TAILQ_NEXT(t, vlines))
595 ; /* nop */
597 if (t != NULL)
598 return t;
600 /* search backward */
601 for (t = vl;
602 t != NULL && t->parent->flags & L_HIDDEN;
603 t = TAILQ_PREV(t, vhead, vlines))
604 ; /* nop */
606 return t;
609 static void
610 redraw_window(WINDOW *win, int off, int height, int width,
611 struct buffer *buffer)
613 struct vline *vl;
614 int l, onscreen;
616 restore_curs_x(buffer);
618 /*
619 * TODO: ignoring buffer->force_update and always
620 * re-rendering. In theory we can recompute the y position
621 * without a re-render, and optimize here. It's not the only
622 * optimisation possible here, wscrl wolud also be an
623 * interesting one.
624 */
626 again:
627 werase(win);
628 buffer->curs_y = 0;
630 if (TAILQ_EMPTY(&buffer->head))
631 goto end;
633 if (buffer->top_line == NULL)
634 buffer->top_line = TAILQ_FIRST(&buffer->head);
636 buffer->top_line = adjust_line(buffer->top_line, buffer);
637 if (buffer->top_line == NULL)
638 goto end;
640 buffer->current_line = adjust_line(buffer->current_line, buffer);
642 l = 0;
643 onscreen = 0;
644 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
645 if (vl->parent->flags & L_HIDDEN)
646 continue;
648 wmove(win, l, 0);
649 print_vline(off, width, win, vl);
651 if (vl == buffer->current_line)
652 onscreen = 1;
654 if (!onscreen)
655 buffer->curs_y++;
657 l++;
658 if (l == height)
659 break;
662 if (!onscreen) {
663 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
664 if (vl == buffer->current_line)
665 break;
666 if (vl->parent->flags & L_HIDDEN)
667 continue;
668 buffer->line_off++;
669 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
672 if (vl != NULL)
673 goto again;
676 buffer->last_line_off = buffer->line_off;
677 buffer->force_redraw = 0;
678 end:
679 wmove(win, buffer->curs_y, buffer->curs_x);
682 static void
683 redraw_help(void)
685 redraw_window(help, 0, help_lines, help_cols, &helpwin);
688 static void
689 redraw_body(struct tab *tab)
691 static struct tab *last_tab;
693 if (last_tab != tab)
694 tab->buffer.force_redraw =1;
695 last_tab = tab;
697 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
700 static inline char
701 trust_status_char(enum trust_state ts)
703 switch (ts) {
704 case TS_UNKNOWN: return '-';
705 case TS_UNTRUSTED: return '!';
706 case TS_TEMP_TRUSTED: return '!';
707 case TS_TRUSTED: return 'v';
708 case TS_VERIFIED: return 'V';
709 default: return 'X';
713 static void
714 redraw_modeline(struct tab *tab)
716 struct buffer *buffer;
717 double pct;
718 int x, y, max_x, max_y;
719 const char *mode;
720 const char *spin = "-\\|/";
722 buffer = current_buffer();
723 mode = buffer->page.name;
725 werase(modeline);
726 wattr_on(modeline, modeline_face.background, NULL);
727 wmove(modeline, 0, 0);
729 wprintw(modeline, "-%c%c %s ",
730 spin[tab->loading_anim_step],
731 trust_status_char(tab->trust),
732 mode == NULL ? "(none)" : mode);
734 pct = (buffer->line_off + buffer->curs_y) * 100.0
735 / buffer->line_max;
737 if (buffer->line_max <= (size_t)body_lines)
738 wprintw(modeline, "All ");
739 else if (buffer->line_off == 0)
740 wprintw(modeline, "Top ");
741 else if (buffer->line_off + body_lines >= buffer->line_max)
742 wprintw(modeline, "Bottom ");
743 else
744 wprintw(modeline, "%.0f%% ", pct);
746 wprintw(modeline, "%d/%d %s ",
747 buffer->line_off + buffer->curs_y,
748 buffer->line_max,
749 tab->hist_cur->h);
751 getyx(modeline, y, x);
752 getmaxyx(modeline, max_y, max_x);
754 (void)y;
755 (void)max_y;
757 for (; x < max_x; ++x)
758 waddstr(modeline, "-");
760 wattr_off(modeline, modeline_face.background, NULL);
763 static void
764 redraw_minibuffer(void)
766 wattr_on(echoarea, minibuffer_face.background, NULL);
767 werase(echoarea);
769 if (in_minibuffer)
770 do_redraw_minibuffer();
771 else
772 do_redraw_echoarea();
774 if (in_minibuffer == MB_COMPREAD)
775 do_redraw_minibuffer_compl();
777 wattr_off(echoarea, minibuffer_face.background, NULL);
780 static void
781 do_redraw_echoarea(void)
783 struct vline *vl;
785 if (ministate.curmesg != NULL)
786 wprintw(echoarea, "%s", ministate.curmesg);
787 else if (*keybuf != '\0')
788 waddstr(echoarea, keybuf);
789 else {
790 /* If nothing else, show the URL at point */
791 vl = current_tab->buffer.current_line;
792 if (vl != NULL && vl->parent->type == LINE_LINK)
793 waddstr(echoarea, vl->parent->alt);
797 static void
798 do_redraw_minibuffer(void)
800 struct buffer *cmplbuf, *buffer;
801 size_t off_y, off_x = 0;
802 const char *start, *c;
804 cmplbuf = &ministate.compl.buffer;
805 buffer = &ministate.buffer;
806 (void)off_y; /* unused, set by getyx */
808 wmove(echoarea, 0, 0);
810 if (in_minibuffer == MB_COMPREAD)
811 wprintw(echoarea, "(%2d) ",
812 cmplbuf->line_max);
814 wprintw(echoarea, "%s", ministate.prompt);
815 if (ministate.hist_cur != NULL)
816 wprintw(echoarea, "(%zu/%zu) ",
817 ministate.hist_off + 1,
818 ministate.history->len);
820 getyx(echoarea, off_y, off_x);
822 start = ministate.hist_cur != NULL
823 ? ministate.hist_cur->h
824 : ministate.buf;
825 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
826 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
827 start = utf8_next_cp(start);
830 waddstr(echoarea, start);
832 if (ministate.curmesg != NULL)
833 wprintw(echoarea, " [%s]", ministate.curmesg);
835 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
838 static void
839 do_redraw_minibuffer_compl(void)
841 redraw_window(minibuffer, 0, 10, COLS,
842 &ministate.compl.buffer);
845 /*
846 * Place the cursor in the right ncurses window. If soft is 1, use
847 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
848 * wrefresh.
849 */
850 static void
851 place_cursor(int soft)
853 int (*touch)(WINDOW *);
855 if (soft)
856 touch = wnoutrefresh;
857 else
858 touch = wrefresh;
860 if (in_minibuffer) {
861 if (side_window)
862 touch(help);
863 touch(body);
864 touch(echoarea);
865 } else if (in_side_window) {
866 touch(body);
867 touch(echoarea);
868 touch(help);
869 } else {
870 if (side_window)
871 touch(help);
872 touch(echoarea);
873 touch(body);
877 static void
878 redraw_tab(struct tab *tab)
880 if (too_small)
881 return;
883 if (side_window) {
884 redraw_help();
885 wnoutrefresh(help);
888 redraw_tabline();
889 redraw_body(tab);
890 redraw_modeline(tab);
891 redraw_minibuffer();
893 wnoutrefresh(tabline);
894 wnoutrefresh(modeline);
896 if (in_minibuffer == MB_COMPREAD)
897 wnoutrefresh(minibuffer);
899 place_cursor(1);
901 doupdate();
903 if (set_title)
904 dprintf(1, "\033]0;%s - Telescope\a",
905 current_tab->buffer.page.title);
908 void
909 start_loading_anim(struct tab *tab)
911 if (tab->loading_anim)
912 return;
913 tab->loading_anim = 1;
914 evtimer_set(&tab->loadingev, update_loading_anim, tab);
915 evtimer_add(&tab->loadingev, &loadingev_timer);
918 static void
919 update_loading_anim(int fd, short ev, void *d)
921 struct tab *tab = d;
923 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
925 if (tab == current_tab) {
926 redraw_modeline(tab);
927 wrefresh(modeline);
928 wrefresh(body);
929 if (in_minibuffer)
930 wrefresh(echoarea);
933 evtimer_add(&tab->loadingev, &loadingev_timer);
936 static void
937 stop_loading_anim(struct tab *tab)
939 if (!tab->loading_anim)
940 return;
941 evtimer_del(&tab->loadingev);
942 tab->loading_anim = 0;
943 tab->loading_anim_step = 0;
945 if (tab != current_tab)
946 return;
948 redraw_modeline(tab);
950 wrefresh(modeline);
951 wrefresh(body);
952 if (in_minibuffer)
953 wrefresh(echoarea);
956 void
957 load_url_in_tab(struct tab *tab, const char *url, const char *base, int nohist)
959 if (!operating) {
960 load_url(tab, url, base, nohist);
961 return;
964 autosave_hook();
966 message("Loading %s...", url);
967 start_loading_anim(tab);
968 load_url(tab, url, base, nohist);
970 redraw_tab(tab);
973 void
974 switch_to_tab(struct tab *tab)
976 current_tab = tab;
977 tab->flags &= ~TAB_URGENT;
979 if (operating && tab->flags & TAB_LAZY)
980 load_url_in_tab(tab, tab->hist_cur->h, NULL, 0);
983 unsigned int
984 tab_new_id(void)
986 return tab_counter++;
989 struct tab *
990 new_tab(const char *url, const char *base, struct tab *after)
992 struct tab *tab;
994 autosave_hook();
996 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
997 event_loopbreak();
998 return NULL;
1000 tab->fd = -1;
1002 TAILQ_INIT(&tab->hist.head);
1004 TAILQ_INIT(&tab->buffer.head);
1005 TAILQ_INIT(&tab->buffer.page.head);
1007 tab->id = tab_new_id();
1008 if (!operating)
1009 tab->flags |= TAB_LAZY;
1010 switch_to_tab(tab);
1012 if (after != NULL)
1013 TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
1014 else
1015 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1017 load_url_in_tab(tab, url, base, 0);
1018 return tab;
1021 int
1022 ui_print_colors(void)
1024 int colors = 0, pairs = 0, can_change = 0;
1025 int columns = 16, lines, color, i, j;
1027 initscr();
1028 if (has_colors()) {
1029 start_color();
1030 use_default_colors();
1032 colors = COLORS;
1033 pairs = COLOR_PAIRS;
1034 can_change = can_change_color();
1036 endwin();
1038 printf("Term info:\n");
1039 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1040 getenv("TERM"), colors, pairs, can_change);
1041 printf("\n");
1043 if (colors == 0) {
1044 printf("No color support\n");
1045 return 0;
1048 printf("Available colors:\n\n");
1049 lines = (colors - 1) / columns + 1;
1050 color = 0;
1051 for (i = 0; i < lines; ++i) {
1052 for (j = 0; j < columns; ++j, ++color) {
1053 printf("\033[0;38;5;%dm %03d", color, color);
1055 printf("\n");
1058 printf("\033[0m");
1059 fflush(stdout);
1060 return 0;
1063 int
1064 ui_init()
1066 setlocale(LC_ALL, "");
1068 if (TAILQ_EMPTY(&global_map.m)) {
1069 fprintf(stderr, "no keys defined!\n");
1070 return 0;
1073 minibuffer_init();
1075 /* initialize help window */
1076 TAILQ_INIT(&helpwin.head);
1077 TAILQ_INIT(&helpwin.page.head);
1079 base_map = &global_map;
1080 current_map = &global_map;
1082 initscr();
1084 if (enable_colors) {
1085 if (has_colors()) {
1086 start_color();
1087 use_default_colors();
1088 } else
1089 enable_colors = 0;
1092 config_apply_style();
1094 raw();
1095 noecho();
1096 nonl();
1097 intrflush(stdscr, FALSE);
1099 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1100 return 0;
1101 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1102 return 0;
1103 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1104 return 0;
1105 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1106 return 0;
1107 if ((minibuffer = newwin(1, COLS, LINES-1, 0)) == NULL)
1108 return 0;
1109 if ((help = newwin(1, 1, 1, 0)) == NULL)
1110 return 0;
1112 body_lines = LINES-3;
1113 body_cols = COLS;
1115 wbkgd(body, body_face.body);
1116 wbkgd(echoarea, minibuffer_face.background);
1118 update_x_offset();
1120 keypad(body, TRUE);
1121 scrollok(body, FALSE);
1123 /* non-blocking input */
1124 wtimeout(body, 0);
1125 wtimeout(help, 0);
1127 mvwprintw(body, 0, 0, "");
1129 evtimer_set(&resizeev, handle_resize, NULL);
1131 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1132 event_add(&stdioev, NULL);
1134 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1135 signal_add(&winchev, NULL);
1137 return 1;
1140 void
1141 ui_main_loop(void)
1143 operating = 1;
1144 switch_to_tab(current_tab);
1145 redraw_tab(current_tab);
1147 event_dispatch();
1150 void
1151 ui_on_tab_loaded(struct tab *tab)
1153 stop_loading_anim(tab);
1154 message("Loaded %s", tab->hist_cur->h);
1156 redraw_tabline();
1157 wrefresh(tabline);
1158 place_cursor(0);
1161 void
1162 ui_on_tab_refresh(struct tab *tab)
1164 wrap_page(&tab->buffer, body_cols);
1165 if (tab == current_tab)
1166 redraw_tab(tab);
1167 else
1168 tab->flags |= TAB_URGENT;
1171 const char *
1172 ui_keyname(int k)
1174 return keyname(k);
1177 void
1178 ui_toggle_side_window(void)
1180 side_window = !side_window;
1181 if (side_window)
1182 recompute_help();
1183 else
1184 in_side_window = 0;
1187 * ugly hack, but otherwise the window doesn't get updated
1188 * until I call rearrange_windows a second time (e.g. via
1189 * C-l). I will be happy to know why something like this is
1190 * needed.
1192 rearrange_windows();
1193 rearrange_windows();
1196 void
1197 ui_schedule_redraw(void)
1199 should_rearrange_windows = 1;
1202 void
1203 ui_require_input(struct tab *tab, int hide, int proto)
1205 void (*fn)(void);
1207 if (proto == PROTO_GEMINI)
1208 fn = ir_select_gemini;
1209 else if (proto == PROTO_GOPHER)
1210 fn = ir_select_gopher;
1211 else
1212 abort();
1214 /* TODO: hard-switching to another tab is ugly */
1215 switch_to_tab(tab);
1217 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1218 &ir_history, NULL, NULL);
1219 strlcpy(ministate.prompt, "Input required: ",
1220 sizeof(ministate.prompt));
1221 redraw_tab(tab);
1224 void
1225 ui_after_message_hook(void)
1227 redraw_minibuffer();
1228 place_cursor(0);
1231 void
1232 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1233 struct tab *data)
1235 yornp(prompt, fn, data);
1236 redraw_tab(current_tab);
1239 void
1240 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1241 struct tab *data)
1243 minibuffer_read(prompt, fn, data);
1244 redraw_tab(current_tab);
1247 void
1248 ui_other_window(void)
1250 if (side_window)
1251 in_side_window = !in_side_window;
1252 else
1253 message("No other window to select");
1256 void
1257 ui_suspend(void)
1259 endwin();
1261 kill(getpid(), SIGSTOP);
1263 refresh();
1264 clear();
1265 rearrange_windows();
1268 void
1269 ui_end(void)
1271 endwin();