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, 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 if (vl->parent->type == LINE_FRINGE && fringe_ignore_offset)
485 off = 0;
487 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
489 wattr_on(window, body_face.left, NULL);
490 for (i = 0; i < off; i++)
491 waddch(window, ' ');
492 wattr_off(window, body_face.left, NULL);
494 wattr_on(window, f->prefix, NULL);
495 wprintw(window, "%s", prfx);
496 wattr_off(window, f->prefix, NULL);
498 wattr_on(window, f->text, NULL);
499 wprintw(window, "%s", text);
500 print_vline_descr(width, window, vl);
501 wattr_off(window, f->text, NULL);
503 getyx(window, y, x);
505 left = width - x;
507 wattr_on(window, f->trail, NULL);
508 for (i = 0; i < left - off; ++i)
509 waddch(window, ' ');
510 wattr_off(window, f->trail, NULL);
512 wattr_on(window, body_face.right, NULL);
513 for (i = 0; i < off; i++)
514 waddch(window, ' ');
515 wattr_off(window, body_face.right, NULL);
519 static void
520 redraw_tabline(void)
522 struct tab *tab;
523 size_t toskip, ots, tabwidth, space, x;
524 int current, y, truncated, pair;
525 const char *title;
526 char buf[25];
528 x = 0;
530 /* unused, but setted by a getyx */
531 (void)y;
533 tabwidth = sizeof(buf)+1;
534 space = COLS-2;
536 toskip = 0;
537 TAILQ_FOREACH(tab, &tabshead, tabs) {
538 toskip++;
539 if (tab == current_tab)
540 break;
543 if (toskip * tabwidth <= space)
544 toskip = 0;
545 else {
546 ots = toskip;
547 toskip--;
548 while (toskip != 0 &&
549 (ots - toskip+1) * tabwidth < space)
550 toskip--;
553 werase(tabline);
554 wattr_on(tabline, tab_face.background, NULL);
555 wprintw(tabline, toskip == 0 ? " " : "<");
556 wattr_off(tabline, tab_face.background, NULL);
558 truncated = 0;
559 TAILQ_FOREACH(tab, &tabshead, tabs) {
560 if (truncated)
561 break;
562 if (toskip != 0) {
563 toskip--;
564 continue;
567 getyx(tabline, y, x);
568 if (x + sizeof(buf)+2 >= (size_t)COLS)
569 truncated = 1;
571 current = tab == current_tab;
573 if (*(title = tab->buffer.page.title) == '\0')
574 title = tab->hist_cur->h;
576 if (tab->flags & TAB_URGENT)
577 strlcpy(buf, "!", sizeof(buf));
578 else
579 strlcpy(buf, " ", sizeof(buf));
581 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
582 /* truncation happens */
583 strlcpy(&buf[sizeof(buf)-4], "...", 4);
584 } else {
585 /* pad with spaces */
586 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
587 /* nop */ ;
590 pair = current ? tab_face.current : tab_face.tab;
591 wattr_on(tabline, pair, NULL);
592 wprintw(tabline, "%s", buf);
593 wattr_off(tabline, pair, NULL);
595 wattr_on(tabline, tab_face.background, NULL);
596 if (TAILQ_NEXT(tab, tabs) != NULL)
597 wprintw(tabline, "┃");
598 wattr_off(tabline, tab_face.background, NULL);
601 wattr_on(tabline, tab_face.background, NULL);
602 for (; x < (size_t)COLS; ++x)
603 waddch(tabline, ' ');
604 if (truncated)
605 mvwprintw(tabline, 0, COLS-1, ">");
606 wattr_off(tabline, tab_face.background, NULL);
609 /*
610 * Compute the first visible line around vl. Try to search forward
611 * until the end of the buffer; if a visible line is not found, search
612 * backward. Return NULL if no viable line was found.
613 */
614 struct vline *
615 adjust_line(struct vline *vl, struct buffer *buffer)
617 struct vline *t;
619 if (vl == NULL)
620 return NULL;
622 if (!(vl->parent->flags & L_HIDDEN))
623 return vl;
625 /* search forward */
626 for (t = vl;
627 t != NULL && t->parent->flags & L_HIDDEN;
628 t = TAILQ_NEXT(t, vlines))
629 ; /* nop */
631 if (t != NULL)
632 return t;
634 /* search backward */
635 for (t = vl;
636 t != NULL && t->parent->flags & L_HIDDEN;
637 t = TAILQ_PREV(t, vhead, vlines))
638 ; /* nop */
640 return t;
643 static void
644 redraw_window(WINDOW *win, int off, int height, int width,
645 int show_fringe, struct buffer *buffer)
647 struct vline *vl;
648 int onscreen = 0, l = 0;
650 restore_curs_x(buffer);
652 /*
653 * TODO: ignoring buffer->force_update and always
654 * re-rendering. In theory we can recompute the y position
655 * without a re-render, and optimize here. It's not the only
656 * optimisation possible here, wscrl wolud also be an
657 * interesting one.
658 */
660 again:
661 werase(win);
662 buffer->curs_y = 0;
664 if (TAILQ_EMPTY(&buffer->head))
665 goto end;
667 if (buffer->top_line == NULL)
668 buffer->top_line = TAILQ_FIRST(&buffer->head);
670 buffer->top_line = adjust_line(buffer->top_line, buffer);
671 if (buffer->top_line == NULL)
672 goto end;
674 buffer->current_line = adjust_line(buffer->current_line, buffer);
676 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
677 if (vl->parent->flags & L_HIDDEN)
678 continue;
680 wmove(win, l, 0);
681 print_vline(off, width, win, vl);
683 if (vl == buffer->current_line)
684 onscreen = 1;
686 if (!onscreen)
687 buffer->curs_y++;
689 l++;
690 if (l == height)
691 break;
694 if (!onscreen) {
695 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
696 if (vl == buffer->current_line)
697 break;
698 if (vl->parent->flags & L_HIDDEN)
699 continue;
700 buffer->line_off++;
701 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
704 if (vl != NULL)
705 goto again;
708 buffer->last_line_off = buffer->line_off;
709 buffer->force_redraw = 0;
710 end:
711 for (; show_fringe && l < height; l++)
712 print_vline(off, width, win, &fringe);
714 wmove(win, buffer->curs_y, buffer->curs_x);
717 static void
718 redraw_download(void)
720 redraw_window(download, 0, download_lines, COLS, 0, &downloadwin);
723 static void
724 redraw_help(void)
726 redraw_window(help, 0, help_lines, help_cols, 1, &helpwin);
729 static void
730 redraw_body(struct tab *tab)
732 static struct tab *last_tab;
734 if (last_tab != tab)
735 tab->buffer.force_redraw =1;
736 last_tab = tab;
738 redraw_window(body, x_offset, body_lines, body_cols, 1, &tab->buffer);
741 static inline char
742 trust_status_char(enum trust_state ts)
744 switch (ts) {
745 case TS_UNKNOWN: return '-';
746 case TS_UNTRUSTED: return '!';
747 case TS_TEMP_TRUSTED: return '!';
748 case TS_TRUSTED: return 'v';
749 case TS_VERIFIED: return 'V';
750 default: return 'X';
754 static void
755 redraw_modeline(struct tab *tab)
757 struct buffer *buffer;
758 double pct;
759 int x, y, max_x, max_y;
760 const char *mode;
761 const char *spin = "-\\|/";
763 buffer = current_buffer();
764 mode = buffer->page.name;
766 werase(modeline);
767 wattr_on(modeline, modeline_face.background, NULL);
768 wmove(modeline, 0, 0);
770 wprintw(modeline, "-%c%c- %s ",
771 spin[tab->loading_anim_step],
772 trust_status_char(tab->trust),
773 mode == NULL ? "(none)" : mode);
775 pct = (buffer->line_off + buffer->curs_y) * 100.0
776 / buffer->line_max;
778 if (buffer->line_max <= (size_t)body_lines)
779 wprintw(modeline, "All ");
780 else if (buffer->line_off == 0)
781 wprintw(modeline, "Top ");
782 else if (buffer->line_off + body_lines >= buffer->line_max)
783 wprintw(modeline, "Bottom ");
784 else
785 wprintw(modeline, "%.0f%% ", pct);
787 wprintw(modeline, "%d/%d %s ",
788 buffer->line_off + buffer->curs_y,
789 buffer->line_max,
790 tab->hist_cur->h);
792 getyx(modeline, y, x);
793 getmaxyx(modeline, max_y, max_x);
795 (void)y;
796 (void)max_y;
798 for (; x < max_x; ++x)
799 waddstr(modeline, "-");
801 wattr_off(modeline, modeline_face.background, NULL);
804 static void
805 redraw_minibuffer(void)
807 wattr_on(echoarea, minibuffer_face.background, NULL);
808 werase(echoarea);
810 if (in_minibuffer)
811 do_redraw_minibuffer();
812 else
813 do_redraw_echoarea();
815 if (in_minibuffer == MB_COMPREAD)
816 do_redraw_minibuffer_compl();
818 wattr_off(echoarea, minibuffer_face.background, NULL);
821 static void
822 do_redraw_echoarea(void)
824 struct vline *vl;
826 if (ministate.curmesg != NULL)
827 wprintw(echoarea, "%s", ministate.curmesg);
828 else if (*keybuf != '\0')
829 waddstr(echoarea, keybuf);
830 else {
831 /* If nothing else, show the URL at point */
832 vl = current_tab->buffer.current_line;
833 if (vl != NULL && vl->parent->type == LINE_LINK)
834 waddstr(echoarea, vl->parent->alt);
838 static void
839 do_redraw_minibuffer(void)
841 struct buffer *cmplbuf, *buffer;
842 size_t off_y, off_x = 0;
843 const char *start, *c;
845 cmplbuf = &ministate.compl.buffer;
846 buffer = &ministate.buffer;
847 (void)off_y; /* unused, set by getyx */
849 wmove(echoarea, 0, 0);
851 if (in_minibuffer == MB_COMPREAD)
852 wprintw(echoarea, "(%2d) ",
853 cmplbuf->line_max);
855 wprintw(echoarea, "%s", ministate.prompt);
856 if (ministate.hist_cur != NULL)
857 wprintw(echoarea, "(%zu/%zu) ",
858 ministate.hist_off + 1,
859 ministate.history->len);
861 getyx(echoarea, off_y, off_x);
863 start = ministate.hist_cur != NULL
864 ? ministate.hist_cur->h
865 : ministate.buf;
866 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
867 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
868 start = utf8_next_cp(start);
871 waddstr(echoarea, start);
873 if (ministate.curmesg != NULL)
874 wprintw(echoarea, " [%s]", ministate.curmesg);
876 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
879 static void
880 do_redraw_minibuffer_compl(void)
882 redraw_window(minibuffer, 0, 10, COLS, 1,
883 &ministate.compl.buffer);
886 /*
887 * Place the cursor in the right ncurses window. If soft is 1, use
888 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
889 * wrefresh.
890 */
891 static void
892 place_cursor(int soft)
894 int (*touch)(WINDOW *);
896 if (soft)
897 touch = wnoutrefresh;
898 else
899 touch = wrefresh;
901 if (in_minibuffer) {
902 if (side_window & SIDE_WINDOW_LEFT)
903 touch(help);
904 if (side_window & SIDE_WINDOW_BOTTOM)
905 touch(download);
906 touch(body);
907 touch(echoarea);
908 } else if (in_side_window & SIDE_WINDOW_LEFT) {
909 touch(body);
910 touch(echoarea);
911 if (in_side_window & SIDE_WINDOW_BOTTOM)
912 touch(download);
913 touch(help);
914 } else if (in_side_window & SIDE_WINDOW_BOTTOM) {
915 touch(body);
916 touch(echoarea);
917 if (in_side_window & SIDE_WINDOW_LEFT)
918 touch(help);
919 touch(download);
920 } else {
921 if (side_window & SIDE_WINDOW_LEFT)
922 touch(help);
923 if (side_window & SIDE_WINDOW_BOTTOM)
924 touch(download);
925 touch(echoarea);
926 touch(body);
930 static void
931 redraw_tab(struct tab *tab)
933 if (too_small)
934 return;
936 if (side_window & SIDE_WINDOW_LEFT) {
937 redraw_help();
938 wnoutrefresh(help);
941 if (side_window & SIDE_WINDOW_BOTTOM) {
942 redraw_download();
943 wnoutrefresh(download);
946 if (show_tab_bar)
947 redraw_tabline();
949 redraw_body(tab);
950 redraw_modeline(tab);
951 redraw_minibuffer();
953 wnoutrefresh(tabline);
954 wnoutrefresh(modeline);
956 if (in_minibuffer == MB_COMPREAD)
957 wnoutrefresh(minibuffer);
959 place_cursor(1);
961 doupdate();
963 if (set_title)
964 dprintf(1, "\033]2;%s - Telescope\a",
965 current_tab->buffer.page.title);
968 void
969 start_loading_anim(struct tab *tab)
971 if (tab->loading_anim)
972 return;
973 tab->loading_anim = 1;
974 evtimer_set(&tab->loadingev, update_loading_anim, tab);
975 evtimer_add(&tab->loadingev, &loadingev_timer);
978 static void
979 update_loading_anim(int fd, short ev, void *d)
981 struct tab *tab = d;
983 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
985 if (tab == current_tab) {
986 redraw_modeline(tab);
987 wrefresh(modeline);
988 wrefresh(body);
989 if (in_minibuffer)
990 wrefresh(echoarea);
993 evtimer_add(&tab->loadingev, &loadingev_timer);
996 static void
997 stop_loading_anim(struct tab *tab)
999 if (!tab->loading_anim)
1000 return;
1001 evtimer_del(&tab->loadingev);
1002 tab->loading_anim = 0;
1003 tab->loading_anim_step = 0;
1005 if (tab != current_tab)
1006 return;
1008 redraw_modeline(tab);
1010 wrefresh(modeline);
1011 wrefresh(body);
1012 if (in_minibuffer)
1013 wrefresh(echoarea);
1016 int
1017 ui_print_colors(void)
1019 int colors = 0, pairs = 0, can_change = 0;
1020 int columns = 16, lines, color, i, j;
1022 initscr();
1023 if (has_colors()) {
1024 start_color();
1025 use_default_colors();
1027 colors = COLORS;
1028 pairs = COLOR_PAIRS;
1029 can_change = can_change_color();
1031 endwin();
1033 printf("Term info:\n");
1034 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1035 getenv("TERM"), colors, pairs, can_change);
1036 printf("\n");
1038 if (colors == 0) {
1039 printf("No color support\n");
1040 return 0;
1043 printf("Available colors:\n\n");
1044 lines = (colors - 1) / columns + 1;
1045 color = 0;
1046 for (i = 0; i < lines; ++i) {
1047 for (j = 0; j < columns; ++j, ++color) {
1048 printf("\033[0;38;5;%dm %03d", color, color);
1050 printf("\n");
1053 printf("\033[0m");
1054 fflush(stdout);
1055 return 0;
1058 int
1059 ui_init()
1061 setlocale(LC_ALL, "");
1063 if (TAILQ_EMPTY(&global_map.m)) {
1064 fprintf(stderr, "no keys defined!\n");
1065 return 0;
1068 minibuffer_init();
1070 /* initialize download window */
1071 TAILQ_INIT(&downloadwin.head);
1072 TAILQ_INIT(&downloadwin.page.head);
1074 /* initialize help window */
1075 TAILQ_INIT(&helpwin.head);
1076 TAILQ_INIT(&helpwin.page.head);
1078 base_map = &global_map;
1079 current_map = &global_map;
1081 initscr();
1083 if (enable_colors) {
1084 if (has_colors()) {
1085 start_color();
1086 use_default_colors();
1087 } else
1088 enable_colors = 0;
1091 config_apply_style();
1093 raw();
1094 noecho();
1095 nonl();
1096 intrflush(stdscr, FALSE);
1098 if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1099 return 0;
1100 if ((body = newwin(1, 1, 0, 0)) == NULL)
1101 return 0;
1102 if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1103 return 0;
1104 if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1105 return 0;
1106 if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1107 return 0;
1108 if ((download = newwin(1, 1, 0, 0)) == NULL)
1109 return 0;
1110 if ((help = newwin(1, 1, 0, 0)) == NULL)
1111 return 0;
1113 wbkgd(body, body_face.body);
1114 wbkgd(download, download_face.background);
1115 wbkgd(echoarea, minibuffer_face.background);
1117 update_x_offset();
1119 keypad(body, TRUE);
1120 scrollok(body, FALSE);
1122 /* non-blocking input */
1123 wtimeout(body, 0);
1124 wtimeout(help, 0);
1126 mvwprintw(body, 0, 0, "");
1128 return 1;
1131 void
1132 ui_main_loop(void)
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 switch_to_tab(current_tab);
1143 rearrange_windows();
1146 void
1147 ui_on_tab_loaded(struct tab *tab)
1149 stop_loading_anim(tab);
1150 message("Loaded %s", tab->hist_cur->h);
1152 if (show_tab_bar)
1153 redraw_tabline();
1155 wrefresh(tabline);
1156 place_cursor(0);
1159 void
1160 ui_on_tab_refresh(struct tab *tab)
1162 wrap_page(&tab->buffer, body_cols);
1163 if (tab == current_tab)
1164 redraw_tab(tab);
1165 else
1166 tab->flags |= TAB_URGENT;
1169 void
1170 ui_on_download_refresh(void)
1172 if (side_window & SIDE_WINDOW_BOTTOM) {
1173 recompute_downloads();
1174 redraw_tab(current_tab);
1178 const char *
1179 ui_keyname(int k)
1181 return keyname(k);
1184 void
1185 ui_toggle_side_window(int kind)
1187 if (in_side_window & kind)
1188 ui_other_window();
1190 side_window ^= kind;
1191 if (side_window & SIDE_WINDOW_LEFT)
1192 recompute_help();
1193 if (side_window & SIDE_WINDOW_BOTTOM)
1194 recompute_downloads();
1197 * ugly hack, but otherwise the window doesn't get updated
1198 * until I call rearrange_windows a second time (e.g. via
1199 * C-l). I will be happy to know why something like this is
1200 * needed.
1202 rearrange_windows();
1203 rearrange_windows();
1206 void
1207 ui_show_downloads_pane(void)
1209 if (!(side_window & SIDE_WINDOW_BOTTOM))
1210 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1213 void
1214 ui_schedule_redraw(void)
1216 should_rearrange_windows = 1;
1219 void
1220 ui_require_input(struct tab *tab, int hide, int proto)
1222 void (*fn)(void);
1224 if (proto == PROTO_GEMINI)
1225 fn = ir_select_gemini;
1226 else if (proto == PROTO_GOPHER)
1227 fn = ir_select_gopher;
1228 else
1229 abort();
1231 /* TODO: hard-switching to another tab is ugly */
1232 switch_to_tab(tab);
1234 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1235 &ir_history, NULL, NULL);
1236 strlcpy(ministate.prompt, "Input required: ",
1237 sizeof(ministate.prompt));
1238 redraw_tab(tab);
1241 void
1242 ui_after_message_hook(void)
1244 redraw_minibuffer();
1245 place_cursor(0);
1248 void
1249 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1250 struct tab *data)
1252 yornp(prompt, fn, data);
1253 redraw_tab(current_tab);
1256 void
1257 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1258 struct tab *data, const char *input)
1260 minibuffer_read(prompt, fn, data);
1262 if (input != NULL) {
1263 strlcpy(ministate.buf, input, sizeof(ministate.buf));
1264 cmd_move_end_of_line(&ministate.buffer);
1267 redraw_tab(current_tab);
1270 void
1271 ui_other_window(void)
1273 if (in_side_window & SIDE_WINDOW_LEFT &&
1274 side_window & SIDE_WINDOW_BOTTOM)
1275 in_side_window = SIDE_WINDOW_BOTTOM;
1276 else if (in_side_window)
1277 in_side_window = 0;
1278 else if (!in_side_window && side_window & SIDE_WINDOW_LEFT)
1279 in_side_window = SIDE_WINDOW_LEFT;
1280 else if (!in_side_window && side_window)
1281 in_side_window = SIDE_WINDOW_BOTTOM;
1282 else
1283 message("No other window to select");
1286 void
1287 ui_suspend(void)
1289 endwin();
1291 kill(getpid(), SIGSTOP);
1293 refresh();
1294 clear();
1295 rearrange_windows();
1298 void
1299 ui_end(void)
1301 endwin();