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 new_tab(void);
110 typedef void (*interactivefn)(struct tab*);
112 static WINDOW *tabline, *body, *modeline, *minibuf;
113 static int body_lines, body_cols;
115 static struct event clminibufev;
116 static int clminibufev_set;
117 static struct timeval clminibufev_timer = { 5, 0 };
118 static struct timeval loadingev_timer = { 0, 250000 };
120 static uint32_t tab_counter;
129 short loading_anim_step;
130 struct event loadingev;
132 TAILQ_HEAD(, line) head;
140 #define KEY(n) ((struct key){0, n})
141 #define CKEY(n) ((struct key){0, (n) & 0x1F})
142 #define MKEY(n) ((struct key){1, n})
143 #define CMKEY(n) ((struct key){1, (n) & 0x1F})
149 { CKEY('p'), cmd_previous_line, },
150 { CKEY('n'), cmd_next_line, },
151 { CKEY('f'), cmd_forward_char, },
152 { CKEY('b'), cmd_backward_char, },
154 { CKEY('L'), cmd_redraw, },
156 { KEY('J'), cmd_scroll_down, },
157 { KEY('K'), cmd_scroll_up, },
159 { KEY('q'), cmd_kill_telescope, },
160 { CKEY('m'), cmd_push_button, },
166 push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
172 if ((vl = calloc(1, sizeof(*vl))) == NULL)
175 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
182 memcpy(vl->line, buf, len);
185 if (TAILQ_EMPTY(&tab->s->head))
186 TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
188 TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
193 empty_vlist(struct tab *tab)
197 tab->s->line_max = 0;
199 TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
200 TAILQ_REMOVE(&tab->s->head, l, lines);
202 /* l->alt references the original line! */
208 restore_cursor(struct tab *tab)
210 wmove(body, tab->s->curs_y, tab->s->curs_x);
214 cmd_previous_line(struct tab *tab)
216 if (--tab->s->curs_y < 0) {
225 cmd_next_line(struct tab *tab)
227 if (++tab->s->curs_y > body_lines-1) {
228 tab->s->curs_y = body_lines-1;
229 cmd_scroll_down(tab);
236 cmd_forward_char(struct tab *tab)
238 tab->s->curs_x = MIN(body_cols-1, tab->s->curs_x+1);
243 cmd_backward_char(struct tab *tab)
245 tab->s->curs_x = MAX(0, tab->s->curs_x-1);
250 cmd_redraw(struct tab *tab)
252 handle_resize(0, 0, NULL);
256 cmd_scroll_up(struct tab *tab)
260 if (tab->s->line_off == 0)
263 l = nth_line(tab, --tab->s->line_off);
270 cmd_scroll_down(struct tab *tab)
275 if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
281 if (tab->s->line_max - tab->s->line_off < body_lines)
284 l = nth_line(tab, tab->s->line_off + body_lines-1);
285 wmove(body, body_lines-1, 0);
290 cmd_kill_telescope(struct tab *tab)
296 cmd_push_button(struct tab *tab)
301 nth = tab->s->line_off + tab->s->curs_y;
302 if (nth > tab->s->line_max)
304 l = nth_line(tab, nth);
305 message("Enter on line: \"%s\"", l->line ? l->line : "");
309 cmd_unbound(struct key k)
311 message("%s%c is undefined",
317 nth_line(struct tab *tab, size_t n)
323 TAILQ_FOREACH(l, &tab->s->head, lines) {
338 TAILQ_FOREACH(t, &tabshead, tabs) {
339 if (t->flags & TAB_CURRENT)
348 dispatch_stdio(int fd, short ev, void *d)
359 /* look for a M-key */
361 wtimeout(body, 0); /* TODO: make escape-time customizable */
374 for (b = bindings; b->fn != NULL; ++b) {
375 if (key.meta == b->key.meta &&
376 key.key == b->key.key) {
377 b->fn(current_tab());
385 restore_cursor(current_tab());
394 handle_clear_minibuf(int fd, short ev, void *d)
403 handle_resize(int sig, short ev, void *d)
411 /* move and resize the windows, in reverse order! */
413 mvwin(minibuf, LINES-1, 0);
414 wresize(minibuf, 1, COLS);
416 mvwin(modeline, LINES-2, 0);
417 wresize(modeline, 1, COLS);
419 wresize(body, LINES-3, COLS);
420 body_lines = LINES-3;
423 wresize(tabline, 1, COLS);
432 * Helper function for wrap_text. Find the end of the current word
433 * and the end of the separator after the word.
436 word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
444 /* find the end of the current world */
445 for (; *s != '\0'; ++s) {
446 if (strchr(sep, *s) != NULL)
452 /* find the end of the separator */
453 for (; *s != '\0'; ++s) {
454 if (strchr(sep, *s) == NULL)
464 emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
467 if (!push_line(tab, l, *line, *off - zero))
469 *line += *off - zero;
475 emitstr(const char **s, size_t len, size_t *off)
479 /* printw("%*s", ...) doesn't seem to respect the precision, so... */
480 for (i = 0; i < len; ++i)
487 * Build a list of visual line by wrapping the given line, assuming
488 * that when printed will have a leading prefix prfx.
490 * TODO: it considers each byte one cell on the screen!
493 wrap_text(struct tab *tab, const char *prfx, struct line *l)
495 size_t zero, off, len, split;
496 const char *endword, *endspc, *line, *linestart;
503 while (word_boundaries(line, " \t-", &endword, &endspc)) {
504 len = endword - line;
505 if (off + len >= body_cols) {
506 emitline(tab, zero, &off, l, &linestart);
507 while (len >= body_cols) {
509 emitline(tab, zero, &off, l, &linestart);
519 /* print the spaces iff not at bol */
520 len = endspc - endword;
523 if (off + len >= body_cols) {
524 emitline(tab, zero, &off, l, &linestart);
533 emitline(tab, zero, &off, l, &linestart);
537 hardwrap_text(struct tab *tab, struct line *l)
540 const char *linestart;
542 len = strlen(l->line);
546 while (len >= COLS) {
549 if (!emitline(tab, 0, &off, l, &linestart))
554 return emitline(tab, 0, &len, l, &linestart);
560 wrap_page(struct tab *tab)
566 TAILQ_FOREACH(l, &tab->page.head, lines) {
569 wrap_text(tab, "", l);
572 wrap_text(tab, "=> ", l);
575 wrap_text(tab, "# ", l);
578 wrap_text(tab, "## ", l);
581 wrap_text(tab, "### ", l);
584 wrap_text(tab, "* ", l);
587 wrap_text(tab, "> ", l);
591 push_line(tab, l, NULL, 0);
593 case LINE_PRE_CONTENT:
594 hardwrap_text(tab, l);
602 print_line(struct line *l)
604 const char *text = l->line;
611 wprintw(body, "%s", text);
614 wattron(body, A_UNDERLINE);
615 wprintw(body, "=> %s", text);
616 wattroff(body, A_UNDERLINE);
619 wattron(body, A_BOLD);
620 wprintw(body, "# %s", text);
621 wattroff(body, A_BOLD);
624 wattron(body, A_BOLD);
625 wprintw(body, "## %s", text);
626 wattroff(body, A_BOLD);
629 wattron(body, A_BOLD);
630 wprintw(body, "### %s", text);
631 wattroff(body, A_BOLD);
634 wprintw(body, "* %s", text);
637 wattron(body, A_DIM);
638 wprintw(body, "> %s", text);
639 wattroff(body, A_DIM);
643 wprintw(body, "```");
645 case LINE_PRE_CONTENT:
646 wprintw(body, "%s", text);
655 wbkgd(tabline, A_REVERSE);
656 mvwprintw(tabline, 0, 0, "TODO: tabs here");
660 redraw_modeline(struct tab *tab)
662 int x, y, max_x, max_y;
663 const char *mode = "text/gemini-mode";
664 const char *spin = "-\\|/";
667 wattron(modeline, A_REVERSE);
668 wmove(modeline, 0, 0);
670 wprintw(modeline, "-%c %s %s ",
671 spin[tab->s->loading_anim_step], mode, tab->url);
672 getyx(modeline, y, x);
673 getmaxyx(modeline, max_y, max_x);
678 for (; x < max_x; ++x)
679 waddstr(modeline, "-");
683 redraw_tab(struct tab *tab)
690 tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
691 if (TAILQ_EMPTY(&tab->s->head))
695 l = nth_line(tab, tab->s->line_off);
696 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
697 wmove(body, line, 0);
700 if (line == body_lines)
705 redraw_modeline(tab);
716 message(const char *fmt, ...)
723 evtimer_del(&clminibufev);
724 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
725 evtimer_add(&clminibufev, &clminibufev_timer);
729 vw_printw(minibuf, fmt, ap);
738 start_loading_anim(struct tab *tab)
740 if (tab->s->loading_anim)
742 tab->s->loading_anim = 1;
743 evtimer_set(&tab->s->loadingev, update_loading_anim, tab);
744 evtimer_add(&tab->s->loadingev, &loadingev_timer);
748 update_loading_anim(int fd, short ev, void *d)
752 tab->s->loading_anim_step = (tab->s->loading_anim_step+1)%4;
754 redraw_modeline(tab);
758 evtimer_add(&tab->s->loadingev, &loadingev_timer);
762 stop_loading_anim(struct tab *tab)
764 if (!tab->s->loading_anim)
766 evtimer_del(&tab->s->loadingev);
767 tab->s->loading_anim = 0;
768 tab->s->loading_anim_step = 0;
772 ui_load_url_in_tab(struct tab *tab, const char *url)
774 message("Loading %s...", url);
775 start_loading_anim(tab);
783 const char *url = "about:new";
785 if ((tab = calloc(1, sizeof(*tab))) == NULL)
788 if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
791 TAILQ_INIT(&tab->s->head);
792 TAILQ_FOREACH(t, &tabshead, tabs) {
793 t->flags &= ~TAB_CURRENT;
796 tab->id = tab_counter++;
797 tab->flags = TAB_CURRENT;
799 if (TAILQ_EMPTY(&tabshead))
800 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
802 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
804 ui_load_url_in_tab(tab, url);
814 setlocale(LC_ALL, "");
821 intrflush(stdscr, FALSE);
822 keypad(stdscr, TRUE);
824 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
826 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
828 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
830 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
833 body_lines = LINES-3;
836 scrollok(body, TRUE);
838 /* non-blocking input */
841 mvwprintw(body, 0, 0, "");
843 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
844 event_add(&stdioev, NULL);
846 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
847 signal_add(&winchev, NULL);
855 ui_on_tab_loaded(struct tab *tab)
857 stop_loading_anim(tab);
858 message("Loaded %s", tab->url);
862 ui_on_tab_refresh(struct tab *tab)
864 if (!(tab->flags & TAB_CURRENT))