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>
71 #define TAB_CURRENT 0x1
73 #define MIN(a, b) ((a) < (b) ? (a) : (b))
74 #define MAX(a, b) ((a) > (b) ? (a) : (b))
78 static struct event stdioev, winchev;
80 static int kbd(const char*);
81 static void kmap_define_key(struct kmap*, const char*, void(*)(struct tab*));
82 static void load_default_keys(void);
83 static int push_line(struct tab*, const struct line*, const char*, size_t);
84 static void empty_vlist(struct tab*);
85 static void restore_cursor(struct tab *);
86 static void cmd_previous_line(struct tab*);
87 static void cmd_next_line(struct tab*);
88 static void cmd_forward_char(struct tab*);
89 static void cmd_backward_char(struct tab*);
90 static void cmd_redraw(struct tab*);
91 static void cmd_scroll_down(struct tab*);
92 static void cmd_scroll_up(struct tab*);
93 static void cmd_kill_telescope(struct tab*);
94 static void cmd_push_button(struct tab*);
95 static struct line *nth_line(struct tab*, size_t);
96 static struct tab *current_tab(void);
97 static void dispatch_stdio(int, short, void*);
98 static void handle_clear_minibuf(int, short, void*);
99 static void handle_resize(int, short, void*);
100 static int word_bourdaries(const char*, const char*, const char**, const char**);
101 static void wrap_text(struct tab*, const char*, struct line*);
102 static int hardwrap_text(struct tab*, struct line*);
103 static int wrap_page(struct tab*);
104 static void print_line(struct line*);
105 static void redraw_tabline(void);
106 static void redraw_modeline(struct tab*);
107 static void redraw_tab(struct tab*);
108 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
109 static void start_loading_anim(struct tab*);
110 static void update_loading_anim(int, short, void*);
111 static void stop_loading_anim(struct tab*);
112 static void load_url_in_tab(struct tab*, const char*);
113 static void new_tab(void);
115 static WINDOW *tabline, *body, *modeline, *minibuf;
116 static int body_lines, body_cols;
118 static struct event clminibufev;
119 static int clminibufev_set;
120 static struct timeval clminibufev_timer = { 5, 0 };
121 static struct timeval loadingev_timer = { 0, 250000 };
123 static uint32_t tab_counter;
132 short loading_anim_step;
133 struct event loadingev;
135 TAILQ_HEAD(, line) head;
138 #define CTRL(n) ((n)&0x1F)
140 static TAILQ_HEAD(kmap, keymap) global_map, *current_map;
145 void (*fn)(struct tab*);
147 TAILQ_ENTRY(keymap) keymaps;
158 { "<down>", KEY_DOWN },
159 { "<left>", KEY_LEFT },
160 { "<right>", KEY_RIGHT },
164 { "enter", CTRL('m') },
165 { "tab", CTRL('i') },
170 for (t = table; t->p != NULL; ++t) {
171 if (has_prefix(key, t->p))
179 kmap_define_key(struct kmap *map, const char *key, void (*fn)(struct tab*))
182 struct keymap *entry;
185 if ((ctrl = has_prefix(key, "C-")))
187 if ((meta = has_prefix(key, "M-")))
196 /* skip key & spaces */
197 while (*key != '\0' && !isspace(*key))
199 while (*key != '\0' && isspace(*key))
202 TAILQ_FOREACH(entry, map, keymaps) {
203 if (entry->meta == meta && entry->key == k) {
213 if ((entry = calloc(1, sizeof(*entry))) == NULL)
218 TAILQ_INIT(&entry->map);
220 if (TAILQ_EMPTY(map))
221 TAILQ_INSERT_HEAD(map, entry, keymaps);
223 TAILQ_INSERT_TAIL(map, entry, keymaps);
234 global_set_key(const char *key, void (*fn)(struct tab*))
236 kmap_define_key(&global_map, key, fn);
240 load_default_keys(void)
243 global_set_key("C-p", cmd_previous_line);
244 global_set_key("C-n", cmd_next_line);
245 global_set_key("C-f", cmd_forward_char);
246 global_set_key("C-b", cmd_backward_char);
249 global_set_key("M-v", cmd_scroll_up);
250 global_set_key("C-v", cmd_scroll_down);
252 global_set_key("C-x C-c", cmd_kill_telescope);
255 global_set_key("k", cmd_previous_line);
256 global_set_key("j", cmd_next_line);
257 global_set_key("l", cmd_forward_char);
258 global_set_key("h", cmd_backward_char);
260 global_set_key("K", cmd_scroll_up);
261 global_set_key("J", cmd_scroll_down);
264 global_set_key("q", cmd_kill_telescope);
267 global_set_key("<up>", cmd_previous_line);
268 global_set_key("<down>", cmd_next_line);
269 global_set_key("<right>", cmd_forward_char);
270 global_set_key("<left>", cmd_backward_char);
272 /* "ncurses standard" */
273 global_set_key("C-l", cmd_redraw);
276 global_set_key("C-m", cmd_push_button);
280 push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
286 if ((vl = calloc(1, sizeof(*vl))) == NULL)
289 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
296 memcpy(vl->line, buf, len);
299 if (TAILQ_EMPTY(&tab->s->head))
300 TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
302 TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
307 empty_vlist(struct tab *tab)
311 tab->s->line_max = 0;
313 TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
314 TAILQ_REMOVE(&tab->s->head, l, lines);
316 /* l->alt references the original line! */
322 restore_cursor(struct tab *tab)
324 wmove(body, tab->s->curs_y, tab->s->curs_x);
328 cmd_previous_line(struct tab *tab)
330 if (--tab->s->curs_y < 0) {
339 cmd_next_line(struct tab *tab)
341 if (++tab->s->curs_y > body_lines-1) {
342 tab->s->curs_y = body_lines-1;
343 cmd_scroll_down(tab);
350 cmd_forward_char(struct tab *tab)
352 tab->s->curs_x = MIN(body_cols-1, tab->s->curs_x+1);
357 cmd_backward_char(struct tab *tab)
359 tab->s->curs_x = MAX(0, tab->s->curs_x-1);
364 cmd_redraw(struct tab *tab)
366 handle_resize(0, 0, NULL);
370 cmd_scroll_up(struct tab *tab)
374 if (tab->s->line_off == 0)
377 l = nth_line(tab, --tab->s->line_off);
384 cmd_scroll_down(struct tab *tab)
389 if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
395 if (tab->s->line_max - tab->s->line_off < body_lines)
398 l = nth_line(tab, tab->s->line_off + body_lines-1);
399 wmove(body, body_lines-1, 0);
404 cmd_kill_telescope(struct tab *tab)
410 cmd_push_button(struct tab *tab)
415 nth = tab->s->line_off + tab->s->curs_y;
416 if (nth > tab->s->line_max)
418 l = nth_line(tab, nth);
419 if (l->type != LINE_LINK)
422 load_url_in_tab(tab, l->alt);
426 nth_line(struct tab *tab, size_t n)
432 TAILQ_FOREACH(l, &tab->s->head, lines) {
447 TAILQ_FOREACH(t, &tabshead, tabs) {
448 if (t->flags & TAB_CURRENT)
457 dispatch_stdio(int fd, short ev, void *d)
466 /* TODO: make escape-time customizable */
475 TAILQ_FOREACH(k, current_map, keymaps) {
476 if (k->meta == meta && k->key == key) {
478 current_map = &k->map;
480 current_map = &global_map;
481 k->fn(current_tab());
487 current_map = &global_map;
489 message("%s%c is undefined",
490 meta ? "M-" : "", key);
493 restore_cursor(current_tab());
502 handle_clear_minibuf(int fd, short ev, void *d)
511 handle_resize(int sig, short ev, void *d)
519 /* move and resize the windows, in reverse order! */
521 mvwin(minibuf, LINES-1, 0);
522 wresize(minibuf, 1, COLS);
524 mvwin(modeline, LINES-2, 0);
525 wresize(modeline, 1, COLS);
527 wresize(body, LINES-3, COLS);
528 body_lines = LINES-3;
531 wresize(tabline, 1, COLS);
540 * Helper function for wrap_text. Find the end of the current word
541 * and the end of the separator after the word.
544 word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
552 /* find the end of the current world */
553 for (; *s != '\0'; ++s) {
554 if (strchr(sep, *s) != NULL)
560 /* find the end of the separator */
561 for (; *s != '\0'; ++s) {
562 if (strchr(sep, *s) == NULL)
572 emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
575 if (!push_line(tab, l, *line, *off - zero))
577 *line += *off - zero;
583 emitstr(const char **s, size_t len, size_t *off)
587 /* printw("%*s", ...) doesn't seem to respect the precision, so... */
588 for (i = 0; i < len; ++i)
595 * Build a list of visual line by wrapping the given line, assuming
596 * that when printed will have a leading prefix prfx.
598 * TODO: it considers each byte one cell on the screen!
601 wrap_text(struct tab *tab, const char *prfx, struct line *l)
603 size_t zero, off, len, split;
604 const char *endword, *endspc, *line, *linestart;
611 while (word_boundaries(line, " \t-", &endword, &endspc)) {
612 len = endword - line;
613 if (off + len >= body_cols) {
614 emitline(tab, zero, &off, l, &linestart);
615 while (len >= body_cols) {
617 emitline(tab, zero, &off, l, &linestart);
627 /* print the spaces iff not at bol */
628 len = endspc - endword;
631 if (off + len >= body_cols) {
632 emitline(tab, zero, &off, l, &linestart);
641 emitline(tab, zero, &off, l, &linestart);
645 hardwrap_text(struct tab *tab, struct line *l)
648 const char *linestart;
650 len = strlen(l->line);
654 while (len >= COLS) {
657 if (!emitline(tab, 0, &off, l, &linestart))
662 return emitline(tab, 0, &len, l, &linestart);
668 wrap_page(struct tab *tab)
674 TAILQ_FOREACH(l, &tab->page.head, lines) {
677 wrap_text(tab, "", l);
680 wrap_text(tab, "=> ", l);
683 wrap_text(tab, "# ", l);
686 wrap_text(tab, "## ", l);
689 wrap_text(tab, "### ", l);
692 wrap_text(tab, "* ", l);
695 wrap_text(tab, "> ", l);
699 push_line(tab, l, NULL, 0);
701 case LINE_PRE_CONTENT:
702 hardwrap_text(tab, l);
710 print_line(struct line *l)
712 const char *text = l->line;
719 wprintw(body, "%s", text);
722 wattron(body, A_UNDERLINE);
723 wprintw(body, "=> %s", text);
724 wattroff(body, A_UNDERLINE);
727 wattron(body, A_BOLD);
728 wprintw(body, "# %s", text);
729 wattroff(body, A_BOLD);
732 wattron(body, A_BOLD);
733 wprintw(body, "## %s", text);
734 wattroff(body, A_BOLD);
737 wattron(body, A_BOLD);
738 wprintw(body, "### %s", text);
739 wattroff(body, A_BOLD);
742 wprintw(body, "* %s", text);
745 wattron(body, A_DIM);
746 wprintw(body, "> %s", text);
747 wattroff(body, A_DIM);
751 wprintw(body, "```");
753 case LINE_PRE_CONTENT:
754 wprintw(body, "%s", text);
763 wbkgd(tabline, A_REVERSE);
764 mvwprintw(tabline, 0, 0, "TODO: tabs here");
768 redraw_modeline(struct tab *tab)
770 int x, y, max_x, max_y;
771 const char *mode = "text/gemini-mode";
772 const char *spin = "-\\|/";
775 wattron(modeline, A_REVERSE);
776 wmove(modeline, 0, 0);
778 wprintw(modeline, "-%c %s %s ",
779 spin[tab->s->loading_anim_step], mode, tab->urlstr);
780 getyx(modeline, y, x);
781 getmaxyx(modeline, max_y, max_x);
786 for (; x < max_x; ++x)
787 waddstr(modeline, "-");
791 redraw_tab(struct tab *tab)
798 tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
799 if (TAILQ_EMPTY(&tab->s->head))
803 l = nth_line(tab, tab->s->line_off);
804 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
805 wmove(body, line, 0);
808 if (line == body_lines)
813 redraw_modeline(tab);
824 message(const char *fmt, ...)
831 evtimer_del(&clminibufev);
832 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
833 evtimer_add(&clminibufev, &clminibufev_timer);
837 vw_printw(minibuf, fmt, ap);
846 start_loading_anim(struct tab *tab)
848 if (tab->s->loading_anim)
850 tab->s->loading_anim = 1;
851 evtimer_set(&tab->s->loadingev, update_loading_anim, tab);
852 evtimer_add(&tab->s->loadingev, &loadingev_timer);
856 update_loading_anim(int fd, short ev, void *d)
860 tab->s->loading_anim_step = (tab->s->loading_anim_step+1)%4;
862 redraw_modeline(tab);
866 evtimer_add(&tab->s->loadingev, &loadingev_timer);
870 stop_loading_anim(struct tab *tab)
872 if (!tab->s->loading_anim)
874 evtimer_del(&tab->s->loadingev);
875 tab->s->loading_anim = 0;
876 tab->s->loading_anim_step = 0;
878 redraw_modeline(tab);
884 load_url_in_tab(struct tab *tab, const char *url)
887 message("Loading %s...", url);
888 start_loading_anim(tab);
900 const char *url = "about:new";
902 if ((tab = calloc(1, sizeof(*tab))) == NULL)
905 if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
908 TAILQ_INIT(&tab->s->head);
909 TAILQ_FOREACH(t, &tabshead, tabs) {
910 t->flags &= ~TAB_CURRENT;
913 tab->id = tab_counter++;
914 tab->flags = TAB_CURRENT;
916 if (TAILQ_EMPTY(&tabshead))
917 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
919 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
921 load_url_in_tab(tab, url);
931 setlocale(LC_ALL, "");
933 TAILQ_INIT(&global_map);
934 current_map = &global_map;
942 intrflush(stdscr, FALSE);
944 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
946 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
948 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
950 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
953 body_lines = LINES-3;
957 scrollok(body, TRUE);
959 /* non-blocking input */
962 mvwprintw(body, 0, 0, "");
964 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
965 event_add(&stdioev, NULL);
967 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
968 signal_add(&winchev, NULL);
976 ui_on_tab_loaded(struct tab *tab)
978 stop_loading_anim(tab);
979 message("Loaded %s", tab->urlstr);
983 ui_on_tab_refresh(struct tab *tab)
985 if (!(tab->flags & TAB_CURRENT))