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 goal = width/4;
446 break;
447 case LINE_HELP:
448 goal = 8;
449 break;
450 default:
451 return;
454 if (vl->parent->alt == NULL)
455 return;
457 (void)y;
458 getyx(window, y, x);
460 if (goal <= x)
461 wprintw(window, " ");
462 for (; goal > x; ++x)
463 wprintw(window, " ");
465 wprintw(window, "%s", vl->parent->alt);
468 /*
469 * Core part of the rendering. It prints a vline starting from the
470 * current cursor position. Printing a vline consists of skipping
471 * `off' columns (for olivetti-mode), print the correct prefix (which
472 * may be the emoji in case of emojified links-lines), printing the
473 * text itself, filling until width - off and filling off columns
474 * again.
475 */
476 static void
477 print_vline(int off, int width, WINDOW *window, struct vline *vl)
479 /*
480 * Believe me or not, I've seen emoji ten code points long!
481 * That means, to stay large, 4*10 bytes + NUL.
482 */
483 char emojibuf[41] = {0};
484 const char *text, *prfx;
485 struct line_face *f;
486 int i, left, x, y;
488 f = &line_faces[vl->parent->type];
490 /* unused, set by getyx */
491 (void)y;
493 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
495 wattr_on(window, body_face.left, NULL);
496 for (i = 0; i < off; i++)
497 waddch(window, ' ');
498 wattr_off(window, body_face.left, NULL);
500 wattr_on(window, f->prefix, NULL);
501 wprintw(window, "%s", prfx);
502 wattr_off(window, f->prefix, NULL);
504 wattr_on(window, f->text, NULL);
505 wprintw(window, "%s", text);
506 print_vline_descr(width, window, vl);
507 wattr_off(window, f->text, NULL);
509 getyx(window, y, x);
511 left = width - x;
513 wattr_on(window, f->trail, NULL);
514 for (i = 0; i < left - off; ++i)
515 waddch(window, ' ');
516 wattr_off(window, f->trail, NULL);
518 wattr_on(window, body_face.right, NULL);
519 for (i = 0; i < off; i++)
520 waddch(window, ' ');
521 wattr_off(window, body_face.right, NULL);
525 static void
526 redraw_tabline(void)
528 struct tab *tab;
529 size_t toskip, ots, tabwidth, space, x;
530 int current, y, truncated, pair;
531 const char *title;
532 char buf[25];
534 x = 0;
536 /* unused, but setted by a getyx */
537 (void)y;
539 tabwidth = sizeof(buf)+1;
540 space = COLS-2;
542 toskip = 0;
543 TAILQ_FOREACH(tab, &tabshead, tabs) {
544 toskip++;
545 if (tab == current_tab)
546 break;
549 if (toskip * tabwidth < space)
550 toskip = 0;
551 else {
552 ots = toskip;
553 toskip--;
554 while (toskip != 0 &&
555 (ots - toskip+1) * tabwidth < space)
556 toskip--;
559 werase(tabline);
560 wattr_on(tabline, tab_face.background, NULL);
561 wprintw(tabline, toskip == 0 ? " " : "<");
562 wattr_off(tabline, tab_face.background, NULL);
564 truncated = 0;
565 TAILQ_FOREACH(tab, &tabshead, tabs) {
566 if (truncated)
567 break;
568 if (toskip != 0) {
569 toskip--;
570 continue;
573 getyx(tabline, y, x);
574 if (x + sizeof(buf)+2 >= (size_t)COLS)
575 truncated = 1;
577 current = tab == current_tab;
579 if (*(title = tab->buffer.page.title) == '\0')
580 title = tab->hist_cur->h;
582 if (tab->flags & TAB_URGENT)
583 strlcpy(buf, "!", sizeof(buf));
584 else
585 strlcpy(buf, " ", sizeof(buf));
587 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
588 /* truncation happens */
589 strlcpy(&buf[sizeof(buf)-4], "...", 4);
590 } else {
591 /* pad with spaces */
592 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
593 /* nop */ ;
596 pair = current ? tab_face.current : tab_face.tab;
597 wattr_on(tabline, pair, NULL);
598 wprintw(tabline, "%s", buf);
599 wattr_off(tabline, pair, NULL);
601 wattr_on(tabline, tab_face.background, NULL);
602 if (TAILQ_NEXT(tab, tabs) != NULL)
603 wprintw(tabline, "┃");
604 wattr_off(tabline, tab_face.background, NULL);
607 wattr_on(tabline, tab_face.background, NULL);
608 for (; x < (size_t)COLS; ++x)
609 waddch(tabline, ' ');
610 if (truncated)
611 mvwprintw(tabline, 0, COLS-1, ">");
612 wattr_off(tabline, tab_face.background, NULL);
615 /*
616 * Compute the first visible line around vl. Try to search forward
617 * until the end of the buffer; if a visible line is not found, search
618 * backward. Return NULL if no viable line was found.
619 */
620 struct vline *
621 adjust_line(struct vline *vl, struct buffer *buffer)
623 struct vline *t;
625 if (vl == NULL)
626 return NULL;
628 if (!(vl->parent->flags & L_HIDDEN))
629 return vl;
631 /* search forward */
632 for (t = vl;
633 t != NULL && t->parent->flags & L_HIDDEN;
634 t = TAILQ_NEXT(t, vlines))
635 ; /* nop */
637 if (t != NULL)
638 return t;
640 /* search backward */
641 for (t = vl;
642 t != NULL && t->parent->flags & L_HIDDEN;
643 t = TAILQ_PREV(t, vhead, vlines))
644 ; /* nop */
646 return t;
649 static void
650 redraw_window(WINDOW *win, int off, int height, int width,
651 struct buffer *buffer)
653 struct vline *vl;
654 int l, onscreen;
656 restore_curs_x(buffer);
658 /*
659 * TODO: ignoring buffer->force_update and always
660 * re-rendering. In theory we can recompute the y position
661 * without a re-render, and optimize here. It's not the only
662 * optimisation possible here, wscrl wolud also be an
663 * interesting one.
664 */
666 again:
667 werase(win);
668 buffer->curs_y = 0;
670 if (TAILQ_EMPTY(&buffer->head))
671 goto end;
673 if (buffer->top_line == NULL)
674 buffer->top_line = TAILQ_FIRST(&buffer->head);
676 buffer->top_line = adjust_line(buffer->top_line, buffer);
677 if (buffer->top_line == NULL)
678 goto end;
680 buffer->current_line = adjust_line(buffer->current_line, buffer);
682 l = 0;
683 onscreen = 0;
684 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
685 if (vl->parent->flags & L_HIDDEN)
686 continue;
688 wmove(win, l, 0);
689 print_vline(off, width, win, vl);
691 if (vl == buffer->current_line)
692 onscreen = 1;
694 if (!onscreen)
695 buffer->curs_y++;
697 l++;
698 if (l == height)
699 break;
702 if (!onscreen) {
703 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
704 if (vl == buffer->current_line)
705 break;
706 if (vl->parent->flags & L_HIDDEN)
707 continue;
708 buffer->line_off++;
709 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
712 if (vl != NULL)
713 goto again;
716 buffer->last_line_off = buffer->line_off;
717 buffer->force_redraw = 0;
718 end:
719 wmove(win, buffer->curs_y, buffer->curs_x);
722 static void
723 redraw_download(void)
725 redraw_window(download, 0, download_lines, COLS, &downloadwin);
728 static void
729 redraw_help(void)
731 redraw_window(help, 0, help_lines, help_cols, &helpwin);
734 static void
735 redraw_body(struct tab *tab)
737 static struct tab *last_tab;
739 if (last_tab != tab)
740 tab->buffer.force_redraw =1;
741 last_tab = tab;
743 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
746 static inline char
747 trust_status_char(enum trust_state ts)
749 switch (ts) {
750 case TS_UNKNOWN: return '-';
751 case TS_UNTRUSTED: return '!';
752 case TS_TEMP_TRUSTED: return '!';
753 case TS_TRUSTED: return 'v';
754 case TS_VERIFIED: return 'V';
755 default: return 'X';
759 static void
760 redraw_modeline(struct tab *tab)
762 struct buffer *buffer;
763 double pct;
764 int x, y, max_x, max_y;
765 const char *mode;
766 const char *spin = "-\\|/";
768 buffer = current_buffer();
769 mode = buffer->page.name;
771 werase(modeline);
772 wattr_on(modeline, modeline_face.background, NULL);
773 wmove(modeline, 0, 0);
775 wprintw(modeline, "-%c%c %s ",
776 spin[tab->loading_anim_step],
777 trust_status_char(tab->trust),
778 mode == NULL ? "(none)" : mode);
780 pct = (buffer->line_off + buffer->curs_y) * 100.0
781 / buffer->line_max;
783 if (buffer->line_max <= (size_t)body_lines)
784 wprintw(modeline, "All ");
785 else if (buffer->line_off == 0)
786 wprintw(modeline, "Top ");
787 else if (buffer->line_off + body_lines >= buffer->line_max)
788 wprintw(modeline, "Bottom ");
789 else
790 wprintw(modeline, "%.0f%% ", pct);
792 wprintw(modeline, "%d/%d %s ",
793 buffer->line_off + buffer->curs_y,
794 buffer->line_max,
795 tab->hist_cur->h);
797 getyx(modeline, y, x);
798 getmaxyx(modeline, max_y, max_x);
800 (void)y;
801 (void)max_y;
803 for (; x < max_x; ++x)
804 waddstr(modeline, "-");
806 wattr_off(modeline, modeline_face.background, NULL);
809 static void
810 redraw_minibuffer(void)
812 wattr_on(echoarea, minibuffer_face.background, NULL);
813 werase(echoarea);
815 if (in_minibuffer)
816 do_redraw_minibuffer();
817 else
818 do_redraw_echoarea();
820 if (in_minibuffer == MB_COMPREAD)
821 do_redraw_minibuffer_compl();
823 wattr_off(echoarea, minibuffer_face.background, NULL);
826 static void
827 do_redraw_echoarea(void)
829 struct vline *vl;
831 if (ministate.curmesg != NULL)
832 wprintw(echoarea, "%s", ministate.curmesg);
833 else if (*keybuf != '\0')
834 waddstr(echoarea, keybuf);
835 else {
836 /* If nothing else, show the URL at point */
837 vl = current_tab->buffer.current_line;
838 if (vl != NULL && vl->parent->type == LINE_LINK)
839 waddstr(echoarea, vl->parent->alt);
843 static void
844 do_redraw_minibuffer(void)
846 struct buffer *cmplbuf, *buffer;
847 size_t off_y, off_x = 0;
848 const char *start, *c;
850 cmplbuf = &ministate.compl.buffer;
851 buffer = &ministate.buffer;
852 (void)off_y; /* unused, set by getyx */
854 wmove(echoarea, 0, 0);
856 if (in_minibuffer == MB_COMPREAD)
857 wprintw(echoarea, "(%2d) ",
858 cmplbuf->line_max);
860 wprintw(echoarea, "%s", ministate.prompt);
861 if (ministate.hist_cur != NULL)
862 wprintw(echoarea, "(%zu/%zu) ",
863 ministate.hist_off + 1,
864 ministate.history->len);
866 getyx(echoarea, off_y, off_x);
868 start = ministate.hist_cur != NULL
869 ? ministate.hist_cur->h
870 : ministate.buf;
871 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
872 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
873 start = utf8_next_cp(start);
876 waddstr(echoarea, start);
878 if (ministate.curmesg != NULL)
879 wprintw(echoarea, " [%s]", ministate.curmesg);
881 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
884 static void
885 do_redraw_minibuffer_compl(void)
887 redraw_window(minibuffer, 0, 10, COLS,
888 &ministate.compl.buffer);
891 /*
892 * Place the cursor in the right ncurses window. If soft is 1, use
893 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
894 * wrefresh.
895 */
896 static void
897 place_cursor(int soft)
899 int (*touch)(WINDOW *);
901 if (soft)
902 touch = wnoutrefresh;
903 else
904 touch = wrefresh;
906 if (in_minibuffer) {
907 if (side_window & SIDE_WINDOW_LEFT)
908 touch(help);
909 if (side_window & SIDE_WINDOW_BOTTOM)
910 touch(download);
911 touch(body);
912 touch(echoarea);
913 } else if (in_side_window & SIDE_WINDOW_LEFT) {
914 touch(body);
915 touch(echoarea);
916 if (in_side_window & SIDE_WINDOW_BOTTOM)
917 touch(download);
918 touch(help);
919 } else if (in_side_window & SIDE_WINDOW_BOTTOM) {
920 touch(body);
921 touch(echoarea);
922 if (in_side_window & SIDE_WINDOW_LEFT)
923 touch(help);
924 touch(download);
925 } else {
926 if (side_window & SIDE_WINDOW_LEFT)
927 touch(help);
928 if (side_window & SIDE_WINDOW_BOTTOM)
929 touch(download);
930 touch(echoarea);
931 touch(body);
935 static void
936 redraw_tab(struct tab *tab)
938 if (too_small)
939 return;
941 if (side_window & SIDE_WINDOW_LEFT) {
942 redraw_help();
943 wnoutrefresh(help);
946 if (side_window & SIDE_WINDOW_BOTTOM) {
947 redraw_download();
948 wnoutrefresh(download);
951 if (show_tab_bar)
952 redraw_tabline();
954 redraw_body(tab);
955 redraw_modeline(tab);
956 redraw_minibuffer();
958 wnoutrefresh(tabline);
959 wnoutrefresh(modeline);
961 if (in_minibuffer == MB_COMPREAD)
962 wnoutrefresh(minibuffer);
964 place_cursor(1);
966 doupdate();
968 if (set_title)
969 dprintf(1, "\033]2;%s - Telescope\a",
970 current_tab->buffer.page.title);
973 void
974 start_loading_anim(struct tab *tab)
976 if (tab->loading_anim)
977 return;
978 tab->loading_anim = 1;
979 evtimer_set(&tab->loadingev, update_loading_anim, tab);
980 evtimer_add(&tab->loadingev, &loadingev_timer);
983 static void
984 update_loading_anim(int fd, short ev, void *d)
986 struct tab *tab = d;
988 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
990 if (tab == current_tab) {
991 redraw_modeline(tab);
992 wrefresh(modeline);
993 wrefresh(body);
994 if (in_minibuffer)
995 wrefresh(echoarea);
998 evtimer_add(&tab->loadingev, &loadingev_timer);
1001 static void
1002 stop_loading_anim(struct tab *tab)
1004 if (!tab->loading_anim)
1005 return;
1006 evtimer_del(&tab->loadingev);
1007 tab->loading_anim = 0;
1008 tab->loading_anim_step = 0;
1010 if (tab != current_tab)
1011 return;
1013 redraw_modeline(tab);
1015 wrefresh(modeline);
1016 wrefresh(body);
1017 if (in_minibuffer)
1018 wrefresh(echoarea);
1021 int
1022 ui_print_colors(void)
1024 int colors = 0, pairs = 0, can_change = 0;
1025 int columns = 16, lines, color, i, j;
1027 initscr();
1028 if (has_colors()) {
1029 start_color();
1030 use_default_colors();
1032 colors = COLORS;
1033 pairs = COLOR_PAIRS;
1034 can_change = can_change_color();
1036 endwin();
1038 printf("Term info:\n");
1039 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1040 getenv("TERM"), colors, pairs, can_change);
1041 printf("\n");
1043 if (colors == 0) {
1044 printf("No color support\n");
1045 return 0;
1048 printf("Available colors:\n\n");
1049 lines = (colors - 1) / columns + 1;
1050 color = 0;
1051 for (i = 0; i < lines; ++i) {
1052 for (j = 0; j < columns; ++j, ++color) {
1053 printf("\033[0;38;5;%dm %03d", color, color);
1055 printf("\n");
1058 printf("\033[0m");
1059 fflush(stdout);
1060 return 0;
1063 int
1064 ui_init()
1066 setlocale(LC_ALL, "");
1068 if (TAILQ_EMPTY(&global_map.m)) {
1069 fprintf(stderr, "no keys defined!\n");
1070 return 0;
1073 minibuffer_init();
1075 /* initialize download window */
1076 TAILQ_INIT(&downloadwin.head);
1077 TAILQ_INIT(&downloadwin.page.head);
1079 /* initialize help window */
1080 TAILQ_INIT(&helpwin.head);
1081 TAILQ_INIT(&helpwin.page.head);
1083 base_map = &global_map;
1084 current_map = &global_map;
1086 initscr();
1088 if (enable_colors) {
1089 if (has_colors()) {
1090 start_color();
1091 use_default_colors();
1092 } else
1093 enable_colors = 0;
1096 config_apply_style();
1098 raw();
1099 noecho();
1100 nonl();
1101 intrflush(stdscr, FALSE);
1103 if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1104 return 0;
1105 if ((body = newwin(1, 1, 0, 0)) == NULL)
1106 return 0;
1107 if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1108 return 0;
1109 if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1110 return 0;
1111 if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1112 return 0;
1113 if ((download = newwin(1, 1, 0, 0)) == NULL)
1114 return 0;
1115 if ((help = newwin(1, 1, 0, 0)) == NULL)
1116 return 0;
1118 wbkgd(body, body_face.body);
1119 wbkgd(download, download_face.background);
1120 wbkgd(echoarea, minibuffer_face.background);
1122 update_x_offset();
1124 keypad(body, TRUE);
1125 scrollok(body, FALSE);
1127 /* non-blocking input */
1128 wtimeout(body, 0);
1129 wtimeout(help, 0);
1131 mvwprintw(body, 0, 0, "");
1133 evtimer_set(&resizeev, handle_resize, NULL);
1135 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1136 event_add(&stdioev, NULL);
1138 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1139 signal_add(&winchev, NULL);
1141 return 1;
1144 void
1145 ui_main_loop(void)
1147 switch_to_tab(current_tab);
1148 rearrange_windows();
1150 event_dispatch();
1153 void
1154 ui_on_tab_loaded(struct tab *tab)
1156 stop_loading_anim(tab);
1157 message("Loaded %s", tab->hist_cur->h);
1159 if (show_tab_bar)
1160 redraw_tabline();
1162 wrefresh(tabline);
1163 place_cursor(0);
1166 void
1167 ui_on_tab_refresh(struct tab *tab)
1169 wrap_page(&tab->buffer, body_cols);
1170 if (tab == current_tab)
1171 redraw_tab(tab);
1172 else
1173 tab->flags |= TAB_URGENT;
1176 void
1177 ui_on_download_refresh(void)
1179 if (side_window & SIDE_WINDOW_BOTTOM) {
1180 recompute_downloads();
1181 redraw_tab(current_tab);
1185 const char *
1186 ui_keyname(int k)
1188 return keyname(k);
1191 void
1192 ui_toggle_side_window(int kind)
1194 if (in_side_window & kind)
1195 ui_other_window();
1197 side_window ^= kind;
1198 if (side_window & SIDE_WINDOW_LEFT)
1199 recompute_help();
1200 if (side_window & SIDE_WINDOW_BOTTOM)
1201 recompute_downloads();
1204 * ugly hack, but otherwise the window doesn't get updated
1205 * until I call rearrange_windows a second time (e.g. via
1206 * C-l). I will be happy to know why something like this is
1207 * needed.
1209 rearrange_windows();
1210 rearrange_windows();
1213 void
1214 ui_show_downloads_pane(void)
1216 if (!(side_window & SIDE_WINDOW_BOTTOM))
1217 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1220 void
1221 ui_schedule_redraw(void)
1223 should_rearrange_windows = 1;
1226 void
1227 ui_require_input(struct tab *tab, int hide, int proto)
1229 void (*fn)(void);
1231 if (proto == PROTO_GEMINI)
1232 fn = ir_select_gemini;
1233 else if (proto == PROTO_GOPHER)
1234 fn = ir_select_gopher;
1235 else
1236 abort();
1238 /* TODO: hard-switching to another tab is ugly */
1239 switch_to_tab(tab);
1241 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1242 &ir_history, NULL, NULL);
1243 strlcpy(ministate.prompt, "Input required: ",
1244 sizeof(ministate.prompt));
1245 redraw_tab(tab);
1248 void
1249 ui_after_message_hook(void)
1251 redraw_minibuffer();
1252 place_cursor(0);
1255 void
1256 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1257 struct tab *data)
1259 yornp(prompt, fn, data);
1260 redraw_tab(current_tab);
1263 void
1264 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1265 struct tab *data, const char *input)
1267 minibuffer_read(prompt, fn, data);
1269 if (input != NULL) {
1270 strlcpy(ministate.buf, input, sizeof(ministate.buf));
1271 cmd_move_end_of_line(&ministate.buffer);
1274 redraw_tab(current_tab);
1277 void
1278 ui_other_window(void)
1280 if (in_side_window & SIDE_WINDOW_LEFT &&
1281 side_window & SIDE_WINDOW_BOTTOM)
1282 in_side_window = SIDE_WINDOW_BOTTOM;
1283 else if (in_side_window)
1284 in_side_window = 0;
1285 else if (!in_side_window && side_window & SIDE_WINDOW_LEFT)
1286 in_side_window = SIDE_WINDOW_LEFT;
1287 else if (!in_side_window && side_window)
1288 in_side_window = SIDE_WINDOW_BOTTOM;
1289 else
1290 message("No other window to select");
1293 void
1294 ui_suspend(void)
1296 endwin();
1298 kill(getpid(), SIGSTOP);
1300 refresh();
1301 clear();
1302 rearrange_windows();
1305 void
1306 ui_end(void)
1308 endwin();