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>
68 #define TAB_CURRENT 0x1
70 #define CTRL(x) ((x)&0x1F)
72 #define MIN(a, b) ((a) < (b) ? (a) : (b))
73 #define MAX(a, b) ((a) > (b) ? (a) : (b))
75 static struct event stdioev, winchev;
77 static int push_line(struct tab*, const struct line*, const char*, size_t);
78 static void empty_vlist(struct tab*);
79 static void restore_cursor(struct tab *);
80 static void cmd_previous_line(int);
81 static void cmd_next_line(int);
82 static void cmd_forward_char(int);
83 static void cmd_backward_char(int);
84 static void cmd_redraw(int);
85 static void cmd_scroll_down(int);
86 static void cmd_scroll_up(int);
87 static void cmd_kill_telescope(int);
88 static struct line *nth_line(struct tab*, size_t);
89 static struct tab *current_tab(void);
90 static void dispatch_stdio(int, short, void*);
91 static void handle_clear_minibuf(int, short, void*);
92 static void handle_resize(int, short, void*);
93 static int word_bourdaries(const char*, const char*, const char**, const char**);
94 static void wrap_text(struct tab*, const char*, struct line*);
95 static int hardwrap_text(struct tab*, struct line*);
96 static int wrap_page(struct tab*);
97 static void print_line(struct line*);
98 static void redraw_tab(struct tab*);
100 typedef void (*interactivefn)(int);
102 static void cmd_unbound(int);
104 static WINDOW *tabline, *body, *modeline, *minibuf;
105 static int body_lines, body_cols;
107 static struct event clminibufev;
108 static int clminibufev_set;
109 static struct timeval clminibufev_timer = { 5, 0 };
117 TAILQ_HEAD(, line) head;
124 { CTRL('p'), cmd_previous_line, },
125 { CTRL('n'), cmd_next_line, },
126 { CTRL('f'), cmd_forward_char, },
127 { CTRL('b'), cmd_backward_char, },
129 { CTRL('L'), cmd_redraw, },
131 { 'J', cmd_scroll_down, },
132 { 'K', cmd_scroll_up, },
134 { 'q', cmd_kill_telescope, },
140 push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
146 if ((vl = calloc(1, sizeof(*vl))) == NULL)
149 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
156 memcpy(vl->line, buf, len);
159 if (TAILQ_EMPTY(&tab->s->head))
160 TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
162 TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
167 empty_vlist(struct tab *tab)
171 tab->s->line_max = 0;
173 TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
174 TAILQ_REMOVE(&tab->s->head, l, lines);
176 /* l->alt references the original line! */
182 restore_cursor(struct tab *tab)
184 wmove(body, tab->s->curs_y, tab->s->curs_x);
188 cmd_previous_line(int k)
194 if (--tab->s->curs_y < 0) {
209 if (++tab->s->curs_y > body_lines-1) {
210 tab->s->curs_y = body_lines-1;
218 cmd_forward_char(int k)
223 tab->s->curs_x = MIN(body_cols-1, tab->s->curs_x+1);
228 cmd_backward_char(int k)
233 tab->s->curs_x = MAX(0, tab->s->curs_x-1);
240 handle_resize(0, 0, NULL);
250 if (tab->s->line_off == 0)
253 l = nth_line(tab, --tab->s->line_off);
260 cmd_scroll_down(int k)
268 if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
274 if (tab->s->line_max - tab->s->line_off < body_lines)
277 l = nth_line(tab, tab->s->line_off + body_lines-1);
278 wmove(body, body_lines-1, 0);
283 cmd_kill_telescope(int k)
292 evtimer_del(&clminibufev);
293 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
294 evtimer_add(&clminibufev, &clminibufev_timer);
298 wprintw(minibuf, "%c is undefined", k);
299 restore_cursor(current_tab());
303 nth_line(struct tab *tab, size_t n)
309 TAILQ_FOREACH(l, &tab->s->head, lines) {
324 TAILQ_FOREACH(t, &tabshead, tabs) {
325 if (t->flags & TAB_CURRENT)
334 dispatch_stdio(int fd, short ev, void *d)
344 for (b = bindings; b->fn != NULL; ++b) {
354 restore_cursor(current_tab());
363 handle_clear_minibuf(int fd, short ev, void *d)
372 handle_resize(int sig, short ev, void *d)
380 /* move and resize the windows, in reverse order! */
382 mvwin(minibuf, LINES-1, 0);
383 wresize(minibuf, 1, COLS);
385 mvwin(modeline, LINES-2, 0);
386 wresize(modeline, 1, COLS);
388 wresize(body, LINES-3, COLS);
389 body_lines = LINES-3;
392 wresize(tabline, 1, COLS);
401 * Helper function for wrap_text. Find the end of the current word
402 * and the end of the separator after the word.
405 word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
413 /* find the end of the current world */
414 for (; *s != '\0'; ++s) {
415 if (strchr(sep, *s) != NULL)
421 /* find the end of the separator */
422 for (; *s != '\0'; ++s) {
423 if (strchr(sep, *s) == NULL)
433 emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
436 if (!push_line(tab, l, *line, *off - zero))
438 *line += *off - zero;
444 emitstr(const char **s, size_t len, size_t *off)
448 /* printw("%*s", ...) doesn't seem to respect the precision, so... */
449 for (i = 0; i < len; ++i)
456 * Build a list of visual line by wrapping the given line, assuming
457 * that when printed will have a leading prefix prfx.
459 * TODO: it considers each byte one cell on the screen!
462 wrap_text(struct tab *tab, const char *prfx, struct line *l)
464 size_t zero, off, len, split;
465 const char *endword, *endspc, *line, *linestart;
472 while (word_boundaries(line, " \t-", &endword, &endspc)) {
473 len = endword - line;
474 if (off + len >= body_cols) {
475 emitline(tab, zero, &off, l, &linestart);
476 while (len >= body_cols) {
478 emitline(tab, zero, &off, l, &linestart);
488 /* print the spaces iff not at bol */
489 len = endspc - endword;
492 if (off + len >= body_cols) {
493 emitline(tab, zero, &off, l, &linestart);
502 emitline(tab, zero, &off, l, &linestart);
506 hardwrap_text(struct tab *tab, struct line *l)
509 const char *linestart;
511 len = strlen(l->line);
515 while (len >= COLS) {
518 if (!emitline(tab, 0, &off, l, &linestart))
526 wrap_page(struct tab *tab)
532 TAILQ_FOREACH(l, &tab->page.head, lines) {
535 wrap_text(tab, "", l);
538 wrap_text(tab, "=> ", l);
541 wrap_text(tab, "# ", l);
544 wrap_text(tab, "## ", l);
547 wrap_text(tab, "### ", l);
550 wrap_text(tab, "* ", l);
553 wrap_text(tab, "> ", l);
557 push_line(tab, l, NULL, 0);
559 case LINE_PRE_CONTENT:
560 hardwrap_text(tab, l);
568 print_line(struct line *l)
570 const char *text = l->line;
577 wprintw(body, "%s", text);
580 wattron(body, A_UNDERLINE);
581 wprintw(body, "=> %s", text);
582 wattroff(body, A_UNDERLINE);
585 wattron(body, A_BOLD);
586 wprintw(body, "# %s", text);
587 wattroff(body, A_BOLD);
590 wattron(body, A_BOLD);
591 wprintw(body, "## %s", text);
592 wattroff(body, A_BOLD);
595 wattron(body, A_BOLD);
596 wprintw(body, "### %s", text);
597 wattroff(body, A_BOLD);
600 wprintw(body, "* %s", text);
603 wattron(body, A_DIM);
604 wprintw(body, "> %s", text);
605 wattroff(body, A_DIM);
609 wprintw(body, "```");
611 case LINE_PRE_CONTENT:
612 wprintw(body, "%s", text);
618 redraw_modeline(struct tab *tab)
620 int x, y, max_x, max_y;
621 const char *url = "TODO:url";
622 const char *mode = "text/gemini-mode";
625 wattron(modeline, A_REVERSE);
626 wmove(modeline, 0, 0);
628 wprintw(modeline, "-- %s %s ", mode, url);
629 getyx(modeline, y, x);
630 getmaxyx(modeline, max_y, max_x);
635 for (; x < max_x; ++x)
636 waddstr(modeline, "-");
640 redraw_tab(struct tab *tab)
647 tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
648 if (TAILQ_EMPTY(&tab->s->head))
652 l = nth_line(tab, tab->s->line_off);
653 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
654 wmove(body, line, 0);
657 if (line == body_lines)
661 redraw_modeline(tab);
674 setlocale(LC_ALL, "");
681 intrflush(stdscr, FALSE);
682 keypad(stdscr, TRUE);
684 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
686 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
688 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
690 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
693 body_lines = LINES-3;
696 scrollok(body, TRUE);
698 /* non-blocking input */
701 mvwprintw(body, 0, 0, "");
703 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
704 event_add(&stdioev, NULL);
706 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
707 signal_add(&winchev, NULL);
713 ui_on_new_tab(struct tab *tab)
717 if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
720 TAILQ_INIT(&tab->s->head);
722 TAILQ_FOREACH(t, &tabshead, tabs) {
723 t->flags &= ~TAB_CURRENT;
725 tab->flags = TAB_CURRENT;
727 /* TODO: redraw the tab list */
728 /* TODO: switch to the new tab */
736 ui_on_tab_refresh(struct tab *tab)
738 if (!(tab->flags & TAB_CURRENT))