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_download(void);
65 static void redraw_help(void);
66 static void redraw_body(struct tab*);
67 static void redraw_modeline(struct tab*);
68 static void redraw_minibuffer(void);
69 static void do_redraw_echoarea(void);
70 static void do_redraw_minibuffer(void);
71 static void do_redraw_minibuffer_compl(void);
72 static void place_cursor(int);
73 static void redraw_tab(struct tab*);
74 static void update_loading_anim(int, short, void*);
75 static void stop_loading_anim(struct tab*);
77 static int should_rearrange_windows;
78 static int show_tab_bar;
79 static int too_small;
80 static int x_offset;
82 struct thiskey thiskey;
83 struct tab *current_tab;
85 static struct event resizeev;
86 static struct timeval resize_timer = { 0, 250000 };
88 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
90 int body_lines, body_cols;
92 static WINDOW *help;
93 /* not static so we can see them from help.c */
94 struct buffer helpwin;
95 int help_lines, help_cols;
97 static WINDOW *download;
98 /* not static so we can see them from download.c */
99 struct buffer downloadwin;
100 int download_lines;
102 static int side_window;
103 static int in_side_window;
105 static struct timeval loadingev_timer = { 0, 250000 };
107 static char keybuf[64];
109 /* XXX: don't forget to init these in main() */
110 struct kmap global_map,
111 minibuffer_map,
112 *current_map,
113 *base_map;
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 /* small hack: don't olivetti-mode the download pane */
162 if (buffer != &downloadwin)
163 buffer->curs_x += x_offset;
165 if (vl == NULL)
166 return;
168 if (vl->parent->data != NULL)
169 buffer->curs_x += utf8_swidth_between(vl->parent->line,
170 vl->parent->data);
171 else {
172 prfx = line_prefixes[vl->parent->type].prfx1;
173 buffer->curs_x += utf8_swidth(prfx);
177 void
178 global_key_unbound(void)
180 message("%s is undefined", keybuf);
183 struct buffer *
184 current_buffer(void)
186 if (in_minibuffer)
187 return &ministate.buffer;
188 if (in_side_window & SIDE_WINDOW_LEFT)
189 return &helpwin;
190 if (in_side_window & SIDE_WINDOW_BOTTOM)
191 return &downloadwin;
192 return &current_tab->buffer;
195 static int
196 readkey(void)
198 uint32_t state = 0;
200 if ((thiskey.key = wgetch(body)) == ERR)
201 return 0;
203 thiskey.meta = thiskey.key == 27;
204 if (thiskey.meta) {
205 thiskey.key = wgetch(body);
206 if (thiskey.key == ERR || thiskey.key == 27) {
207 thiskey.meta = 0;
208 thiskey.key = 27;
212 thiskey.cp = 0;
214 if ((unsigned int)thiskey.key >= UINT8_MAX)
215 return 1;
217 while (1) {
218 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
219 break;
220 if ((thiskey.key = wgetch(body)) == ERR) {
221 message("Error decoding user input");
222 return 0;
226 return 1;
229 static void
230 dispatch_stdio(int fd, short ev, void *d)
232 struct keymap *k;
233 const char *keyname;
234 char tmp[5] = {0};
236 /* TODO: schedule a redraw? */
237 if (too_small)
238 return;
240 if (!readkey())
241 return;
243 if (keybuf[0] != '\0')
244 strlcat(keybuf, " ", sizeof(keybuf));
245 if (thiskey.meta)
246 strlcat(keybuf, "M-", sizeof(keybuf));
247 if (thiskey.cp != 0) {
248 utf8_encode(thiskey.cp, tmp);
249 strlcat(keybuf, tmp, sizeof(keybuf));
250 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
251 strlcat(keybuf, keyname, sizeof(keybuf));
252 } else {
253 tmp[0] = thiskey.key;
254 strlcat(keybuf, tmp, sizeof(keybuf));
257 TAILQ_FOREACH(k, &current_map->m, keymaps) {
258 if (k->meta == thiskey.meta &&
259 k->key == thiskey.key) {
260 if (k->fn == NULL)
261 current_map = &k->map;
262 else {
263 current_map = base_map;
264 strlcpy(keybuf, "", sizeof(keybuf));
265 k->fn(current_buffer());
267 goto done;
271 if (current_map->unhandled_input != NULL)
272 current_map->unhandled_input();
273 else
274 global_key_unbound();
276 strlcpy(keybuf, "", sizeof(keybuf));
277 current_map = base_map;
279 done:
280 if (side_window & SIDE_WINDOW_LEFT)
281 recompute_help();
283 if (should_rearrange_windows)
284 rearrange_windows();
285 redraw_tab(current_tab);
288 static void
289 handle_resize(int sig, short ev, void *d)
291 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
292 event_del(&resizeev);
294 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
295 evtimer_add(&resizeev, &resize_timer);
298 static void
299 handle_resize_nodelay(int s, short ev, void *d)
301 endwin();
302 refresh();
303 clear();
305 rearrange_windows();
308 static inline int
309 should_show_tab_bar(void)
311 if (tab_bar_show == -1)
312 return 0;
313 if (tab_bar_show == 0)
314 return 1;
316 return TAILQ_NEXT(TAILQ_FIRST(&tabshead), tabs) != NULL;
319 static void
320 rearrange_windows(void)
322 int lines;
323 int minibuffer_lines;
325 should_rearrange_windows = 0;
326 show_tab_bar = should_show_tab_bar();
328 lines = LINES;
330 /* 3 lines for the ui and 12 for the */
331 if ((too_small = lines < 15)) {
332 erase();
333 printw("Window too small.");
334 refresh();
335 return;
338 /* move and resize the windows, in reverse order! */
340 if (in_minibuffer == MB_COMPREAD) {
341 minibuffer_lines = MIN(10, lines/2);
342 mvwin(minibuffer, lines - minibuffer_lines, 0);
343 wresize(minibuffer, minibuffer_lines, COLS);
344 lines -= minibuffer_lines;
346 wrap_page(&ministate.compl.buffer, COLS);
349 mvwin(echoarea, --lines, 0);
350 wresize(echoarea, 1, COLS);
352 mvwin(modeline, --lines, 0);
353 wresize(modeline, 1, COLS);
355 if (side_window & SIDE_WINDOW_BOTTOM) {
356 download_lines = MIN(5, lines/2);
357 mvwin(download, lines - download_lines, 0);
358 wresize(download, download_lines, COLS);
359 lines -= download_lines;
361 wrap_page(&downloadwin, COLS);
364 body_lines = show_tab_bar ? --lines : lines;
365 body_cols = COLS;
367 /*
368 * Here we make the assumption that show_tab_bar is either 0
369 * or 1, and reuse that as argument to mvwin.
370 */
371 if (side_window & SIDE_WINDOW_LEFT) {
372 help_cols = 0.3 * COLS;
373 help_lines = lines;
374 mvwin(help, show_tab_bar, 0);
375 wresize(help, help_lines, help_cols);
377 wrap_page(&helpwin, help_cols);
379 body_cols = COLS - help_cols - 1;
380 mvwin(body, show_tab_bar, help_cols);
381 } else
382 mvwin(body, show_tab_bar, 0);
384 update_x_offset();
385 wresize(body, body_lines, body_cols);
387 if (show_tab_bar)
388 wresize(tabline, 1, COLS);
390 wrap_page(&current_tab->buffer, body_cols);
391 redraw_tab(current_tab);
394 static void
395 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
396 const char **prfx_ret, const char **text_ret)
398 int type, i, cont, width;
399 char *space, *t;
401 if ((*text_ret = vl->line) == NULL)
402 *text_ret = "";
404 cont = vl->flags & L_CONTINUATION;
405 type = vl->parent->type;
406 if (!cont)
407 *prfx_ret = line_prefixes[type].prfx1;
408 else
409 *prfx_ret = line_prefixes[type].prfx2;
411 space = vl->parent->data;
412 if (!emojify_link || type != LINE_LINK || space == NULL)
413 return;
415 if (cont) {
416 memset(buf, 0, len);
417 width = utf8_swidth_between(vl->parent->line, space);
418 for (i = 0; i < width + 1; ++i)
419 strlcat(buf, " ", len);
420 } else {
421 strlcpy(buf, *text_ret, len);
422 if ((t = strchr(buf, ' ')) != NULL)
423 *t = '\0';
424 strlcat(buf, " ", len);
426 /* skip the emoji */
427 *text_ret += (space - vl->parent->line) + 1;
430 *prfx_ret = buf;
433 static inline void
434 print_vline_descr(int width, WINDOW *window, struct vline *vl)
436 int x, y, goal;
438 switch (vl->parent->type) {
439 case LINE_COMPL:
440 case LINE_COMPL_CURRENT:
441 goal = width/2;
442 break;
443 case LINE_DOWNLOAD:
444 case LINE_DOWNLOAD_DONE:
445 case LINE_DOWNLOAD_INFO:
446 goal = width/4;
447 break;
448 case LINE_HELP:
449 goal = 8;
450 break;
451 default:
452 return;
455 if (vl->parent->alt == NULL)
456 return;
458 (void)y;
459 getyx(window, y, x);
461 if (goal <= x)
462 wprintw(window, " ");
463 for (; goal > x; ++x)
464 wprintw(window, " ");
466 wprintw(window, "%s", vl->parent->alt);
469 /*
470 * Core part of the rendering. It prints a vline starting from the
471 * current cursor position. Printing a vline consists of skipping
472 * `off' columns (for olivetti-mode), print the correct prefix (which
473 * may be the emoji in case of emojified links-lines), printing the
474 * text itself, filling until width - off and filling off columns
475 * again.
476 */
477 static void
478 print_vline(int off, int width, WINDOW *window, struct vline *vl)
480 /*
481 * Believe me or not, I've seen emoji ten code points long!
482 * That means, to stay large, 4*10 bytes + NUL.
483 */
484 char emojibuf[41] = {0};
485 const char *text, *prfx;
486 struct line_face *f;
487 int i, left, x, y;
489 f = &line_faces[vl->parent->type];
491 /* unused, set by getyx */
492 (void)y;
494 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
496 wattr_on(window, body_face.left, NULL);
497 for (i = 0; i < off; i++)
498 waddch(window, ' ');
499 wattr_off(window, body_face.left, NULL);
501 wattr_on(window, f->prefix, NULL);
502 wprintw(window, "%s", prfx);
503 wattr_off(window, f->prefix, NULL);
505 wattr_on(window, f->text, NULL);
506 wprintw(window, "%s", text);
507 print_vline_descr(width, window, vl);
508 wattr_off(window, f->text, NULL);
510 getyx(window, y, x);
512 left = width - x;
514 wattr_on(window, f->trail, NULL);
515 for (i = 0; i < left - off; ++i)
516 waddch(window, ' ');
517 wattr_off(window, f->trail, NULL);
519 wattr_on(window, body_face.right, NULL);
520 for (i = 0; i < off; i++)
521 waddch(window, ' ');
522 wattr_off(window, body_face.right, NULL);
526 static void
527 redraw_tabline(void)
529 struct tab *tab;
530 size_t toskip, ots, tabwidth, space, x;
531 int current, y, truncated, pair;
532 const char *title;
533 char buf[25];
535 x = 0;
537 /* unused, but setted by a getyx */
538 (void)y;
540 tabwidth = sizeof(buf)+1;
541 space = COLS-2;
543 toskip = 0;
544 TAILQ_FOREACH(tab, &tabshead, tabs) {
545 toskip++;
546 if (tab == current_tab)
547 break;
550 if (toskip * tabwidth < space)
551 toskip = 0;
552 else {
553 ots = toskip;
554 toskip--;
555 while (toskip != 0 &&
556 (ots - toskip+1) * tabwidth < space)
557 toskip--;
560 werase(tabline);
561 wattr_on(tabline, tab_face.background, NULL);
562 wprintw(tabline, toskip == 0 ? " " : "<");
563 wattr_off(tabline, tab_face.background, NULL);
565 truncated = 0;
566 TAILQ_FOREACH(tab, &tabshead, tabs) {
567 if (truncated)
568 break;
569 if (toskip != 0) {
570 toskip--;
571 continue;
574 getyx(tabline, y, x);
575 if (x + sizeof(buf)+2 >= (size_t)COLS)
576 truncated = 1;
578 current = tab == current_tab;
580 if (*(title = tab->buffer.page.title) == '\0')
581 title = tab->hist_cur->h;
583 if (tab->flags & TAB_URGENT)
584 strlcpy(buf, "!", sizeof(buf));
585 else
586 strlcpy(buf, " ", sizeof(buf));
588 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
589 /* truncation happens */
590 strlcpy(&buf[sizeof(buf)-4], "...", 4);
591 } else {
592 /* pad with spaces */
593 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
594 /* nop */ ;
597 pair = current ? tab_face.current : tab_face.tab;
598 wattr_on(tabline, pair, NULL);
599 wprintw(tabline, "%s", buf);
600 wattr_off(tabline, pair, NULL);
602 wattr_on(tabline, tab_face.background, NULL);
603 if (TAILQ_NEXT(tab, tabs) != NULL)
604 wprintw(tabline, "┃");
605 wattr_off(tabline, tab_face.background, NULL);
608 wattr_on(tabline, tab_face.background, NULL);
609 for (; x < (size_t)COLS; ++x)
610 waddch(tabline, ' ');
611 if (truncated)
612 mvwprintw(tabline, 0, COLS-1, ">");
613 wattr_off(tabline, tab_face.background, NULL);
616 /*
617 * Compute the first visible line around vl. Try to search forward
618 * until the end of the buffer; if a visible line is not found, search
619 * backward. Return NULL if no viable line was found.
620 */
621 struct vline *
622 adjust_line(struct vline *vl, struct buffer *buffer)
624 struct vline *t;
626 if (vl == NULL)
627 return NULL;
629 if (!(vl->parent->flags & L_HIDDEN))
630 return vl;
632 /* search forward */
633 for (t = vl;
634 t != NULL && t->parent->flags & L_HIDDEN;
635 t = TAILQ_NEXT(t, vlines))
636 ; /* nop */
638 if (t != NULL)
639 return t;
641 /* search backward */
642 for (t = vl;
643 t != NULL && t->parent->flags & L_HIDDEN;
644 t = TAILQ_PREV(t, vhead, vlines))
645 ; /* nop */
647 return t;
650 static void
651 redraw_window(WINDOW *win, int off, int height, int width,
652 struct buffer *buffer)
654 struct vline *vl;
655 int l, onscreen;
657 restore_curs_x(buffer);
659 /*
660 * TODO: ignoring buffer->force_update and always
661 * re-rendering. In theory we can recompute the y position
662 * without a re-render, and optimize here. It's not the only
663 * optimisation possible here, wscrl wolud also be an
664 * interesting one.
665 */
667 again:
668 werase(win);
669 buffer->curs_y = 0;
671 if (TAILQ_EMPTY(&buffer->head))
672 goto end;
674 if (buffer->top_line == NULL)
675 buffer->top_line = TAILQ_FIRST(&buffer->head);
677 buffer->top_line = adjust_line(buffer->top_line, buffer);
678 if (buffer->top_line == NULL)
679 goto end;
681 buffer->current_line = adjust_line(buffer->current_line, buffer);
683 l = 0;
684 onscreen = 0;
685 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
686 if (vl->parent->flags & L_HIDDEN)
687 continue;
689 wmove(win, l, 0);
690 print_vline(off, width, win, vl);
692 if (vl == buffer->current_line)
693 onscreen = 1;
695 if (!onscreen)
696 buffer->curs_y++;
698 l++;
699 if (l == height)
700 break;
703 if (!onscreen) {
704 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
705 if (vl == buffer->current_line)
706 break;
707 if (vl->parent->flags & L_HIDDEN)
708 continue;
709 buffer->line_off++;
710 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
713 if (vl != NULL)
714 goto again;
717 buffer->last_line_off = buffer->line_off;
718 buffer->force_redraw = 0;
719 end:
720 wmove(win, buffer->curs_y, buffer->curs_x);
723 static void
724 redraw_download(void)
726 redraw_window(download, 0, download_lines, COLS, &downloadwin);
729 static void
730 redraw_help(void)
732 redraw_window(help, 0, help_lines, help_cols, &helpwin);
735 static void
736 redraw_body(struct tab *tab)
738 static struct tab *last_tab;
740 if (last_tab != tab)
741 tab->buffer.force_redraw =1;
742 last_tab = tab;
744 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
747 static inline char
748 trust_status_char(enum trust_state ts)
750 switch (ts) {
751 case TS_UNKNOWN: return '-';
752 case TS_UNTRUSTED: return '!';
753 case TS_TEMP_TRUSTED: return '!';
754 case TS_TRUSTED: return 'v';
755 case TS_VERIFIED: return 'V';
756 default: return 'X';
760 static void
761 redraw_modeline(struct tab *tab)
763 struct buffer *buffer;
764 double pct;
765 int x, y, max_x, max_y;
766 const char *mode;
767 const char *spin = "-\\|/";
769 buffer = current_buffer();
770 mode = buffer->page.name;
772 werase(modeline);
773 wattr_on(modeline, modeline_face.background, NULL);
774 wmove(modeline, 0, 0);
776 wprintw(modeline, "-%c%c %s ",
777 spin[tab->loading_anim_step],
778 trust_status_char(tab->trust),
779 mode == NULL ? "(none)" : mode);
781 pct = (buffer->line_off + buffer->curs_y) * 100.0
782 / buffer->line_max;
784 if (buffer->line_max <= (size_t)body_lines)
785 wprintw(modeline, "All ");
786 else if (buffer->line_off == 0)
787 wprintw(modeline, "Top ");
788 else if (buffer->line_off + body_lines >= buffer->line_max)
789 wprintw(modeline, "Bottom ");
790 else
791 wprintw(modeline, "%.0f%% ", pct);
793 wprintw(modeline, "%d/%d %s ",
794 buffer->line_off + buffer->curs_y,
795 buffer->line_max,
796 tab->hist_cur->h);
798 getyx(modeline, y, x);
799 getmaxyx(modeline, max_y, max_x);
801 (void)y;
802 (void)max_y;
804 for (; x < max_x; ++x)
805 waddstr(modeline, "-");
807 wattr_off(modeline, modeline_face.background, NULL);
810 static void
811 redraw_minibuffer(void)
813 wattr_on(echoarea, minibuffer_face.background, NULL);
814 werase(echoarea);
816 if (in_minibuffer)
817 do_redraw_minibuffer();
818 else
819 do_redraw_echoarea();
821 if (in_minibuffer == MB_COMPREAD)
822 do_redraw_minibuffer_compl();
824 wattr_off(echoarea, minibuffer_face.background, NULL);
827 static void
828 do_redraw_echoarea(void)
830 struct vline *vl;
832 if (ministate.curmesg != NULL)
833 wprintw(echoarea, "%s", ministate.curmesg);
834 else if (*keybuf != '\0')
835 waddstr(echoarea, keybuf);
836 else {
837 /* If nothing else, show the URL at point */
838 vl = current_tab->buffer.current_line;
839 if (vl != NULL && vl->parent->type == LINE_LINK)
840 waddstr(echoarea, vl->parent->alt);
844 static void
845 do_redraw_minibuffer(void)
847 struct buffer *cmplbuf, *buffer;
848 size_t off_y, off_x = 0;
849 const char *start, *c;
851 cmplbuf = &ministate.compl.buffer;
852 buffer = &ministate.buffer;
853 (void)off_y; /* unused, set by getyx */
855 wmove(echoarea, 0, 0);
857 if (in_minibuffer == MB_COMPREAD)
858 wprintw(echoarea, "(%2d) ",
859 cmplbuf->line_max);
861 wprintw(echoarea, "%s", ministate.prompt);
862 if (ministate.hist_cur != NULL)
863 wprintw(echoarea, "(%zu/%zu) ",
864 ministate.hist_off + 1,
865 ministate.history->len);
867 getyx(echoarea, off_y, off_x);
869 start = ministate.hist_cur != NULL
870 ? ministate.hist_cur->h
871 : ministate.buf;
872 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
873 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
874 start = utf8_next_cp(start);
877 waddstr(echoarea, start);
879 if (ministate.curmesg != NULL)
880 wprintw(echoarea, " [%s]", ministate.curmesg);
882 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
885 static void
886 do_redraw_minibuffer_compl(void)
888 redraw_window(minibuffer, 0, 10, COLS,
889 &ministate.compl.buffer);
892 /*
893 * Place the cursor in the right ncurses window. If soft is 1, use
894 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
895 * wrefresh.
896 */
897 static void
898 place_cursor(int soft)
900 int (*touch)(WINDOW *);
902 if (soft)
903 touch = wnoutrefresh;
904 else
905 touch = wrefresh;
907 if (in_minibuffer) {
908 if (side_window & SIDE_WINDOW_LEFT)
909 touch(help);
910 if (side_window & SIDE_WINDOW_BOTTOM)
911 touch(download);
912 touch(body);
913 touch(echoarea);
914 } else if (in_side_window & SIDE_WINDOW_LEFT) {
915 touch(body);
916 touch(echoarea);
917 if (in_side_window & SIDE_WINDOW_BOTTOM)
918 touch(download);
919 touch(help);
920 } else if (in_side_window & SIDE_WINDOW_BOTTOM) {
921 touch(body);
922 touch(echoarea);
923 if (in_side_window & SIDE_WINDOW_LEFT)
924 touch(help);
925 touch(download);
926 } else {
927 if (side_window & SIDE_WINDOW_LEFT)
928 touch(help);
929 if (side_window & SIDE_WINDOW_BOTTOM)
930 touch(download);
931 touch(echoarea);
932 touch(body);
936 static void
937 redraw_tab(struct tab *tab)
939 if (too_small)
940 return;
942 if (side_window & SIDE_WINDOW_LEFT) {
943 redraw_help();
944 wnoutrefresh(help);
947 if (side_window & SIDE_WINDOW_BOTTOM) {
948 redraw_download();
949 wnoutrefresh(download);
952 if (show_tab_bar)
953 redraw_tabline();
955 redraw_body(tab);
956 redraw_modeline(tab);
957 redraw_minibuffer();
959 wnoutrefresh(tabline);
960 wnoutrefresh(modeline);
962 if (in_minibuffer == MB_COMPREAD)
963 wnoutrefresh(minibuffer);
965 place_cursor(1);
967 doupdate();
969 if (set_title)
970 dprintf(1, "\033]2;%s - Telescope\a",
971 current_tab->buffer.page.title);
974 void
975 start_loading_anim(struct tab *tab)
977 if (tab->loading_anim)
978 return;
979 tab->loading_anim = 1;
980 evtimer_set(&tab->loadingev, update_loading_anim, tab);
981 evtimer_add(&tab->loadingev, &loadingev_timer);
984 static void
985 update_loading_anim(int fd, short ev, void *d)
987 struct tab *tab = d;
989 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
991 if (tab == current_tab) {
992 redraw_modeline(tab);
993 wrefresh(modeline);
994 wrefresh(body);
995 if (in_minibuffer)
996 wrefresh(echoarea);
999 evtimer_add(&tab->loadingev, &loadingev_timer);
1002 static void
1003 stop_loading_anim(struct tab *tab)
1005 if (!tab->loading_anim)
1006 return;
1007 evtimer_del(&tab->loadingev);
1008 tab->loading_anim = 0;
1009 tab->loading_anim_step = 0;
1011 if (tab != current_tab)
1012 return;
1014 redraw_modeline(tab);
1016 wrefresh(modeline);
1017 wrefresh(body);
1018 if (in_minibuffer)
1019 wrefresh(echoarea);
1022 int
1023 ui_print_colors(void)
1025 int colors = 0, pairs = 0, can_change = 0;
1026 int columns = 16, lines, color, i, j;
1028 initscr();
1029 if (has_colors()) {
1030 start_color();
1031 use_default_colors();
1033 colors = COLORS;
1034 pairs = COLOR_PAIRS;
1035 can_change = can_change_color();
1037 endwin();
1039 printf("Term info:\n");
1040 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1041 getenv("TERM"), colors, pairs, can_change);
1042 printf("\n");
1044 if (colors == 0) {
1045 printf("No color support\n");
1046 return 0;
1049 printf("Available colors:\n\n");
1050 lines = (colors - 1) / columns + 1;
1051 color = 0;
1052 for (i = 0; i < lines; ++i) {
1053 for (j = 0; j < columns; ++j, ++color) {
1054 printf("\033[0;38;5;%dm %03d", color, color);
1056 printf("\n");
1059 printf("\033[0m");
1060 fflush(stdout);
1061 return 0;
1064 int
1065 ui_init()
1067 setlocale(LC_ALL, "");
1069 if (TAILQ_EMPTY(&global_map.m)) {
1070 fprintf(stderr, "no keys defined!\n");
1071 return 0;
1074 minibuffer_init();
1076 /* initialize download window */
1077 TAILQ_INIT(&downloadwin.head);
1078 TAILQ_INIT(&downloadwin.page.head);
1080 /* initialize help window */
1081 TAILQ_INIT(&helpwin.head);
1082 TAILQ_INIT(&helpwin.page.head);
1084 base_map = &global_map;
1085 current_map = &global_map;
1087 initscr();
1089 if (enable_colors) {
1090 if (has_colors()) {
1091 start_color();
1092 use_default_colors();
1093 } else
1094 enable_colors = 0;
1097 config_apply_style();
1099 raw();
1100 noecho();
1101 nonl();
1102 intrflush(stdscr, FALSE);
1104 if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1105 return 0;
1106 if ((body = newwin(1, 1, 0, 0)) == NULL)
1107 return 0;
1108 if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1109 return 0;
1110 if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1111 return 0;
1112 if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1113 return 0;
1114 if ((download = newwin(1, 1, 0, 0)) == NULL)
1115 return 0;
1116 if ((help = newwin(1, 1, 0, 0)) == NULL)
1117 return 0;
1119 wbkgd(body, body_face.body);
1120 wbkgd(download, download_face.background);
1121 wbkgd(echoarea, minibuffer_face.background);
1123 update_x_offset();
1125 keypad(body, TRUE);
1126 scrollok(body, FALSE);
1128 /* non-blocking input */
1129 wtimeout(body, 0);
1130 wtimeout(help, 0);
1132 mvwprintw(body, 0, 0, "");
1134 evtimer_set(&resizeev, handle_resize, NULL);
1136 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1137 event_add(&stdioev, NULL);
1139 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1140 signal_add(&winchev, NULL);
1142 return 1;
1145 void
1146 ui_main_loop(void)
1148 switch_to_tab(current_tab);
1149 rearrange_windows();
1151 event_dispatch();
1154 void
1155 ui_on_tab_loaded(struct tab *tab)
1157 stop_loading_anim(tab);
1158 message("Loaded %s", tab->hist_cur->h);
1160 if (show_tab_bar)
1161 redraw_tabline();
1163 wrefresh(tabline);
1164 place_cursor(0);
1167 void
1168 ui_on_tab_refresh(struct tab *tab)
1170 wrap_page(&tab->buffer, body_cols);
1171 if (tab == current_tab)
1172 redraw_tab(tab);
1173 else
1174 tab->flags |= TAB_URGENT;
1177 void
1178 ui_on_download_refresh(void)
1180 if (side_window & SIDE_WINDOW_BOTTOM) {
1181 recompute_downloads();
1182 redraw_tab(current_tab);
1186 const char *
1187 ui_keyname(int k)
1189 return keyname(k);
1192 void
1193 ui_toggle_side_window(int kind)
1195 if (in_side_window & kind)
1196 ui_other_window();
1198 side_window ^= kind;
1199 if (side_window & SIDE_WINDOW_LEFT)
1200 recompute_help();
1201 if (side_window & SIDE_WINDOW_BOTTOM)
1202 recompute_downloads();
1205 * ugly hack, but otherwise the window doesn't get updated
1206 * until I call rearrange_windows a second time (e.g. via
1207 * C-l). I will be happy to know why something like this is
1208 * needed.
1210 rearrange_windows();
1211 rearrange_windows();
1214 void
1215 ui_show_downloads_pane(void)
1217 if (!(side_window & SIDE_WINDOW_BOTTOM))
1218 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1221 void
1222 ui_schedule_redraw(void)
1224 should_rearrange_windows = 1;
1227 void
1228 ui_require_input(struct tab *tab, int hide, int proto)
1230 void (*fn)(void);
1232 if (proto == PROTO_GEMINI)
1233 fn = ir_select_gemini;
1234 else if (proto == PROTO_GOPHER)
1235 fn = ir_select_gopher;
1236 else
1237 abort();
1239 /* TODO: hard-switching to another tab is ugly */
1240 switch_to_tab(tab);
1242 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1243 &ir_history, NULL, NULL);
1244 strlcpy(ministate.prompt, "Input required: ",
1245 sizeof(ministate.prompt));
1246 redraw_tab(tab);
1249 void
1250 ui_after_message_hook(void)
1252 redraw_minibuffer();
1253 place_cursor(0);
1256 void
1257 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1258 struct tab *data)
1260 yornp(prompt, fn, data);
1261 redraw_tab(current_tab);
1264 void
1265 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1266 struct tab *data, const char *input)
1268 minibuffer_read(prompt, fn, data);
1270 if (input != NULL) {
1271 strlcpy(ministate.buf, input, sizeof(ministate.buf));
1272 cmd_move_end_of_line(&ministate.buffer);
1275 redraw_tab(current_tab);
1278 void
1279 ui_other_window(void)
1281 if (in_side_window & SIDE_WINDOW_LEFT &&
1282 side_window & SIDE_WINDOW_BOTTOM)
1283 in_side_window = SIDE_WINDOW_BOTTOM;
1284 else if (in_side_window)
1285 in_side_window = 0;
1286 else if (!in_side_window && side_window & SIDE_WINDOW_LEFT)
1287 in_side_window = SIDE_WINDOW_LEFT;
1288 else if (!in_side_window && side_window)
1289 in_side_window = SIDE_WINDOW_BOTTOM;
1290 else
1291 message("No other window to select");
1294 void
1295 ui_suspend(void)
1297 endwin();
1299 kill(getpid(), SIGSTOP);
1301 refresh();
1302 clear();
1303 rearrange_windows();
1306 void
1307 ui_end(void)
1309 endwin();