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(int);
82 static void cmd_next_line(int);
83 static void cmd_forward_char(int);
84 static void cmd_backward_char(int);
85 static void cmd_redraw(int);
86 static void cmd_scroll_down(int);
87 static void cmd_scroll_up(int);
88 static void cmd_kill_telescope(int);
89 static void cmd_push_button(int);
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_tab(struct tab*);
102 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
103 static void new_tab(void);
105 typedef void (*interactivefn)(int);
107 static WINDOW *tabline, *body, *modeline, *minibuf;
108 static int body_lines, body_cols;
110 static struct event clminibufev;
111 static int clminibufev_set;
112 static struct timeval clminibufev_timer = { 5, 0 };
114 static uint32_t tab_counter;
122 TAILQ_HEAD(, line) head;
132 #define KEY(n) ((struct key){0, n})
133 #define CKEY(n) ((struct key){0, (n) & 0x1F})
134 #define MKEY(n) ((struct key){METAFLG, n})
135 #define CMKEY(n) ((struct key){METAFLG, (n) & 0x1F})
141 { CKEY('p'), cmd_previous_line, },
142 { CKEY('n'), cmd_next_line, },
143 { CKEY('f'), cmd_forward_char, },
144 { CKEY('b'), cmd_backward_char, },
146 { CKEY('L'), cmd_redraw, },
148 { KEY('J'), cmd_scroll_down, },
149 { KEY('K'), cmd_scroll_up, },
151 { KEY('q'), cmd_kill_telescope, },
152 { CKEY('m'), cmd_push_button, },
158 push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
164 if ((vl = calloc(1, sizeof(*vl))) == NULL)
167 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
174 memcpy(vl->line, buf, len);
177 if (TAILQ_EMPTY(&tab->s->head))
178 TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
180 TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
185 empty_vlist(struct tab *tab)
189 tab->s->line_max = 0;
191 TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
192 TAILQ_REMOVE(&tab->s->head, l, lines);
194 /* l->alt references the original line! */
200 restore_cursor(struct tab *tab)
202 wmove(body, tab->s->curs_y, tab->s->curs_x);
206 cmd_previous_line(int k)
212 if (--tab->s->curs_y < 0) {
227 if (++tab->s->curs_y > body_lines-1) {
228 tab->s->curs_y = body_lines-1;
236 cmd_forward_char(int k)
241 tab->s->curs_x = MIN(body_cols-1, tab->s->curs_x+1);
246 cmd_backward_char(int k)
251 tab->s->curs_x = MAX(0, tab->s->curs_x-1);
258 handle_resize(0, 0, NULL);
268 if (tab->s->line_off == 0)
271 l = nth_line(tab, --tab->s->line_off);
278 cmd_scroll_down(int k)
286 if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
292 if (tab->s->line_max - tab->s->line_off < body_lines)
295 l = nth_line(tab, tab->s->line_off + body_lines-1);
296 wmove(body, body_lines-1, 0);
301 cmd_kill_telescope(int k)
307 cmd_push_button(int k)
315 nth = tab->s->line_off + tab->s->curs_y;
316 if (nth > tab->s->line_max)
318 l = nth_line(tab, nth);
319 message("Enter on line: \"%s\"", l->line ? l->line : "");
323 cmd_unbound(struct key k)
325 message("%s%c is undefined",
326 (k.flags & METAFLG) ? "M-" : "",
331 nth_line(struct tab *tab, size_t n)
337 TAILQ_FOREACH(l, &tab->s->head, lines) {
352 TAILQ_FOREACH(t, &tabshead, tabs) {
353 if (t->flags & TAB_CURRENT)
362 dispatch_stdio(int fd, short ev, void *d)
375 /* look for a M-key */
377 wtimeout(body, 0); /* TODO: make escape-time customizable */
387 for (b = bindings; b->fn != NULL; ++b) {
388 if (key.flags == b->key.flags &&
389 key.key == b->key.key) {
398 restore_cursor(current_tab());
407 handle_clear_minibuf(int fd, short ev, void *d)
416 handle_resize(int sig, short ev, void *d)
424 /* move and resize the windows, in reverse order! */
426 mvwin(minibuf, LINES-1, 0);
427 wresize(minibuf, 1, COLS);
429 mvwin(modeline, LINES-2, 0);
430 wresize(modeline, 1, COLS);
432 wresize(body, LINES-3, COLS);
433 body_lines = LINES-3;
436 wresize(tabline, 1, COLS);
445 * Helper function for wrap_text. Find the end of the current word
446 * and the end of the separator after the word.
449 word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
457 /* find the end of the current world */
458 for (; *s != '\0'; ++s) {
459 if (strchr(sep, *s) != NULL)
465 /* find the end of the separator */
466 for (; *s != '\0'; ++s) {
467 if (strchr(sep, *s) == NULL)
477 emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
480 if (!push_line(tab, l, *line, *off - zero))
482 *line += *off - zero;
488 emitstr(const char **s, size_t len, size_t *off)
492 /* printw("%*s", ...) doesn't seem to respect the precision, so... */
493 for (i = 0; i < len; ++i)
500 * Build a list of visual line by wrapping the given line, assuming
501 * that when printed will have a leading prefix prfx.
503 * TODO: it considers each byte one cell on the screen!
506 wrap_text(struct tab *tab, const char *prfx, struct line *l)
508 size_t zero, off, len, split;
509 const char *endword, *endspc, *line, *linestart;
516 while (word_boundaries(line, " \t-", &endword, &endspc)) {
517 len = endword - line;
518 if (off + len >= body_cols) {
519 emitline(tab, zero, &off, l, &linestart);
520 while (len >= body_cols) {
522 emitline(tab, zero, &off, l, &linestart);
532 /* print the spaces iff not at bol */
533 len = endspc - endword;
536 if (off + len >= body_cols) {
537 emitline(tab, zero, &off, l, &linestart);
546 emitline(tab, zero, &off, l, &linestart);
550 hardwrap_text(struct tab *tab, struct line *l)
553 const char *linestart;
555 len = strlen(l->line);
559 while (len >= COLS) {
562 if (!emitline(tab, 0, &off, l, &linestart))
567 return emitline(tab, 0, &len, l, &linestart);
573 wrap_page(struct tab *tab)
579 TAILQ_FOREACH(l, &tab->page.head, lines) {
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);
597 wrap_text(tab, "* ", l);
600 wrap_text(tab, "> ", l);
604 push_line(tab, l, NULL, 0);
606 case LINE_PRE_CONTENT:
607 hardwrap_text(tab, l);
615 print_line(struct line *l)
617 const char *text = l->line;
624 wprintw(body, "%s", text);
627 wattron(body, A_UNDERLINE);
628 wprintw(body, "=> %s", text);
629 wattroff(body, A_UNDERLINE);
632 wattron(body, A_BOLD);
633 wprintw(body, "# %s", text);
634 wattroff(body, A_BOLD);
637 wattron(body, A_BOLD);
638 wprintw(body, "## %s", text);
639 wattroff(body, A_BOLD);
642 wattron(body, A_BOLD);
643 wprintw(body, "### %s", text);
644 wattroff(body, A_BOLD);
647 wprintw(body, "* %s", text);
650 wattron(body, A_DIM);
651 wprintw(body, "> %s", text);
652 wattroff(body, A_DIM);
656 wprintw(body, "```");
658 case LINE_PRE_CONTENT:
659 wprintw(body, "%s", text);
665 redraw_modeline(struct tab *tab)
667 int x, y, max_x, max_y;
668 const char *url = "TODO:url";
669 const char *mode = "text/gemini-mode";
672 wattron(modeline, A_REVERSE);
673 wmove(modeline, 0, 0);
675 wprintw(modeline, "-- %s %s ", mode, url);
676 getyx(modeline, y, x);
677 getmaxyx(modeline, max_y, max_x);
682 for (; x < max_x; ++x)
683 waddstr(modeline, "-");
687 redraw_tab(struct tab *tab)
694 tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
695 if (TAILQ_EMPTY(&tab->s->head))
699 l = nth_line(tab, tab->s->line_off);
700 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
701 wmove(body, line, 0);
704 if (line == body_lines)
708 redraw_modeline(tab);
719 message(const char *fmt, ...)
726 evtimer_del(&clminibufev);
727 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
728 evtimer_add(&clminibufev, &clminibufev_timer);
732 vw_printw(minibuf, fmt, ap);
744 const char *url = "about:new";
746 if ((tab = calloc(1, sizeof(*tab))) == NULL)
749 if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
752 TAILQ_INIT(&tab->s->head);
753 TAILQ_FOREACH(t, &tabshead, tabs) {
754 t->flags &= ~TAB_CURRENT;
757 tab->id = tab_counter++;
758 tab->flags = TAB_CURRENT;
760 if (TAILQ_EMPTY(&tabshead))
761 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
763 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
775 setlocale(LC_ALL, "");
782 intrflush(stdscr, FALSE);
783 keypad(stdscr, TRUE);
785 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
787 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
789 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
791 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
794 body_lines = LINES-3;
797 scrollok(body, TRUE);
799 /* non-blocking input */
802 mvwprintw(body, 0, 0, "");
804 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
805 event_add(&stdioev, NULL);
807 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
808 signal_add(&winchev, NULL);
816 ui_on_tab_refresh(struct tab *tab)
818 if (!(tab->flags & TAB_CURRENT))