2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
18 * Ncurses UI for telescope.
24 * There's a simple text wrapping algorithm.
26 * 1. if it's a line in a pre-formatted block:
29 * 2. there is enough room for the next word?
31 * b. no: break the current line.
32 * i. while there isn't enough space to draw the current
34 * ii. draw the remainder of the current word (can be the
36 * 3. render the spaces after the word
37 * a. but if there is not enough room break the line and
45 * ncurses allows you to scroll a window, but when a line goes out of
46 * the visible area it's forgotten. We keep a list of formatted lines
47 * (``visual lines'') that we know fits in the window, and draw them.
48 * This way is easy to scroll: just call wscrl and then render the
51 * This means that on every resize we have to clear our list of lines
52 * and re-render everything. A clever approach would be to do this
55 * TODO: make the text formatting on-demand.
59 #include <telescope.h>
69 #define TAB_CURRENT 0x1
71 #define MIN(a, b) ((a) < (b) ? (a) : (b))
72 #define MAX(a, b) ((a) > (b) ? (a) : (b))
76 static struct event stdioev, winchev;
78 static int push_line(struct tab*, const struct line*, const char*, size_t);
79 static void empty_vlist(struct tab*);
80 static void restore_cursor(struct tab *);
81 static void cmd_previous_line(struct tab*);
82 static void cmd_next_line(struct tab*);
83 static void cmd_forward_char(struct tab*);
84 static void cmd_backward_char(struct tab*);
85 static void cmd_redraw(struct tab*);
86 static void cmd_scroll_down(struct tab*);
87 static void cmd_scroll_up(struct tab*);
88 static void cmd_kill_telescope(struct tab*);
89 static void cmd_push_button(struct tab*);
90 static void cmd_unbound(struct key);
91 static struct line *nth_line(struct tab*, size_t);
92 static struct tab *current_tab(void);
93 static void dispatch_stdio(int, short, void*);
94 static void handle_clear_minibuf(int, short, void*);
95 static void handle_resize(int, short, void*);
96 static int word_bourdaries(const char*, const char*, const char**, const char**);
97 static void wrap_text(struct tab*, const char*, struct line*);
98 static int hardwrap_text(struct tab*, struct line*);
99 static int wrap_page(struct tab*);
100 static void print_line(struct line*);
101 static void redraw_tabline(void);
102 static void redraw_modeline(struct tab*);
103 static void redraw_tab(struct tab*);
104 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
105 static void start_loading_anim(struct tab*);
106 static void update_loading_anim(int, short, void*);
107 static void stop_loading_anim(struct tab*);
108 static void load_url_in_tab(struct tab*, const char*);
109 static void new_tab(void);
111 typedef void (*interactivefn)(struct tab*);
113 static WINDOW *tabline, *body, *modeline, *minibuf;
114 static int body_lines, body_cols;
116 static struct event clminibufev;
117 static int clminibufev_set;
118 static struct timeval clminibufev_timer = { 5, 0 };
119 static struct timeval loadingev_timer = { 0, 250000 };
121 static uint32_t tab_counter;
130 short loading_anim_step;
131 struct event loadingev;
133 TAILQ_HEAD(, line) head;
141 #define KEY(n) ((struct key){0, n})
142 #define CKEY(n) ((struct key){0, (n) & 0x1F})
143 #define MKEY(n) ((struct key){1, n})
144 #define CMKEY(n) ((struct key){1, (n) & 0x1F})
150 { CKEY('p'), cmd_previous_line, },
151 { CKEY('n'), cmd_next_line, },
152 { CKEY('f'), cmd_forward_char, },
153 { CKEY('b'), cmd_backward_char, },
155 { {0,KEY_UP}, cmd_previous_line, },
156 { {0,KEY_DOWN}, cmd_next_line, },
158 { CKEY('L'), cmd_redraw, },
160 { KEY('J'), cmd_scroll_down, },
161 { KEY('K'), cmd_scroll_up, },
163 { KEY('q'), cmd_kill_telescope, },
164 { CKEY('m'), cmd_push_button, },
170 push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
176 if ((vl = calloc(1, sizeof(*vl))) == NULL)
179 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
186 memcpy(vl->line, buf, len);
189 if (TAILQ_EMPTY(&tab->s->head))
190 TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
192 TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
197 empty_vlist(struct tab *tab)
201 tab->s->line_max = 0;
203 TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
204 TAILQ_REMOVE(&tab->s->head, l, lines);
206 /* l->alt references the original line! */
212 restore_cursor(struct tab *tab)
214 wmove(body, tab->s->curs_y, tab->s->curs_x);
218 cmd_previous_line(struct tab *tab)
220 if (--tab->s->curs_y < 0) {
229 cmd_next_line(struct tab *tab)
231 if (++tab->s->curs_y > body_lines-1) {
232 tab->s->curs_y = body_lines-1;
233 cmd_scroll_down(tab);
240 cmd_forward_char(struct tab *tab)
242 tab->s->curs_x = MIN(body_cols-1, tab->s->curs_x+1);
247 cmd_backward_char(struct tab *tab)
249 tab->s->curs_x = MAX(0, tab->s->curs_x-1);
254 cmd_redraw(struct tab *tab)
256 handle_resize(0, 0, NULL);
260 cmd_scroll_up(struct tab *tab)
264 if (tab->s->line_off == 0)
267 l = nth_line(tab, --tab->s->line_off);
274 cmd_scroll_down(struct tab *tab)
279 if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
285 if (tab->s->line_max - tab->s->line_off < body_lines)
288 l = nth_line(tab, tab->s->line_off + body_lines-1);
289 wmove(body, body_lines-1, 0);
294 cmd_kill_telescope(struct tab *tab)
300 cmd_push_button(struct tab *tab)
305 nth = tab->s->line_off + tab->s->curs_y;
306 if (nth > tab->s->line_max)
308 l = nth_line(tab, nth);
309 if (l->type != LINE_LINK)
312 load_url_in_tab(tab, l->alt);
316 cmd_unbound(struct key k)
318 message("%s%c is undefined",
324 nth_line(struct tab *tab, size_t n)
330 TAILQ_FOREACH(l, &tab->s->head, lines) {
345 TAILQ_FOREACH(t, &tabshead, tabs) {
346 if (t->flags & TAB_CURRENT)
355 dispatch_stdio(int fd, short ev, void *d)
366 /* look for a M-key */
368 wtimeout(body, 0); /* TODO: make escape-time customizable */
381 for (b = bindings; b->fn != NULL; ++b) {
382 if (key.meta == b->key.meta &&
383 key.key == b->key.key) {
384 b->fn(current_tab());
392 restore_cursor(current_tab());
401 handle_clear_minibuf(int fd, short ev, void *d)
410 handle_resize(int sig, short ev, void *d)
418 /* move and resize the windows, in reverse order! */
420 mvwin(minibuf, LINES-1, 0);
421 wresize(minibuf, 1, COLS);
423 mvwin(modeline, LINES-2, 0);
424 wresize(modeline, 1, COLS);
426 wresize(body, LINES-3, COLS);
427 body_lines = LINES-3;
430 wresize(tabline, 1, COLS);
439 * Helper function for wrap_text. Find the end of the current word
440 * and the end of the separator after the word.
443 word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
451 /* find the end of the current world */
452 for (; *s != '\0'; ++s) {
453 if (strchr(sep, *s) != NULL)
459 /* find the end of the separator */
460 for (; *s != '\0'; ++s) {
461 if (strchr(sep, *s) == NULL)
471 emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
474 if (!push_line(tab, l, *line, *off - zero))
476 *line += *off - zero;
482 emitstr(const char **s, size_t len, size_t *off)
486 /* printw("%*s", ...) doesn't seem to respect the precision, so... */
487 for (i = 0; i < len; ++i)
494 * Build a list of visual line by wrapping the given line, assuming
495 * that when printed will have a leading prefix prfx.
497 * TODO: it considers each byte one cell on the screen!
500 wrap_text(struct tab *tab, const char *prfx, struct line *l)
502 size_t zero, off, len, split;
503 const char *endword, *endspc, *line, *linestart;
510 while (word_boundaries(line, " \t-", &endword, &endspc)) {
511 len = endword - line;
512 if (off + len >= body_cols) {
513 emitline(tab, zero, &off, l, &linestart);
514 while (len >= body_cols) {
516 emitline(tab, zero, &off, l, &linestart);
526 /* print the spaces iff not at bol */
527 len = endspc - endword;
530 if (off + len >= body_cols) {
531 emitline(tab, zero, &off, l, &linestart);
540 emitline(tab, zero, &off, l, &linestart);
544 hardwrap_text(struct tab *tab, struct line *l)
547 const char *linestart;
549 len = strlen(l->line);
553 while (len >= COLS) {
556 if (!emitline(tab, 0, &off, l, &linestart))
561 return emitline(tab, 0, &len, l, &linestart);
567 wrap_page(struct tab *tab)
573 TAILQ_FOREACH(l, &tab->page.head, lines) {
576 wrap_text(tab, "", l);
579 wrap_text(tab, "=> ", l);
582 wrap_text(tab, "# ", l);
585 wrap_text(tab, "## ", l);
588 wrap_text(tab, "### ", l);
591 wrap_text(tab, "* ", l);
594 wrap_text(tab, "> ", l);
598 push_line(tab, l, NULL, 0);
600 case LINE_PRE_CONTENT:
601 hardwrap_text(tab, l);
609 print_line(struct line *l)
611 const char *text = l->line;
618 wprintw(body, "%s", text);
621 wattron(body, A_UNDERLINE);
622 wprintw(body, "=> %s", text);
623 wattroff(body, A_UNDERLINE);
626 wattron(body, A_BOLD);
627 wprintw(body, "# %s", text);
628 wattroff(body, A_BOLD);
631 wattron(body, A_BOLD);
632 wprintw(body, "## %s", text);
633 wattroff(body, A_BOLD);
636 wattron(body, A_BOLD);
637 wprintw(body, "### %s", text);
638 wattroff(body, A_BOLD);
641 wprintw(body, "* %s", text);
644 wattron(body, A_DIM);
645 wprintw(body, "> %s", text);
646 wattroff(body, A_DIM);
650 wprintw(body, "```");
652 case LINE_PRE_CONTENT:
653 wprintw(body, "%s", text);
662 wbkgd(tabline, A_REVERSE);
663 mvwprintw(tabline, 0, 0, "TODO: tabs here");
667 redraw_modeline(struct tab *tab)
669 int x, y, max_x, max_y;
670 const char *mode = "text/gemini-mode";
671 const char *spin = "-\\|/";
674 wattron(modeline, A_REVERSE);
675 wmove(modeline, 0, 0);
677 wprintw(modeline, "-%c %s %s ",
678 spin[tab->s->loading_anim_step], mode, tab->urlstr);
679 getyx(modeline, y, x);
680 getmaxyx(modeline, max_y, max_x);
685 for (; x < max_x; ++x)
686 waddstr(modeline, "-");
690 redraw_tab(struct tab *tab)
697 tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
698 if (TAILQ_EMPTY(&tab->s->head))
702 l = nth_line(tab, tab->s->line_off);
703 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
704 wmove(body, line, 0);
707 if (line == body_lines)
712 redraw_modeline(tab);
723 message(const char *fmt, ...)
730 evtimer_del(&clminibufev);
731 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
732 evtimer_add(&clminibufev, &clminibufev_timer);
736 vw_printw(minibuf, fmt, ap);
745 start_loading_anim(struct tab *tab)
747 if (tab->s->loading_anim)
749 tab->s->loading_anim = 1;
750 evtimer_set(&tab->s->loadingev, update_loading_anim, tab);
751 evtimer_add(&tab->s->loadingev, &loadingev_timer);
755 update_loading_anim(int fd, short ev, void *d)
759 tab->s->loading_anim_step = (tab->s->loading_anim_step+1)%4;
761 redraw_modeline(tab);
765 evtimer_add(&tab->s->loadingev, &loadingev_timer);
769 stop_loading_anim(struct tab *tab)
771 if (!tab->s->loading_anim)
773 evtimer_del(&tab->s->loadingev);
774 tab->s->loading_anim = 0;
775 tab->s->loading_anim_step = 0;
777 redraw_modeline(tab);
783 load_url_in_tab(struct tab *tab, const char *url)
786 message("Loading %s...", url);
787 start_loading_anim(tab);
799 const char *url = "about:new";
801 if ((tab = calloc(1, sizeof(*tab))) == NULL)
804 if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
807 TAILQ_INIT(&tab->s->head);
808 TAILQ_FOREACH(t, &tabshead, tabs) {
809 t->flags &= ~TAB_CURRENT;
812 tab->id = tab_counter++;
813 tab->flags = TAB_CURRENT;
815 if (TAILQ_EMPTY(&tabshead))
816 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
818 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
820 load_url_in_tab(tab, url);
830 setlocale(LC_ALL, "");
837 intrflush(stdscr, FALSE);
839 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
841 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
843 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
845 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
848 body_lines = LINES-3;
852 scrollok(body, TRUE);
854 /* non-blocking input */
857 mvwprintw(body, 0, 0, "");
859 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
860 event_add(&stdioev, NULL);
862 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
863 signal_add(&winchev, NULL);
871 ui_on_tab_loaded(struct tab *tab)
873 stop_loading_anim(tab);
874 message("Loaded %s", tab->urlstr);
878 ui_on_tab_refresh(struct tab *tab)
880 if (!(tab->flags & TAB_CURRENT))