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 <event.h>
38 #include <locale.h>
39 #include <signal.h>
40 #include <stdarg.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
45 #include "defaults.h"
46 #include "minibuffer.h"
47 #include "telescope.h"
48 #include "ui.h"
49 #include "utf8.h"
51 static struct event stdioev, winchev;
53 static void restore_curs_x(struct buffer *);
55 static int readkey(void);
56 static void dispatch_stdio(int, short, void*);
57 static void handle_resize(int, short, void*);
58 static void handle_resize_nodelay(int, short, void*);
59 static void rearrange_windows(void);
60 static void line_prefix_and_text(struct vline *, char *, size_t, const char **, const char **);
61 static void print_vline(int, int, WINDOW*, struct vline*);
62 static void redraw_tabline(void);
63 static void redraw_window(WINDOW*, int, int, int, struct buffer*);
64 static void redraw_help(void);
65 static void redraw_body(struct tab*);
66 static void redraw_modeline(struct tab*);
67 static void redraw_minibuffer(void);
68 static void do_redraw_echoarea(void);
69 static void do_redraw_minibuffer(void);
70 static void do_redraw_minibuffer_compl(void);
71 static void place_cursor(int);
72 static void redraw_tab(struct tab*);
73 static void update_loading_anim(int, short, void*);
74 static void stop_loading_anim(struct tab*);
76 /*
77 * Used to know when we're finished loading.
78 */
79 static int operating;
81 static int should_rearrange_windows;
82 static int too_small;
83 static int x_offset;
85 struct thiskey thiskey;
86 struct tab *current_tab;
88 static struct event resizeev;
89 static struct timeval resize_timer = { 0, 250000 };
91 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
93 int body_lines, body_cols;
95 static WINDOW *help;
96 /* not static so we can see them from help.c */
97 struct buffer helpwin;
98 int help_lines, help_cols;
100 static int side_window;
102 static struct timeval loadingev_timer = { 0, 250000 };
104 static uint32_t tab_counter;
106 static char keybuf[64];
108 struct kmap global_map,
109 minibuffer_map,
110 *current_map,
111 *base_map;
113 static inline void
114 update_x_offset(void)
116 if (olivetti_mode && fill_column < body_cols)
117 x_offset = (body_cols - fill_column)/2;
118 else
119 x_offset = 0;
122 void
123 save_excursion(struct excursion *place, struct buffer *buffer)
125 place->curs_x = buffer->curs_x;
126 place->curs_y = buffer->curs_y;
127 place->line_off = buffer->line_off;
128 place->top_line = buffer->top_line;
129 place->current_line = buffer->current_line;
130 place->cpoff = buffer->cpoff;
133 void
134 restore_excursion(struct excursion *place, struct buffer *buffer)
136 buffer->curs_x = place->curs_x;
137 buffer->curs_y = place->curs_y;
138 buffer->line_off = place->line_off;
139 buffer->top_line = place->top_line;
140 buffer->current_line = place->current_line;
141 buffer->cpoff = place->cpoff;
144 static void
145 restore_curs_x(struct buffer *buffer)
147 struct vline *vl;
148 const char *prfx, *text;
150 vl = buffer->current_line;
151 if (vl == NULL || vl->line == NULL)
152 buffer->curs_x = buffer->cpoff = 0;
153 else if (vl->parent->data != NULL) {
154 text = vl->parent->data;
155 buffer->curs_x = utf8_snwidth(text + 1, buffer->cpoff) + 1;
156 } else
157 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
159 buffer->curs_x += x_offset;
161 if (vl == NULL)
162 return;
164 if (vl->parent->data != NULL)
165 buffer->curs_x += utf8_swidth_between(vl->parent->line, vl->parent->data);
166 else {
167 prfx = line_prefixes[vl->parent->type].prfx1;
168 buffer->curs_x += utf8_swidth(prfx);
172 void
173 global_key_unbound(void)
175 message("%s is undefined", keybuf);
178 struct buffer *
179 current_buffer(void)
181 if (in_minibuffer)
182 return &ministate.buffer;
183 return &current_tab->buffer;
186 static int
187 readkey(void)
189 uint32_t state = 0;
191 if ((thiskey.key = wgetch(body)) == ERR)
192 return 0;
194 thiskey.meta = thiskey.key == 27;
195 if (thiskey.meta) {
196 thiskey.key = wgetch(body);
197 if (thiskey.key == ERR || thiskey.key == 27) {
198 thiskey.meta = 0;
199 thiskey.key = 27;
203 thiskey.cp = 0;
204 if ((unsigned int)thiskey.key < UINT8_MAX) {
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;
215 return 1;
218 static void
219 dispatch_stdio(int fd, short ev, void *d)
221 struct keymap *k;
222 const char *keyname;
223 char tmp[5] = {0};
225 /* TODO: schedule a redraw? */
226 if (too_small)
227 return;
229 if (!readkey())
230 return;
232 if (keybuf[0] != '\0')
233 strlcat(keybuf, " ", sizeof(keybuf));
234 if (thiskey.meta)
235 strlcat(keybuf, "M-", sizeof(keybuf));
236 if (thiskey.cp != 0) {
237 utf8_encode(thiskey.cp, tmp);
238 strlcat(keybuf, tmp, sizeof(keybuf));
239 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
240 strlcat(keybuf, keyname, sizeof(keybuf));
241 } else {
242 tmp[0] = thiskey.key;
243 strlcat(keybuf, tmp, sizeof(keybuf));
246 TAILQ_FOREACH(k, &current_map->m, keymaps) {
247 if (k->meta == thiskey.meta &&
248 k->key == thiskey.key) {
249 if (k->fn == NULL)
250 current_map = &k->map;
251 else {
252 current_map = base_map;
253 strlcpy(keybuf, "", sizeof(keybuf));
254 k->fn(current_buffer());
256 goto done;
260 if (current_map->unhandled_input != NULL)
261 current_map->unhandled_input();
262 else
263 global_key_unbound();
265 strlcpy(keybuf, "", sizeof(keybuf));
266 current_map = base_map;
268 done:
269 if (side_window)
270 recompute_help();
272 if (should_rearrange_windows)
273 rearrange_windows();
274 redraw_tab(current_tab);
277 static void
278 handle_resize(int sig, short ev, void *d)
280 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
281 event_del(&resizeev);
283 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
284 evtimer_add(&resizeev, &resize_timer);
287 static void
288 handle_resize_nodelay(int s, short ev, void *d)
290 endwin();
291 refresh();
292 clear();
294 rearrange_windows();
297 static void
298 rearrange_windows(void)
300 int lines;
302 should_rearrange_windows = 0;
304 lines = LINES;
306 if ((too_small = lines < 15)) {
307 erase();
308 printw("Window too small.");
309 refresh();
310 return;
313 /* move and resize the windows, in reverse order! */
315 if (in_minibuffer == MB_COMPREAD) {
316 mvwin(minibuffer, lines-10, 0);
317 wresize(minibuffer, 10, COLS);
318 lines -= 10;
320 wrap_page(&ministate.compl.buffer, COLS);
323 mvwin(echoarea, --lines, 0);
324 wresize(echoarea, 1, COLS);
326 mvwin(modeline, --lines, 0);
327 wresize(modeline, 1, COLS);
329 body_lines = --lines;
330 body_cols = COLS;
332 if (side_window) {
333 help_cols = 0.3 * COLS;
334 help_lines = lines;
335 mvwin(help, 1, 0);
336 wresize(help, help_lines, help_cols);
338 wrap_page(&helpwin, help_cols);
340 body_cols = COLS - help_cols - 1;
341 mvwin(body, 1, help_cols);
342 } else
343 mvwin(body, 1, 0);
345 update_x_offset();
346 wresize(body, body_lines, body_cols);
348 wresize(tabline, 1, COLS);
350 wrap_page(&current_tab->buffer, body_cols);
351 redraw_tab(current_tab);
354 static void
355 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
356 const char **prfx_ret, const char **text_ret)
358 int type, i, cont, width;
359 char *space, *t;
361 if ((*text_ret = vl->line) == NULL)
362 *text_ret = "";
364 cont = vl->flags & L_CONTINUATION;
365 type = vl->parent->type;
366 if (!cont)
367 *prfx_ret = line_prefixes[type].prfx1;
368 else
369 *prfx_ret = line_prefixes[type].prfx2;
371 space = vl->parent->data;
372 if (!emojify_link || type != LINE_LINK || space == NULL)
373 return;
375 if (cont) {
376 memset(buf, 0, len);
377 width = utf8_swidth_between(vl->parent->line, space);
378 for (i = 0; i < width + 1; ++i)
379 strlcat(buf, " ", len);
380 } else {
381 strlcpy(buf, *text_ret, len);
382 if ((t = strchr(buf, ' ')) != NULL)
383 *t = '\0';
384 strlcat(buf, " ", len);
386 /* skip the emoji */
387 *text_ret += (space - vl->parent->line) + 1;
390 *prfx_ret = buf;
393 static inline void
394 print_vline_descr(int width, WINDOW *window, struct vline *vl)
396 int x, y, goal;
398 if (vl->parent->type != LINE_COMPL &&
399 vl->parent->type != LINE_COMPL_CURRENT)
400 return;
402 if (vl->parent->alt == NULL)
403 return;
405 (void)y;
406 getyx(window, y, x);
408 goal = width/2;
409 if (goal <= x)
410 wprintw(window, " ");
411 for (; goal > x; ++x)
412 wprintw(window, " ");
414 wprintw(window, "%s", vl->parent->alt);
417 /*
418 * Core part of the rendering. It prints a vline starting from the
419 * current cursor position. Printing a vline consists of skipping
420 * `off' columns (for olivetti-mode), print the correct prefix (which
421 * may be the emoji in case of emojified links-lines), printing the
422 * text itself, filling until width - off and filling off columns
423 * again.
424 */
425 static void
426 print_vline(int off, int width, WINDOW *window, struct vline *vl)
428 /*
429 * Believe me or not, I've seen emoji ten code points long!
430 * That means, to stay large, 4*10 bytes + NUL.
431 */
432 char emojibuf[41] = {0};
433 const char *text, *prfx;
434 struct line_face *f;
435 int i, left, x, y;
437 f = &line_faces[vl->parent->type];
439 /* unused, set by getyx */
440 (void)y;
442 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
444 wattr_on(window, body_face.left, NULL);
445 for (i = 0; i < off; i++)
446 waddch(window, ' ');
447 wattr_off(window, body_face.left, NULL);
449 wattr_on(window, f->prefix, NULL);
450 wprintw(window, "%s", prfx);
451 wattr_off(window, f->prefix, NULL);
453 wattr_on(window, f->text, NULL);
454 wprintw(window, "%s", text);
455 print_vline_descr(width, window, vl);
456 wattr_off(window, f->text, NULL);
458 getyx(window, y, x);
460 left = width - x;
462 wattr_on(window, f->trail, NULL);
463 for (i = 0; i < left - off; ++i)
464 waddch(window, ' ');
465 wattr_off(window, f->trail, NULL);
467 wattr_on(window, body_face.right, NULL);
468 for (i = 0; i < off; i++)
469 waddch(window, ' ');
470 wattr_off(window, body_face.right, NULL);
474 static void
475 redraw_tabline(void)
477 struct tab *tab;
478 size_t toskip, ots, tabwidth, space, x;
479 int current, y, truncated, pair;
480 const char *title;
481 char buf[25];
483 x = 0;
485 /* unused, but setted by a getyx */
486 (void)y;
488 tabwidth = sizeof(buf)+1;
489 space = COLS-2;
491 toskip = 0;
492 TAILQ_FOREACH(tab, &tabshead, tabs) {
493 toskip++;
494 if (tab == current_tab)
495 break;
498 if (toskip * tabwidth < space)
499 toskip = 0;
500 else {
501 ots = toskip;
502 toskip--;
503 while (toskip != 0 &&
504 (ots - toskip+1) * tabwidth < space)
505 toskip--;
508 werase(tabline);
509 wattr_on(tabline, tab_face.background, NULL);
510 wprintw(tabline, toskip == 0 ? " " : "<");
511 wattr_off(tabline, tab_face.background, NULL);
513 truncated = 0;
514 TAILQ_FOREACH(tab, &tabshead, tabs) {
515 if (truncated)
516 break;
517 if (toskip != 0) {
518 toskip--;
519 continue;
522 getyx(tabline, y, x);
523 if (x + sizeof(buf)+2 >= (size_t)COLS)
524 truncated = 1;
526 current = tab == current_tab;
528 if (*(title = tab->buffer.page.title) == '\0')
529 title = tab->hist_cur->h;
531 if (tab->flags & TAB_URGENT)
532 strlcpy(buf, "!", sizeof(buf));
533 else
534 strlcpy(buf, " ", sizeof(buf));
536 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
537 /* truncation happens */
538 strlcpy(&buf[sizeof(buf)-4], "...", 4);
539 } else {
540 /* pad with spaces */
541 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
542 /* nop */ ;
545 pair = current ? tab_face.current : tab_face.tab;
546 wattr_on(tabline, pair, NULL);
547 wprintw(tabline, "%s", buf);
548 wattr_off(tabline, pair, NULL);
550 wattr_on(tabline, tab_face.background, NULL);
551 if (TAILQ_NEXT(tab, tabs) != NULL)
552 wprintw(tabline, "│");
553 wattr_off(tabline, tab_face.background, NULL);
556 wattr_on(tabline, tab_face.background, NULL);
557 for (; x < (size_t)COLS; ++x)
558 waddch(tabline, ' ');
559 if (truncated)
560 mvwprintw(tabline, 0, COLS-1, ">");
561 wattr_off(tabline, tab_face.background, NULL);
564 /*
565 * Compute the first visible line around vl. Try to search forward
566 * until the end of the buffer; if a visible line is not found, search
567 * backward. Return NULL if no viable line was found.
568 */
569 struct vline *
570 adjust_line(struct vline *vl, struct buffer *buffer)
572 struct vline *t;
574 if (vl == NULL)
575 return NULL;
577 if (!(vl->parent->flags & L_HIDDEN))
578 return vl;
580 /* search forward */
581 for (t = vl;
582 t != NULL && t->parent->flags & L_HIDDEN;
583 t = TAILQ_NEXT(t, vlines))
584 ; /* nop */
586 if (t != NULL)
587 return t;
589 /* search backward */
590 for (t = vl;
591 t != NULL && t->parent->flags & L_HIDDEN;
592 t = TAILQ_PREV(t, vhead, vlines))
593 ; /* nop */
595 return t;
598 static void
599 redraw_window(WINDOW *win, int off, int height, int width, struct buffer *buffer)
601 struct vline *vl;
602 int l, onscreen;
604 restore_curs_x(buffer);
606 /*
607 * TODO: ignoring buffer->force_update and always
608 * re-rendering. In theory we can recompute the y position
609 * without a re-render, and optimize here. It's not the only
610 * optimisation possible here, wscrl wolud also be an
611 * interesting one.
612 */
614 again:
615 werase(win);
616 buffer->curs_y = 0;
618 if (TAILQ_EMPTY(&buffer->head))
619 goto end;
621 if (buffer->top_line == NULL)
622 buffer->top_line = TAILQ_FIRST(&buffer->head);
624 buffer->top_line = adjust_line(buffer->top_line, buffer);
625 if (buffer->top_line == NULL)
626 goto end;
628 buffer->current_line = adjust_line(buffer->current_line, buffer);
630 l = 0;
631 onscreen = 0;
632 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
633 if (vl->parent->flags & L_HIDDEN)
634 continue;
636 wmove(win, l, 0);
637 print_vline(off, width, win, vl);
639 if (vl == buffer->current_line)
640 onscreen = 1;
642 if (!onscreen)
643 buffer->curs_y++;
645 l++;
646 if (l == height)
647 break;
650 if (!onscreen) {
651 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
652 if (vl == buffer->current_line)
653 break;
654 if (vl->parent->flags & L_HIDDEN)
655 continue;
656 buffer->line_off++;
657 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
660 if (vl != NULL)
661 goto again;
664 buffer->last_line_off = buffer->line_off;
665 buffer->force_redraw = 0;
666 end:
667 wmove(win, buffer->curs_y, buffer->curs_x);
670 static void
671 redraw_help(void)
673 redraw_window(help, 0, help_lines, help_cols, &helpwin);
676 static void
677 redraw_body(struct tab *tab)
679 static struct tab *last_tab;
681 if (last_tab != tab)
682 tab->buffer.force_redraw =1;
683 last_tab = tab;
685 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
688 static inline char
689 trust_status_char(enum trust_state ts)
691 switch (ts) {
692 case TS_UNKNOWN: return '-';
693 case TS_UNTRUSTED: return '!';
694 case TS_TEMP_TRUSTED: return '!';
695 case TS_TRUSTED: return 'v';
696 case TS_VERIFIED: return 'V';
697 default: return 'X';
701 static void
702 redraw_modeline(struct tab *tab)
704 double pct;
705 int x, y, max_x, max_y;
706 const char *mode = tab->buffer.page.name;
707 const char *spin = "-\\|/";
709 werase(modeline);
710 wattr_on(modeline, modeline_face.background, NULL);
711 wmove(modeline, 0, 0);
713 wprintw(modeline, "-%c%c %s ",
714 spin[tab->loading_anim_step],
715 trust_status_char(tab->trust),
716 mode == NULL ? "(none)" : mode);
718 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0
719 / tab->buffer.line_max;
721 if (tab->buffer.line_max <= (size_t)body_lines)
722 wprintw(modeline, "All ");
723 else if (tab->buffer.line_off == 0)
724 wprintw(modeline, "Top ");
725 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
726 wprintw(modeline, "Bottom ");
727 else
728 wprintw(modeline, "%.0f%% ", pct);
730 wprintw(modeline, "%d/%d %s ",
731 tab->buffer.line_off + tab->buffer.curs_y,
732 tab->buffer.line_max,
733 tab->hist_cur->h);
735 getyx(modeline, y, x);
736 getmaxyx(modeline, max_y, max_x);
738 (void)y;
739 (void)max_y;
741 for (; x < max_x; ++x)
742 waddstr(modeline, "-");
744 wattr_off(modeline, modeline_face.background, NULL);
747 static void
748 redraw_minibuffer(void)
750 wattr_on(echoarea, minibuffer_face.background, NULL);
751 werase(echoarea);
753 if (in_minibuffer)
754 do_redraw_minibuffer();
755 else
756 do_redraw_echoarea();
758 if (in_minibuffer == MB_COMPREAD)
759 do_redraw_minibuffer_compl();
761 wattr_off(echoarea, minibuffer_face.background, NULL);
764 static void
765 do_redraw_echoarea(void)
767 struct vline *vl;
769 if (ministate.curmesg != NULL)
770 wprintw(echoarea, "%s", ministate.curmesg);
771 else if (*keybuf != '\0')
772 waddstr(echoarea, keybuf);
773 else {
774 /* If nothing else, show the URL at point */
775 vl = current_tab->buffer.current_line;
776 if (vl != NULL && vl->parent->type == LINE_LINK)
777 waddstr(echoarea, vl->parent->alt);
781 static void
782 do_redraw_minibuffer(void)
784 size_t off_y, off_x = 0;
785 const char *start, *c;
787 /* unused, set by getyx */
788 (void)off_y;
790 wmove(echoarea, 0, 0);
792 if (in_minibuffer == MB_COMPREAD)
793 wprintw(echoarea, "(%2d) ",
794 ministate.compl.buffer.line_max);
796 wprintw(echoarea, "%s", ministate.prompt);
797 if (ministate.hist_cur != NULL)
798 wprintw(echoarea, "(%zu/%zu) ",
799 ministate.hist_off + 1,
800 ministate.history->len);
802 getyx(echoarea, off_y, off_x);
804 start = ministate.hist_cur != NULL
805 ? ministate.hist_cur->h
806 : ministate.buf;
807 c = utf8_nth(ministate.buffer.current_line->line,
808 ministate.buffer.cpoff);
809 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
810 start = utf8_next_cp(start);
813 waddstr(echoarea, start);
815 if (ministate.curmesg != NULL)
816 wprintw(echoarea, " [%s]", ministate.curmesg);
818 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
821 static void
822 do_redraw_minibuffer_compl(void)
824 redraw_window(minibuffer, 0, 10, body_cols,
825 &ministate.compl.buffer);
828 /*
829 * Place the cursor in the right ncurses window. If soft is 1, use
830 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
831 * wrefresh.
832 */
833 static void
834 place_cursor(int soft)
836 int (*touch)(WINDOW *);
838 if (soft)
839 touch = wnoutrefresh;
840 else
841 touch = wrefresh;
843 if (in_minibuffer) {
844 touch(body);
845 touch(echoarea);
846 } else {
847 touch(echoarea);
848 touch(body);
852 static void
853 redraw_tab(struct tab *tab)
855 if (too_small)
856 return;
858 if (side_window) {
859 redraw_help();
860 wnoutrefresh(help);
863 redraw_tabline();
864 redraw_body(tab);
865 redraw_modeline(tab);
866 redraw_minibuffer();
868 wnoutrefresh(tabline);
869 wnoutrefresh(modeline);
871 if (in_minibuffer == MB_COMPREAD)
872 wnoutrefresh(minibuffer);
874 place_cursor(1);
876 doupdate();
878 if (set_title)
879 dprintf(1, "\033]0;%s - Telescope\a",
880 current_tab->buffer.page.title);
883 void
884 start_loading_anim(struct tab *tab)
886 if (tab->loading_anim)
887 return;
888 tab->loading_anim = 1;
889 evtimer_set(&tab->loadingev, update_loading_anim, tab);
890 evtimer_add(&tab->loadingev, &loadingev_timer);
893 static void
894 update_loading_anim(int fd, short ev, void *d)
896 struct tab *tab = d;
898 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
900 if (tab == current_tab) {
901 redraw_modeline(tab);
902 wrefresh(modeline);
903 wrefresh(body);
904 if (in_minibuffer)
905 wrefresh(echoarea);
908 evtimer_add(&tab->loadingev, &loadingev_timer);
911 static void
912 stop_loading_anim(struct tab *tab)
914 if (!tab->loading_anim)
915 return;
916 evtimer_del(&tab->loadingev);
917 tab->loading_anim = 0;
918 tab->loading_anim_step = 0;
920 if (tab != current_tab)
921 return;
923 redraw_modeline(tab);
925 wrefresh(modeline);
926 wrefresh(body);
927 if (in_minibuffer)
928 wrefresh(echoarea);
931 void
932 load_url_in_tab(struct tab *tab, const char *url, const char *base)
934 if (!operating) {
935 load_url(tab, url, base);
936 return;
939 message("Loading %s...", url);
940 start_loading_anim(tab);
941 load_url(tab, url, base);
943 redraw_tab(tab);
946 void
947 switch_to_tab(struct tab *tab)
949 current_tab = tab;
950 tab->flags &= ~TAB_URGENT;
952 if (operating && tab->flags & TAB_LAZY)
953 load_url_in_tab(tab, tab->hist_cur->h, NULL);
956 unsigned int
957 tab_new_id(void)
959 return tab_counter++;
962 struct tab *
963 new_tab(const char *url, const char *base)
965 struct tab *tab;
967 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
968 event_loopbreak();
969 return NULL;
971 tab->fd = -1;
973 TAILQ_INIT(&tab->hist.head);
975 TAILQ_INIT(&tab->buffer.head);
977 tab->id = tab_new_id();
978 if (!operating)
979 tab->flags |= TAB_LAZY;
980 switch_to_tab(tab);
982 if (TAILQ_EMPTY(&tabshead))
983 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
984 else
985 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
987 load_url_in_tab(tab, url, base);
988 return tab;
991 int
992 ui_print_colors(void)
994 int colors = 0, pairs = 0, can_change = 0;
995 int columns = 16, lines, color, i, j;
997 initscr();
998 if (has_colors()) {
999 start_color();
1000 use_default_colors();
1002 colors = COLORS;
1003 pairs = COLOR_PAIRS;
1004 can_change = can_change_color();
1006 endwin();
1008 printf("Term info:\n");
1009 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1010 getenv("TERM"), colors, pairs, can_change);
1011 printf("\n");
1013 if (colors == 0) {
1014 printf("No color support\n");
1015 return 0;
1018 printf("Available colors:\n\n");
1019 lines = (colors - 1) / columns + 1;
1020 color = 0;
1021 for (i = 0; i < lines; ++i) {
1022 for (j = 0; j < columns; ++j, ++color) {
1023 printf("\033[0;38;5;%dm %03d", color, color);
1025 printf("\n");
1028 printf("\033[0m");
1029 fflush(stdout);
1030 return 0;
1033 int
1034 ui_init()
1036 setlocale(LC_ALL, "");
1038 minibuffer_init();
1040 /* initialize help window */
1041 TAILQ_INIT(&helpwin.head);
1043 base_map = &global_map;
1044 current_map = &global_map;
1046 initscr();
1048 if (enable_colors) {
1049 if (has_colors()) {
1050 start_color();
1051 use_default_colors();
1052 } else
1053 enable_colors = 0;
1056 config_apply_style();
1058 raw();
1059 noecho();
1060 nonl();
1061 intrflush(stdscr, FALSE);
1063 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1064 return 0;
1065 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1066 return 0;
1067 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1068 return 0;
1069 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1070 return 0;
1071 if ((minibuffer = newwin(1, COLS, LINES-1, 0)) == NULL)
1072 return 0;
1073 if ((help = newwin(1, 1, 1, 0)) == NULL)
1074 return 0;
1076 body_lines = LINES-3;
1077 body_cols = COLS;
1079 wbkgd(body, body_face.body);
1080 wbkgd(echoarea, minibuffer_face.background);
1082 update_x_offset();
1084 keypad(body, TRUE);
1085 scrollok(body, FALSE);
1087 /* non-blocking input */
1088 wtimeout(body, 0);
1090 mvwprintw(body, 0, 0, "");
1092 evtimer_set(&resizeev, handle_resize, NULL);
1094 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1095 event_add(&stdioev, NULL);
1097 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1098 signal_add(&winchev, NULL);
1100 return 1;
1103 void
1104 ui_main_loop(void)
1106 operating = 1;
1107 switch_to_tab(current_tab);
1108 redraw_tab(current_tab);
1110 event_dispatch();
1113 void
1114 ui_on_tab_loaded(struct tab *tab)
1116 stop_loading_anim(tab);
1117 message("Loaded %s", tab->hist_cur->h);
1119 redraw_tabline();
1120 wrefresh(tabline);
1121 place_cursor(0);
1124 void
1125 ui_on_tab_refresh(struct tab *tab)
1127 wrap_page(&tab->buffer, body_cols);
1128 if (tab == current_tab)
1129 redraw_tab(tab);
1130 else
1131 tab->flags |= TAB_URGENT;
1134 const char *
1135 ui_keyname(int k)
1137 return keyname(k);
1140 void
1141 ui_toggle_side_window(void)
1143 side_window = !side_window;
1144 if (side_window)
1145 recompute_help();
1148 * ugly hack, but otherwise the window doesn't get updated
1149 * until I call rearrange_windows a second time (e.g. via
1150 * C-l). I will be happy to know why something like this is
1151 * needed.
1153 rearrange_windows();
1154 rearrange_windows();
1157 void
1158 ui_schedule_redraw(void)
1160 should_rearrange_windows = 1;
1163 void
1164 ui_require_input(struct tab *tab, int hide)
1166 /* TODO: hard-switching to another tab is ugly */
1167 switch_to_tab(tab);
1169 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1170 &ir_history, NULL, NULL);
1171 strlcpy(ministate.prompt, "Input required: ",
1172 sizeof(ministate.prompt));
1173 redraw_tab(tab);
1176 void
1177 ui_after_message_hook(void)
1179 redraw_minibuffer();
1180 place_cursor(0);
1183 void
1184 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1185 struct tab *data)
1187 yornp(prompt, fn, data);
1188 redraw_tab(current_tab);
1191 void
1192 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1193 struct tab *data)
1195 minibuffer_read(prompt, fn, data);
1196 redraw_tab(current_tab);
1199 void
1200 ui_suspend(void)
1202 endwin();
1204 kill(getpid(), SIGSTOP);
1206 refresh();
1207 clear();
1208 rearrange_windows();
1211 void
1212 ui_end(void)
1214 endwin();