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;
208 if ((unsigned int)thiskey.key >= UINT8_MAX)
209 return 1;
211 while (1) {
212 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
213 break;
214 if ((thiskey.key = wgetch(body)) == ERR) {
215 message("Error decoding user input");
216 return 0;
220 return 1;
223 static void
224 dispatch_stdio(int fd, short ev, void *d)
226 struct keymap *k;
227 const char *keyname;
228 char tmp[5] = {0};
230 /* TODO: schedule a redraw? */
231 if (too_small)
232 return;
234 if (!readkey())
235 return;
237 if (keybuf[0] != '\0')
238 strlcat(keybuf, " ", sizeof(keybuf));
239 if (thiskey.meta)
240 strlcat(keybuf, "M-", sizeof(keybuf));
241 if (thiskey.cp != 0) {
242 utf8_encode(thiskey.cp, tmp);
243 strlcat(keybuf, tmp, sizeof(keybuf));
244 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
245 strlcat(keybuf, keyname, sizeof(keybuf));
246 } else {
247 tmp[0] = thiskey.key;
248 strlcat(keybuf, tmp, sizeof(keybuf));
251 TAILQ_FOREACH(k, &current_map->m, keymaps) {
252 if (k->meta == thiskey.meta &&
253 k->key == thiskey.key) {
254 if (k->fn == NULL)
255 current_map = &k->map;
256 else {
257 current_map = base_map;
258 strlcpy(keybuf, "", sizeof(keybuf));
259 k->fn(current_buffer());
261 goto done;
265 if (current_map->unhandled_input != NULL)
266 current_map->unhandled_input();
267 else
268 global_key_unbound();
270 strlcpy(keybuf, "", sizeof(keybuf));
271 current_map = base_map;
273 done:
274 if (side_window)
275 recompute_help();
277 if (should_rearrange_windows)
278 rearrange_windows();
279 redraw_tab(current_tab);
282 static void
283 handle_resize(int sig, short ev, void *d)
285 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
286 event_del(&resizeev);
288 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
289 evtimer_add(&resizeev, &resize_timer);
292 static void
293 handle_resize_nodelay(int s, short ev, void *d)
295 endwin();
296 refresh();
297 clear();
299 rearrange_windows();
302 static void
303 rearrange_windows(void)
305 int lines;
307 should_rearrange_windows = 0;
309 lines = LINES;
311 if ((too_small = lines < 15)) {
312 erase();
313 printw("Window too small.");
314 refresh();
315 return;
318 /* move and resize the windows, in reverse order! */
320 if (in_minibuffer == MB_COMPREAD) {
321 mvwin(minibuffer, lines-10, 0);
322 wresize(minibuffer, 10, COLS);
323 lines -= 10;
325 wrap_page(&ministate.compl.buffer, COLS);
328 mvwin(echoarea, --lines, 0);
329 wresize(echoarea, 1, COLS);
331 mvwin(modeline, --lines, 0);
332 wresize(modeline, 1, COLS);
334 body_lines = --lines;
335 body_cols = COLS;
337 if (side_window) {
338 help_cols = 0.3 * COLS;
339 help_lines = lines;
340 mvwin(help, 1, 0);
341 wresize(help, help_lines, help_cols);
343 wrap_page(&helpwin, help_cols);
345 body_cols = COLS - help_cols - 1;
346 mvwin(body, 1, help_cols);
347 } else
348 mvwin(body, 1, 0);
350 update_x_offset();
351 wresize(body, body_lines, body_cols);
353 wresize(tabline, 1, COLS);
355 wrap_page(&current_tab->buffer, body_cols);
356 redraw_tab(current_tab);
359 static void
360 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
361 const char **prfx_ret, const char **text_ret)
363 int type, i, cont, width;
364 char *space, *t;
366 if ((*text_ret = vl->line) == NULL)
367 *text_ret = "";
369 cont = vl->flags & L_CONTINUATION;
370 type = vl->parent->type;
371 if (!cont)
372 *prfx_ret = line_prefixes[type].prfx1;
373 else
374 *prfx_ret = line_prefixes[type].prfx2;
376 space = vl->parent->data;
377 if (!emojify_link || type != LINE_LINK || space == NULL)
378 return;
380 if (cont) {
381 memset(buf, 0, len);
382 width = utf8_swidth_between(vl->parent->line, space);
383 for (i = 0; i < width + 1; ++i)
384 strlcat(buf, " ", len);
385 } else {
386 strlcpy(buf, *text_ret, len);
387 if ((t = strchr(buf, ' ')) != NULL)
388 *t = '\0';
389 strlcat(buf, " ", len);
391 /* skip the emoji */
392 *text_ret += (space - vl->parent->line) + 1;
395 *prfx_ret = buf;
398 static inline void
399 print_vline_descr(int width, WINDOW *window, struct vline *vl)
401 int x, y, goal;
403 if (vl->parent->type != LINE_COMPL &&
404 vl->parent->type != LINE_COMPL_CURRENT &&
405 vl->parent->type != LINE_HELP)
406 return;
408 if (vl->parent->alt == NULL)
409 return;
411 (void)y;
412 getyx(window, y, x);
414 if (vl->parent->type == LINE_HELP)
415 goal = 8;
416 else
417 goal = width/2;
419 if (goal <= x)
420 wprintw(window, " ");
421 for (; goal > x; ++x)
422 wprintw(window, " ");
424 wprintw(window, "%s", vl->parent->alt);
427 /*
428 * Core part of the rendering. It prints a vline starting from the
429 * current cursor position. Printing a vline consists of skipping
430 * `off' columns (for olivetti-mode), print the correct prefix (which
431 * may be the emoji in case of emojified links-lines), printing the
432 * text itself, filling until width - off and filling off columns
433 * again.
434 */
435 static void
436 print_vline(int off, int width, WINDOW *window, struct vline *vl)
438 /*
439 * Believe me or not, I've seen emoji ten code points long!
440 * That means, to stay large, 4*10 bytes + NUL.
441 */
442 char emojibuf[41] = {0};
443 const char *text, *prfx;
444 struct line_face *f;
445 int i, left, x, y;
447 f = &line_faces[vl->parent->type];
449 /* unused, set by getyx */
450 (void)y;
452 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
454 wattr_on(window, body_face.left, NULL);
455 for (i = 0; i < off; i++)
456 waddch(window, ' ');
457 wattr_off(window, body_face.left, NULL);
459 wattr_on(window, f->prefix, NULL);
460 wprintw(window, "%s", prfx);
461 wattr_off(window, f->prefix, NULL);
463 wattr_on(window, f->text, NULL);
464 wprintw(window, "%s", text);
465 print_vline_descr(width, window, vl);
466 wattr_off(window, f->text, NULL);
468 getyx(window, y, x);
470 left = width - x;
472 wattr_on(window, f->trail, NULL);
473 for (i = 0; i < left - off; ++i)
474 waddch(window, ' ');
475 wattr_off(window, f->trail, NULL);
477 wattr_on(window, body_face.right, NULL);
478 for (i = 0; i < off; i++)
479 waddch(window, ' ');
480 wattr_off(window, body_face.right, NULL);
484 static void
485 redraw_tabline(void)
487 struct tab *tab;
488 size_t toskip, ots, tabwidth, space, x;
489 int current, y, truncated, pair;
490 const char *title;
491 char buf[25];
493 x = 0;
495 /* unused, but setted by a getyx */
496 (void)y;
498 tabwidth = sizeof(buf)+1;
499 space = COLS-2;
501 toskip = 0;
502 TAILQ_FOREACH(tab, &tabshead, tabs) {
503 toskip++;
504 if (tab == current_tab)
505 break;
508 if (toskip * tabwidth < space)
509 toskip = 0;
510 else {
511 ots = toskip;
512 toskip--;
513 while (toskip != 0 &&
514 (ots - toskip+1) * tabwidth < space)
515 toskip--;
518 werase(tabline);
519 wattr_on(tabline, tab_face.background, NULL);
520 wprintw(tabline, toskip == 0 ? " " : "<");
521 wattr_off(tabline, tab_face.background, NULL);
523 truncated = 0;
524 TAILQ_FOREACH(tab, &tabshead, tabs) {
525 if (truncated)
526 break;
527 if (toskip != 0) {
528 toskip--;
529 continue;
532 getyx(tabline, y, x);
533 if (x + sizeof(buf)+2 >= (size_t)COLS)
534 truncated = 1;
536 current = tab == current_tab;
538 if (*(title = tab->buffer.page.title) == '\0')
539 title = tab->hist_cur->h;
541 if (tab->flags & TAB_URGENT)
542 strlcpy(buf, "!", sizeof(buf));
543 else
544 strlcpy(buf, " ", sizeof(buf));
546 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
547 /* truncation happens */
548 strlcpy(&buf[sizeof(buf)-4], "...", 4);
549 } else {
550 /* pad with spaces */
551 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
552 /* nop */ ;
555 pair = current ? tab_face.current : tab_face.tab;
556 wattr_on(tabline, pair, NULL);
557 wprintw(tabline, "%s", buf);
558 wattr_off(tabline, pair, NULL);
560 wattr_on(tabline, tab_face.background, NULL);
561 if (TAILQ_NEXT(tab, tabs) != NULL)
562 wprintw(tabline, "┃");
563 wattr_off(tabline, tab_face.background, NULL);
566 wattr_on(tabline, tab_face.background, NULL);
567 for (; x < (size_t)COLS; ++x)
568 waddch(tabline, ' ');
569 if (truncated)
570 mvwprintw(tabline, 0, COLS-1, ">");
571 wattr_off(tabline, tab_face.background, NULL);
574 /*
575 * Compute the first visible line around vl. Try to search forward
576 * until the end of the buffer; if a visible line is not found, search
577 * backward. Return NULL if no viable line was found.
578 */
579 struct vline *
580 adjust_line(struct vline *vl, struct buffer *buffer)
582 struct vline *t;
584 if (vl == NULL)
585 return NULL;
587 if (!(vl->parent->flags & L_HIDDEN))
588 return vl;
590 /* search forward */
591 for (t = vl;
592 t != NULL && t->parent->flags & L_HIDDEN;
593 t = TAILQ_NEXT(t, vlines))
594 ; /* nop */
596 if (t != NULL)
597 return t;
599 /* search backward */
600 for (t = vl;
601 t != NULL && t->parent->flags & L_HIDDEN;
602 t = TAILQ_PREV(t, vhead, vlines))
603 ; /* nop */
605 return t;
608 static void
609 redraw_window(WINDOW *win, int off, int height, int width, struct buffer *buffer)
611 struct vline *vl;
612 int l, onscreen;
614 restore_curs_x(buffer);
616 /*
617 * TODO: ignoring buffer->force_update and always
618 * re-rendering. In theory we can recompute the y position
619 * without a re-render, and optimize here. It's not the only
620 * optimisation possible here, wscrl wolud also be an
621 * interesting one.
622 */
624 again:
625 werase(win);
626 buffer->curs_y = 0;
628 if (TAILQ_EMPTY(&buffer->head))
629 goto end;
631 if (buffer->top_line == NULL)
632 buffer->top_line = TAILQ_FIRST(&buffer->head);
634 buffer->top_line = adjust_line(buffer->top_line, buffer);
635 if (buffer->top_line == NULL)
636 goto end;
638 buffer->current_line = adjust_line(buffer->current_line, buffer);
640 l = 0;
641 onscreen = 0;
642 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
643 if (vl->parent->flags & L_HIDDEN)
644 continue;
646 wmove(win, l, 0);
647 print_vline(off, width, win, vl);
649 if (vl == buffer->current_line)
650 onscreen = 1;
652 if (!onscreen)
653 buffer->curs_y++;
655 l++;
656 if (l == height)
657 break;
660 if (!onscreen) {
661 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
662 if (vl == buffer->current_line)
663 break;
664 if (vl->parent->flags & L_HIDDEN)
665 continue;
666 buffer->line_off++;
667 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
670 if (vl != NULL)
671 goto again;
674 buffer->last_line_off = buffer->line_off;
675 buffer->force_redraw = 0;
676 end:
677 wmove(win, buffer->curs_y, buffer->curs_x);
680 static void
681 redraw_help(void)
683 redraw_window(help, 0, help_lines, help_cols, &helpwin);
686 static void
687 redraw_body(struct tab *tab)
689 static struct tab *last_tab;
691 if (last_tab != tab)
692 tab->buffer.force_redraw =1;
693 last_tab = tab;
695 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
698 static inline char
699 trust_status_char(enum trust_state ts)
701 switch (ts) {
702 case TS_UNKNOWN: return '-';
703 case TS_UNTRUSTED: return '!';
704 case TS_TEMP_TRUSTED: return '!';
705 case TS_TRUSTED: return 'v';
706 case TS_VERIFIED: return 'V';
707 default: return 'X';
711 static void
712 redraw_modeline(struct tab *tab)
714 struct buffer *buffer;
715 double pct;
716 int x, y, max_x, max_y;
717 const char *mode;
718 const char *spin = "-\\|/";
720 buffer = current_buffer();
721 mode = buffer->page.name;
723 werase(modeline);
724 wattr_on(modeline, modeline_face.background, NULL);
725 wmove(modeline, 0, 0);
727 wprintw(modeline, "-%c%c %s ",
728 spin[tab->loading_anim_step],
729 trust_status_char(tab->trust),
730 mode == NULL ? "(none)" : mode);
732 pct = (buffer->line_off + buffer->curs_y) * 100.0
733 / buffer->line_max;
735 if (buffer->line_max <= (size_t)body_lines)
736 wprintw(modeline, "All ");
737 else if (buffer->line_off == 0)
738 wprintw(modeline, "Top ");
739 else if (buffer->line_off + body_lines >= buffer->line_max)
740 wprintw(modeline, "Bottom ");
741 else
742 wprintw(modeline, "%.0f%% ", pct);
744 wprintw(modeline, "%d/%d %s ",
745 buffer->line_off + buffer->curs_y,
746 buffer->line_max,
747 tab->hist_cur->h);
749 getyx(modeline, y, x);
750 getmaxyx(modeline, max_y, max_x);
752 (void)y;
753 (void)max_y;
755 for (; x < max_x; ++x)
756 waddstr(modeline, "-");
758 wattr_off(modeline, modeline_face.background, NULL);
761 static void
762 redraw_minibuffer(void)
764 wattr_on(echoarea, minibuffer_face.background, NULL);
765 werase(echoarea);
767 if (in_minibuffer)
768 do_redraw_minibuffer();
769 else
770 do_redraw_echoarea();
772 if (in_minibuffer == MB_COMPREAD)
773 do_redraw_minibuffer_compl();
775 wattr_off(echoarea, minibuffer_face.background, NULL);
778 static void
779 do_redraw_echoarea(void)
781 struct vline *vl;
783 if (ministate.curmesg != NULL)
784 wprintw(echoarea, "%s", ministate.curmesg);
785 else if (*keybuf != '\0')
786 waddstr(echoarea, keybuf);
787 else {
788 /* If nothing else, show the URL at point */
789 vl = current_tab->buffer.current_line;
790 if (vl != NULL && vl->parent->type == LINE_LINK)
791 waddstr(echoarea, vl->parent->alt);
795 static void
796 do_redraw_minibuffer(void)
798 struct buffer *cmplbuf, *buffer;
799 size_t off_y, off_x = 0;
800 const char *start, *c;
802 cmplbuf = &ministate.compl.buffer;
803 buffer = &ministate.buffer;
804 (void)off_y; /* unused, set by getyx */
806 wmove(echoarea, 0, 0);
808 if (in_minibuffer == MB_COMPREAD)
809 wprintw(echoarea, "(%2d) ",
810 cmplbuf->line_max);
812 wprintw(echoarea, "%s", ministate.prompt);
813 if (ministate.hist_cur != NULL)
814 wprintw(echoarea, "(%zu/%zu) ",
815 ministate.hist_off + 1,
816 ministate.history->len);
818 getyx(echoarea, off_y, off_x);
820 start = ministate.hist_cur != NULL
821 ? ministate.hist_cur->h
822 : ministate.buf;
823 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
824 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
825 start = utf8_next_cp(start);
828 waddstr(echoarea, start);
830 if (ministate.curmesg != NULL)
831 wprintw(echoarea, " [%s]", ministate.curmesg);
833 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
836 static void
837 do_redraw_minibuffer_compl(void)
839 redraw_window(minibuffer, 0, 10, COLS,
840 &ministate.compl.buffer);
843 /*
844 * Place the cursor in the right ncurses window. If soft is 1, use
845 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
846 * wrefresh.
847 */
848 static void
849 place_cursor(int soft)
851 int (*touch)(WINDOW *);
853 if (soft)
854 touch = wnoutrefresh;
855 else
856 touch = wrefresh;
858 if (in_minibuffer) {
859 if (side_window)
860 touch(help);
861 touch(body);
862 touch(echoarea);
863 } else if (in_side_window) {
864 touch(body);
865 touch(echoarea);
866 touch(help);
867 } else {
868 if (side_window)
869 touch(help);
870 touch(echoarea);
871 touch(body);
875 static void
876 redraw_tab(struct tab *tab)
878 if (too_small)
879 return;
881 if (side_window) {
882 redraw_help();
883 wnoutrefresh(help);
886 redraw_tabline();
887 redraw_body(tab);
888 redraw_modeline(tab);
889 redraw_minibuffer();
891 wnoutrefresh(tabline);
892 wnoutrefresh(modeline);
894 if (in_minibuffer == MB_COMPREAD)
895 wnoutrefresh(minibuffer);
897 place_cursor(1);
899 doupdate();
901 if (set_title)
902 dprintf(1, "\033]0;%s - Telescope\a",
903 current_tab->buffer.page.title);
906 void
907 start_loading_anim(struct tab *tab)
909 if (tab->loading_anim)
910 return;
911 tab->loading_anim = 1;
912 evtimer_set(&tab->loadingev, update_loading_anim, tab);
913 evtimer_add(&tab->loadingev, &loadingev_timer);
916 static void
917 update_loading_anim(int fd, short ev, void *d)
919 struct tab *tab = d;
921 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
923 if (tab == current_tab) {
924 redraw_modeline(tab);
925 wrefresh(modeline);
926 wrefresh(body);
927 if (in_minibuffer)
928 wrefresh(echoarea);
931 evtimer_add(&tab->loadingev, &loadingev_timer);
934 static void
935 stop_loading_anim(struct tab *tab)
937 if (!tab->loading_anim)
938 return;
939 evtimer_del(&tab->loadingev);
940 tab->loading_anim = 0;
941 tab->loading_anim_step = 0;
943 if (tab != current_tab)
944 return;
946 redraw_modeline(tab);
948 wrefresh(modeline);
949 wrefresh(body);
950 if (in_minibuffer)
951 wrefresh(echoarea);
954 void
955 load_url_in_tab(struct tab *tab, const char *url, const char *base, int nohist)
957 if (!operating) {
958 load_url(tab, url, base, nohist);
959 return;
962 autosave_hook();
964 message("Loading %s...", url);
965 start_loading_anim(tab);
966 load_url(tab, url, base, nohist);
968 redraw_tab(tab);
971 void
972 switch_to_tab(struct tab *tab)
974 current_tab = tab;
975 tab->flags &= ~TAB_URGENT;
977 if (operating && tab->flags & TAB_LAZY)
978 load_url_in_tab(tab, tab->hist_cur->h, NULL, 0);
981 unsigned int
982 tab_new_id(void)
984 return tab_counter++;
987 struct tab *
988 new_tab(const char *url, const char *base, struct tab *after)
990 struct tab *tab;
992 autosave_hook();
994 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
995 event_loopbreak();
996 return NULL;
998 tab->fd = -1;
1000 TAILQ_INIT(&tab->hist.head);
1002 TAILQ_INIT(&tab->buffer.head);
1003 TAILQ_INIT(&tab->buffer.page.head);
1005 tab->id = tab_new_id();
1006 if (!operating)
1007 tab->flags |= TAB_LAZY;
1008 switch_to_tab(tab);
1010 if (after != NULL)
1011 TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
1012 else
1013 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1015 load_url_in_tab(tab, url, base, 0);
1016 return tab;
1019 int
1020 ui_print_colors(void)
1022 int colors = 0, pairs = 0, can_change = 0;
1023 int columns = 16, lines, color, i, j;
1025 initscr();
1026 if (has_colors()) {
1027 start_color();
1028 use_default_colors();
1030 colors = COLORS;
1031 pairs = COLOR_PAIRS;
1032 can_change = can_change_color();
1034 endwin();
1036 printf("Term info:\n");
1037 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1038 getenv("TERM"), colors, pairs, can_change);
1039 printf("\n");
1041 if (colors == 0) {
1042 printf("No color support\n");
1043 return 0;
1046 printf("Available colors:\n\n");
1047 lines = (colors - 1) / columns + 1;
1048 color = 0;
1049 for (i = 0; i < lines; ++i) {
1050 for (j = 0; j < columns; ++j, ++color) {
1051 printf("\033[0;38;5;%dm %03d", color, color);
1053 printf("\n");
1056 printf("\033[0m");
1057 fflush(stdout);
1058 return 0;
1061 int
1062 ui_init()
1064 setlocale(LC_ALL, "");
1066 if (TAILQ_EMPTY(&global_map.m)) {
1067 fprintf(stderr, "no keys defined!\n");
1068 return 0;
1071 minibuffer_init();
1073 /* initialize help window */
1074 TAILQ_INIT(&helpwin.head);
1075 TAILQ_INIT(&helpwin.page.head);
1077 base_map = &global_map;
1078 current_map = &global_map;
1080 initscr();
1082 if (enable_colors) {
1083 if (has_colors()) {
1084 start_color();
1085 use_default_colors();
1086 } else
1087 enable_colors = 0;
1090 config_apply_style();
1092 raw();
1093 noecho();
1094 nonl();
1095 intrflush(stdscr, FALSE);
1097 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1098 return 0;
1099 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1100 return 0;
1101 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1102 return 0;
1103 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1104 return 0;
1105 if ((minibuffer = newwin(1, COLS, LINES-1, 0)) == NULL)
1106 return 0;
1107 if ((help = newwin(1, 1, 1, 0)) == NULL)
1108 return 0;
1110 body_lines = LINES-3;
1111 body_cols = COLS;
1113 wbkgd(body, body_face.body);
1114 wbkgd(echoarea, minibuffer_face.background);
1116 update_x_offset();
1118 keypad(body, TRUE);
1119 scrollok(body, FALSE);
1121 /* non-blocking input */
1122 wtimeout(body, 0);
1123 wtimeout(help, 0);
1125 mvwprintw(body, 0, 0, "");
1127 evtimer_set(&resizeev, handle_resize, NULL);
1129 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1130 event_add(&stdioev, NULL);
1132 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1133 signal_add(&winchev, NULL);
1135 return 1;
1138 void
1139 ui_main_loop(void)
1141 operating = 1;
1142 switch_to_tab(current_tab);
1143 redraw_tab(current_tab);
1145 event_dispatch();
1148 void
1149 ui_on_tab_loaded(struct tab *tab)
1151 stop_loading_anim(tab);
1152 message("Loaded %s", tab->hist_cur->h);
1154 redraw_tabline();
1155 wrefresh(tabline);
1156 place_cursor(0);
1159 void
1160 ui_on_tab_refresh(struct tab *tab)
1162 wrap_page(&tab->buffer, body_cols);
1163 if (tab == current_tab)
1164 redraw_tab(tab);
1165 else
1166 tab->flags |= TAB_URGENT;
1169 const char *
1170 ui_keyname(int k)
1172 return keyname(k);
1175 void
1176 ui_toggle_side_window(void)
1178 side_window = !side_window;
1179 if (side_window)
1180 recompute_help();
1181 else
1182 in_side_window = 0;
1185 * ugly hack, but otherwise the window doesn't get updated
1186 * until I call rearrange_windows a second time (e.g. via
1187 * C-l). I will be happy to know why something like this is
1188 * needed.
1190 rearrange_windows();
1191 rearrange_windows();
1194 void
1195 ui_schedule_redraw(void)
1197 should_rearrange_windows = 1;
1200 void
1201 ui_require_input(struct tab *tab, int hide, int proto)
1203 void (*fn)(void);
1205 if (proto == PROTO_GEMINI)
1206 fn = ir_select_gemini;
1207 else if (proto == PROTO_GOPHER)
1208 fn = ir_select_gopher;
1209 else
1210 abort();
1212 /* TODO: hard-switching to another tab is ugly */
1213 switch_to_tab(tab);
1215 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1216 &ir_history, NULL, NULL);
1217 strlcpy(ministate.prompt, "Input required: ",
1218 sizeof(ministate.prompt));
1219 redraw_tab(tab);
1222 void
1223 ui_after_message_hook(void)
1225 redraw_minibuffer();
1226 place_cursor(0);
1229 void
1230 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1231 struct tab *data)
1233 yornp(prompt, fn, data);
1234 redraw_tab(current_tab);
1237 void
1238 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1239 struct tab *data)
1241 minibuffer_read(prompt, fn, data);
1242 redraw_tab(current_tab);
1245 void
1246 ui_other_window(void)
1248 if (side_window)
1249 in_side_window = !in_side_window;
1250 else
1251 message("No other window to select");
1254 void
1255 ui_suspend(void)
1257 endwin();
1259 kill(getpid(), SIGSTOP);
1261 refresh();
1262 clear();
1263 rearrange_windows();
1266 void
1267 ui_end(void)
1269 endwin();