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 "session.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 static int should_rearrange_windows;
77 static int show_tab_bar;
78 static int too_small;
79 static int x_offset;
81 struct thiskey thiskey;
82 struct tab *current_tab;
84 static struct event resizeev;
85 static struct timeval resize_timer = { 0, 250000 };
87 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
89 int body_lines, body_cols;
91 static WINDOW *help;
92 /* not static so we can see them from help.c */
93 struct buffer helpwin;
94 int help_lines, help_cols;
96 static int side_window;
97 static int in_side_window;
99 static struct timeval loadingev_timer = { 0, 250000 };
101 static char keybuf[64];
103 /* XXX: don't forget to init these in main() */
104 struct kmap global_map,
105 minibuffer_map,
106 *current_map,
107 *base_map;
109 static inline void
110 update_x_offset(void)
112 if (olivetti_mode && fill_column < body_cols)
113 x_offset = (body_cols - fill_column)/2;
114 else
115 x_offset = 0;
118 void
119 save_excursion(struct excursion *place, struct buffer *buffer)
121 place->curs_x = buffer->curs_x;
122 place->curs_y = buffer->curs_y;
123 place->line_off = buffer->line_off;
124 place->top_line = buffer->top_line;
125 place->current_line = buffer->current_line;
126 place->cpoff = buffer->cpoff;
129 void
130 restore_excursion(struct excursion *place, struct buffer *buffer)
132 buffer->curs_x = place->curs_x;
133 buffer->curs_y = place->curs_y;
134 buffer->line_off = place->line_off;
135 buffer->top_line = place->top_line;
136 buffer->current_line = place->current_line;
137 buffer->cpoff = place->cpoff;
140 static void
141 restore_curs_x(struct buffer *buffer)
143 struct vline *vl;
144 const char *prfx, *text;
146 vl = buffer->current_line;
147 if (vl == NULL || vl->line == NULL)
148 buffer->curs_x = buffer->cpoff = 0;
149 else if (vl->parent->data != NULL) {
150 text = vl->parent->data;
151 buffer->curs_x = utf8_snwidth(text + 1, buffer->cpoff) + 1;
152 } else
153 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
155 buffer->curs_x += x_offset;
157 if (vl == NULL)
158 return;
160 if (vl->parent->data != NULL)
161 buffer->curs_x += utf8_swidth_between(vl->parent->line,
162 vl->parent->data);
163 else {
164 prfx = line_prefixes[vl->parent->type].prfx1;
165 buffer->curs_x += utf8_swidth(prfx);
169 void
170 global_key_unbound(void)
172 message("%s is undefined", keybuf);
175 struct buffer *
176 current_buffer(void)
178 if (in_minibuffer)
179 return &ministate.buffer;
180 if (in_side_window)
181 return &helpwin;
182 return &current_tab->buffer;
185 static int
186 readkey(void)
188 uint32_t state = 0;
190 if ((thiskey.key = wgetch(body)) == ERR)
191 return 0;
193 thiskey.meta = thiskey.key == 27;
194 if (thiskey.meta) {
195 thiskey.key = wgetch(body);
196 if (thiskey.key == ERR || thiskey.key == 27) {
197 thiskey.meta = 0;
198 thiskey.key = 27;
202 thiskey.cp = 0;
204 if ((unsigned int)thiskey.key >= UINT8_MAX)
205 return 1;
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;
216 return 1;
219 static void
220 dispatch_stdio(int fd, short ev, void *d)
222 struct keymap *k;
223 const char *keyname;
224 char tmp[5] = {0};
226 /* TODO: schedule a redraw? */
227 if (too_small)
228 return;
230 if (!readkey())
231 return;
233 if (keybuf[0] != '\0')
234 strlcat(keybuf, " ", sizeof(keybuf));
235 if (thiskey.meta)
236 strlcat(keybuf, "M-", sizeof(keybuf));
237 if (thiskey.cp != 0) {
238 utf8_encode(thiskey.cp, tmp);
239 strlcat(keybuf, tmp, sizeof(keybuf));
240 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
241 strlcat(keybuf, keyname, sizeof(keybuf));
242 } else {
243 tmp[0] = thiskey.key;
244 strlcat(keybuf, tmp, sizeof(keybuf));
247 TAILQ_FOREACH(k, &current_map->m, keymaps) {
248 if (k->meta == thiskey.meta &&
249 k->key == thiskey.key) {
250 if (k->fn == NULL)
251 current_map = &k->map;
252 else {
253 current_map = base_map;
254 strlcpy(keybuf, "", sizeof(keybuf));
255 k->fn(current_buffer());
257 goto done;
261 if (current_map->unhandled_input != NULL)
262 current_map->unhandled_input();
263 else
264 global_key_unbound();
266 strlcpy(keybuf, "", sizeof(keybuf));
267 current_map = base_map;
269 done:
270 if (side_window)
271 recompute_help();
273 if (should_rearrange_windows)
274 rearrange_windows();
275 redraw_tab(current_tab);
278 static void
279 handle_resize(int sig, short ev, void *d)
281 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
282 event_del(&resizeev);
284 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
285 evtimer_add(&resizeev, &resize_timer);
288 static void
289 handle_resize_nodelay(int s, short ev, void *d)
291 endwin();
292 refresh();
293 clear();
295 rearrange_windows();
298 static inline int
299 should_show_tab_bar(void)
301 if (tab_bar_show == -1)
302 return 0;
303 if (tab_bar_show == 0)
304 return 1;
306 return TAILQ_NEXT(TAILQ_FIRST(&tabshead), tabs) != NULL;
309 static void
310 rearrange_windows(void)
312 int lines;
314 should_rearrange_windows = 0;
315 show_tab_bar = should_show_tab_bar();
317 lines = LINES;
319 if ((too_small = lines < 15)) {
320 erase();
321 printw("Window too small.");
322 refresh();
323 return;
326 /* move and resize the windows, in reverse order! */
328 if (in_minibuffer == MB_COMPREAD) {
329 mvwin(minibuffer, lines-10, 0);
330 wresize(minibuffer, 10, COLS);
331 lines -= 10;
333 wrap_page(&ministate.compl.buffer, COLS);
336 mvwin(echoarea, --lines, 0);
337 wresize(echoarea, 1, COLS);
339 mvwin(modeline, --lines, 0);
340 wresize(modeline, 1, COLS);
342 body_lines = show_tab_bar ? --lines : lines;
343 body_cols = COLS;
345 /*
346 * Here we make the assumption that show_tab_bar is either 0
347 * or 1, and reuse that as argument to mvwin.
348 */
349 if (side_window) {
350 help_cols = 0.3 * COLS;
351 help_lines = lines;
352 mvwin(help, show_tab_bar, 0);
353 wresize(help, help_lines, help_cols);
355 wrap_page(&helpwin, help_cols);
357 body_cols = COLS - help_cols - 1;
358 mvwin(body, show_tab_bar, help_cols);
359 } else
360 mvwin(body, show_tab_bar, 0);
362 update_x_offset();
363 wresize(body, body_lines, body_cols);
365 if (show_tab_bar)
366 wresize(tabline, 1, COLS);
368 wrap_page(&current_tab->buffer, body_cols);
369 redraw_tab(current_tab);
372 static void
373 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
374 const char **prfx_ret, const char **text_ret)
376 int type, i, cont, width;
377 char *space, *t;
379 if ((*text_ret = vl->line) == NULL)
380 *text_ret = "";
382 cont = vl->flags & L_CONTINUATION;
383 type = vl->parent->type;
384 if (!cont)
385 *prfx_ret = line_prefixes[type].prfx1;
386 else
387 *prfx_ret = line_prefixes[type].prfx2;
389 space = vl->parent->data;
390 if (!emojify_link || type != LINE_LINK || space == NULL)
391 return;
393 if (cont) {
394 memset(buf, 0, len);
395 width = utf8_swidth_between(vl->parent->line, space);
396 for (i = 0; i < width + 1; ++i)
397 strlcat(buf, " ", len);
398 } else {
399 strlcpy(buf, *text_ret, len);
400 if ((t = strchr(buf, ' ')) != NULL)
401 *t = '\0';
402 strlcat(buf, " ", len);
404 /* skip the emoji */
405 *text_ret += (space - vl->parent->line) + 1;
408 *prfx_ret = buf;
411 static inline void
412 print_vline_descr(int width, WINDOW *window, struct vline *vl)
414 int x, y, goal;
416 if (vl->parent->type != LINE_COMPL &&
417 vl->parent->type != LINE_COMPL_CURRENT &&
418 vl->parent->type != LINE_HELP)
419 return;
421 if (vl->parent->alt == NULL)
422 return;
424 (void)y;
425 getyx(window, y, x);
427 if (vl->parent->type == LINE_HELP)
428 goal = 8;
429 else
430 goal = width/2;
432 if (goal <= x)
433 wprintw(window, " ");
434 for (; goal > x; ++x)
435 wprintw(window, " ");
437 wprintw(window, "%s", vl->parent->alt);
440 static inline void
441 print_line_break(int width, WINDOW *window)
443 int x, y, len;
444 const char *sep = "-*-*-";
446 len = utf8_swidth(sep);
448 getyx(window, y, x);
449 mvwprintw(window, y, width/2 - len, sep);
452 /*
453 * Core part of the rendering. It prints a vline starting from the
454 * current cursor position. Printing a vline consists of skipping
455 * `off' columns (for olivetti-mode), print the correct prefix (which
456 * may be the emoji in case of emojified links-lines), printing the
457 * text itself, filling until width - off and filling off columns
458 * again.
459 */
460 static void
461 print_vline(int off, int width, WINDOW *window, struct vline *vl)
463 /*
464 * Believe me or not, I've seen emoji ten code points long!
465 * That means, to stay large, 4*10 bytes + NUL.
466 */
467 char emojibuf[41] = {0};
468 const char *text, *prfx;
469 struct line_face *f;
470 int i, left, x, y;
472 f = &line_faces[vl->parent->type];
474 /* unused, set by getyx */
475 (void)y;
477 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
479 wattr_on(window, body_face.left, NULL);
480 for (i = 0; i < off; i++)
481 waddch(window, ' ');
482 wattr_off(window, body_face.left, NULL);
484 if (vl->parent->type == LINE_BREAK) {
485 print_line_break(width, window);
486 } else {
487 wattr_on(window, f->prefix, NULL);
488 wprintw(window, "%s", prfx);
489 wattr_off(window, f->prefix, NULL);
492 wattr_on(window, f->text, NULL);
493 wprintw(window, "%s", text);
494 print_vline_descr(width, window, vl);
495 wattr_off(window, f->text, NULL);
497 getyx(window, y, x);
499 left = width - x;
501 wattr_on(window, f->trail, NULL);
502 for (i = 0; i < left - off; ++i)
503 waddch(window, ' ');
504 wattr_off(window, f->trail, NULL);
506 wattr_on(window, body_face.right, NULL);
507 for (i = 0; i < off; i++)
508 waddch(window, ' ');
509 wattr_off(window, body_face.right, NULL);
513 static void
514 redraw_tabline(void)
516 struct tab *tab;
517 size_t toskip, ots, tabwidth, space, x;
518 int current, y, truncated, pair;
519 const char *title;
520 char buf[25];
522 x = 0;
524 /* unused, but setted by a getyx */
525 (void)y;
527 tabwidth = sizeof(buf)+1;
528 space = COLS-2;
530 toskip = 0;
531 TAILQ_FOREACH(tab, &tabshead, tabs) {
532 toskip++;
533 if (tab == current_tab)
534 break;
537 if (toskip * tabwidth < space)
538 toskip = 0;
539 else {
540 ots = toskip;
541 toskip--;
542 while (toskip != 0 &&
543 (ots - toskip+1) * tabwidth < space)
544 toskip--;
547 werase(tabline);
548 wattr_on(tabline, tab_face.background, NULL);
549 wprintw(tabline, toskip == 0 ? " " : "<");
550 wattr_off(tabline, tab_face.background, NULL);
552 truncated = 0;
553 TAILQ_FOREACH(tab, &tabshead, tabs) {
554 if (truncated)
555 break;
556 if (toskip != 0) {
557 toskip--;
558 continue;
561 getyx(tabline, y, x);
562 if (x + sizeof(buf)+2 >= (size_t)COLS)
563 truncated = 1;
565 current = tab == current_tab;
567 if (*(title = tab->buffer.page.title) == '\0')
568 title = tab->hist_cur->h;
570 if (tab->flags & TAB_URGENT)
571 strlcpy(buf, "!", sizeof(buf));
572 else
573 strlcpy(buf, " ", sizeof(buf));
575 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
576 /* truncation happens */
577 strlcpy(&buf[sizeof(buf)-4], "...", 4);
578 } else {
579 /* pad with spaces */
580 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
581 /* nop */ ;
584 pair = current ? tab_face.current : tab_face.tab;
585 wattr_on(tabline, pair, NULL);
586 wprintw(tabline, "%s", buf);
587 wattr_off(tabline, pair, NULL);
589 wattr_on(tabline, tab_face.background, NULL);
590 if (TAILQ_NEXT(tab, tabs) != NULL)
591 wprintw(tabline, "┃");
592 wattr_off(tabline, tab_face.background, NULL);
595 wattr_on(tabline, tab_face.background, NULL);
596 for (; x < (size_t)COLS; ++x)
597 waddch(tabline, ' ');
598 if (truncated)
599 mvwprintw(tabline, 0, COLS-1, ">");
600 wattr_off(tabline, tab_face.background, NULL);
603 /*
604 * Compute the first visible line around vl. Try to search forward
605 * until the end of the buffer; if a visible line is not found, search
606 * backward. Return NULL if no viable line was found.
607 */
608 struct vline *
609 adjust_line(struct vline *vl, struct buffer *buffer)
611 struct vline *t;
613 if (vl == NULL)
614 return NULL;
616 if (!(vl->parent->flags & L_HIDDEN))
617 return vl;
619 /* search forward */
620 for (t = vl;
621 t != NULL && t->parent->flags & L_HIDDEN;
622 t = TAILQ_NEXT(t, vlines))
623 ; /* nop */
625 if (t != NULL)
626 return t;
628 /* search backward */
629 for (t = vl;
630 t != NULL && t->parent->flags & L_HIDDEN;
631 t = TAILQ_PREV(t, vhead, vlines))
632 ; /* nop */
634 return t;
637 static void
638 redraw_window(WINDOW *win, int off, int height, int width,
639 struct buffer *buffer)
641 struct vline *vl;
642 int l, onscreen;
644 restore_curs_x(buffer);
646 /*
647 * TODO: ignoring buffer->force_update and always
648 * re-rendering. In theory we can recompute the y position
649 * without a re-render, and optimize here. It's not the only
650 * optimisation possible here, wscrl wolud also be an
651 * interesting one.
652 */
654 again:
655 werase(win);
656 buffer->curs_y = 0;
658 if (TAILQ_EMPTY(&buffer->head))
659 goto end;
661 if (buffer->top_line == NULL)
662 buffer->top_line = TAILQ_FIRST(&buffer->head);
664 buffer->top_line = adjust_line(buffer->top_line, buffer);
665 if (buffer->top_line == NULL)
666 goto end;
668 buffer->current_line = adjust_line(buffer->current_line, buffer);
670 l = 0;
671 onscreen = 0;
672 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
673 if (vl->parent->flags & L_HIDDEN)
674 continue;
676 wmove(win, l, 0);
677 print_vline(off, width, win, vl);
679 if (vl == buffer->current_line)
680 onscreen = 1;
682 if (!onscreen)
683 buffer->curs_y++;
685 l++;
686 if (l == height)
687 break;
690 if (!onscreen) {
691 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
692 if (vl == buffer->current_line)
693 break;
694 if (vl->parent->flags & L_HIDDEN)
695 continue;
696 buffer->line_off++;
697 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
700 if (vl != NULL)
701 goto again;
704 buffer->last_line_off = buffer->line_off;
705 buffer->force_redraw = 0;
706 end:
707 wmove(win, buffer->curs_y, buffer->curs_x);
710 static void
711 redraw_help(void)
713 redraw_window(help, 0, help_lines, help_cols, &helpwin);
716 static void
717 redraw_body(struct tab *tab)
719 static struct tab *last_tab;
721 if (last_tab != tab)
722 tab->buffer.force_redraw =1;
723 last_tab = tab;
725 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
728 static inline char
729 trust_status_char(enum trust_state ts)
731 switch (ts) {
732 case TS_UNKNOWN: return '-';
733 case TS_UNTRUSTED: return '!';
734 case TS_TEMP_TRUSTED: return '!';
735 case TS_TRUSTED: return 'v';
736 case TS_VERIFIED: return 'V';
737 default: return 'X';
741 static void
742 redraw_modeline(struct tab *tab)
744 struct buffer *buffer;
745 double pct;
746 int x, y, max_x, max_y;
747 const char *mode;
748 const char *spin = "-\\|/";
750 buffer = current_buffer();
751 mode = buffer->page.name;
753 werase(modeline);
754 wattr_on(modeline, modeline_face.background, NULL);
755 wmove(modeline, 0, 0);
757 wprintw(modeline, "-%c%c %s ",
758 spin[tab->loading_anim_step],
759 trust_status_char(tab->trust),
760 mode == NULL ? "(none)" : mode);
762 pct = (buffer->line_off + buffer->curs_y) * 100.0
763 / buffer->line_max;
765 if (buffer->line_max <= (size_t)body_lines)
766 wprintw(modeline, "All ");
767 else if (buffer->line_off == 0)
768 wprintw(modeline, "Top ");
769 else if (buffer->line_off + body_lines >= buffer->line_max)
770 wprintw(modeline, "Bottom ");
771 else
772 wprintw(modeline, "%.0f%% ", pct);
774 wprintw(modeline, "%d/%d %s ",
775 buffer->line_off + buffer->curs_y,
776 buffer->line_max,
777 tab->hist_cur->h);
779 getyx(modeline, y, x);
780 getmaxyx(modeline, max_y, max_x);
782 (void)y;
783 (void)max_y;
785 for (; x < max_x; ++x)
786 waddstr(modeline, "-");
788 wattr_off(modeline, modeline_face.background, NULL);
791 static void
792 redraw_minibuffer(void)
794 wattr_on(echoarea, minibuffer_face.background, NULL);
795 werase(echoarea);
797 if (in_minibuffer)
798 do_redraw_minibuffer();
799 else
800 do_redraw_echoarea();
802 if (in_minibuffer == MB_COMPREAD)
803 do_redraw_minibuffer_compl();
805 wattr_off(echoarea, minibuffer_face.background, NULL);
808 static void
809 do_redraw_echoarea(void)
811 struct vline *vl;
813 if (ministate.curmesg != NULL)
814 wprintw(echoarea, "%s", ministate.curmesg);
815 else if (*keybuf != '\0')
816 waddstr(echoarea, keybuf);
817 else {
818 /* If nothing else, show the URL at point */
819 vl = current_tab->buffer.current_line;
820 if (vl != NULL && vl->parent->type == LINE_LINK)
821 waddstr(echoarea, vl->parent->alt);
825 static void
826 do_redraw_minibuffer(void)
828 struct buffer *cmplbuf, *buffer;
829 size_t off_y, off_x = 0;
830 const char *start, *c;
832 cmplbuf = &ministate.compl.buffer;
833 buffer = &ministate.buffer;
834 (void)off_y; /* unused, set by getyx */
836 wmove(echoarea, 0, 0);
838 if (in_minibuffer == MB_COMPREAD)
839 wprintw(echoarea, "(%2d) ",
840 cmplbuf->line_max);
842 wprintw(echoarea, "%s", ministate.prompt);
843 if (ministate.hist_cur != NULL)
844 wprintw(echoarea, "(%zu/%zu) ",
845 ministate.hist_off + 1,
846 ministate.history->len);
848 getyx(echoarea, off_y, off_x);
850 start = ministate.hist_cur != NULL
851 ? ministate.hist_cur->h
852 : ministate.buf;
853 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
854 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
855 start = utf8_next_cp(start);
858 waddstr(echoarea, start);
860 if (ministate.curmesg != NULL)
861 wprintw(echoarea, " [%s]", ministate.curmesg);
863 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
866 static void
867 do_redraw_minibuffer_compl(void)
869 redraw_window(minibuffer, 0, 10, COLS,
870 &ministate.compl.buffer);
873 /*
874 * Place the cursor in the right ncurses window. If soft is 1, use
875 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
876 * wrefresh.
877 */
878 static void
879 place_cursor(int soft)
881 int (*touch)(WINDOW *);
883 if (soft)
884 touch = wnoutrefresh;
885 else
886 touch = wrefresh;
888 if (in_minibuffer) {
889 if (side_window)
890 touch(help);
891 touch(body);
892 touch(echoarea);
893 } else if (in_side_window) {
894 touch(body);
895 touch(echoarea);
896 touch(help);
897 } else {
898 if (side_window)
899 touch(help);
900 touch(echoarea);
901 touch(body);
905 static void
906 redraw_tab(struct tab *tab)
908 if (too_small)
909 return;
911 if (side_window) {
912 redraw_help();
913 wnoutrefresh(help);
916 if (show_tab_bar)
917 redraw_tabline();
919 redraw_body(tab);
920 redraw_modeline(tab);
921 redraw_minibuffer();
923 wnoutrefresh(tabline);
924 wnoutrefresh(modeline);
926 if (in_minibuffer == MB_COMPREAD)
927 wnoutrefresh(minibuffer);
929 place_cursor(1);
931 doupdate();
933 if (set_title)
934 dprintf(1, "\033]2;%s - Telescope\a",
935 current_tab->buffer.page.title);
938 void
939 start_loading_anim(struct tab *tab)
941 if (tab->loading_anim)
942 return;
943 tab->loading_anim = 1;
944 evtimer_set(&tab->loadingev, update_loading_anim, tab);
945 evtimer_add(&tab->loadingev, &loadingev_timer);
948 static void
949 update_loading_anim(int fd, short ev, void *d)
951 struct tab *tab = d;
953 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
955 if (tab == current_tab) {
956 redraw_modeline(tab);
957 wrefresh(modeline);
958 wrefresh(body);
959 if (in_minibuffer)
960 wrefresh(echoarea);
963 evtimer_add(&tab->loadingev, &loadingev_timer);
966 static void
967 stop_loading_anim(struct tab *tab)
969 if (!tab->loading_anim)
970 return;
971 evtimer_del(&tab->loadingev);
972 tab->loading_anim = 0;
973 tab->loading_anim_step = 0;
975 if (tab != current_tab)
976 return;
978 redraw_modeline(tab);
980 wrefresh(modeline);
981 wrefresh(body);
982 if (in_minibuffer)
983 wrefresh(echoarea);
986 int
987 ui_print_colors(void)
989 int colors = 0, pairs = 0, can_change = 0;
990 int columns = 16, lines, color, i, j;
992 initscr();
993 if (has_colors()) {
994 start_color();
995 use_default_colors();
997 colors = COLORS;
998 pairs = COLOR_PAIRS;
999 can_change = can_change_color();
1001 endwin();
1003 printf("Term info:\n");
1004 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1005 getenv("TERM"), colors, pairs, can_change);
1006 printf("\n");
1008 if (colors == 0) {
1009 printf("No color support\n");
1010 return 0;
1013 printf("Available colors:\n\n");
1014 lines = (colors - 1) / columns + 1;
1015 color = 0;
1016 for (i = 0; i < lines; ++i) {
1017 for (j = 0; j < columns; ++j, ++color) {
1018 printf("\033[0;38;5;%dm %03d", color, color);
1020 printf("\n");
1023 printf("\033[0m");
1024 fflush(stdout);
1025 return 0;
1028 int
1029 ui_init()
1031 setlocale(LC_ALL, "");
1033 if (TAILQ_EMPTY(&global_map.m)) {
1034 fprintf(stderr, "no keys defined!\n");
1035 return 0;
1038 minibuffer_init();
1040 /* initialize help window */
1041 TAILQ_INIT(&helpwin.head);
1042 TAILQ_INIT(&helpwin.page.head);
1044 base_map = &global_map;
1045 current_map = &global_map;
1047 initscr();
1049 if (enable_colors) {
1050 if (has_colors()) {
1051 start_color();
1052 use_default_colors();
1053 } else
1054 enable_colors = 0;
1057 config_apply_style();
1059 raw();
1060 noecho();
1061 nonl();
1062 intrflush(stdscr, FALSE);
1064 if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1065 return 0;
1066 if ((body = newwin(1, 1, 0, 0)) == NULL)
1067 return 0;
1068 if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1069 return 0;
1070 if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1071 return 0;
1072 if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1073 return 0;
1074 if ((help = newwin(1, 1, 0, 0)) == NULL)
1075 return 0;
1077 wbkgd(body, body_face.body);
1078 wbkgd(echoarea, minibuffer_face.background);
1080 update_x_offset();
1082 keypad(body, TRUE);
1083 scrollok(body, FALSE);
1085 /* non-blocking input */
1086 wtimeout(body, 0);
1087 wtimeout(help, 0);
1089 mvwprintw(body, 0, 0, "");
1091 evtimer_set(&resizeev, handle_resize, NULL);
1093 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1094 event_add(&stdioev, NULL);
1096 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1097 signal_add(&winchev, NULL);
1099 return 1;
1102 void
1103 ui_main_loop(void)
1105 switch_to_tab(current_tab);
1106 rearrange_windows();
1108 event_dispatch();
1111 void
1112 ui_on_tab_loaded(struct tab *tab)
1114 stop_loading_anim(tab);
1115 message("Loaded %s", tab->hist_cur->h);
1117 if (show_tab_bar)
1118 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();
1146 else
1147 in_side_window = 0;
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, int proto)
1168 void (*fn)(void);
1170 if (proto == PROTO_GEMINI)
1171 fn = ir_select_gemini;
1172 else if (proto == PROTO_GOPHER)
1173 fn = ir_select_gopher;
1174 else
1175 abort();
1177 /* TODO: hard-switching to another tab is ugly */
1178 switch_to_tab(tab);
1180 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1181 &ir_history, NULL, NULL);
1182 strlcpy(ministate.prompt, "Input required: ",
1183 sizeof(ministate.prompt));
1184 redraw_tab(tab);
1187 void
1188 ui_after_message_hook(void)
1190 redraw_minibuffer();
1191 place_cursor(0);
1194 void
1195 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1196 struct tab *data)
1198 yornp(prompt, fn, data);
1199 redraw_tab(current_tab);
1202 void
1203 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1204 struct tab *data, const char *input)
1206 minibuffer_read(prompt, fn, data);
1208 if (input != NULL) {
1209 strlcpy(ministate.buf, input, sizeof(ministate.buf));
1210 cmd_move_end_of_line(&ministate.buffer);
1213 redraw_tab(current_tab);
1216 void
1217 ui_other_window(void)
1219 if (side_window)
1220 in_side_window = !in_side_window;
1221 else
1222 message("No other window to select");
1225 void
1226 ui_suspend(void)
1228 endwin();
1230 kill(getpid(), SIGSTOP);
1232 refresh();
1233 clear();
1234 rearrange_windows();
1237 void
1238 ui_end(void)
1240 endwin();