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;
101 int download_cols;
103 static int side_window;
104 static int in_side_window;
106 static struct timeval loadingev_timer = { 0, 250000 };
108 static char keybuf[64];
110 /* XXX: don't forget to init these in main() */
111 struct kmap global_map,
112 minibuffer_map,
113 *current_map,
114 *base_map;
116 static inline void
117 update_x_offset(void)
119 if (olivetti_mode && fill_column < body_cols)
120 x_offset = (body_cols - fill_column)/2;
121 else
122 x_offset = 0;
125 void
126 save_excursion(struct excursion *place, struct buffer *buffer)
128 place->curs_x = buffer->curs_x;
129 place->curs_y = buffer->curs_y;
130 place->line_off = buffer->line_off;
131 place->top_line = buffer->top_line;
132 place->current_line = buffer->current_line;
133 place->cpoff = buffer->cpoff;
136 void
137 restore_excursion(struct excursion *place, struct buffer *buffer)
139 buffer->curs_x = place->curs_x;
140 buffer->curs_y = place->curs_y;
141 buffer->line_off = place->line_off;
142 buffer->top_line = place->top_line;
143 buffer->current_line = place->current_line;
144 buffer->cpoff = place->cpoff;
147 static void
148 restore_curs_x(struct buffer *buffer)
150 struct vline *vl;
151 const char *prfx, *text;
153 vl = buffer->current_line;
154 if (vl == NULL || vl->line == NULL)
155 buffer->curs_x = buffer->cpoff = 0;
156 else if (vl->parent->data != NULL) {
157 text = vl->parent->data;
158 buffer->curs_x = utf8_snwidth(text + 1, buffer->cpoff) + 1;
159 } else
160 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
162 /* small hack: don't olivetti-mode the download pane */
163 if (buffer != &downloadwin)
164 buffer->curs_x += x_offset;
166 if (vl == NULL)
167 return;
169 if (vl->parent->data != NULL)
170 buffer->curs_x += utf8_swidth_between(vl->parent->line,
171 vl->parent->data);
172 else {
173 prfx = line_prefixes[vl->parent->type].prfx1;
174 buffer->curs_x += utf8_swidth(prfx);
178 void
179 global_key_unbound(void)
181 message("%s is undefined", keybuf);
184 struct buffer *
185 current_buffer(void)
187 if (in_minibuffer)
188 return &ministate.buffer;
189 if (in_side_window & SIDE_WINDOW_LEFT)
190 return &helpwin;
191 if (in_side_window & SIDE_WINDOW_BOTTOM)
192 return &downloadwin;
193 return &current_tab->buffer;
196 static int
197 readkey(void)
199 uint32_t state = 0;
201 if ((thiskey.key = wgetch(body)) == ERR)
202 return 0;
204 thiskey.meta = thiskey.key == '\e';
205 if (thiskey.meta) {
206 thiskey.key = wgetch(body);
207 if (thiskey.key == ERR || thiskey.key == '\e') {
208 thiskey.meta = 0;
209 thiskey.key = '\e';
213 thiskey.cp = 0;
215 if ((unsigned int)thiskey.key >= UINT8_MAX)
216 return 1;
218 while (1) {
219 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
220 break;
221 if ((thiskey.key = wgetch(body)) == ERR) {
222 message("Error decoding user input");
223 return 0;
227 return 1;
230 static void
231 dispatch_stdio(int fd, short ev, void *d)
233 int lk;
234 const char *keyname;
235 char tmp[5] = {0};
237 /* TODO: schedule a redraw? */
238 if (too_small)
239 return;
241 if (!readkey())
242 return;
244 if (keybuf[0] != '\0')
245 strlcat(keybuf, " ", sizeof(keybuf));
246 if (thiskey.meta)
247 strlcat(keybuf, "M-", sizeof(keybuf));
248 if (thiskey.cp != 0) {
249 utf8_encode(thiskey.cp, tmp);
250 strlcat(keybuf, tmp, sizeof(keybuf));
251 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
252 strlcat(keybuf, keyname, sizeof(keybuf));
253 } else {
254 tmp[0] = thiskey.key;
255 strlcat(keybuf, tmp, sizeof(keybuf));
258 lk = lookup_key(&current_map, &thiskey, current_buffer());
259 if (lk == LK_UNBOUND) {
260 if (current_map->unhandled_input != NULL)
261 current_map->unhandled_input();
262 else
263 global_key_unbound();
265 if (lk != LK_ADVANCED_MAP) {
266 current_map = base_map;
267 strlcpy(keybuf, "", sizeof(keybuf));
270 if (side_window & SIDE_WINDOW_LEFT)
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;
313 int minibuffer_lines;
315 should_rearrange_windows = 0;
316 show_tab_bar = should_show_tab_bar();
318 lines = LINES;
320 /* 3 lines for the ui and 12 for the */
321 if ((too_small = lines < 15)) {
322 erase();
323 printw("Window too small.");
324 refresh();
325 return;
328 /* move and resize the windows, in reverse order! */
330 if (in_minibuffer == MB_COMPREAD) {
331 minibuffer_lines = MIN(10, lines/2);
332 mvwin(minibuffer, lines - minibuffer_lines, 0);
333 wresize(minibuffer, minibuffer_lines, COLS);
334 lines -= minibuffer_lines;
336 wrap_page(&ministate.compl.buffer, COLS);
339 mvwin(echoarea, --lines, 0);
340 wresize(echoarea, 1, COLS);
342 mvwin(modeline, --lines, 0);
343 wresize(modeline, 1, COLS);
345 if (side_window & SIDE_WINDOW_BOTTOM) {
346 download_lines = MIN(5, lines/2);
347 download_cols = COLS;
348 mvwin(download, lines - download_lines, 0);
349 wresize(download, download_lines, download_cols);
350 lines -= download_lines;
352 wrap_page(&downloadwin, download_cols);
355 body_lines = show_tab_bar ? --lines : lines;
356 body_cols = COLS;
358 /*
359 * Here we make the assumption that show_tab_bar is either 0
360 * or 1, and reuse that as argument to mvwin.
361 */
362 if (side_window & SIDE_WINDOW_LEFT) {
363 help_cols = 0.3 * COLS;
364 help_lines = lines;
365 mvwin(help, show_tab_bar, 0);
366 wresize(help, help_lines, help_cols);
368 wrap_page(&helpwin, help_cols);
370 body_cols = COLS - help_cols - 1;
371 mvwin(body, show_tab_bar, help_cols);
372 } else
373 mvwin(body, show_tab_bar, 0);
375 update_x_offset();
376 wresize(body, body_lines, body_cols);
378 if (show_tab_bar)
379 wresize(tabline, 1, COLS);
381 wrap_page(&current_tab->buffer, body_cols);
382 redraw_tab(current_tab);
385 static void
386 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
387 const char **prfx_ret, const char **text_ret)
389 int type, i, cont, width;
390 char *space, *t;
392 if ((*text_ret = vl->line) == NULL)
393 *text_ret = "";
395 cont = vl->flags & L_CONTINUATION;
396 type = vl->parent->type;
397 if (!cont)
398 *prfx_ret = line_prefixes[type].prfx1;
399 else
400 *prfx_ret = line_prefixes[type].prfx2;
402 space = vl->parent->data;
403 if (!emojify_link || type != LINE_LINK || space == NULL)
404 return;
406 if (cont) {
407 memset(buf, 0, len);
408 width = utf8_swidth_between(vl->parent->line, space);
409 for (i = 0; i < width + 1; ++i)
410 strlcat(buf, " ", len);
411 } else {
412 strlcpy(buf, *text_ret, len);
413 if ((t = strchr(buf, ' ')) != NULL)
414 *t = '\0';
415 strlcat(buf, " ", len);
417 /* skip the emoji */
418 *text_ret += (space - vl->parent->line) + 1;
421 *prfx_ret = buf;
424 static inline void
425 print_vline_descr(int width, WINDOW *window, struct vline *vl)
427 int x, y, goal;
429 switch (vl->parent->type) {
430 case LINE_COMPL:
431 case LINE_COMPL_CURRENT:
432 goal = width/2;
433 break;
434 case LINE_DOWNLOAD:
435 case LINE_DOWNLOAD_DONE:
436 goal = width/4;
437 break;
438 case LINE_HELP:
439 goal = 8;
440 break;
441 default:
442 return;
445 if (vl->parent->alt == NULL)
446 return;
448 (void)y;
449 getyx(window, y, x);
451 if (goal <= x)
452 wprintw(window, " ");
453 for (; goal > x; ++x)
454 wprintw(window, " ");
456 wprintw(window, "%s", vl->parent->alt);
459 /*
460 * Core part of the rendering. It prints a vline starting from the
461 * current cursor position. Printing a vline consists of skipping
462 * `off' columns (for olivetti-mode), print the correct prefix (which
463 * may be the emoji in case of emojified links-lines), printing the
464 * text itself, filling until width - off and filling off columns
465 * again.
466 */
467 static void
468 print_vline(int off, int width, WINDOW *window, struct vline *vl)
470 /*
471 * Believe me or not, I've seen emoji ten code points long!
472 * That means, to stay large, 4*10 bytes + NUL.
473 */
474 char emojibuf[41] = {0};
475 const char *text, *prfx;
476 struct line_face *f;
477 int i, left, x, y;
479 f = &line_faces[vl->parent->type];
481 /* unused, set by getyx */
482 (void)y;
484 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
486 wattr_on(window, body_face.left, NULL);
487 for (i = 0; i < off; i++)
488 waddch(window, ' ');
489 wattr_off(window, body_face.left, NULL);
491 wattr_on(window, f->prefix, NULL);
492 wprintw(window, "%s", prfx);
493 wattr_off(window, f->prefix, NULL);
495 wattr_on(window, f->text, NULL);
496 wprintw(window, "%s", text);
497 print_vline_descr(width, window, vl);
498 wattr_off(window, f->text, NULL);
500 getyx(window, y, x);
502 left = width - x;
504 wattr_on(window, f->trail, NULL);
505 for (i = 0; i < left - off; ++i)
506 waddch(window, ' ');
507 wattr_off(window, f->trail, NULL);
509 wattr_on(window, body_face.right, NULL);
510 for (i = 0; i < off; i++)
511 waddch(window, ' ');
512 wattr_off(window, body_face.right, NULL);
516 static void
517 redraw_tabline(void)
519 struct tab *tab;
520 size_t toskip, ots, tabwidth, space, x;
521 int current, y, truncated, pair;
522 const char *title;
523 char buf[25];
525 x = 0;
527 /* unused, but setted by a getyx */
528 (void)y;
530 tabwidth = sizeof(buf)+1;
531 space = COLS-2;
533 toskip = 0;
534 TAILQ_FOREACH(tab, &tabshead, tabs) {
535 toskip++;
536 if (tab == current_tab)
537 break;
540 if (toskip * tabwidth < space)
541 toskip = 0;
542 else {
543 ots = toskip;
544 toskip--;
545 while (toskip != 0 &&
546 (ots - toskip+1) * tabwidth < space)
547 toskip--;
550 werase(tabline);
551 wattr_on(tabline, tab_face.background, NULL);
552 wprintw(tabline, toskip == 0 ? " " : "<");
553 wattr_off(tabline, tab_face.background, NULL);
555 truncated = 0;
556 TAILQ_FOREACH(tab, &tabshead, tabs) {
557 if (truncated)
558 break;
559 if (toskip != 0) {
560 toskip--;
561 continue;
564 getyx(tabline, y, x);
565 if (x + sizeof(buf)+2 >= (size_t)COLS)
566 truncated = 1;
568 current = tab == current_tab;
570 if (*(title = tab->buffer.page.title) == '\0')
571 title = tab->hist_cur->h;
573 if (tab->flags & TAB_URGENT)
574 strlcpy(buf, "!", sizeof(buf));
575 else
576 strlcpy(buf, " ", sizeof(buf));
578 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
579 /* truncation happens */
580 strlcpy(&buf[sizeof(buf)-4], "...", 4);
581 } else {
582 /* pad with spaces */
583 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
584 /* nop */ ;
587 pair = current ? tab_face.current : tab_face.tab;
588 wattr_on(tabline, pair, NULL);
589 wprintw(tabline, "%s", buf);
590 wattr_off(tabline, pair, NULL);
592 wattr_on(tabline, tab_face.background, NULL);
593 if (TAILQ_NEXT(tab, tabs) != NULL)
594 wprintw(tabline, "┃");
595 wattr_off(tabline, tab_face.background, NULL);
598 wattr_on(tabline, tab_face.background, NULL);
599 for (; x < (size_t)COLS; ++x)
600 waddch(tabline, ' ');
601 if (truncated)
602 mvwprintw(tabline, 0, COLS-1, ">");
603 wattr_off(tabline, tab_face.background, NULL);
606 /*
607 * Compute the first visible line around vl. Try to search forward
608 * until the end of the buffer; if a visible line is not found, search
609 * backward. Return NULL if no viable line was found.
610 */
611 struct vline *
612 adjust_line(struct vline *vl, struct buffer *buffer)
614 struct vline *t;
616 if (vl == NULL)
617 return NULL;
619 if (!(vl->parent->flags & L_HIDDEN))
620 return vl;
622 /* search forward */
623 for (t = vl;
624 t != NULL && t->parent->flags & L_HIDDEN;
625 t = TAILQ_NEXT(t, vlines))
626 ; /* nop */
628 if (t != NULL)
629 return t;
631 /* search backward */
632 for (t = vl;
633 t != NULL && t->parent->flags & L_HIDDEN;
634 t = TAILQ_PREV(t, vhead, vlines))
635 ; /* nop */
637 return t;
640 static void
641 redraw_window(WINDOW *win, int off, int height, int width,
642 struct buffer *buffer)
644 struct vline *vl;
645 int l, onscreen;
647 restore_curs_x(buffer);
649 /*
650 * TODO: ignoring buffer->force_update and always
651 * re-rendering. In theory we can recompute the y position
652 * without a re-render, and optimize here. It's not the only
653 * optimisation possible here, wscrl wolud also be an
654 * interesting one.
655 */
657 again:
658 werase(win);
659 buffer->curs_y = 0;
661 if (TAILQ_EMPTY(&buffer->head))
662 goto end;
664 if (buffer->top_line == NULL)
665 buffer->top_line = TAILQ_FIRST(&buffer->head);
667 buffer->top_line = adjust_line(buffer->top_line, buffer);
668 if (buffer->top_line == NULL)
669 goto end;
671 buffer->current_line = adjust_line(buffer->current_line, buffer);
673 l = 0;
674 onscreen = 0;
675 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
676 if (vl->parent->flags & L_HIDDEN)
677 continue;
679 wmove(win, l, 0);
680 print_vline(off, width, win, vl);
682 if (vl == buffer->current_line)
683 onscreen = 1;
685 if (!onscreen)
686 buffer->curs_y++;
688 l++;
689 if (l == height)
690 break;
693 if (!onscreen) {
694 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
695 if (vl == buffer->current_line)
696 break;
697 if (vl->parent->flags & L_HIDDEN)
698 continue;
699 buffer->line_off++;
700 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
703 if (vl != NULL)
704 goto again;
707 buffer->last_line_off = buffer->line_off;
708 buffer->force_redraw = 0;
709 end:
710 wmove(win, buffer->curs_y, buffer->curs_x);
713 static void
714 redraw_download(void)
716 redraw_window(download, 0, download_lines, COLS, &downloadwin);
719 static void
720 redraw_help(void)
722 redraw_window(help, 0, help_lines, help_cols, &helpwin);
725 static void
726 redraw_body(struct tab *tab)
728 static struct tab *last_tab;
730 if (last_tab != tab)
731 tab->buffer.force_redraw =1;
732 last_tab = tab;
734 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
737 static inline char
738 trust_status_char(enum trust_state ts)
740 switch (ts) {
741 case TS_UNKNOWN: return '-';
742 case TS_UNTRUSTED: return '!';
743 case TS_TEMP_TRUSTED: return '!';
744 case TS_TRUSTED: return 'v';
745 case TS_VERIFIED: return 'V';
746 default: return 'X';
750 static void
751 redraw_modeline(struct tab *tab)
753 struct buffer *buffer;
754 double pct;
755 int x, y, max_x, max_y;
756 const char *mode;
757 const char *spin = "-\\|/";
759 buffer = current_buffer();
760 mode = buffer->page.name;
762 werase(modeline);
763 wattr_on(modeline, modeline_face.background, NULL);
764 wmove(modeline, 0, 0);
766 wprintw(modeline, "-%c%c %s ",
767 spin[tab->loading_anim_step],
768 trust_status_char(tab->trust),
769 mode == NULL ? "(none)" : mode);
771 pct = (buffer->line_off + buffer->curs_y) * 100.0
772 / buffer->line_max;
774 if (buffer->line_max <= (size_t)body_lines)
775 wprintw(modeline, "All ");
776 else if (buffer->line_off == 0)
777 wprintw(modeline, "Top ");
778 else if (buffer->line_off + body_lines >= buffer->line_max)
779 wprintw(modeline, "Bottom ");
780 else
781 wprintw(modeline, "%.0f%% ", pct);
783 wprintw(modeline, "%d/%d %s ",
784 buffer->line_off + buffer->curs_y,
785 buffer->line_max,
786 tab->hist_cur->h);
788 getyx(modeline, y, x);
789 getmaxyx(modeline, max_y, max_x);
791 (void)y;
792 (void)max_y;
794 for (; x < max_x; ++x)
795 waddstr(modeline, "-");
797 wattr_off(modeline, modeline_face.background, NULL);
800 static void
801 redraw_minibuffer(void)
803 wattr_on(echoarea, minibuffer_face.background, NULL);
804 werase(echoarea);
806 if (in_minibuffer)
807 do_redraw_minibuffer();
808 else
809 do_redraw_echoarea();
811 if (in_minibuffer == MB_COMPREAD)
812 do_redraw_minibuffer_compl();
814 wattr_off(echoarea, minibuffer_face.background, NULL);
817 static void
818 do_redraw_echoarea(void)
820 struct vline *vl;
822 if (ministate.curmesg != NULL)
823 wprintw(echoarea, "%s", ministate.curmesg);
824 else if (*keybuf != '\0')
825 waddstr(echoarea, keybuf);
826 else {
827 /* If nothing else, show the URL at point */
828 vl = current_tab->buffer.current_line;
829 if (vl != NULL && vl->parent->type == LINE_LINK)
830 waddstr(echoarea, vl->parent->alt);
834 static void
835 do_redraw_minibuffer(void)
837 struct buffer *cmplbuf, *buffer;
838 size_t off_y, off_x = 0;
839 const char *start, *c;
841 cmplbuf = &ministate.compl.buffer;
842 buffer = &ministate.buffer;
843 (void)off_y; /* unused, set by getyx */
845 wmove(echoarea, 0, 0);
847 if (in_minibuffer == MB_COMPREAD)
848 wprintw(echoarea, "(%2d) ",
849 cmplbuf->line_max);
851 wprintw(echoarea, "%s", ministate.prompt);
852 if (ministate.hist_cur != NULL)
853 wprintw(echoarea, "(%zu/%zu) ",
854 ministate.hist_off + 1,
855 ministate.history->len);
857 getyx(echoarea, off_y, off_x);
859 start = ministate.hist_cur != NULL
860 ? ministate.hist_cur->h
861 : ministate.buf;
862 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
863 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
864 start = utf8_next_cp(start);
867 waddstr(echoarea, start);
869 if (ministate.curmesg != NULL)
870 wprintw(echoarea, " [%s]", ministate.curmesg);
872 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
875 static void
876 do_redraw_minibuffer_compl(void)
878 redraw_window(minibuffer, 0, 10, COLS,
879 &ministate.compl.buffer);
882 /*
883 * Place the cursor in the right ncurses window. If soft is 1, use
884 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
885 * wrefresh.
886 */
887 static void
888 place_cursor(int soft)
890 int (*touch)(WINDOW *);
892 if (soft)
893 touch = wnoutrefresh;
894 else
895 touch = wrefresh;
897 if (in_minibuffer) {
898 if (side_window & SIDE_WINDOW_LEFT)
899 touch(help);
900 if (side_window & SIDE_WINDOW_BOTTOM)
901 touch(download);
902 touch(body);
903 touch(echoarea);
904 } else if (in_side_window & SIDE_WINDOW_LEFT) {
905 touch(body);
906 touch(echoarea);
907 if (in_side_window & SIDE_WINDOW_BOTTOM)
908 touch(download);
909 touch(help);
910 } else if (in_side_window & SIDE_WINDOW_BOTTOM) {
911 touch(body);
912 touch(echoarea);
913 if (in_side_window & SIDE_WINDOW_LEFT)
914 touch(help);
915 touch(download);
916 } else {
917 if (side_window & SIDE_WINDOW_LEFT)
918 touch(help);
919 if (side_window & SIDE_WINDOW_BOTTOM)
920 touch(download);
921 touch(echoarea);
922 touch(body);
926 static void
927 redraw_tab(struct tab *tab)
929 if (too_small)
930 return;
932 if (side_window & SIDE_WINDOW_LEFT) {
933 redraw_help();
934 wnoutrefresh(help);
937 if (side_window & SIDE_WINDOW_BOTTOM) {
938 redraw_download();
939 wnoutrefresh(download);
942 if (show_tab_bar)
943 redraw_tabline();
945 redraw_body(tab);
946 redraw_modeline(tab);
947 redraw_minibuffer();
949 wnoutrefresh(tabline);
950 wnoutrefresh(modeline);
952 if (in_minibuffer == MB_COMPREAD)
953 wnoutrefresh(minibuffer);
955 place_cursor(1);
957 doupdate();
959 if (set_title)
960 dprintf(1, "\033]2;%s - Telescope\a",
961 current_tab->buffer.page.title);
964 void
965 start_loading_anim(struct tab *tab)
967 if (tab->loading_anim)
968 return;
969 tab->loading_anim = 1;
970 evtimer_set(&tab->loadingev, update_loading_anim, tab);
971 evtimer_add(&tab->loadingev, &loadingev_timer);
974 static void
975 update_loading_anim(int fd, short ev, void *d)
977 struct tab *tab = d;
979 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
981 if (tab == current_tab) {
982 redraw_modeline(tab);
983 wrefresh(modeline);
984 wrefresh(body);
985 if (in_minibuffer)
986 wrefresh(echoarea);
989 evtimer_add(&tab->loadingev, &loadingev_timer);
992 static void
993 stop_loading_anim(struct tab *tab)
995 if (!tab->loading_anim)
996 return;
997 evtimer_del(&tab->loadingev);
998 tab->loading_anim = 0;
999 tab->loading_anim_step = 0;
1001 if (tab != current_tab)
1002 return;
1004 redraw_modeline(tab);
1006 wrefresh(modeline);
1007 wrefresh(body);
1008 if (in_minibuffer)
1009 wrefresh(echoarea);
1012 int
1013 ui_print_colors(void)
1015 int colors = 0, pairs = 0, can_change = 0;
1016 int columns = 16, lines, color, i, j;
1018 initscr();
1019 if (has_colors()) {
1020 start_color();
1021 use_default_colors();
1023 colors = COLORS;
1024 pairs = COLOR_PAIRS;
1025 can_change = can_change_color();
1027 endwin();
1029 printf("Term info:\n");
1030 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1031 getenv("TERM"), colors, pairs, can_change);
1032 printf("\n");
1034 if (colors == 0) {
1035 printf("No color support\n");
1036 return 0;
1039 printf("Available colors:\n\n");
1040 lines = (colors - 1) / columns + 1;
1041 color = 0;
1042 for (i = 0; i < lines; ++i) {
1043 for (j = 0; j < columns; ++j, ++color) {
1044 printf("\033[0;38;5;%dm %03d", color, color);
1046 printf("\n");
1049 printf("\033[0m");
1050 fflush(stdout);
1051 return 0;
1054 int
1055 ui_init()
1057 setlocale(LC_ALL, "");
1059 if (TAILQ_EMPTY(&global_map.m)) {
1060 fprintf(stderr, "no keys defined!\n");
1061 return 0;
1064 minibuffer_init();
1066 /* initialize download window */
1067 TAILQ_INIT(&downloadwin.head);
1068 TAILQ_INIT(&downloadwin.page.head);
1070 /* initialize help window */
1071 TAILQ_INIT(&helpwin.head);
1072 TAILQ_INIT(&helpwin.page.head);
1074 base_map = &global_map;
1075 current_map = &global_map;
1077 initscr();
1079 if (enable_colors) {
1080 if (has_colors()) {
1081 start_color();
1082 use_default_colors();
1083 } else
1084 enable_colors = 0;
1087 config_apply_style();
1089 raw();
1090 noecho();
1091 nonl();
1092 intrflush(stdscr, FALSE);
1094 if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1095 return 0;
1096 if ((body = newwin(1, 1, 0, 0)) == NULL)
1097 return 0;
1098 if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1099 return 0;
1100 if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1101 return 0;
1102 if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1103 return 0;
1104 if ((download = newwin(1, 1, 0, 0)) == NULL)
1105 return 0;
1106 if ((help = newwin(1, 1, 0, 0)) == NULL)
1107 return 0;
1109 wbkgd(body, body_face.body);
1110 wbkgd(download, download_face.background);
1111 wbkgd(echoarea, minibuffer_face.background);
1113 update_x_offset();
1115 keypad(body, TRUE);
1116 scrollok(body, FALSE);
1118 /* non-blocking input */
1119 wtimeout(body, 0);
1120 wtimeout(help, 0);
1122 mvwprintw(body, 0, 0, "");
1124 return 1;
1127 void
1128 ui_main_loop(void)
1130 evtimer_set(&resizeev, handle_resize, NULL);
1132 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1133 event_add(&stdioev, NULL);
1135 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1136 signal_add(&winchev, NULL);
1138 switch_to_tab(current_tab);
1139 rearrange_windows();
1142 void
1143 ui_on_tab_loaded(struct tab *tab)
1145 stop_loading_anim(tab);
1146 message("Loaded %s", tab->hist_cur->h);
1148 if (show_tab_bar)
1149 redraw_tabline();
1151 wrefresh(tabline);
1152 place_cursor(0);
1155 void
1156 ui_on_tab_refresh(struct tab *tab)
1158 wrap_page(&tab->buffer, body_cols);
1159 if (tab == current_tab)
1160 redraw_tab(tab);
1161 else
1162 tab->flags |= TAB_URGENT;
1165 void
1166 ui_on_download_refresh(void)
1168 if (side_window & SIDE_WINDOW_BOTTOM) {
1169 recompute_downloads();
1170 redraw_tab(current_tab);
1174 const char *
1175 ui_keyname(int k)
1177 return keyname(k);
1180 void
1181 ui_toggle_side_window(int kind)
1183 if (in_side_window & kind)
1184 ui_other_window();
1186 side_window ^= kind;
1187 if (side_window & SIDE_WINDOW_LEFT)
1188 recompute_help();
1189 if (side_window & SIDE_WINDOW_BOTTOM)
1190 recompute_downloads();
1193 * ugly hack, but otherwise the window doesn't get updated
1194 * until I call rearrange_windows a second time (e.g. via
1195 * C-l). I will be happy to know why something like this is
1196 * needed.
1198 rearrange_windows();
1199 rearrange_windows();
1202 void
1203 ui_show_downloads_pane(void)
1205 if (!(side_window & SIDE_WINDOW_BOTTOM))
1206 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1209 void
1210 ui_schedule_redraw(void)
1212 should_rearrange_windows = 1;
1215 void
1216 ui_require_input(struct tab *tab, int hide, int proto)
1218 void (*fn)(void);
1220 if (proto == PROTO_GEMINI)
1221 fn = ir_select_gemini;
1222 else if (proto == PROTO_GOPHER)
1223 fn = ir_select_gopher;
1224 else
1225 abort();
1227 /* TODO: hard-switching to another tab is ugly */
1228 switch_to_tab(tab);
1230 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1231 &ir_history, NULL, NULL);
1232 strlcpy(ministate.prompt, "Input required: ",
1233 sizeof(ministate.prompt));
1234 redraw_tab(tab);
1237 void
1238 ui_after_message_hook(void)
1240 redraw_minibuffer();
1241 place_cursor(0);
1244 void
1245 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1246 struct tab *data)
1248 yornp(prompt, fn, data);
1249 redraw_tab(current_tab);
1252 void
1253 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1254 struct tab *data, const char *input)
1256 minibuffer_read(prompt, fn, data);
1258 if (input != NULL) {
1259 strlcpy(ministate.buf, input, sizeof(ministate.buf));
1260 cmd_move_end_of_line(&ministate.buffer);
1263 redraw_tab(current_tab);
1266 void
1267 ui_other_window(void)
1269 if (in_side_window & SIDE_WINDOW_LEFT &&
1270 side_window & SIDE_WINDOW_BOTTOM)
1271 in_side_window = SIDE_WINDOW_BOTTOM;
1272 else if (in_side_window)
1273 in_side_window = 0;
1274 else if (!in_side_window && side_window & SIDE_WINDOW_LEFT)
1275 in_side_window = SIDE_WINDOW_LEFT;
1276 else if (!in_side_window && side_window)
1277 in_side_window = SIDE_WINDOW_BOTTOM;
1278 else
1279 message("No other window to select");
1282 void
1283 ui_suspend(void)
1285 endwin();
1287 kill(getpid(), SIGSTOP);
1289 refresh();
1290 clear();
1291 rearrange_windows();
1294 void
1295 ui_end(void)
1297 endwin();