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 int lk;
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 lk = lookup_key(&current_map, &thiskey, current_buffer());
258 if (lk == LK_UNBOUND) {
259 if (current_map->unhandled_input != NULL)
260 current_map->unhandled_input();
261 else
262 global_key_unbound();
264 if (lk != LK_ADVANCED_MAP) {
265 current_map = base_map;
266 strlcpy(keybuf, "", sizeof(keybuf));
269 if (side_window & SIDE_WINDOW_LEFT)
270 recompute_help();
272 if (should_rearrange_windows)
273 rearrange_windows();
274 redraw_tab(current_tab);
277 static void
278 handle_resize(int sig, short ev, void *d)
280 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
281 event_del(&resizeev);
283 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
284 evtimer_add(&resizeev, &resize_timer);
287 static void
288 handle_resize_nodelay(int s, short ev, void *d)
290 endwin();
291 refresh();
292 clear();
294 rearrange_windows();
297 static inline int
298 should_show_tab_bar(void)
300 if (tab_bar_show == -1)
301 return 0;
302 if (tab_bar_show == 0)
303 return 1;
305 return TAILQ_NEXT(TAILQ_FIRST(&tabshead), tabs) != NULL;
308 static void
309 rearrange_windows(void)
311 int lines;
312 int minibuffer_lines;
314 should_rearrange_windows = 0;
315 show_tab_bar = should_show_tab_bar();
317 lines = LINES;
319 /* 3 lines for the ui and 12 for the */
320 if ((too_small = lines < 15)) {
321 erase();
322 printw("Window too small.");
323 refresh();
324 return;
327 /* move and resize the windows, in reverse order! */
329 if (in_minibuffer == MB_COMPREAD) {
330 minibuffer_lines = MIN(10, lines/2);
331 mvwin(minibuffer, lines - minibuffer_lines, 0);
332 wresize(minibuffer, minibuffer_lines, COLS);
333 lines -= minibuffer_lines;
335 wrap_page(&ministate.compl.buffer, COLS);
338 mvwin(echoarea, --lines, 0);
339 wresize(echoarea, 1, COLS);
341 mvwin(modeline, --lines, 0);
342 wresize(modeline, 1, COLS);
344 if (side_window & SIDE_WINDOW_BOTTOM) {
345 download_lines = MIN(5, lines/2);
346 mvwin(download, lines - download_lines, 0);
347 wresize(download, download_lines, COLS);
348 lines -= download_lines;
350 wrap_page(&downloadwin, COLS);
353 body_lines = show_tab_bar ? --lines : lines;
354 body_cols = COLS;
356 /*
357 * Here we make the assumption that show_tab_bar is either 0
358 * or 1, and reuse that as argument to mvwin.
359 */
360 if (side_window & SIDE_WINDOW_LEFT) {
361 help_cols = 0.3 * COLS;
362 help_lines = lines;
363 mvwin(help, show_tab_bar, 0);
364 wresize(help, help_lines, help_cols);
366 wrap_page(&helpwin, help_cols);
368 body_cols = COLS - help_cols - 1;
369 mvwin(body, show_tab_bar, help_cols);
370 } else
371 mvwin(body, show_tab_bar, 0);
373 update_x_offset();
374 wresize(body, body_lines, body_cols);
376 if (show_tab_bar)
377 wresize(tabline, 1, COLS);
379 wrap_page(&current_tab->buffer, body_cols);
380 redraw_tab(current_tab);
383 static void
384 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
385 const char **prfx_ret, const char **text_ret)
387 int type, i, cont, width;
388 char *space, *t;
390 if ((*text_ret = vl->line) == NULL)
391 *text_ret = "";
393 cont = vl->flags & L_CONTINUATION;
394 type = vl->parent->type;
395 if (!cont)
396 *prfx_ret = line_prefixes[type].prfx1;
397 else
398 *prfx_ret = line_prefixes[type].prfx2;
400 space = vl->parent->data;
401 if (!emojify_link || type != LINE_LINK || space == NULL)
402 return;
404 if (cont) {
405 memset(buf, 0, len);
406 width = utf8_swidth_between(vl->parent->line, space);
407 for (i = 0; i < width + 1; ++i)
408 strlcat(buf, " ", len);
409 } else {
410 strlcpy(buf, *text_ret, len);
411 if ((t = strchr(buf, ' ')) != NULL)
412 *t = '\0';
413 strlcat(buf, " ", len);
415 /* skip the emoji */
416 *text_ret += (space - vl->parent->line) + 1;
419 *prfx_ret = buf;
422 static inline void
423 print_vline_descr(int width, WINDOW *window, struct vline *vl)
425 int x, y, goal;
427 switch (vl->parent->type) {
428 case LINE_COMPL:
429 case LINE_COMPL_CURRENT:
430 goal = width/2;
431 break;
432 case LINE_DOWNLOAD:
433 case LINE_DOWNLOAD_DONE:
434 goal = width/4;
435 break;
436 case LINE_HELP:
437 goal = 8;
438 break;
439 default:
440 return;
443 if (vl->parent->alt == NULL)
444 return;
446 (void)y;
447 getyx(window, y, x);
449 if (goal <= x)
450 wprintw(window, " ");
451 for (; goal > x; ++x)
452 wprintw(window, " ");
454 wprintw(window, "%s", vl->parent->alt);
457 /*
458 * Core part of the rendering. It prints a vline starting from the
459 * current cursor position. Printing a vline consists of skipping
460 * `off' columns (for olivetti-mode), print the correct prefix (which
461 * may be the emoji in case of emojified links-lines), printing the
462 * text itself, filling until width - off and filling off columns
463 * again.
464 */
465 static void
466 print_vline(int off, int width, WINDOW *window, struct vline *vl)
468 /*
469 * Believe me or not, I've seen emoji ten code points long!
470 * That means, to stay large, 4*10 bytes + NUL.
471 */
472 char emojibuf[41] = {0};
473 const char *text, *prfx;
474 struct line_face *f;
475 int i, left, x, y;
477 f = &line_faces[vl->parent->type];
479 /* unused, set by getyx */
480 (void)y;
482 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
484 wattr_on(window, body_face.left, NULL);
485 for (i = 0; i < off; i++)
486 waddch(window, ' ');
487 wattr_off(window, body_face.left, NULL);
489 wattr_on(window, f->prefix, NULL);
490 wprintw(window, "%s", prfx);
491 wattr_off(window, f->prefix, NULL);
493 wattr_on(window, f->text, NULL);
494 wprintw(window, "%s", text);
495 print_vline_descr(width, window, vl);
496 wattr_off(window, f->text, NULL);
498 getyx(window, y, x);
500 left = width - x;
502 wattr_on(window, f->trail, NULL);
503 for (i = 0; i < left - off; ++i)
504 waddch(window, ' ');
505 wattr_off(window, f->trail, NULL);
507 wattr_on(window, body_face.right, NULL);
508 for (i = 0; i < off; i++)
509 waddch(window, ' ');
510 wattr_off(window, body_face.right, NULL);
514 static void
515 redraw_tabline(void)
517 struct tab *tab;
518 size_t toskip, ots, tabwidth, space, x;
519 int current, y, truncated, pair;
520 const char *title;
521 char buf[25];
523 x = 0;
525 /* unused, but setted by a getyx */
526 (void)y;
528 tabwidth = sizeof(buf)+1;
529 space = COLS-2;
531 toskip = 0;
532 TAILQ_FOREACH(tab, &tabshead, tabs) {
533 toskip++;
534 if (tab == current_tab)
535 break;
538 if (toskip * tabwidth < space)
539 toskip = 0;
540 else {
541 ots = toskip;
542 toskip--;
543 while (toskip != 0 &&
544 (ots - toskip+1) * tabwidth < space)
545 toskip--;
548 werase(tabline);
549 wattr_on(tabline, tab_face.background, NULL);
550 wprintw(tabline, toskip == 0 ? " " : "<");
551 wattr_off(tabline, tab_face.background, NULL);
553 truncated = 0;
554 TAILQ_FOREACH(tab, &tabshead, tabs) {
555 if (truncated)
556 break;
557 if (toskip != 0) {
558 toskip--;
559 continue;
562 getyx(tabline, y, x);
563 if (x + sizeof(buf)+2 >= (size_t)COLS)
564 truncated = 1;
566 current = tab == current_tab;
568 if (*(title = tab->buffer.page.title) == '\0')
569 title = tab->hist_cur->h;
571 if (tab->flags & TAB_URGENT)
572 strlcpy(buf, "!", sizeof(buf));
573 else
574 strlcpy(buf, " ", sizeof(buf));
576 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
577 /* truncation happens */
578 strlcpy(&buf[sizeof(buf)-4], "...", 4);
579 } else {
580 /* pad with spaces */
581 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
582 /* nop */ ;
585 pair = current ? tab_face.current : tab_face.tab;
586 wattr_on(tabline, pair, NULL);
587 wprintw(tabline, "%s", buf);
588 wattr_off(tabline, pair, NULL);
590 wattr_on(tabline, tab_face.background, NULL);
591 if (TAILQ_NEXT(tab, tabs) != NULL)
592 wprintw(tabline, "┃");
593 wattr_off(tabline, tab_face.background, NULL);
596 wattr_on(tabline, tab_face.background, NULL);
597 for (; x < (size_t)COLS; ++x)
598 waddch(tabline, ' ');
599 if (truncated)
600 mvwprintw(tabline, 0, COLS-1, ">");
601 wattr_off(tabline, tab_face.background, NULL);
604 /*
605 * Compute the first visible line around vl. Try to search forward
606 * until the end of the buffer; if a visible line is not found, search
607 * backward. Return NULL if no viable line was found.
608 */
609 struct vline *
610 adjust_line(struct vline *vl, struct buffer *buffer)
612 struct vline *t;
614 if (vl == NULL)
615 return NULL;
617 if (!(vl->parent->flags & L_HIDDEN))
618 return vl;
620 /* search forward */
621 for (t = vl;
622 t != NULL && t->parent->flags & L_HIDDEN;
623 t = TAILQ_NEXT(t, vlines))
624 ; /* nop */
626 if (t != NULL)
627 return t;
629 /* search backward */
630 for (t = vl;
631 t != NULL && t->parent->flags & L_HIDDEN;
632 t = TAILQ_PREV(t, vhead, vlines))
633 ; /* nop */
635 return t;
638 static void
639 redraw_window(WINDOW *win, int off, int height, int width,
640 struct buffer *buffer)
642 struct vline *vl;
643 int l, onscreen;
645 restore_curs_x(buffer);
647 /*
648 * TODO: ignoring buffer->force_update and always
649 * re-rendering. In theory we can recompute the y position
650 * without a re-render, and optimize here. It's not the only
651 * optimisation possible here, wscrl wolud also be an
652 * interesting one.
653 */
655 again:
656 werase(win);
657 buffer->curs_y = 0;
659 if (TAILQ_EMPTY(&buffer->head))
660 goto end;
662 if (buffer->top_line == NULL)
663 buffer->top_line = TAILQ_FIRST(&buffer->head);
665 buffer->top_line = adjust_line(buffer->top_line, buffer);
666 if (buffer->top_line == NULL)
667 goto end;
669 buffer->current_line = adjust_line(buffer->current_line, buffer);
671 l = 0;
672 onscreen = 0;
673 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
674 if (vl->parent->flags & L_HIDDEN)
675 continue;
677 wmove(win, l, 0);
678 print_vline(off, width, win, vl);
680 if (vl == buffer->current_line)
681 onscreen = 1;
683 if (!onscreen)
684 buffer->curs_y++;
686 l++;
687 if (l == height)
688 break;
691 if (!onscreen) {
692 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
693 if (vl == buffer->current_line)
694 break;
695 if (vl->parent->flags & L_HIDDEN)
696 continue;
697 buffer->line_off++;
698 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
701 if (vl != NULL)
702 goto again;
705 buffer->last_line_off = buffer->line_off;
706 buffer->force_redraw = 0;
707 end:
708 wmove(win, buffer->curs_y, buffer->curs_x);
711 static void
712 redraw_download(void)
714 redraw_window(download, 0, download_lines, COLS, &downloadwin);
717 static void
718 redraw_help(void)
720 redraw_window(help, 0, help_lines, help_cols, &helpwin);
723 static void
724 redraw_body(struct tab *tab)
726 static struct tab *last_tab;
728 if (last_tab != tab)
729 tab->buffer.force_redraw =1;
730 last_tab = tab;
732 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
735 static inline char
736 trust_status_char(enum trust_state ts)
738 switch (ts) {
739 case TS_UNKNOWN: return '-';
740 case TS_UNTRUSTED: return '!';
741 case TS_TEMP_TRUSTED: return '!';
742 case TS_TRUSTED: return 'v';
743 case TS_VERIFIED: return 'V';
744 default: return 'X';
748 static void
749 redraw_modeline(struct tab *tab)
751 struct buffer *buffer;
752 double pct;
753 int x, y, max_x, max_y;
754 const char *mode;
755 const char *spin = "-\\|/";
757 buffer = current_buffer();
758 mode = buffer->page.name;
760 werase(modeline);
761 wattr_on(modeline, modeline_face.background, NULL);
762 wmove(modeline, 0, 0);
764 wprintw(modeline, "-%c%c %s ",
765 spin[tab->loading_anim_step],
766 trust_status_char(tab->trust),
767 mode == NULL ? "(none)" : mode);
769 pct = (buffer->line_off + buffer->curs_y) * 100.0
770 / buffer->line_max;
772 if (buffer->line_max <= (size_t)body_lines)
773 wprintw(modeline, "All ");
774 else if (buffer->line_off == 0)
775 wprintw(modeline, "Top ");
776 else if (buffer->line_off + body_lines >= buffer->line_max)
777 wprintw(modeline, "Bottom ");
778 else
779 wprintw(modeline, "%.0f%% ", pct);
781 wprintw(modeline, "%d/%d %s ",
782 buffer->line_off + buffer->curs_y,
783 buffer->line_max,
784 tab->hist_cur->h);
786 getyx(modeline, y, x);
787 getmaxyx(modeline, max_y, max_x);
789 (void)y;
790 (void)max_y;
792 for (; x < max_x; ++x)
793 waddstr(modeline, "-");
795 wattr_off(modeline, modeline_face.background, NULL);
798 static void
799 redraw_minibuffer(void)
801 wattr_on(echoarea, minibuffer_face.background, NULL);
802 werase(echoarea);
804 if (in_minibuffer)
805 do_redraw_minibuffer();
806 else
807 do_redraw_echoarea();
809 if (in_minibuffer == MB_COMPREAD)
810 do_redraw_minibuffer_compl();
812 wattr_off(echoarea, minibuffer_face.background, NULL);
815 static void
816 do_redraw_echoarea(void)
818 struct vline *vl;
820 if (ministate.curmesg != NULL)
821 wprintw(echoarea, "%s", ministate.curmesg);
822 else if (*keybuf != '\0')
823 waddstr(echoarea, keybuf);
824 else {
825 /* If nothing else, show the URL at point */
826 vl = current_tab->buffer.current_line;
827 if (vl != NULL && vl->parent->type == LINE_LINK)
828 waddstr(echoarea, vl->parent->alt);
832 static void
833 do_redraw_minibuffer(void)
835 struct buffer *cmplbuf, *buffer;
836 size_t off_y, off_x = 0;
837 const char *start, *c;
839 cmplbuf = &ministate.compl.buffer;
840 buffer = &ministate.buffer;
841 (void)off_y; /* unused, set by getyx */
843 wmove(echoarea, 0, 0);
845 if (in_minibuffer == MB_COMPREAD)
846 wprintw(echoarea, "(%2d) ",
847 cmplbuf->line_max);
849 wprintw(echoarea, "%s", ministate.prompt);
850 if (ministate.hist_cur != NULL)
851 wprintw(echoarea, "(%zu/%zu) ",
852 ministate.hist_off + 1,
853 ministate.history->len);
855 getyx(echoarea, off_y, off_x);
857 start = ministate.hist_cur != NULL
858 ? ministate.hist_cur->h
859 : ministate.buf;
860 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
861 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
862 start = utf8_next_cp(start);
865 waddstr(echoarea, start);
867 if (ministate.curmesg != NULL)
868 wprintw(echoarea, " [%s]", ministate.curmesg);
870 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
873 static void
874 do_redraw_minibuffer_compl(void)
876 redraw_window(minibuffer, 0, 10, COLS,
877 &ministate.compl.buffer);
880 /*
881 * Place the cursor in the right ncurses window. If soft is 1, use
882 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
883 * wrefresh.
884 */
885 static void
886 place_cursor(int soft)
888 int (*touch)(WINDOW *);
890 if (soft)
891 touch = wnoutrefresh;
892 else
893 touch = wrefresh;
895 if (in_minibuffer) {
896 if (side_window & SIDE_WINDOW_LEFT)
897 touch(help);
898 if (side_window & SIDE_WINDOW_BOTTOM)
899 touch(download);
900 touch(body);
901 touch(echoarea);
902 } else if (in_side_window & SIDE_WINDOW_LEFT) {
903 touch(body);
904 touch(echoarea);
905 if (in_side_window & SIDE_WINDOW_BOTTOM)
906 touch(download);
907 touch(help);
908 } else if (in_side_window & SIDE_WINDOW_BOTTOM) {
909 touch(body);
910 touch(echoarea);
911 if (in_side_window & SIDE_WINDOW_LEFT)
912 touch(help);
913 touch(download);
914 } else {
915 if (side_window & SIDE_WINDOW_LEFT)
916 touch(help);
917 if (side_window & SIDE_WINDOW_BOTTOM)
918 touch(download);
919 touch(echoarea);
920 touch(body);
924 static void
925 redraw_tab(struct tab *tab)
927 if (too_small)
928 return;
930 if (side_window & SIDE_WINDOW_LEFT) {
931 redraw_help();
932 wnoutrefresh(help);
935 if (side_window & SIDE_WINDOW_BOTTOM) {
936 redraw_download();
937 wnoutrefresh(download);
940 if (show_tab_bar)
941 redraw_tabline();
943 redraw_body(tab);
944 redraw_modeline(tab);
945 redraw_minibuffer();
947 wnoutrefresh(tabline);
948 wnoutrefresh(modeline);
950 if (in_minibuffer == MB_COMPREAD)
951 wnoutrefresh(minibuffer);
953 place_cursor(1);
955 doupdate();
957 if (set_title)
958 dprintf(1, "\033]2;%s - Telescope\a",
959 current_tab->buffer.page.title);
962 void
963 start_loading_anim(struct tab *tab)
965 if (tab->loading_anim)
966 return;
967 tab->loading_anim = 1;
968 evtimer_set(&tab->loadingev, update_loading_anim, tab);
969 evtimer_add(&tab->loadingev, &loadingev_timer);
972 static void
973 update_loading_anim(int fd, short ev, void *d)
975 struct tab *tab = d;
977 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
979 if (tab == current_tab) {
980 redraw_modeline(tab);
981 wrefresh(modeline);
982 wrefresh(body);
983 if (in_minibuffer)
984 wrefresh(echoarea);
987 evtimer_add(&tab->loadingev, &loadingev_timer);
990 static void
991 stop_loading_anim(struct tab *tab)
993 if (!tab->loading_anim)
994 return;
995 evtimer_del(&tab->loadingev);
996 tab->loading_anim = 0;
997 tab->loading_anim_step = 0;
999 if (tab != current_tab)
1000 return;
1002 redraw_modeline(tab);
1004 wrefresh(modeline);
1005 wrefresh(body);
1006 if (in_minibuffer)
1007 wrefresh(echoarea);
1010 int
1011 ui_print_colors(void)
1013 int colors = 0, pairs = 0, can_change = 0;
1014 int columns = 16, lines, color, i, j;
1016 initscr();
1017 if (has_colors()) {
1018 start_color();
1019 use_default_colors();
1021 colors = COLORS;
1022 pairs = COLOR_PAIRS;
1023 can_change = can_change_color();
1025 endwin();
1027 printf("Term info:\n");
1028 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1029 getenv("TERM"), colors, pairs, can_change);
1030 printf("\n");
1032 if (colors == 0) {
1033 printf("No color support\n");
1034 return 0;
1037 printf("Available colors:\n\n");
1038 lines = (colors - 1) / columns + 1;
1039 color = 0;
1040 for (i = 0; i < lines; ++i) {
1041 for (j = 0; j < columns; ++j, ++color) {
1042 printf("\033[0;38;5;%dm %03d", color, color);
1044 printf("\n");
1047 printf("\033[0m");
1048 fflush(stdout);
1049 return 0;
1052 int
1053 ui_init()
1055 setlocale(LC_ALL, "");
1057 if (TAILQ_EMPTY(&global_map.m)) {
1058 fprintf(stderr, "no keys defined!\n");
1059 return 0;
1062 minibuffer_init();
1064 /* initialize download window */
1065 TAILQ_INIT(&downloadwin.head);
1066 TAILQ_INIT(&downloadwin.page.head);
1068 /* initialize help window */
1069 TAILQ_INIT(&helpwin.head);
1070 TAILQ_INIT(&helpwin.page.head);
1072 base_map = &global_map;
1073 current_map = &global_map;
1075 initscr();
1077 if (enable_colors) {
1078 if (has_colors()) {
1079 start_color();
1080 use_default_colors();
1081 } else
1082 enable_colors = 0;
1085 config_apply_style();
1087 raw();
1088 noecho();
1089 nonl();
1090 intrflush(stdscr, FALSE);
1092 if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1093 return 0;
1094 if ((body = newwin(1, 1, 0, 0)) == NULL)
1095 return 0;
1096 if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1097 return 0;
1098 if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1099 return 0;
1100 if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1101 return 0;
1102 if ((download = newwin(1, 1, 0, 0)) == NULL)
1103 return 0;
1104 if ((help = newwin(1, 1, 0, 0)) == NULL)
1105 return 0;
1107 wbkgd(body, body_face.body);
1108 wbkgd(download, download_face.background);
1109 wbkgd(echoarea, minibuffer_face.background);
1111 update_x_offset();
1113 keypad(body, TRUE);
1114 scrollok(body, FALSE);
1116 /* non-blocking input */
1117 wtimeout(body, 0);
1118 wtimeout(help, 0);
1120 mvwprintw(body, 0, 0, "");
1122 evtimer_set(&resizeev, handle_resize, NULL);
1124 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1125 event_add(&stdioev, NULL);
1127 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1128 signal_add(&winchev, NULL);
1130 return 1;
1133 void
1134 ui_main_loop(void)
1136 switch_to_tab(current_tab);
1137 rearrange_windows();
1139 event_dispatch();
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();