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 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 if (in_side_window)
184 return &helpwin;
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 vl->parent->type != LINE_HELP)
403 return;
405 if (vl->parent->alt == NULL)
406 return;
408 (void)y;
409 getyx(window, y, x);
411 if (vl->parent->type == LINE_HELP)
412 goal = 8;
413 else
414 goal = width/2;
416 if (goal <= x)
417 wprintw(window, " ");
418 for (; goal > x; ++x)
419 wprintw(window, " ");
421 wprintw(window, "%s", vl->parent->alt);
424 /*
425 * Core part of the rendering. It prints a vline starting from the
426 * current cursor position. Printing a vline consists of skipping
427 * `off' columns (for olivetti-mode), print the correct prefix (which
428 * may be the emoji in case of emojified links-lines), printing the
429 * text itself, filling until width - off and filling off columns
430 * again.
431 */
432 static void
433 print_vline(int off, int width, WINDOW *window, struct vline *vl)
435 /*
436 * Believe me or not, I've seen emoji ten code points long!
437 * That means, to stay large, 4*10 bytes + NUL.
438 */
439 char emojibuf[41] = {0};
440 const char *text, *prfx;
441 struct line_face *f;
442 int i, left, x, y;
444 f = &line_faces[vl->parent->type];
446 /* unused, set by getyx */
447 (void)y;
449 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
451 wattr_on(window, body_face.left, NULL);
452 for (i = 0; i < off; i++)
453 waddch(window, ' ');
454 wattr_off(window, body_face.left, NULL);
456 wattr_on(window, f->prefix, NULL);
457 wprintw(window, "%s", prfx);
458 wattr_off(window, f->prefix, NULL);
460 wattr_on(window, f->text, NULL);
461 wprintw(window, "%s", text);
462 print_vline_descr(width, window, vl);
463 wattr_off(window, f->text, NULL);
465 getyx(window, y, x);
467 left = width - x;
469 wattr_on(window, f->trail, NULL);
470 for (i = 0; i < left - off; ++i)
471 waddch(window, ' ');
472 wattr_off(window, f->trail, NULL);
474 wattr_on(window, body_face.right, NULL);
475 for (i = 0; i < off; i++)
476 waddch(window, ' ');
477 wattr_off(window, body_face.right, NULL);
481 static void
482 redraw_tabline(void)
484 struct tab *tab;
485 size_t toskip, ots, tabwidth, space, x;
486 int current, y, truncated, pair;
487 const char *title;
488 char buf[25];
490 x = 0;
492 /* unused, but setted by a getyx */
493 (void)y;
495 tabwidth = sizeof(buf)+1;
496 space = COLS-2;
498 toskip = 0;
499 TAILQ_FOREACH(tab, &tabshead, tabs) {
500 toskip++;
501 if (tab == current_tab)
502 break;
505 if (toskip * tabwidth < space)
506 toskip = 0;
507 else {
508 ots = toskip;
509 toskip--;
510 while (toskip != 0 &&
511 (ots - toskip+1) * tabwidth < space)
512 toskip--;
515 werase(tabline);
516 wattr_on(tabline, tab_face.background, NULL);
517 wprintw(tabline, toskip == 0 ? " " : "<");
518 wattr_off(tabline, tab_face.background, NULL);
520 truncated = 0;
521 TAILQ_FOREACH(tab, &tabshead, tabs) {
522 if (truncated)
523 break;
524 if (toskip != 0) {
525 toskip--;
526 continue;
529 getyx(tabline, y, x);
530 if (x + sizeof(buf)+2 >= (size_t)COLS)
531 truncated = 1;
533 current = tab == current_tab;
535 if (*(title = tab->buffer.page.title) == '\0')
536 title = tab->hist_cur->h;
538 if (tab->flags & TAB_URGENT)
539 strlcpy(buf, "!", sizeof(buf));
540 else
541 strlcpy(buf, " ", sizeof(buf));
543 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
544 /* truncation happens */
545 strlcpy(&buf[sizeof(buf)-4], "...", 4);
546 } else {
547 /* pad with spaces */
548 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
549 /* nop */ ;
552 pair = current ? tab_face.current : tab_face.tab;
553 wattr_on(tabline, pair, NULL);
554 wprintw(tabline, "%s", buf);
555 wattr_off(tabline, pair, NULL);
557 wattr_on(tabline, tab_face.background, NULL);
558 if (TAILQ_NEXT(tab, tabs) != NULL)
559 wprintw(tabline, "┃");
560 wattr_off(tabline, tab_face.background, NULL);
563 wattr_on(tabline, tab_face.background, NULL);
564 for (; x < (size_t)COLS; ++x)
565 waddch(tabline, ' ');
566 if (truncated)
567 mvwprintw(tabline, 0, COLS-1, ">");
568 wattr_off(tabline, tab_face.background, NULL);
571 /*
572 * Compute the first visible line around vl. Try to search forward
573 * until the end of the buffer; if a visible line is not found, search
574 * backward. Return NULL if no viable line was found.
575 */
576 struct vline *
577 adjust_line(struct vline *vl, struct buffer *buffer)
579 struct vline *t;
581 if (vl == NULL)
582 return NULL;
584 if (!(vl->parent->flags & L_HIDDEN))
585 return vl;
587 /* search forward */
588 for (t = vl;
589 t != NULL && t->parent->flags & L_HIDDEN;
590 t = TAILQ_NEXT(t, vlines))
591 ; /* nop */
593 if (t != NULL)
594 return t;
596 /* search backward */
597 for (t = vl;
598 t != NULL && t->parent->flags & L_HIDDEN;
599 t = TAILQ_PREV(t, vhead, vlines))
600 ; /* nop */
602 return t;
605 static void
606 redraw_window(WINDOW *win, int off, int height, int width, struct buffer *buffer)
608 struct vline *vl;
609 int l, onscreen;
611 restore_curs_x(buffer);
613 /*
614 * TODO: ignoring buffer->force_update and always
615 * re-rendering. In theory we can recompute the y position
616 * without a re-render, and optimize here. It's not the only
617 * optimisation possible here, wscrl wolud also be an
618 * interesting one.
619 */
621 again:
622 werase(win);
623 buffer->curs_y = 0;
625 if (TAILQ_EMPTY(&buffer->head))
626 goto end;
628 if (buffer->top_line == NULL)
629 buffer->top_line = TAILQ_FIRST(&buffer->head);
631 buffer->top_line = adjust_line(buffer->top_line, buffer);
632 if (buffer->top_line == NULL)
633 goto end;
635 buffer->current_line = adjust_line(buffer->current_line, buffer);
637 l = 0;
638 onscreen = 0;
639 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
640 if (vl->parent->flags & L_HIDDEN)
641 continue;
643 wmove(win, l, 0);
644 print_vline(off, width, win, vl);
646 if (vl == buffer->current_line)
647 onscreen = 1;
649 if (!onscreen)
650 buffer->curs_y++;
652 l++;
653 if (l == height)
654 break;
657 if (!onscreen) {
658 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
659 if (vl == buffer->current_line)
660 break;
661 if (vl->parent->flags & L_HIDDEN)
662 continue;
663 buffer->line_off++;
664 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
667 if (vl != NULL)
668 goto again;
671 buffer->last_line_off = buffer->line_off;
672 buffer->force_redraw = 0;
673 end:
674 wmove(win, buffer->curs_y, buffer->curs_x);
677 static void
678 redraw_help(void)
680 redraw_window(help, 0, help_lines, help_cols, &helpwin);
683 static void
684 redraw_body(struct tab *tab)
686 static struct tab *last_tab;
688 if (last_tab != tab)
689 tab->buffer.force_redraw =1;
690 last_tab = tab;
692 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
695 static inline char
696 trust_status_char(enum trust_state ts)
698 switch (ts) {
699 case TS_UNKNOWN: return '-';
700 case TS_UNTRUSTED: return '!';
701 case TS_TEMP_TRUSTED: return '!';
702 case TS_TRUSTED: return 'v';
703 case TS_VERIFIED: return 'V';
704 default: return 'X';
708 static void
709 redraw_modeline(struct tab *tab)
711 struct buffer *buffer;
712 double pct;
713 int x, y, max_x, max_y;
714 const char *mode;
715 const char *spin = "-\\|/";
717 buffer = current_buffer();
718 mode = buffer->page.name;
720 werase(modeline);
721 wattr_on(modeline, modeline_face.background, NULL);
722 wmove(modeline, 0, 0);
724 wprintw(modeline, "-%c%c %s ",
725 spin[tab->loading_anim_step],
726 trust_status_char(tab->trust),
727 mode == NULL ? "(none)" : mode);
729 pct = (buffer->line_off + buffer->curs_y) * 100.0
730 / buffer->line_max;
732 if (buffer->line_max <= (size_t)body_lines)
733 wprintw(modeline, "All ");
734 else if (buffer->line_off == 0)
735 wprintw(modeline, "Top ");
736 else if (buffer->line_off + body_lines >= buffer->line_max)
737 wprintw(modeline, "Bottom ");
738 else
739 wprintw(modeline, "%.0f%% ", pct);
741 wprintw(modeline, "%d/%d %s ",
742 buffer->line_off + buffer->curs_y,
743 buffer->line_max,
744 tab->hist_cur->h);
746 getyx(modeline, y, x);
747 getmaxyx(modeline, max_y, max_x);
749 (void)y;
750 (void)max_y;
752 for (; x < max_x; ++x)
753 waddstr(modeline, "-");
755 wattr_off(modeline, modeline_face.background, NULL);
758 static void
759 redraw_minibuffer(void)
761 wattr_on(echoarea, minibuffer_face.background, NULL);
762 werase(echoarea);
764 if (in_minibuffer)
765 do_redraw_minibuffer();
766 else
767 do_redraw_echoarea();
769 if (in_minibuffer == MB_COMPREAD)
770 do_redraw_minibuffer_compl();
772 wattr_off(echoarea, minibuffer_face.background, NULL);
775 static void
776 do_redraw_echoarea(void)
778 struct vline *vl;
780 if (ministate.curmesg != NULL)
781 wprintw(echoarea, "%s", ministate.curmesg);
782 else if (*keybuf != '\0')
783 waddstr(echoarea, keybuf);
784 else {
785 /* If nothing else, show the URL at point */
786 vl = current_tab->buffer.current_line;
787 if (vl != NULL && vl->parent->type == LINE_LINK)
788 waddstr(echoarea, vl->parent->alt);
792 static void
793 do_redraw_minibuffer(void)
795 struct buffer *cmplbuf, *buffer;
796 size_t off_y, off_x = 0;
797 const char *start, *c;
799 cmplbuf = &ministate.compl.buffer;
800 buffer = &ministate.buffer;
801 (void)off_y; /* unused, set by getyx */
803 wmove(echoarea, 0, 0);
805 if (in_minibuffer == MB_COMPREAD)
806 wprintw(echoarea, "(%2d) ",
807 cmplbuf->line_max);
809 wprintw(echoarea, "%s", ministate.prompt);
810 if (ministate.hist_cur != NULL)
811 wprintw(echoarea, "(%zu/%zu) ",
812 ministate.hist_off + 1,
813 ministate.history->len);
815 getyx(echoarea, off_y, off_x);
817 start = ministate.hist_cur != NULL
818 ? ministate.hist_cur->h
819 : ministate.buf;
820 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
821 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
822 start = utf8_next_cp(start);
825 waddstr(echoarea, start);
827 if (ministate.curmesg != NULL)
828 wprintw(echoarea, " [%s]", ministate.curmesg);
830 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
833 static void
834 do_redraw_minibuffer_compl(void)
836 redraw_window(minibuffer, 0, 10, COLS,
837 &ministate.compl.buffer);
840 /*
841 * Place the cursor in the right ncurses window. If soft is 1, use
842 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
843 * wrefresh.
844 */
845 static void
846 place_cursor(int soft)
848 int (*touch)(WINDOW *);
850 if (soft)
851 touch = wnoutrefresh;
852 else
853 touch = wrefresh;
855 if (in_minibuffer) {
856 if (side_window)
857 touch(help);
858 touch(body);
859 touch(echoarea);
860 } else if (in_side_window) {
861 touch(body);
862 touch(echoarea);
863 touch(help);
864 } else {
865 if (side_window)
866 touch(help);
867 touch(echoarea);
868 touch(body);
872 static void
873 redraw_tab(struct tab *tab)
875 if (too_small)
876 return;
878 if (side_window) {
879 redraw_help();
880 wnoutrefresh(help);
883 redraw_tabline();
884 redraw_body(tab);
885 redraw_modeline(tab);
886 redraw_minibuffer();
888 wnoutrefresh(tabline);
889 wnoutrefresh(modeline);
891 if (in_minibuffer == MB_COMPREAD)
892 wnoutrefresh(minibuffer);
894 place_cursor(1);
896 doupdate();
898 if (set_title)
899 dprintf(1, "\033]0;%s - Telescope\a",
900 current_tab->buffer.page.title);
903 void
904 start_loading_anim(struct tab *tab)
906 if (tab->loading_anim)
907 return;
908 tab->loading_anim = 1;
909 evtimer_set(&tab->loadingev, update_loading_anim, tab);
910 evtimer_add(&tab->loadingev, &loadingev_timer);
913 static void
914 update_loading_anim(int fd, short ev, void *d)
916 struct tab *tab = d;
918 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
920 if (tab == current_tab) {
921 redraw_modeline(tab);
922 wrefresh(modeline);
923 wrefresh(body);
924 if (in_minibuffer)
925 wrefresh(echoarea);
928 evtimer_add(&tab->loadingev, &loadingev_timer);
931 static void
932 stop_loading_anim(struct tab *tab)
934 if (!tab->loading_anim)
935 return;
936 evtimer_del(&tab->loadingev);
937 tab->loading_anim = 0;
938 tab->loading_anim_step = 0;
940 if (tab != current_tab)
941 return;
943 redraw_modeline(tab);
945 wrefresh(modeline);
946 wrefresh(body);
947 if (in_minibuffer)
948 wrefresh(echoarea);
951 void
952 load_url_in_tab(struct tab *tab, const char *url, const char *base)
954 if (!operating) {
955 load_url(tab, url, base);
956 return;
959 message("Loading %s...", url);
960 start_loading_anim(tab);
961 load_url(tab, url, base);
963 redraw_tab(tab);
966 void
967 switch_to_tab(struct tab *tab)
969 current_tab = tab;
970 tab->flags &= ~TAB_URGENT;
972 if (operating && tab->flags & TAB_LAZY)
973 load_url_in_tab(tab, tab->hist_cur->h, NULL);
976 unsigned int
977 tab_new_id(void)
979 return tab_counter++;
982 struct tab *
983 new_tab(const char *url, const char *base)
985 struct tab *tab;
987 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
988 event_loopbreak();
989 return NULL;
991 tab->fd = -1;
993 TAILQ_INIT(&tab->hist.head);
995 TAILQ_INIT(&tab->buffer.head);
997 tab->id = tab_new_id();
998 if (!operating)
999 tab->flags |= TAB_LAZY;
1000 switch_to_tab(tab);
1002 if (TAILQ_EMPTY(&tabshead))
1003 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1004 else
1005 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1007 load_url_in_tab(tab, url, base);
1008 return tab;
1011 int
1012 ui_print_colors(void)
1014 int colors = 0, pairs = 0, can_change = 0;
1015 int columns = 16, lines, color, i, j;
1017 initscr();
1018 if (has_colors()) {
1019 start_color();
1020 use_default_colors();
1022 colors = COLORS;
1023 pairs = COLOR_PAIRS;
1024 can_change = can_change_color();
1026 endwin();
1028 printf("Term info:\n");
1029 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1030 getenv("TERM"), colors, pairs, can_change);
1031 printf("\n");
1033 if (colors == 0) {
1034 printf("No color support\n");
1035 return 0;
1038 printf("Available colors:\n\n");
1039 lines = (colors - 1) / columns + 1;
1040 color = 0;
1041 for (i = 0; i < lines; ++i) {
1042 for (j = 0; j < columns; ++j, ++color) {
1043 printf("\033[0;38;5;%dm %03d", color, color);
1045 printf("\n");
1048 printf("\033[0m");
1049 fflush(stdout);
1050 return 0;
1053 int
1054 ui_init()
1056 setlocale(LC_ALL, "");
1058 minibuffer_init();
1060 /* initialize help window */
1061 TAILQ_INIT(&helpwin.head);
1063 base_map = &global_map;
1064 current_map = &global_map;
1066 initscr();
1068 if (enable_colors) {
1069 if (has_colors()) {
1070 start_color();
1071 use_default_colors();
1072 } else
1073 enable_colors = 0;
1076 config_apply_style();
1078 raw();
1079 noecho();
1080 nonl();
1081 intrflush(stdscr, FALSE);
1083 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1084 return 0;
1085 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1086 return 0;
1087 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1088 return 0;
1089 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1090 return 0;
1091 if ((minibuffer = newwin(1, COLS, LINES-1, 0)) == NULL)
1092 return 0;
1093 if ((help = newwin(1, 1, 1, 0)) == NULL)
1094 return 0;
1096 body_lines = LINES-3;
1097 body_cols = COLS;
1099 wbkgd(body, body_face.body);
1100 wbkgd(echoarea, minibuffer_face.background);
1102 update_x_offset();
1104 keypad(body, TRUE);
1105 scrollok(body, FALSE);
1107 /* non-blocking input */
1108 wtimeout(body, 0);
1109 wtimeout(help, 0);
1111 mvwprintw(body, 0, 0, "");
1113 evtimer_set(&resizeev, handle_resize, NULL);
1115 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1116 event_add(&stdioev, NULL);
1118 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1119 signal_add(&winchev, NULL);
1121 return 1;
1124 void
1125 ui_main_loop(void)
1127 operating = 1;
1128 switch_to_tab(current_tab);
1129 redraw_tab(current_tab);
1131 event_dispatch();
1134 void
1135 ui_on_tab_loaded(struct tab *tab)
1137 stop_loading_anim(tab);
1138 message("Loaded %s", tab->hist_cur->h);
1140 redraw_tabline();
1141 wrefresh(tabline);
1142 place_cursor(0);
1145 void
1146 ui_on_tab_refresh(struct tab *tab)
1148 wrap_page(&tab->buffer, body_cols);
1149 if (tab == current_tab)
1150 redraw_tab(tab);
1151 else
1152 tab->flags |= TAB_URGENT;
1155 const char *
1156 ui_keyname(int k)
1158 return keyname(k);
1161 void
1162 ui_toggle_side_window(void)
1164 side_window = !side_window;
1165 if (side_window)
1166 recompute_help();
1167 else
1168 in_side_window = 0;
1171 * ugly hack, but otherwise the window doesn't get updated
1172 * until I call rearrange_windows a second time (e.g. via
1173 * C-l). I will be happy to know why something like this is
1174 * needed.
1176 rearrange_windows();
1177 rearrange_windows();
1180 void
1181 ui_schedule_redraw(void)
1183 should_rearrange_windows = 1;
1186 void
1187 ui_require_input(struct tab *tab, int hide)
1189 /* TODO: hard-switching to another tab is ugly */
1190 switch_to_tab(tab);
1192 enter_minibuffer(sensible_self_insert, ir_select, exit_minibuffer,
1193 &ir_history, NULL, NULL);
1194 strlcpy(ministate.prompt, "Input required: ",
1195 sizeof(ministate.prompt));
1196 redraw_tab(tab);
1199 void
1200 ui_after_message_hook(void)
1202 redraw_minibuffer();
1203 place_cursor(0);
1206 void
1207 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1208 struct tab *data)
1210 yornp(prompt, fn, data);
1211 redraw_tab(current_tab);
1214 void
1215 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1216 struct tab *data)
1218 minibuffer_read(prompt, fn, data);
1219 redraw_tab(current_tab);
1222 void
1223 ui_other_window(void)
1225 if (side_window)
1226 in_side_window = !in_side_window;
1227 else
1228 message("No other window to select");
1231 void
1232 ui_suspend(void)
1234 endwin();
1236 kill(getpid(), SIGSTOP);
1238 refresh();
1239 clear();
1240 rearrange_windows();
1243 void
1244 ui_end(void)
1246 endwin();