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_HELP:
444 case LINE_DOWNLOAD:
445 case LINE_DOWNLOAD_DONE:
446 case LINE_DOWNLOAD_INFO:
447 goal = 8;
448 break;
449 default:
450 return;
453 if (vl->parent->alt == NULL)
454 return;
456 (void)y;
457 getyx(window, y, x);
459 if (goal <= x)
460 wprintw(window, " ");
461 for (; goal > x; ++x)
462 wprintw(window, " ");
464 wprintw(window, "%s", vl->parent->alt);
467 /*
468 * Core part of the rendering. It prints a vline starting from the
469 * current cursor position. Printing a vline consists of skipping
470 * `off' columns (for olivetti-mode), print the correct prefix (which
471 * may be the emoji in case of emojified links-lines), printing the
472 * text itself, filling until width - off and filling off columns
473 * again.
474 */
475 static void
476 print_vline(int off, int width, WINDOW *window, struct vline *vl)
478 /*
479 * Believe me or not, I've seen emoji ten code points long!
480 * That means, to stay large, 4*10 bytes + NUL.
481 */
482 char emojibuf[41] = {0};
483 const char *text, *prfx;
484 struct line_face *f;
485 int i, left, x, y;
487 f = &line_faces[vl->parent->type];
489 /* unused, set by getyx */
490 (void)y;
492 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
494 wattr_on(window, body_face.left, NULL);
495 for (i = 0; i < off; i++)
496 waddch(window, ' ');
497 wattr_off(window, body_face.left, NULL);
499 wattr_on(window, f->prefix, NULL);
500 wprintw(window, "%s", prfx);
501 wattr_off(window, f->prefix, NULL);
503 wattr_on(window, f->text, NULL);
504 wprintw(window, "%s", text);
505 print_vline_descr(width, window, vl);
506 wattr_off(window, f->text, NULL);
508 getyx(window, y, x);
510 left = width - x;
512 wattr_on(window, f->trail, NULL);
513 for (i = 0; i < left - off; ++i)
514 waddch(window, ' ');
515 wattr_off(window, f->trail, NULL);
517 wattr_on(window, body_face.right, NULL);
518 for (i = 0; i < off; i++)
519 waddch(window, ' ');
520 wattr_off(window, body_face.right, NULL);
524 static void
525 redraw_tabline(void)
527 struct tab *tab;
528 size_t toskip, ots, tabwidth, space, x;
529 int current, y, truncated, pair;
530 const char *title;
531 char buf[25];
533 x = 0;
535 /* unused, but setted by a getyx */
536 (void)y;
538 tabwidth = sizeof(buf)+1;
539 space = COLS-2;
541 toskip = 0;
542 TAILQ_FOREACH(tab, &tabshead, tabs) {
543 toskip++;
544 if (tab == current_tab)
545 break;
548 if (toskip * tabwidth < space)
549 toskip = 0;
550 else {
551 ots = toskip;
552 toskip--;
553 while (toskip != 0 &&
554 (ots - toskip+1) * tabwidth < space)
555 toskip--;
558 werase(tabline);
559 wattr_on(tabline, tab_face.background, NULL);
560 wprintw(tabline, toskip == 0 ? " " : "<");
561 wattr_off(tabline, tab_face.background, NULL);
563 truncated = 0;
564 TAILQ_FOREACH(tab, &tabshead, tabs) {
565 if (truncated)
566 break;
567 if (toskip != 0) {
568 toskip--;
569 continue;
572 getyx(tabline, y, x);
573 if (x + sizeof(buf)+2 >= (size_t)COLS)
574 truncated = 1;
576 current = tab == current_tab;
578 if (*(title = tab->buffer.page.title) == '\0')
579 title = tab->hist_cur->h;
581 if (tab->flags & TAB_URGENT)
582 strlcpy(buf, "!", sizeof(buf));
583 else
584 strlcpy(buf, " ", sizeof(buf));
586 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
587 /* truncation happens */
588 strlcpy(&buf[sizeof(buf)-4], "...", 4);
589 } else {
590 /* pad with spaces */
591 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
592 /* nop */ ;
595 pair = current ? tab_face.current : tab_face.tab;
596 wattr_on(tabline, pair, NULL);
597 wprintw(tabline, "%s", buf);
598 wattr_off(tabline, pair, NULL);
600 wattr_on(tabline, tab_face.background, NULL);
601 if (TAILQ_NEXT(tab, tabs) != NULL)
602 wprintw(tabline, "┃");
603 wattr_off(tabline, tab_face.background, NULL);
606 wattr_on(tabline, tab_face.background, NULL);
607 for (; x < (size_t)COLS; ++x)
608 waddch(tabline, ' ');
609 if (truncated)
610 mvwprintw(tabline, 0, COLS-1, ">");
611 wattr_off(tabline, tab_face.background, NULL);
614 /*
615 * Compute the first visible line around vl. Try to search forward
616 * until the end of the buffer; if a visible line is not found, search
617 * backward. Return NULL if no viable line was found.
618 */
619 struct vline *
620 adjust_line(struct vline *vl, struct buffer *buffer)
622 struct vline *t;
624 if (vl == NULL)
625 return NULL;
627 if (!(vl->parent->flags & L_HIDDEN))
628 return vl;
630 /* search forward */
631 for (t = vl;
632 t != NULL && t->parent->flags & L_HIDDEN;
633 t = TAILQ_NEXT(t, vlines))
634 ; /* nop */
636 if (t != NULL)
637 return t;
639 /* search backward */
640 for (t = vl;
641 t != NULL && t->parent->flags & L_HIDDEN;
642 t = TAILQ_PREV(t, vhead, vlines))
643 ; /* nop */
645 return t;
648 static void
649 redraw_window(WINDOW *win, int off, int height, int width,
650 struct buffer *buffer)
652 struct vline *vl;
653 int l, onscreen;
655 restore_curs_x(buffer);
657 /*
658 * TODO: ignoring buffer->force_update and always
659 * re-rendering. In theory we can recompute the y position
660 * without a re-render, and optimize here. It's not the only
661 * optimisation possible here, wscrl wolud also be an
662 * interesting one.
663 */
665 again:
666 werase(win);
667 buffer->curs_y = 0;
669 if (TAILQ_EMPTY(&buffer->head))
670 goto end;
672 if (buffer->top_line == NULL)
673 buffer->top_line = TAILQ_FIRST(&buffer->head);
675 buffer->top_line = adjust_line(buffer->top_line, buffer);
676 if (buffer->top_line == NULL)
677 goto end;
679 buffer->current_line = adjust_line(buffer->current_line, buffer);
681 l = 0;
682 onscreen = 0;
683 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
684 if (vl->parent->flags & L_HIDDEN)
685 continue;
687 wmove(win, l, 0);
688 print_vline(off, width, win, vl);
690 if (vl == buffer->current_line)
691 onscreen = 1;
693 if (!onscreen)
694 buffer->curs_y++;
696 l++;
697 if (l == height)
698 break;
701 if (!onscreen) {
702 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
703 if (vl == buffer->current_line)
704 break;
705 if (vl->parent->flags & L_HIDDEN)
706 continue;
707 buffer->line_off++;
708 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
711 if (vl != NULL)
712 goto again;
715 buffer->last_line_off = buffer->line_off;
716 buffer->force_redraw = 0;
717 end:
718 wmove(win, buffer->curs_y, buffer->curs_x);
721 static void
722 redraw_download(void)
724 redraw_window(download, 0, download_lines, COLS, &downloadwin);
727 static void
728 redraw_help(void)
730 redraw_window(help, 0, help_lines, help_cols, &helpwin);
733 static void
734 redraw_body(struct tab *tab)
736 static struct tab *last_tab;
738 if (last_tab != tab)
739 tab->buffer.force_redraw =1;
740 last_tab = tab;
742 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
745 static inline char
746 trust_status_char(enum trust_state ts)
748 switch (ts) {
749 case TS_UNKNOWN: return '-';
750 case TS_UNTRUSTED: return '!';
751 case TS_TEMP_TRUSTED: return '!';
752 case TS_TRUSTED: return 'v';
753 case TS_VERIFIED: return 'V';
754 default: return 'X';
758 static void
759 redraw_modeline(struct tab *tab)
761 struct buffer *buffer;
762 double pct;
763 int x, y, max_x, max_y;
764 const char *mode;
765 const char *spin = "-\\|/";
767 buffer = current_buffer();
768 mode = buffer->page.name;
770 werase(modeline);
771 wattr_on(modeline, modeline_face.background, NULL);
772 wmove(modeline, 0, 0);
774 wprintw(modeline, "-%c%c %s ",
775 spin[tab->loading_anim_step],
776 trust_status_char(tab->trust),
777 mode == NULL ? "(none)" : mode);
779 pct = (buffer->line_off + buffer->curs_y) * 100.0
780 / buffer->line_max;
782 if (buffer->line_max <= (size_t)body_lines)
783 wprintw(modeline, "All ");
784 else if (buffer->line_off == 0)
785 wprintw(modeline, "Top ");
786 else if (buffer->line_off + body_lines >= buffer->line_max)
787 wprintw(modeline, "Bottom ");
788 else
789 wprintw(modeline, "%.0f%% ", pct);
791 wprintw(modeline, "%d/%d %s ",
792 buffer->line_off + buffer->curs_y,
793 buffer->line_max,
794 tab->hist_cur->h);
796 getyx(modeline, y, x);
797 getmaxyx(modeline, max_y, max_x);
799 (void)y;
800 (void)max_y;
802 for (; x < max_x; ++x)
803 waddstr(modeline, "-");
805 wattr_off(modeline, modeline_face.background, NULL);
808 static void
809 redraw_minibuffer(void)
811 wattr_on(echoarea, minibuffer_face.background, NULL);
812 werase(echoarea);
814 if (in_minibuffer)
815 do_redraw_minibuffer();
816 else
817 do_redraw_echoarea();
819 if (in_minibuffer == MB_COMPREAD)
820 do_redraw_minibuffer_compl();
822 wattr_off(echoarea, minibuffer_face.background, NULL);
825 static void
826 do_redraw_echoarea(void)
828 struct vline *vl;
830 if (ministate.curmesg != NULL)
831 wprintw(echoarea, "%s", ministate.curmesg);
832 else if (*keybuf != '\0')
833 waddstr(echoarea, keybuf);
834 else {
835 /* If nothing else, show the URL at point */
836 vl = current_tab->buffer.current_line;
837 if (vl != NULL && vl->parent->type == LINE_LINK)
838 waddstr(echoarea, vl->parent->alt);
842 static void
843 do_redraw_minibuffer(void)
845 struct buffer *cmplbuf, *buffer;
846 size_t off_y, off_x = 0;
847 const char *start, *c;
849 cmplbuf = &ministate.compl.buffer;
850 buffer = &ministate.buffer;
851 (void)off_y; /* unused, set by getyx */
853 wmove(echoarea, 0, 0);
855 if (in_minibuffer == MB_COMPREAD)
856 wprintw(echoarea, "(%2d) ",
857 cmplbuf->line_max);
859 wprintw(echoarea, "%s", ministate.prompt);
860 if (ministate.hist_cur != NULL)
861 wprintw(echoarea, "(%zu/%zu) ",
862 ministate.hist_off + 1,
863 ministate.history->len);
865 getyx(echoarea, off_y, off_x);
867 start = ministate.hist_cur != NULL
868 ? ministate.hist_cur->h
869 : ministate.buf;
870 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
871 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
872 start = utf8_next_cp(start);
875 waddstr(echoarea, start);
877 if (ministate.curmesg != NULL)
878 wprintw(echoarea, " [%s]", ministate.curmesg);
880 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
883 static void
884 do_redraw_minibuffer_compl(void)
886 redraw_window(minibuffer, 0, 10, COLS,
887 &ministate.compl.buffer);
890 /*
891 * Place the cursor in the right ncurses window. If soft is 1, use
892 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
893 * wrefresh.
894 */
895 static void
896 place_cursor(int soft)
898 int (*touch)(WINDOW *);
900 if (soft)
901 touch = wnoutrefresh;
902 else
903 touch = wrefresh;
905 if (in_minibuffer) {
906 if (side_window & SIDE_WINDOW_LEFT)
907 touch(help);
908 if (side_window & SIDE_WINDOW_BOTTOM)
909 touch(download);
910 touch(body);
911 touch(echoarea);
912 } else if (in_side_window & SIDE_WINDOW_LEFT) {
913 touch(body);
914 touch(echoarea);
915 if (in_side_window & SIDE_WINDOW_BOTTOM)
916 touch(download);
917 touch(help);
918 } else if (in_side_window & SIDE_WINDOW_BOTTOM) {
919 touch(body);
920 touch(echoarea);
921 if (in_side_window & SIDE_WINDOW_LEFT)
922 touch(help);
923 touch(download);
924 } else {
925 if (side_window & SIDE_WINDOW_LEFT)
926 touch(help);
927 if (side_window & SIDE_WINDOW_BOTTOM)
928 touch(download);
929 touch(echoarea);
930 touch(body);
934 static void
935 redraw_tab(struct tab *tab)
937 if (too_small)
938 return;
940 if (side_window & SIDE_WINDOW_LEFT) {
941 redraw_help();
942 wnoutrefresh(help);
945 if (side_window & SIDE_WINDOW_BOTTOM) {
946 redraw_download();
947 wnoutrefresh(download);
950 if (show_tab_bar)
951 redraw_tabline();
953 redraw_body(tab);
954 redraw_modeline(tab);
955 redraw_minibuffer();
957 wnoutrefresh(tabline);
958 wnoutrefresh(modeline);
960 if (in_minibuffer == MB_COMPREAD)
961 wnoutrefresh(minibuffer);
963 place_cursor(1);
965 doupdate();
967 if (set_title)
968 dprintf(1, "\033]2;%s - Telescope\a",
969 current_tab->buffer.page.title);
972 void
973 start_loading_anim(struct tab *tab)
975 if (tab->loading_anim)
976 return;
977 tab->loading_anim = 1;
978 evtimer_set(&tab->loadingev, update_loading_anim, tab);
979 evtimer_add(&tab->loadingev, &loadingev_timer);
982 static void
983 update_loading_anim(int fd, short ev, void *d)
985 struct tab *tab = d;
987 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
989 if (tab == current_tab) {
990 redraw_modeline(tab);
991 wrefresh(modeline);
992 wrefresh(body);
993 if (in_minibuffer)
994 wrefresh(echoarea);
997 evtimer_add(&tab->loadingev, &loadingev_timer);
1000 static void
1001 stop_loading_anim(struct tab *tab)
1003 if (!tab->loading_anim)
1004 return;
1005 evtimer_del(&tab->loadingev);
1006 tab->loading_anim = 0;
1007 tab->loading_anim_step = 0;
1009 if (tab != current_tab)
1010 return;
1012 redraw_modeline(tab);
1014 wrefresh(modeline);
1015 wrefresh(body);
1016 if (in_minibuffer)
1017 wrefresh(echoarea);
1020 int
1021 ui_print_colors(void)
1023 int colors = 0, pairs = 0, can_change = 0;
1024 int columns = 16, lines, color, i, j;
1026 initscr();
1027 if (has_colors()) {
1028 start_color();
1029 use_default_colors();
1031 colors = COLORS;
1032 pairs = COLOR_PAIRS;
1033 can_change = can_change_color();
1035 endwin();
1037 printf("Term info:\n");
1038 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1039 getenv("TERM"), colors, pairs, can_change);
1040 printf("\n");
1042 if (colors == 0) {
1043 printf("No color support\n");
1044 return 0;
1047 printf("Available colors:\n\n");
1048 lines = (colors - 1) / columns + 1;
1049 color = 0;
1050 for (i = 0; i < lines; ++i) {
1051 for (j = 0; j < columns; ++j, ++color) {
1052 printf("\033[0;38;5;%dm %03d", color, color);
1054 printf("\n");
1057 printf("\033[0m");
1058 fflush(stdout);
1059 return 0;
1062 int
1063 ui_init()
1065 setlocale(LC_ALL, "");
1067 if (TAILQ_EMPTY(&global_map.m)) {
1068 fprintf(stderr, "no keys defined!\n");
1069 return 0;
1072 minibuffer_init();
1074 /* initialize download window */
1075 TAILQ_INIT(&downloadwin.head);
1076 TAILQ_INIT(&downloadwin.page.head);
1078 /* initialize help window */
1079 TAILQ_INIT(&helpwin.head);
1080 TAILQ_INIT(&helpwin.page.head);
1082 base_map = &global_map;
1083 current_map = &global_map;
1085 initscr();
1087 if (enable_colors) {
1088 if (has_colors()) {
1089 start_color();
1090 use_default_colors();
1091 } else
1092 enable_colors = 0;
1095 config_apply_style();
1097 raw();
1098 noecho();
1099 nonl();
1100 intrflush(stdscr, FALSE);
1102 if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1103 return 0;
1104 if ((body = newwin(1, 1, 0, 0)) == NULL)
1105 return 0;
1106 if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1107 return 0;
1108 if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1109 return 0;
1110 if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1111 return 0;
1112 if ((download = newwin(1, 1, 0, 0)) == NULL)
1113 return 0;
1114 if ((help = newwin(1, 1, 0, 0)) == NULL)
1115 return 0;
1117 wbkgd(body, body_face.body);
1118 wbkgd(download, download_face.background);
1119 wbkgd(echoarea, minibuffer_face.background);
1121 update_x_offset();
1123 keypad(body, TRUE);
1124 scrollok(body, FALSE);
1126 /* non-blocking input */
1127 wtimeout(body, 0);
1128 wtimeout(help, 0);
1130 mvwprintw(body, 0, 0, "");
1132 evtimer_set(&resizeev, handle_resize, NULL);
1134 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1135 event_add(&stdioev, NULL);
1137 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1138 signal_add(&winchev, NULL);
1140 return 1;
1143 void
1144 ui_main_loop(void)
1146 switch_to_tab(current_tab);
1147 rearrange_windows();
1149 event_dispatch();
1152 void
1153 ui_on_tab_loaded(struct tab *tab)
1155 stop_loading_anim(tab);
1156 message("Loaded %s", tab->hist_cur->h);
1158 if (show_tab_bar)
1159 redraw_tabline();
1161 wrefresh(tabline);
1162 place_cursor(0);
1165 void
1166 ui_on_tab_refresh(struct tab *tab)
1168 wrap_page(&tab->buffer, body_cols);
1169 if (tab == current_tab)
1170 redraw_tab(tab);
1171 else
1172 tab->flags |= TAB_URGENT;
1175 const char *
1176 ui_keyname(int k)
1178 return keyname(k);
1181 void
1182 ui_toggle_side_window(int kind)
1184 if (in_side_window & kind)
1185 ui_other_window();
1187 side_window ^= kind;
1188 if (side_window & SIDE_WINDOW_LEFT)
1189 recompute_help();
1190 if (side_window & SIDE_WINDOW_BOTTOM)
1191 recompute_downloads();
1194 * ugly hack, but otherwise the window doesn't get updated
1195 * until I call rearrange_windows a second time (e.g. via
1196 * C-l). I will be happy to know why something like this is
1197 * needed.
1199 rearrange_windows();
1200 rearrange_windows();
1203 void
1204 ui_schedule_redraw(void)
1206 should_rearrange_windows = 1;
1209 void
1210 ui_require_input(struct tab *tab, int hide, int proto)
1212 void (*fn)(void);
1214 if (proto == PROTO_GEMINI)
1215 fn = ir_select_gemini;
1216 else if (proto == PROTO_GOPHER)
1217 fn = ir_select_gopher;
1218 else
1219 abort();
1221 /* TODO: hard-switching to another tab is ugly */
1222 switch_to_tab(tab);
1224 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1225 &ir_history, NULL, NULL);
1226 strlcpy(ministate.prompt, "Input required: ",
1227 sizeof(ministate.prompt));
1228 redraw_tab(tab);
1231 void
1232 ui_after_message_hook(void)
1234 redraw_minibuffer();
1235 place_cursor(0);
1238 void
1239 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1240 struct tab *data)
1242 yornp(prompt, fn, data);
1243 redraw_tab(current_tab);
1246 void
1247 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1248 struct tab *data, const char *input)
1250 minibuffer_read(prompt, fn, data);
1252 if (input != NULL) {
1253 strlcpy(ministate.buf, input, sizeof(ministate.buf));
1254 cmd_move_end_of_line(&ministate.buffer);
1257 redraw_tab(current_tab);
1260 void
1261 ui_other_window(void)
1263 if (in_side_window & SIDE_WINDOW_LEFT &&
1264 side_window & SIDE_WINDOW_BOTTOM)
1265 in_side_window = SIDE_WINDOW_BOTTOM;
1266 else if (in_side_window)
1267 in_side_window = 0;
1268 else if (!in_side_window && side_window & SIDE_WINDOW_LEFT)
1269 in_side_window = SIDE_WINDOW_LEFT;
1270 else if (!in_side_window && side_window)
1271 in_side_window = SIDE_WINDOW_BOTTOM;
1272 else
1273 message("No other window to select");
1276 void
1277 ui_suspend(void)
1279 endwin();
1281 kill(getpid(), SIGSTOP);
1283 refresh();
1284 clear();
1285 rearrange_windows();
1288 void
1289 ui_end(void)
1291 endwin();