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 struct tab *current_tab(void);
80 static void dispatch_stdio(int, short, void*);
81 static void handle_resize(int, short, void*);
82 static int word_bourdaries(const char*, const char*, const char**, const char**);
83 static void wrap_text(struct tab*, const char*, struct line*);
84 static int hardwrap_text(struct tab*, struct line*);
85 static int wrap_page(struct tab*);
86 static void redraw_tab(struct tab*);
88 typedef void (*interactivefn)(int);
90 static void cmd_previous_line(int);
91 static void cmd_next_line(int);
92 static void cmd_forward_char(int);
93 static void cmd_backward_char(int);
94 static void cmd_redraw(int);
95 static void cmd_scroll_down(int);
96 static void cmd_scroll_up(int);
97 static void cmd_kill_telescope(int);
99 static void cmd_unbound(int);
106 TAILQ_HEAD(, line) head;
110 push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
114 if ((vl = calloc(1, sizeof(*vl))) == NULL)
117 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
124 memcpy(vl->line, buf, len);
127 if (TAILQ_EMPTY(&tab->s->head))
128 TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
130 TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
135 empty_vlist(struct tab *tab)
139 TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
140 TAILQ_REMOVE(&tab->s->head, l, lines);
142 /* l->alt references the original line! */
151 { CTRL('p'), cmd_previous_line, },
152 { CTRL('n'), cmd_next_line, },
153 { CTRL('f'), cmd_forward_char, },
154 { CTRL('b'), cmd_backward_char, },
156 { CTRL('L'), cmd_redraw, },
158 { 'J', cmd_scroll_down, },
159 { 'K', cmd_scroll_up, },
161 { 'q', cmd_kill_telescope, },
167 cmd_previous_line(int k)
172 tab->s->curs_y = MAX(0, tab->s->curs_y-1);
173 move(tab->s->curs_y, tab->s->curs_x);
182 tab->s->curs_y = MIN(LINES, tab->s->curs_y-1);
183 move(tab->s->curs_y, tab->s->curs_x);
187 cmd_forward_char(int k)
192 cmd_backward_char(int k)
200 redraw_tab(current_tab());
204 cmd_scroll_down(int k)
216 cmd_kill_telescope(int k)
224 /* TODO: flash a message */
232 TAILQ_FOREACH(t, &tabshead, tabs) {
233 if (t->flags & TAB_CURRENT)
242 dispatch_stdio(int fd, short ev, void *d)
252 for (b = bindings; b->fn != NULL; ++b) {
266 handle_resize(int sig, short ev, void *d)
281 * Helper function for wrap_text. Find the end of the current word
282 * and the end of the separator after the word.
285 word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
293 /* find the end of the current world */
294 for (; *s != '\0'; ++s) {
295 if (strchr(sep, *s) != NULL)
301 /* find the end of the separator */
302 for (; *s != '\0'; ++s) {
303 if (strchr(sep, *s) == NULL)
313 emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
316 if (!push_line(tab, l, *line, *off - zero))
318 *line += *off - zero;
324 emitstr(const char **s, size_t len, size_t *off)
328 /* printw("%*s", ...) doesn't seem to respect the precision, so... */
329 for (i = 0; i < len; ++i)
336 * Build a list of visual line by wrapping the given line, assuming
337 * that when printed will have a leading prefix prfx.
339 * TODO: it considers each byte one cell on the screen!
342 wrap_text(struct tab *tab, const char *prfx, struct line *l)
344 size_t zero, off, len, split;
345 const char *endword, *endspc, *line, *linestart;
352 while (word_boundaries(line, " \t-", &endword, &endspc)) {
353 len = endword - line;
354 if (off + len >= COLS) {
355 emitline(tab, zero, &off, l, &linestart);
356 while (len >= COLS) {
358 emitline(tab, zero, &off, l, &linestart);
368 /* print the spaces iff not at bol */
369 len = endspc - endword;
372 if (off + len >= COLS) {
373 emitline(tab, zero, &off, l, &linestart);
382 emitline(tab, zero, &off, l, &linestart);
386 hardwrap_text(struct tab *tab, struct line *l)
389 const char *linestart;
391 len = strlen(l->line);
395 while (len >= COLS) {
398 if (!emitline(tab, 0, &off, l, &linestart))
406 wrap_page(struct tab *tab)
412 TAILQ_FOREACH(l, &tab->page.head, lines) {
415 wrap_text(tab, "", l);
418 wrap_text(tab, "=> ", l);
421 wrap_text(tab, "# ", l);
424 wrap_text(tab, "## ", l);
427 wrap_text(tab, "### ", l);
430 wrap_text(tab, "* ", l);
433 wrap_text(tab, "> ", l);
437 push_line(tab, l, NULL, 0);
439 case LINE_PRE_CONTENT:
440 hardwrap_text(tab, l);
448 print_line(struct line *l)
475 case LINE_PRE_CONTENT:
480 printw("%s", l->line);
485 redraw_tab(struct tab *tab)
491 TAILQ_FOREACH(l, &tab->s->head, lines) {
495 move(tab->s->curs_y, tab->s->curs_x);
502 setlocale(LC_ALL, "");
509 intrflush(stdscr, FALSE);
510 keypad(stdscr, TRUE);
512 scrollok(stdscr, TRUE);
514 /* non-blocking input */
519 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
520 event_add(&stdioev, NULL);
522 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
523 signal_add(&winchev, NULL);
529 ui_on_new_tab(struct tab *tab)
533 if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
536 TAILQ_INIT(&tab->s->head);
538 TAILQ_FOREACH(t, &tabshead, tabs) {
539 t->flags &= ~TAB_CURRENT;
541 tab->flags = TAB_CURRENT;
543 /* TODO: redraw the tab list */
544 /* TODO: switch to the new tab */
552 ui_on_tab_refresh(struct tab *tab)
554 if (!(tab->flags & TAB_CURRENT))