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_resize(int, short, void*);
92 static int word_bourdaries(const char*, const char*, const char**, const char**);
93 static void wrap_text(struct tab*, const char*, struct line*);
94 static int hardwrap_text(struct tab*, struct line*);
95 static int wrap_page(struct tab*);
96 static void print_line(struct line*);
97 static void redraw_tab(struct tab*);
99 typedef void (*interactivefn)(int);
101 static void cmd_unbound(int);
103 static WINDOW *tabline, *body, *modeline, *minibuf;
104 static int body_lines, body_cols;
106 static struct event clminibufev;
107 static int clminibufev_set;
115 TAILQ_HEAD(, line) head;
122 { CTRL('p'), cmd_previous_line, },
123 { CTRL('n'), cmd_next_line, },
124 { CTRL('f'), cmd_forward_char, },
125 { CTRL('b'), cmd_backward_char, },
127 { CTRL('L'), cmd_redraw, },
129 { 'J', cmd_scroll_down, },
130 { 'K', cmd_scroll_up, },
132 { 'q', cmd_kill_telescope, },
138 push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
144 if ((vl = calloc(1, sizeof(*vl))) == NULL)
147 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
154 memcpy(vl->line, buf, len);
157 if (TAILQ_EMPTY(&tab->s->head))
158 TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
160 TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
165 empty_vlist(struct tab *tab)
169 tab->s->line_max = 0;
171 TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
172 TAILQ_REMOVE(&tab->s->head, l, lines);
174 /* l->alt references the original line! */
180 restore_cursor(struct tab *tab)
182 wmove(body, tab->s->curs_y, tab->s->curs_x);
186 cmd_previous_line(int k)
191 tab->s->curs_y = MAX(0, tab->s->curs_y-1);
201 tab->s->curs_y = MIN(LINES-1, tab->s->curs_y+1);
206 cmd_forward_char(int k)
211 tab->s->curs_x = MIN(COLS-1, tab->s->curs_x+1);
216 cmd_backward_char(int k)
221 tab->s->curs_x = MAX(0, tab->s->curs_x-1);
229 redraw_tab(current_tab());
239 if (tab->s->line_off == 0)
242 l = nth_line(tab, --tab->s->line_off);
249 cmd_scroll_down(int k)
257 if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
263 if (tab->s->line_max - tab->s->line_off < body_lines)
266 l = nth_line(tab, tab->s->line_off + body_lines-1);
267 wmove(body, body_lines-1, 0);
272 cmd_kill_telescope(int k)
281 wprintw(minibuf, "Unknown key %c", k);
282 restore_cursor(current_tab());
286 nth_line(struct tab *tab, size_t n)
292 TAILQ_FOREACH(l, &tab->s->head, lines) {
307 TAILQ_FOREACH(t, &tabshead, tabs) {
308 if (t->flags & TAB_CURRENT)
317 dispatch_stdio(int fd, short ev, void *d)
327 for (b = bindings; b->fn != NULL; ++b) {
337 restore_cursor(current_tab());
346 handle_resize(int sig, short ev, void *d)
354 /* move and resize the windows, in reverse order! */
356 mvwin(minibuf, LINES-2, 0);
357 wresize(minibuf, 1, COLS);
359 mvwin(modeline, LINES-2, 0);
360 wresize(modeline, 1, COLS);
362 wresize(body, LINES-3, COLS);
363 body_lines = LINES-3;
366 wresize(tabline, 1, COLS);
375 * Helper function for wrap_text. Find the end of the current word
376 * and the end of the separator after the word.
379 word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
387 /* find the end of the current world */
388 for (; *s != '\0'; ++s) {
389 if (strchr(sep, *s) != NULL)
395 /* find the end of the separator */
396 for (; *s != '\0'; ++s) {
397 if (strchr(sep, *s) == NULL)
407 emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
410 if (!push_line(tab, l, *line, *off - zero))
412 *line += *off - zero;
418 emitstr(const char **s, size_t len, size_t *off)
422 /* printw("%*s", ...) doesn't seem to respect the precision, so... */
423 for (i = 0; i < len; ++i)
430 * Build a list of visual line by wrapping the given line, assuming
431 * that when printed will have a leading prefix prfx.
433 * TODO: it considers each byte one cell on the screen!
436 wrap_text(struct tab *tab, const char *prfx, struct line *l)
438 size_t zero, off, len, split;
439 const char *endword, *endspc, *line, *linestart;
446 while (word_boundaries(line, " \t-", &endword, &endspc)) {
447 len = endword - line;
448 if (off + len >= body_cols) {
449 emitline(tab, zero, &off, l, &linestart);
450 while (len >= body_cols) {
452 emitline(tab, zero, &off, l, &linestart);
462 /* print the spaces iff not at bol */
463 len = endspc - endword;
466 if (off + len >= body_cols) {
467 emitline(tab, zero, &off, l, &linestart);
476 emitline(tab, zero, &off, l, &linestart);
480 hardwrap_text(struct tab *tab, struct line *l)
483 const char *linestart;
485 len = strlen(l->line);
489 while (len >= COLS) {
492 if (!emitline(tab, 0, &off, l, &linestart))
500 wrap_page(struct tab *tab)
506 TAILQ_FOREACH(l, &tab->page.head, lines) {
509 wrap_text(tab, "", l);
512 wrap_text(tab, "=> ", l);
515 wrap_text(tab, "# ", l);
518 wrap_text(tab, "## ", l);
521 wrap_text(tab, "### ", l);
524 wrap_text(tab, "* ", l);
527 wrap_text(tab, "> ", l);
531 push_line(tab, l, NULL, 0);
533 case LINE_PRE_CONTENT:
534 hardwrap_text(tab, l);
542 print_line(struct line *l)
548 wprintw(body, "=> ");
554 wprintw(body, "## ");
557 wprintw(body, "### ");
567 wprintw(body, "```");
569 case LINE_PRE_CONTENT:
574 wprintw(body, "%s", l->line);
578 redraw_modeline(struct tab *tab)
580 int x, y, max_x, max_y;
581 const char *url = "TODO:url";
582 const char *mode = "text/gemini-mode";
585 wattron(modeline, A_REVERSE);
586 wmove(modeline, 0, 0);
588 wprintw(modeline, "-- %s %s ", mode, url);
589 getyx(modeline, y, x);
590 getmaxyx(modeline, max_y, max_x);
595 for (; x < max_x; ++x)
596 waddstr(modeline, "-");
600 redraw_tab(struct tab *tab)
607 tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
608 if (TAILQ_EMPTY(&tab->s->head))
612 l = nth_line(tab, tab->s->line_off);
613 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
614 wmove(body, line, 0);
617 if (line == body_lines)
621 redraw_modeline(tab);
634 setlocale(LC_ALL, "");
641 intrflush(stdscr, FALSE);
642 keypad(stdscr, TRUE);
644 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
646 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
648 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
650 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
653 body_lines = LINES-3;
656 scrollok(body, TRUE);
658 /* non-blocking input */
661 mvwprintw(body, 0, 0, "");
663 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
664 event_add(&stdioev, NULL);
666 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
667 signal_add(&winchev, NULL);
673 ui_on_new_tab(struct tab *tab)
677 if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
680 TAILQ_INIT(&tab->s->head);
682 TAILQ_FOREACH(t, &tabshead, tabs) {
683 t->flags &= ~TAB_CURRENT;
685 tab->flags = TAB_CURRENT;
687 /* TODO: redraw the tab list */
688 /* TODO: switch to the new tab */
696 ui_on_tab_refresh(struct tab *tab)
698 if (!(tab->flags & TAB_CURRENT))