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 int in_minibuffer;
115 static inline void
116 update_x_offset(void)
118 if (olivetti_mode && fill_column < body_cols)
119 x_offset = (body_cols - fill_column)/2;
120 else
121 x_offset = 0;
124 void
125 save_excursion(struct excursion *place, struct buffer *buffer)
127 place->curs_x = buffer->curs_x;
128 place->curs_y = buffer->curs_y;
129 place->line_off = buffer->line_off;
130 place->top_line = buffer->top_line;
131 place->current_line = buffer->current_line;
132 place->cpoff = buffer->cpoff;
135 void
136 restore_excursion(struct excursion *place, struct buffer *buffer)
138 buffer->curs_x = place->curs_x;
139 buffer->curs_y = place->curs_y;
140 buffer->line_off = place->line_off;
141 buffer->top_line = place->top_line;
142 buffer->current_line = place->current_line;
143 buffer->cpoff = place->cpoff;
146 static void
147 restore_curs_x(struct buffer *buffer)
149 struct vline *vl;
150 const char *prfx, *text;
152 vl = buffer->current_line;
153 if (vl == NULL || vl->line == NULL)
154 buffer->curs_x = buffer->cpoff = 0;
155 else if (vl->parent->data != NULL) {
156 text = vl->parent->data;
157 buffer->curs_x = utf8_snwidth(text + 1, buffer->cpoff) + 1;
158 } else
159 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
161 buffer->curs_x += x_offset;
163 if (vl == NULL)
164 return;
166 if (vl->parent->data != NULL)
167 buffer->curs_x += utf8_swidth_between(vl->parent->line, 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 return &current_tab->buffer;
188 static int
189 readkey(void)
191 uint32_t state = 0;
193 if ((thiskey.key = wgetch(body)) == ERR)
194 return 0;
196 thiskey.meta = thiskey.key == 27;
197 if (thiskey.meta) {
198 thiskey.key = wgetch(body);
199 if (thiskey.key == ERR || thiskey.key == 27) {
200 thiskey.meta = 0;
201 thiskey.key = 27;
205 thiskey.cp = 0;
206 if ((unsigned int)thiskey.key < UINT8_MAX) {
207 while (1) {
208 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
209 break;
210 if ((thiskey.key = wgetch(body)) == ERR) {
211 message("Error decoding user input");
212 return 0;
217 return 1;
220 static void
221 dispatch_stdio(int fd, short ev, void *d)
223 struct keymap *k;
224 const char *keyname;
225 char tmp[5] = {0};
227 /* TODO: schedule a redraw? */
228 if (too_small)
229 return;
231 if (!readkey())
232 return;
234 if (keybuf[0] != '\0')
235 strlcat(keybuf, " ", sizeof(keybuf));
236 if (thiskey.meta)
237 strlcat(keybuf, "M-", sizeof(keybuf));
238 if (thiskey.cp != 0) {
239 utf8_encode(thiskey.cp, tmp);
240 strlcat(keybuf, tmp, sizeof(keybuf));
241 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
242 strlcat(keybuf, keyname, sizeof(keybuf));
243 } else {
244 tmp[0] = thiskey.key;
245 strlcat(keybuf, tmp, sizeof(keybuf));
248 TAILQ_FOREACH(k, &current_map->m, keymaps) {
249 if (k->meta == thiskey.meta &&
250 k->key == thiskey.key) {
251 if (k->fn == NULL)
252 current_map = &k->map;
253 else {
254 current_map = base_map;
255 strlcpy(keybuf, "", sizeof(keybuf));
256 k->fn(current_buffer());
258 goto done;
262 if (current_map->unhandled_input != NULL)
263 current_map->unhandled_input();
264 else
265 global_key_unbound();
267 strlcpy(keybuf, "", sizeof(keybuf));
268 current_map = base_map;
270 done:
271 if (side_window)
272 recompute_help();
274 if (should_rearrange_windows)
275 rearrange_windows();
276 redraw_tab(current_tab);
279 static void
280 handle_resize(int sig, short ev, void *d)
282 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
283 event_del(&resizeev);
285 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
286 evtimer_add(&resizeev, &resize_timer);
289 static void
290 handle_resize_nodelay(int s, short ev, void *d)
292 endwin();
293 refresh();
294 clear();
296 rearrange_windows();
299 static void
300 rearrange_windows(void)
302 int lines;
304 should_rearrange_windows = 0;
306 lines = LINES;
308 if ((too_small = lines < 15)) {
309 erase();
310 printw("Window too small.");
311 refresh();
312 return;
315 /* move and resize the windows, in reverse order! */
317 if (in_minibuffer == MB_COMPREAD) {
318 mvwin(minibuffer, lines-10, 0);
319 wresize(minibuffer, 10, COLS);
320 lines -= 10;
322 wrap_page(&ministate.compl.buffer, COLS);
325 mvwin(echoarea, --lines, 0);
326 wresize(echoarea, 1, COLS);
328 mvwin(modeline, --lines, 0);
329 wresize(modeline, 1, COLS);
331 body_lines = --lines;
332 body_cols = COLS;
334 if (side_window) {
335 help_cols = 0.3 * COLS;
336 help_lines = lines;
337 mvwin(help, 1, 0);
338 wresize(help, help_lines, help_cols);
340 wrap_page(&helpwin, help_cols);
342 body_cols = COLS - help_cols - 1;
343 mvwin(body, 1, help_cols);
344 } else
345 mvwin(body, 1, 0);
347 update_x_offset();
348 wresize(body, body_lines, body_cols);
350 wresize(tabline, 1, COLS);
352 wrap_page(&current_tab->buffer, body_cols);
353 redraw_tab(current_tab);
356 static void
357 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
358 const char **prfx_ret, const char **text_ret)
360 int type, i, cont, width;
361 char *space, *t;
363 if ((*text_ret = vl->line) == NULL)
364 *text_ret = "";
366 cont = vl->flags & L_CONTINUATION;
367 type = vl->parent->type;
368 if (!cont)
369 *prfx_ret = line_prefixes[type].prfx1;
370 else
371 *prfx_ret = line_prefixes[type].prfx2;
373 space = vl->parent->data;
374 if (!emojify_link || type != LINE_LINK || space == NULL)
375 return;
377 if (cont) {
378 memset(buf, 0, len);
379 width = utf8_swidth_between(vl->parent->line, space);
380 for (i = 0; i < width + 1; ++i)
381 strlcat(buf, " ", len);
382 } else {
383 strlcpy(buf, *text_ret, len);
384 if ((t = strchr(buf, ' ')) != NULL)
385 *t = '\0';
386 strlcat(buf, " ", len);
388 /* skip the emoji */
389 *text_ret += (space - vl->parent->line) + 1;
392 *prfx_ret = buf;
395 static inline void
396 print_vline_descr(int width, WINDOW *window, struct vline *vl)
398 int x, y, goal;
400 if (vl->parent->type != LINE_COMPL &&
401 vl->parent->type != LINE_COMPL_CURRENT)
402 return;
404 if (vl->parent->alt == NULL)
405 return;
407 (void)y;
408 getyx(window, y, x);
410 goal = width/2;
411 if (goal <= x)
412 wprintw(window, " ");
413 for (; goal > x; ++x)
414 wprintw(window, " ");
416 wprintw(window, "%s", vl->parent->alt);
419 /*
420 * Core part of the rendering. It prints a vline starting from the
421 * current cursor position. Printing a vline consists of skipping
422 * `off' columns (for olivetti-mode), print the correct prefix (which
423 * may be the emoji in case of emojified links-lines), printing the
424 * text itself, filling until width - off and filling off columns
425 * again.
426 */
427 static void
428 print_vline(int off, int width, WINDOW *window, struct vline *vl)
430 /*
431 * Believe me or not, I've seen emoji ten code points long!
432 * That means, to stay large, 4*10 bytes + NUL.
433 */
434 char emojibuf[41] = {0};
435 const char *text, *prfx;
436 struct line_face *f;
437 int i, left, x, y;
439 f = &line_faces[vl->parent->type];
441 /* unused, set by getyx */
442 (void)y;
444 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
446 wattr_on(window, body_face.left, NULL);
447 for (i = 0; i < off; i++)
448 waddch(window, ' ');
449 wattr_off(window, body_face.left, NULL);
451 wattr_on(window, f->prefix, NULL);
452 wprintw(window, "%s", prfx);
453 wattr_off(window, f->prefix, NULL);
455 wattr_on(window, f->text, NULL);
456 wprintw(window, "%s", text);
457 print_vline_descr(width, window, vl);
458 wattr_off(window, f->text, NULL);
460 getyx(window, y, x);
462 left = width - x;
464 wattr_on(window, f->trail, NULL);
465 for (i = 0; i < left - off; ++i)
466 waddch(window, ' ');
467 wattr_off(window, f->trail, NULL);
469 wattr_on(window, body_face.right, NULL);
470 for (i = 0; i < off; i++)
471 waddch(window, ' ');
472 wattr_off(window, body_face.right, NULL);
476 static void
477 redraw_tabline(void)
479 struct tab *tab;
480 size_t toskip, ots, tabwidth, space, x;
481 int current, y, truncated, pair;
482 const char *title;
483 char buf[25];
485 x = 0;
487 /* unused, but setted by a getyx */
488 (void)y;
490 tabwidth = sizeof(buf)+1;
491 space = COLS-2;
493 toskip = 0;
494 TAILQ_FOREACH(tab, &tabshead, tabs) {
495 toskip++;
496 if (tab == current_tab)
497 break;
500 if (toskip * tabwidth < space)
501 toskip = 0;
502 else {
503 ots = toskip;
504 toskip--;
505 while (toskip != 0 &&
506 (ots - toskip+1) * tabwidth < space)
507 toskip--;
510 werase(tabline);
511 wattr_on(tabline, tab_face.background, NULL);
512 wprintw(tabline, toskip == 0 ? " " : "<");
513 wattr_off(tabline, tab_face.background, NULL);
515 truncated = 0;
516 TAILQ_FOREACH(tab, &tabshead, tabs) {
517 if (truncated)
518 break;
519 if (toskip != 0) {
520 toskip--;
521 continue;
524 getyx(tabline, y, x);
525 if (x + sizeof(buf)+2 >= (size_t)COLS)
526 truncated = 1;
528 current = tab == current_tab;
530 if (*(title = tab->buffer.page.title) == '\0')
531 title = tab->hist_cur->h;
533 if (tab->flags & TAB_URGENT)
534 strlcpy(buf, "!", sizeof(buf));
535 else
536 strlcpy(buf, " ", sizeof(buf));
538 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
539 /* truncation happens */
540 strlcpy(&buf[sizeof(buf)-4], "...", 4);
541 } else {
542 /* pad with spaces */
543 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
544 /* nop */ ;
547 pair = current ? tab_face.current : tab_face.tab;
548 wattr_on(tabline, pair, NULL);
549 wprintw(tabline, "%s", buf);
550 wattr_off(tabline, pair, NULL);
552 wattr_on(tabline, tab_face.background, NULL);
553 if (TAILQ_NEXT(tab, tabs) != NULL)
554 wprintw(tabline, "│");
555 wattr_off(tabline, tab_face.background, NULL);
558 wattr_on(tabline, tab_face.background, NULL);
559 for (; x < (size_t)COLS; ++x)
560 waddch(tabline, ' ');
561 if (truncated)
562 mvwprintw(tabline, 0, COLS-1, ">");
563 wattr_off(tabline, tab_face.background, NULL);
566 /*
567 * Compute the first visible line around vl. Try to search forward
568 * until the end of the buffer; if a visible line is not found, search
569 * backward. Return NULL if no viable line was found.
570 */
571 struct vline *
572 adjust_line(struct vline *vl, struct buffer *buffer)
574 struct vline *t;
576 if (vl == NULL)
577 return NULL;
579 if (!(vl->parent->flags & L_HIDDEN))
580 return vl;
582 /* search forward */
583 for (t = vl;
584 t != NULL && t->parent->flags & L_HIDDEN;
585 t = TAILQ_NEXT(t, vlines))
586 ; /* nop */
588 if (t != NULL)
589 return t;
591 /* search backward */
592 for (t = vl;
593 t != NULL && t->parent->flags & L_HIDDEN;
594 t = TAILQ_PREV(t, vhead, vlines))
595 ; /* nop */
597 return t;
600 static void
601 redraw_window(WINDOW *win, int off, int height, int width, struct buffer *buffer)
603 struct vline *vl;
604 int l, onscreen;
606 restore_curs_x(buffer);
608 /*
609 * TODO: ignoring buffer->force_update and always
610 * re-rendering. In theory we can recompute the y position
611 * without a re-render, and optimize here. It's not the only
612 * optimisation possible here, wscrl wolud also be an
613 * interesting one.
614 */
616 again:
617 werase(win);
618 buffer->curs_y = 0;
620 if (TAILQ_EMPTY(&buffer->head))
621 goto end;
623 if (buffer->top_line == NULL)
624 buffer->top_line = TAILQ_FIRST(&buffer->head);
626 buffer->top_line = adjust_line(buffer->top_line, buffer);
627 if (buffer->top_line == NULL)
628 goto end;
630 buffer->current_line = adjust_line(buffer->current_line, buffer);
632 l = 0;
633 onscreen = 0;
634 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
635 if (vl->parent->flags & L_HIDDEN)
636 continue;
638 wmove(win, l, 0);
639 print_vline(off, width, win, vl);
641 if (vl == buffer->current_line)
642 onscreen = 1;
644 if (!onscreen)
645 buffer->curs_y++;
647 l++;
648 if (l == height)
649 break;
652 if (!onscreen) {
653 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
654 if (vl == buffer->current_line)
655 break;
656 if (vl->parent->flags & L_HIDDEN)
657 continue;
658 buffer->line_off++;
659 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
662 if (vl != NULL)
663 goto again;
666 buffer->last_line_off = buffer->line_off;
667 buffer->force_redraw = 0;
668 end:
669 wmove(win, buffer->curs_y, buffer->curs_x);
672 static void
673 redraw_help(void)
675 redraw_window(help, 0, help_lines, help_cols, &helpwin);
678 static void
679 redraw_body(struct tab *tab)
681 static struct tab *last_tab;
683 if (last_tab != tab)
684 tab->buffer.force_redraw =1;
685 last_tab = tab;
687 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
690 static inline char
691 trust_status_char(enum trust_state ts)
693 switch (ts) {
694 case TS_UNKNOWN: return '-';
695 case TS_UNTRUSTED: return '!';
696 case TS_TEMP_TRUSTED: return '!';
697 case TS_TRUSTED: return 'v';
698 case TS_VERIFIED: return 'V';
699 default: return 'X';
703 static void
704 redraw_modeline(struct tab *tab)
706 double pct;
707 int x, y, max_x, max_y;
708 const char *mode = tab->buffer.page.name;
709 const char *spin = "-\\|/";
711 werase(modeline);
712 wattr_on(modeline, modeline_face.background, NULL);
713 wmove(modeline, 0, 0);
715 wprintw(modeline, "-%c%c %s ",
716 spin[tab->loading_anim_step],
717 trust_status_char(tab->trust),
718 mode == NULL ? "(none)" : mode);
720 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0
721 / tab->buffer.line_max;
723 if (tab->buffer.line_max <= (size_t)body_lines)
724 wprintw(modeline, "All ");
725 else if (tab->buffer.line_off == 0)
726 wprintw(modeline, "Top ");
727 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
728 wprintw(modeline, "Bottom ");
729 else
730 wprintw(modeline, "%.0f%% ", pct);
732 wprintw(modeline, "%d/%d %s ",
733 tab->buffer.line_off + tab->buffer.curs_y,
734 tab->buffer.line_max,
735 tab->hist_cur->h);
737 getyx(modeline, y, x);
738 getmaxyx(modeline, max_y, max_x);
740 (void)y;
741 (void)max_y;
743 for (; x < max_x; ++x)
744 waddstr(modeline, "-");
746 wattr_off(modeline, modeline_face.background, NULL);
749 static void
750 redraw_minibuffer(void)
752 wattr_on(echoarea, minibuffer_face.background, NULL);
753 werase(echoarea);
755 if (in_minibuffer)
756 do_redraw_minibuffer();
757 else
758 do_redraw_echoarea();
760 if (in_minibuffer == MB_COMPREAD)
761 do_redraw_minibuffer_compl();
763 wattr_off(echoarea, minibuffer_face.background, NULL);
766 static void
767 do_redraw_echoarea(void)
769 struct vline *vl;
771 if (ministate.curmesg != NULL)
772 wprintw(echoarea, "%s", ministate.curmesg);
773 else if (*keybuf != '\0')
774 waddstr(echoarea, keybuf);
775 else {
776 /* If nothing else, show the URL at point */
777 vl = current_tab->buffer.current_line;
778 if (vl != NULL && vl->parent->type == LINE_LINK)
779 waddstr(echoarea, vl->parent->alt);
783 static void
784 do_redraw_minibuffer(void)
786 size_t off_y, off_x = 0;
787 const char *start, *c;
789 /* unused, set by getyx */
790 (void)off_y;
792 wmove(echoarea, 0, 0);
794 if (in_minibuffer == MB_COMPREAD)
795 wprintw(echoarea, "(%2d) ",
796 ministate.compl.buffer.line_max);
798 wprintw(echoarea, "%s", ministate.prompt);
799 if (ministate.hist_cur != NULL)
800 wprintw(echoarea, "(%zu/%zu) ",
801 ministate.hist_off + 1,
802 ministate.history->len);
804 getyx(echoarea, off_y, off_x);
806 start = ministate.hist_cur != NULL
807 ? ministate.hist_cur->h
808 : ministate.buf;
809 c = utf8_nth(ministate.buffer.current_line->line,
810 ministate.buffer.cpoff);
811 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
812 start = utf8_next_cp(start);
815 waddstr(echoarea, start);
817 if (ministate.curmesg != NULL)
818 wprintw(echoarea, " [%s]", ministate.curmesg);
820 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
823 static void
824 do_redraw_minibuffer_compl(void)
826 redraw_window(minibuffer, 0, 10, body_cols,
827 &ministate.compl.buffer);
830 /*
831 * Place the cursor in the right ncurses window. If soft is 1, use
832 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
833 * wrefresh.
834 */
835 static void
836 place_cursor(int soft)
838 int (*touch)(WINDOW *);
840 if (soft)
841 touch = wnoutrefresh;
842 else
843 touch = wrefresh;
845 if (in_minibuffer) {
846 touch(body);
847 touch(echoarea);
848 } else {
849 touch(echoarea);
850 touch(body);
854 static void
855 redraw_tab(struct tab *tab)
857 if (too_small)
858 return;
860 if (side_window) {
861 redraw_help();
862 wnoutrefresh(help);
865 redraw_tabline();
866 redraw_body(tab);
867 redraw_modeline(tab);
868 redraw_minibuffer();
870 wnoutrefresh(tabline);
871 wnoutrefresh(modeline);
873 if (in_minibuffer == MB_COMPREAD)
874 wnoutrefresh(minibuffer);
876 place_cursor(1);
878 doupdate();
880 if (set_title)
881 dprintf(1, "\033]0;%s - Telescope\a",
882 current_tab->buffer.page.title);
885 void
886 start_loading_anim(struct tab *tab)
888 if (tab->loading_anim)
889 return;
890 tab->loading_anim = 1;
891 evtimer_set(&tab->loadingev, update_loading_anim, tab);
892 evtimer_add(&tab->loadingev, &loadingev_timer);
895 static void
896 update_loading_anim(int fd, short ev, void *d)
898 struct tab *tab = d;
900 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
902 if (tab == current_tab) {
903 redraw_modeline(tab);
904 wrefresh(modeline);
905 wrefresh(body);
906 if (in_minibuffer)
907 wrefresh(echoarea);
910 evtimer_add(&tab->loadingev, &loadingev_timer);
913 static void
914 stop_loading_anim(struct tab *tab)
916 if (!tab->loading_anim)
917 return;
918 evtimer_del(&tab->loadingev);
919 tab->loading_anim = 0;
920 tab->loading_anim_step = 0;
922 if (tab != current_tab)
923 return;
925 redraw_modeline(tab);
927 wrefresh(modeline);
928 wrefresh(body);
929 if (in_minibuffer)
930 wrefresh(echoarea);
933 void
934 load_url_in_tab(struct tab *tab, const char *url, const char *base)
936 if (!operating) {
937 load_url(tab, url, base);
938 return;
941 message("Loading %s...", url);
942 start_loading_anim(tab);
943 load_url(tab, url, base);
945 redraw_tab(tab);
948 void
949 switch_to_tab(struct tab *tab)
951 current_tab = tab;
952 tab->flags &= ~TAB_URGENT;
954 if (operating && tab->flags & TAB_LAZY)
955 load_url_in_tab(tab, tab->hist_cur->h, NULL);
958 unsigned int
959 tab_new_id(void)
961 return tab_counter++;
964 struct tab *
965 new_tab(const char *url, const char *base)
967 struct tab *tab;
969 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
970 event_loopbreak();
971 return NULL;
973 tab->fd = -1;
975 TAILQ_INIT(&tab->hist.head);
977 TAILQ_INIT(&tab->buffer.head);
979 tab->id = tab_new_id();
980 if (!operating)
981 tab->flags |= TAB_LAZY;
982 switch_to_tab(tab);
984 if (TAILQ_EMPTY(&tabshead))
985 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
986 else
987 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
989 load_url_in_tab(tab, url, base);
990 return tab;
993 int
994 ui_print_colors(void)
996 int colors = 0, pairs = 0, can_change = 0;
997 int columns = 16, lines, color, i, j;
999 initscr();
1000 if (has_colors()) {
1001 start_color();
1002 use_default_colors();
1004 colors = COLORS;
1005 pairs = COLOR_PAIRS;
1006 can_change = can_change_color();
1008 endwin();
1010 printf("Term info:\n");
1011 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1012 getenv("TERM"), colors, pairs, can_change);
1013 printf("\n");
1015 if (colors == 0) {
1016 printf("No color support\n");
1017 return 0;
1020 printf("Available colors:\n\n");
1021 lines = (colors - 1) / columns + 1;
1022 color = 0;
1023 for (i = 0; i < lines; ++i) {
1024 for (j = 0; j < columns; ++j, ++color) {
1025 printf("\033[0;38;5;%dm %03d", color, color);
1027 printf("\n");
1030 printf("\033[0m");
1031 fflush(stdout);
1032 return 0;
1035 int
1036 ui_init()
1038 setlocale(LC_ALL, "");
1040 minibuffer_init();
1042 /* initialize help window */
1043 TAILQ_INIT(&helpwin.head);
1045 base_map = &global_map;
1046 current_map = &global_map;
1048 initscr();
1050 if (enable_colors) {
1051 if (has_colors()) {
1052 start_color();
1053 use_default_colors();
1054 } else
1055 enable_colors = 0;
1058 config_apply_style();
1060 raw();
1061 noecho();
1062 nonl();
1063 intrflush(stdscr, FALSE);
1065 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1066 return 0;
1067 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1068 return 0;
1069 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1070 return 0;
1071 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1072 return 0;
1073 if ((minibuffer = newwin(1, COLS, LINES-1, 0)) == NULL)
1074 return 0;
1075 if ((help = newwin(1, 1, 1, 0)) == NULL)
1076 return 0;
1078 body_lines = LINES-3;
1079 body_cols = COLS;
1081 wbkgd(body, body_face.body);
1082 wbkgd(echoarea, minibuffer_face.background);
1084 update_x_offset();
1086 keypad(body, TRUE);
1087 scrollok(body, FALSE);
1089 /* non-blocking input */
1090 wtimeout(body, 0);
1092 mvwprintw(body, 0, 0, "");
1094 evtimer_set(&resizeev, handle_resize, NULL);
1096 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1097 event_add(&stdioev, NULL);
1099 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1100 signal_add(&winchev, NULL);
1102 return 1;
1105 void
1106 ui_main_loop(void)
1108 operating = 1;
1109 switch_to_tab(current_tab);
1110 redraw_tab(current_tab);
1112 event_dispatch();
1115 void
1116 ui_on_tab_loaded(struct tab *tab)
1118 stop_loading_anim(tab);
1119 message("Loaded %s", tab->hist_cur->h);
1121 redraw_tabline();
1122 wrefresh(tabline);
1123 place_cursor(0);
1126 void
1127 ui_on_tab_refresh(struct tab *tab)
1129 wrap_page(&tab->buffer, body_cols);
1130 if (tab == current_tab)
1131 redraw_tab(tab);
1132 else
1133 tab->flags |= TAB_URGENT;
1136 const char *
1137 ui_keyname(int k)
1139 return keyname(k);
1142 void
1143 ui_toggle_side_window(void)
1145 side_window = !side_window;
1146 if (side_window)
1147 recompute_help();
1150 * ugly hack, but otherwise the window doesn't get updated
1151 * until I call rearrange_windows a second time (e.g. via
1152 * C-l). I will be happy to know why something like this is
1153 * needed.
1155 rearrange_windows();
1156 rearrange_windows();
1159 void
1160 ui_schedule_redraw(void)
1162 should_rearrange_windows = 1;
1165 void
1166 ui_require_input(struct tab *tab, int hide)
1168 /* TODO: hard-switching to another tab is ugly */
1169 switch_to_tab(tab);
1171 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1172 &ir_history, NULL, NULL);
1173 strlcpy(ministate.prompt, "Input required: ",
1174 sizeof(ministate.prompt));
1175 redraw_tab(tab);
1178 void
1179 ui_after_message_hook(void)
1181 redraw_minibuffer();
1182 place_cursor(0);
1185 void
1186 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1187 struct tab *data)
1189 yornp(prompt, fn, data);
1190 redraw_tab(current_tab);
1193 void
1194 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1195 struct tab *data)
1197 minibuffer_read(prompt, fn, data);
1198 redraw_tab(current_tab);
1201 void
1202 ui_suspend(void)
1204 endwin();
1206 kill(getpid(), SIGSTOP);
1208 refresh();
1209 clear();
1210 rearrange_windows();
1213 void
1214 ui_end(void)
1216 endwin();