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 CTRL(x) ((x)&0x1F)
73 #define MIN(a, b) ((a) < (b) ? (a) : (b))
74 #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 struct line *nth_line(struct tab*, size_t);
90 static struct tab *current_tab(void);
91 static void dispatch_stdio(int, short, void*);
92 static void handle_clear_minibuf(int, short, void*);
93 static void handle_resize(int, short, void*);
94 static int word_bourdaries(const char*, const char*, const char**, const char**);
95 static void wrap_text(struct tab*, const char*, struct line*);
96 static int hardwrap_text(struct tab*, struct line*);
97 static int wrap_page(struct tab*);
98 static void print_line(struct line*);
99 static void redraw_tab(struct tab*);
100 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
102 typedef void (*interactivefn)(int);
104 static void cmd_unbound(int);
106 static WINDOW *tabline, *body, *modeline, *minibuf;
107 static int body_lines, body_cols;
109 static struct event clminibufev;
110 static int clminibufev_set;
111 static struct timeval clminibufev_timer = { 5, 0 };
119 TAILQ_HEAD(, line) head;
126 { CTRL('p'), cmd_previous_line, },
127 { CTRL('n'), cmd_next_line, },
128 { CTRL('f'), cmd_forward_char, },
129 { CTRL('b'), cmd_backward_char, },
131 { CTRL('L'), cmd_redraw, },
133 { 'J', cmd_scroll_down, },
134 { 'K', cmd_scroll_up, },
136 { 'q', cmd_kill_telescope, },
142 push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
148 if ((vl = calloc(1, sizeof(*vl))) == NULL)
151 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
158 memcpy(vl->line, buf, len);
161 if (TAILQ_EMPTY(&tab->s->head))
162 TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
164 TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
169 empty_vlist(struct tab *tab)
173 tab->s->line_max = 0;
175 TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
176 TAILQ_REMOVE(&tab->s->head, l, lines);
178 /* l->alt references the original line! */
184 restore_cursor(struct tab *tab)
186 wmove(body, tab->s->curs_y, tab->s->curs_x);
190 cmd_previous_line(int k)
196 if (--tab->s->curs_y < 0) {
211 if (++tab->s->curs_y > body_lines-1) {
212 tab->s->curs_y = body_lines-1;
220 cmd_forward_char(int k)
225 tab->s->curs_x = MIN(body_cols-1, tab->s->curs_x+1);
230 cmd_backward_char(int k)
235 tab->s->curs_x = MAX(0, tab->s->curs_x-1);
242 handle_resize(0, 0, NULL);
252 if (tab->s->line_off == 0)
255 l = nth_line(tab, --tab->s->line_off);
262 cmd_scroll_down(int k)
270 if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
276 if (tab->s->line_max - tab->s->line_off < body_lines)
279 l = nth_line(tab, tab->s->line_off + body_lines-1);
280 wmove(body, body_lines-1, 0);
285 cmd_kill_telescope(int k)
293 message("%c is undefined", k);
297 nth_line(struct tab *tab, size_t n)
303 TAILQ_FOREACH(l, &tab->s->head, lines) {
318 TAILQ_FOREACH(t, &tabshead, tabs) {
319 if (t->flags & TAB_CURRENT)
328 dispatch_stdio(int fd, short ev, void *d)
338 for (b = bindings; b->fn != NULL; ++b) {
348 restore_cursor(current_tab());
357 handle_clear_minibuf(int fd, short ev, void *d)
366 handle_resize(int sig, short ev, void *d)
374 /* move and resize the windows, in reverse order! */
376 mvwin(minibuf, LINES-1, 0);
377 wresize(minibuf, 1, COLS);
379 mvwin(modeline, LINES-2, 0);
380 wresize(modeline, 1, COLS);
382 wresize(body, LINES-3, COLS);
383 body_lines = LINES-3;
386 wresize(tabline, 1, COLS);
395 * Helper function for wrap_text. Find the end of the current word
396 * and the end of the separator after the word.
399 word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
407 /* find the end of the current world */
408 for (; *s != '\0'; ++s) {
409 if (strchr(sep, *s) != NULL)
415 /* find the end of the separator */
416 for (; *s != '\0'; ++s) {
417 if (strchr(sep, *s) == NULL)
427 emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
430 if (!push_line(tab, l, *line, *off - zero))
432 *line += *off - zero;
438 emitstr(const char **s, size_t len, size_t *off)
442 /* printw("%*s", ...) doesn't seem to respect the precision, so... */
443 for (i = 0; i < len; ++i)
450 * Build a list of visual line by wrapping the given line, assuming
451 * that when printed will have a leading prefix prfx.
453 * TODO: it considers each byte one cell on the screen!
456 wrap_text(struct tab *tab, const char *prfx, struct line *l)
458 size_t zero, off, len, split;
459 const char *endword, *endspc, *line, *linestart;
466 while (word_boundaries(line, " \t-", &endword, &endspc)) {
467 len = endword - line;
468 if (off + len >= body_cols) {
469 emitline(tab, zero, &off, l, &linestart);
470 while (len >= body_cols) {
472 emitline(tab, zero, &off, l, &linestart);
482 /* print the spaces iff not at bol */
483 len = endspc - endword;
486 if (off + len >= body_cols) {
487 emitline(tab, zero, &off, l, &linestart);
496 emitline(tab, zero, &off, l, &linestart);
500 hardwrap_text(struct tab *tab, struct line *l)
503 const char *linestart;
505 len = strlen(l->line);
509 while (len >= COLS) {
512 if (!emitline(tab, 0, &off, l, &linestart))
520 wrap_page(struct tab *tab)
526 TAILQ_FOREACH(l, &tab->page.head, lines) {
529 wrap_text(tab, "", l);
532 wrap_text(tab, "=> ", l);
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);
551 push_line(tab, l, NULL, 0);
553 case LINE_PRE_CONTENT:
554 hardwrap_text(tab, l);
562 print_line(struct line *l)
564 const char *text = l->line;
571 wprintw(body, "%s", text);
574 wattron(body, A_UNDERLINE);
575 wprintw(body, "=> %s", text);
576 wattroff(body, A_UNDERLINE);
579 wattron(body, A_BOLD);
580 wprintw(body, "# %s", text);
581 wattroff(body, A_BOLD);
584 wattron(body, A_BOLD);
585 wprintw(body, "## %s", text);
586 wattroff(body, A_BOLD);
589 wattron(body, A_BOLD);
590 wprintw(body, "### %s", text);
591 wattroff(body, A_BOLD);
594 wprintw(body, "* %s", text);
597 wattron(body, A_DIM);
598 wprintw(body, "> %s", text);
599 wattroff(body, A_DIM);
603 wprintw(body, "```");
605 case LINE_PRE_CONTENT:
606 wprintw(body, "%s", text);
612 redraw_modeline(struct tab *tab)
614 int x, y, max_x, max_y;
615 const char *url = "TODO:url";
616 const char *mode = "text/gemini-mode";
619 wattron(modeline, A_REVERSE);
620 wmove(modeline, 0, 0);
622 wprintw(modeline, "-- %s %s ", mode, url);
623 getyx(modeline, y, x);
624 getmaxyx(modeline, max_y, max_x);
629 for (; x < max_x; ++x)
630 waddstr(modeline, "-");
634 redraw_tab(struct tab *tab)
641 tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
642 if (TAILQ_EMPTY(&tab->s->head))
646 l = nth_line(tab, tab->s->line_off);
647 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
648 wmove(body, line, 0);
651 if (line == body_lines)
655 redraw_modeline(tab);
666 message(const char *fmt, ...)
673 evtimer_del(&clminibufev);
674 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
675 evtimer_add(&clminibufev, &clminibufev_timer);
679 vw_printw(minibuf, fmt, ap);
690 setlocale(LC_ALL, "");
697 intrflush(stdscr, FALSE);
698 keypad(stdscr, TRUE);
700 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
702 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
704 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
706 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
709 body_lines = LINES-3;
712 scrollok(body, TRUE);
714 /* non-blocking input */
717 mvwprintw(body, 0, 0, "");
719 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
720 event_add(&stdioev, NULL);
722 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
723 signal_add(&winchev, NULL);
729 ui_on_new_tab(struct tab *tab)
733 if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
736 TAILQ_INIT(&tab->s->head);
738 TAILQ_FOREACH(t, &tabshead, tabs) {
739 t->flags &= ~TAB_CURRENT;
741 tab->flags = TAB_CURRENT;
743 /* TODO: redraw the tab list */
744 /* TODO: switch to the new tab */
752 ui_on_tab_refresh(struct tab *tab)
754 if (!(tab->flags & TAB_CURRENT))