2 5e11c00c 2021-03-02 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 5e11c00c 2021-03-02 op * Permission to use, copy, modify, and distribute this software for any
5 5e11c00c 2021-03-02 op * purpose with or without fee is hereby granted, provided that the above
6 5e11c00c 2021-03-02 op * copyright notice and this permission notice appear in all copies.
8 5e11c00c 2021-03-02 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 5e11c00c 2021-03-02 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 5e11c00c 2021-03-02 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 5e11c00c 2021-03-02 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 5e11c00c 2021-03-02 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 5e11c00c 2021-03-02 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 5e11c00c 2021-03-02 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1d08c280 2021-03-06 op * Ncurses UI for telescope.
21 1d08c280 2021-03-06 op * Text wrapping
22 1d08c280 2021-03-06 op * =============
24 1d08c280 2021-03-06 op * There's a simple text wrapping algorithm.
26 1d08c280 2021-03-06 op * 1. if it's a line in a pre-formatted block:
27 1d08c280 2021-03-06 op * a. hard wrap.
29 1d08c280 2021-03-06 op * 2. there is enough room for the next word?
30 1d08c280 2021-03-06 op * a. yes: render it
31 1d08c280 2021-03-06 op * b. no: break the current line.
32 1d08c280 2021-03-06 op * i. while there isn't enough space to draw the current
33 1d08c280 2021-03-06 op * word, hard-wrap it
34 1d08c280 2021-03-06 op * ii. draw the remainder of the current word (can be the
35 1d08c280 2021-03-06 op * the entirely)
36 1d08c280 2021-03-06 op * 3. render the spaces after the word
37 1d08c280 2021-03-06 op * a. but if there is not enough room break the line and
42 1d08c280 2021-03-06 op * Text scrolling
43 1d08c280 2021-03-06 op * ==============
45 1d08c280 2021-03-06 op * ncurses allows you to scroll a window, but when a line goes out of
46 1d08c280 2021-03-06 op * the visible area it's forgotten. We keep a list of formatted lines
47 1d08c280 2021-03-06 op * (``visual lines'') that we know fits in the window, and draw them.
48 1d08c280 2021-03-06 op * This way is easy to scroll: just call wscrl and then render the
49 1d08c280 2021-03-06 op * first/last line!
51 1d08c280 2021-03-06 op * This means that on every resize we have to clear our list of lines
52 1d08c280 2021-03-06 op * and re-render everything. A clever approach would be to do this
53 1d08c280 2021-03-06 op * ``on-demand''.
55 1d08c280 2021-03-06 op * TODO: make the text formatting on-demand.
59 5e11c00c 2021-03-02 op #include <telescope.h>
61 5e11c00c 2021-03-02 op #include <curses.h>
62 5e11c00c 2021-03-02 op #include <event.h>
63 5e11c00c 2021-03-02 op #include <locale.h>
64 5e11c00c 2021-03-02 op #include <signal.h>
65 eb259e66 2021-03-02 op #include <stdlib.h>
66 eb259e66 2021-03-02 op #include <string.h>
68 5e11c00c 2021-03-02 op #define TAB_CURRENT 0x1
70 eb259e66 2021-03-02 op #define CTRL(x) ((x)&0x1F)
72 1d08c280 2021-03-06 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
73 1d08c280 2021-03-06 op #define MAX(a, b) ((a) > (b) ? (a) : (b))
75 5e11c00c 2021-03-02 op static struct event stdioev, winchev;
77 1d08c280 2021-03-06 op static int push_line(struct tab*, const struct line*, const char*, size_t);
78 1d08c280 2021-03-06 op static void empty_vlist(struct tab*);
79 5e11c00c 2021-03-02 op static struct tab *current_tab(void);
80 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
81 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
82 eb259e66 2021-03-02 op static int word_bourdaries(const char*, const char*, const char**, const char**);
83 1d08c280 2021-03-06 op static void wrap_text(struct tab*, const char*, struct line*);
84 1d08c280 2021-03-06 op static int hardwrap_text(struct tab*, struct line*);
85 1d08c280 2021-03-06 op static int wrap_page(struct tab*);
86 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
88 1d08c280 2021-03-06 op typedef void (*interactivefn)(int);
90 1d08c280 2021-03-06 op static void cmd_previous_line(int);
91 1d08c280 2021-03-06 op static void cmd_next_line(int);
92 1d08c280 2021-03-06 op static void cmd_forward_char(int);
93 1d08c280 2021-03-06 op static void cmd_backward_char(int);
94 1d08c280 2021-03-06 op static void cmd_redraw(int);
95 1d08c280 2021-03-06 op static void cmd_scroll_down(int);
96 1d08c280 2021-03-06 op static void cmd_scroll_up(int);
97 1d08c280 2021-03-06 op static void cmd_kill_telescope(int);
99 1d08c280 2021-03-06 op static void cmd_unbound(int);
101 1d08c280 2021-03-06 op struct ui_state {
104 1d08c280 2021-03-06 op int line_off;
106 1d08c280 2021-03-06 op TAILQ_HEAD(, line) head;
110 1d08c280 2021-03-06 op push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
112 1d08c280 2021-03-06 op struct line *vl;
114 1d08c280 2021-03-06 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
117 1d08c280 2021-03-06 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
122 1d08c280 2021-03-06 op vl->type = l->type;
123 1d08c280 2021-03-06 op if (len != 0)
124 1d08c280 2021-03-06 op memcpy(vl->line, buf, len);
125 1d08c280 2021-03-06 op vl->alt = l->alt;
127 1d08c280 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
128 1d08c280 2021-03-06 op TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
130 1d08c280 2021-03-06 op TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
135 1d08c280 2021-03-06 op empty_vlist(struct tab *tab)
137 1d08c280 2021-03-06 op struct line *l, *t;
139 1d08c280 2021-03-06 op TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
140 1d08c280 2021-03-06 op TAILQ_REMOVE(&tab->s->head, l, lines);
141 1d08c280 2021-03-06 op free(l->line);
142 1d08c280 2021-03-06 op /* l->alt references the original line! */
147 1d08c280 2021-03-06 op struct binding {
149 1d08c280 2021-03-06 op interactivefn fn;
150 1d08c280 2021-03-06 op } bindings[] = {
151 1d08c280 2021-03-06 op { CTRL('p'), cmd_previous_line, },
152 1d08c280 2021-03-06 op { CTRL('n'), cmd_next_line, },
153 1d08c280 2021-03-06 op { CTRL('f'), cmd_forward_char, },
154 1d08c280 2021-03-06 op { CTRL('b'), cmd_backward_char, },
156 1d08c280 2021-03-06 op { CTRL('L'), cmd_redraw, },
158 1d08c280 2021-03-06 op { 'J', cmd_scroll_down, },
159 1d08c280 2021-03-06 op { 'K', cmd_scroll_up, },
161 1d08c280 2021-03-06 op { 'q', cmd_kill_telescope, },
163 1d08c280 2021-03-06 op { 0, NULL, },
167 1d08c280 2021-03-06 op cmd_previous_line(int k)
169 1d08c280 2021-03-06 op struct tab *tab;
171 1d08c280 2021-03-06 op tab = current_tab();
172 1d08c280 2021-03-06 op tab->s->curs_y = MAX(0, tab->s->curs_y-1);
173 1d08c280 2021-03-06 op move(tab->s->curs_y, tab->s->curs_x);
177 1d08c280 2021-03-06 op cmd_next_line(int k)
179 1d08c280 2021-03-06 op struct tab *tab;
181 1d08c280 2021-03-06 op tab = current_tab();
182 1d08c280 2021-03-06 op tab->s->curs_y = MIN(LINES, tab->s->curs_y-1);
183 1d08c280 2021-03-06 op move(tab->s->curs_y, tab->s->curs_x);
187 1d08c280 2021-03-06 op cmd_forward_char(int k)
192 1d08c280 2021-03-06 op cmd_backward_char(int k)
197 1d08c280 2021-03-06 op cmd_redraw(int k)
200 1d08c280 2021-03-06 op redraw_tab(current_tab());
204 1d08c280 2021-03-06 op cmd_scroll_down(int k)
206 1d08c280 2021-03-06 op wscrl(stdscr, -1);
210 1d08c280 2021-03-06 op cmd_scroll_up(int k)
212 1d08c280 2021-03-06 op wscrl(stdscr, 1);
216 1d08c280 2021-03-06 op cmd_kill_telescope(int k)
218 1d08c280 2021-03-06 op event_loopbreak();
222 1d08c280 2021-03-06 op cmd_unbound(int k)
224 1d08c280 2021-03-06 op /* TODO: flash a message */
227 5e11c00c 2021-03-02 op static struct tab *
228 5e11c00c 2021-03-02 op current_tab(void)
230 5e11c00c 2021-03-02 op struct tab *t;
232 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
233 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
237 5e11c00c 2021-03-02 op /* unreachable */
242 5e11c00c 2021-03-02 op dispatch_stdio(int fd, short ev, void *d)
244 1d08c280 2021-03-06 op struct binding *b;
249 1d08c280 2021-03-06 op if (k == ERR)
252 1d08c280 2021-03-06 op for (b = bindings; b->fn != NULL; ++b) {
253 1d08c280 2021-03-06 op if (k == b->key) {
259 1d08c280 2021-03-06 op cmd_unbound(k);
266 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
268 1d08c280 2021-03-06 op struct tab *tab;
274 1d08c280 2021-03-06 op tab = current_tab();
276 1d08c280 2021-03-06 op wrap_page(tab);
277 1d08c280 2021-03-06 op redraw_tab(tab);
281 eb259e66 2021-03-02 op * Helper function for wrap_text. Find the end of the current word
282 eb259e66 2021-03-02 op * and the end of the separator after the word.
285 eb259e66 2021-03-02 op word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
287 eb259e66 2021-03-02 op *endword = s;
288 eb259e66 2021-03-02 op *endword = s;
290 eb259e66 2021-03-02 op if (*s == '\0')
293 eb259e66 2021-03-02 op /* find the end of the current world */
294 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
295 eb259e66 2021-03-02 op if (strchr(sep, *s) != NULL)
299 eb259e66 2021-03-02 op *endword = s;
301 eb259e66 2021-03-02 op /* find the end of the separator */
302 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
303 eb259e66 2021-03-02 op if (strchr(sep, *s) == NULL)
312 1d08c280 2021-03-06 op static inline int
313 1d08c280 2021-03-06 op emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
314 1d08c280 2021-03-06 op const char **line)
316 1d08c280 2021-03-06 op if (!push_line(tab, l, *line, *off - zero))
318 1d08c280 2021-03-06 op *line += *off - zero;
323 eb259e66 2021-03-02 op static inline void
324 eb259e66 2021-03-02 op emitstr(const char **s, size_t len, size_t *off)
328 eb259e66 2021-03-02 op /* printw("%*s", ...) doesn't seem to respect the precision, so... */
329 eb259e66 2021-03-02 op for (i = 0; i < len; ++i)
330 eb259e66 2021-03-02 op addch((*s)[i]);
336 1d08c280 2021-03-06 op * Build a list of visual line by wrapping the given line, assuming
337 1d08c280 2021-03-06 op * that when printed will have a leading prefix prfx.
339 eb259e66 2021-03-02 op * TODO: it considers each byte one cell on the screen!
342 1d08c280 2021-03-06 op wrap_text(struct tab *tab, const char *prfx, struct line *l)
344 eb259e66 2021-03-02 op size_t zero, off, len, split;
345 1d08c280 2021-03-06 op const char *endword, *endspc, *line, *linestart;
347 1d08c280 2021-03-06 op zero = strlen(prfx);
349 1d08c280 2021-03-06 op line = l->line;
350 1d08c280 2021-03-06 op linestart = l->line;
352 1d08c280 2021-03-06 op while (word_boundaries(line, " \t-", &endword, &endspc)) {
353 eb259e66 2021-03-02 op len = endword - line;
354 1d08c280 2021-03-06 op if (off + len >= COLS) {
355 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
356 eb259e66 2021-03-02 op while (len >= COLS) {
357 eb259e66 2021-03-02 op /* hard wrap */
358 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
359 eb259e66 2021-03-02 op len -= COLS-1;
360 eb259e66 2021-03-02 op line += COLS-1;
363 eb259e66 2021-03-02 op if (len != 0)
368 eb259e66 2021-03-02 op /* print the spaces iff not at bol */
369 eb259e66 2021-03-02 op len = endspc - endword;
370 eb259e66 2021-03-02 op /* line = endspc; */
371 eb259e66 2021-03-02 op if (off != zero) {
372 1d08c280 2021-03-06 op if (off + len >= COLS) {
373 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
374 1d08c280 2021-03-06 op linestart = endspc;
379 eb259e66 2021-03-02 op line = endspc;
382 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
386 1d08c280 2021-03-06 op hardwrap_text(struct tab *tab, struct line *l)
388 1d08c280 2021-03-06 op size_t off, len;
389 1d08c280 2021-03-06 op const char *linestart;
391 1d08c280 2021-03-06 op len = strlen(l->line);
393 1d08c280 2021-03-06 op linestart = l->line;
395 1d08c280 2021-03-06 op while (len >= COLS) {
396 1d08c280 2021-03-06 op len -= COLS-1;
397 1d08c280 2021-03-06 op off = COLS-1;
398 1d08c280 2021-03-06 op if (!emitline(tab, 0, &off, l, &linestart))
406 1d08c280 2021-03-06 op wrap_page(struct tab *tab)
408 1d08c280 2021-03-06 op struct line *l;
410 1d08c280 2021-03-06 op empty_vlist(tab);
412 5e11c00c 2021-03-02 op TAILQ_FOREACH(l, &tab->page.head, lines) {
413 5e11c00c 2021-03-02 op switch (l->type) {
414 5e11c00c 2021-03-02 op case LINE_TEXT:
415 1d08c280 2021-03-06 op wrap_text(tab, "", l);
417 5e11c00c 2021-03-02 op case LINE_LINK:
418 1d08c280 2021-03-06 op wrap_text(tab, "=> ", l);
420 5e11c00c 2021-03-02 op case LINE_TITLE_1:
421 1d08c280 2021-03-06 op wrap_text(tab, "# ", l);
423 5e11c00c 2021-03-02 op case LINE_TITLE_2:
424 1d08c280 2021-03-06 op wrap_text(tab, "## ", l);
426 5e11c00c 2021-03-02 op case LINE_TITLE_3:
427 1d08c280 2021-03-06 op wrap_text(tab, "### ", l);
429 5e11c00c 2021-03-02 op case LINE_ITEM:
430 1d08c280 2021-03-06 op wrap_text(tab, "* ", l);
432 5e11c00c 2021-03-02 op case LINE_QUOTE:
433 1d08c280 2021-03-06 op wrap_text(tab, "> ", l);
435 5e11c00c 2021-03-02 op case LINE_PRE_START:
436 5e11c00c 2021-03-02 op case LINE_PRE_END:
437 1d08c280 2021-03-06 op push_line(tab, l, NULL, 0);
439 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
440 1d08c280 2021-03-06 op hardwrap_text(tab, l);
447 1d08c280 2021-03-06 op static inline void
448 1d08c280 2021-03-06 op print_line(struct line *l)
450 1d08c280 2021-03-06 op switch (l->type) {
451 1d08c280 2021-03-06 op case LINE_TEXT:
453 1d08c280 2021-03-06 op case LINE_LINK:
454 1d08c280 2021-03-06 op printw("=> ");
456 1d08c280 2021-03-06 op case LINE_TITLE_1:
457 1d08c280 2021-03-06 op printw("# ");
459 1d08c280 2021-03-06 op case LINE_TITLE_2:
460 1d08c280 2021-03-06 op printw("## ");
462 1d08c280 2021-03-06 op case LINE_TITLE_3:
463 1d08c280 2021-03-06 op printw("### ");
465 1d08c280 2021-03-06 op case LINE_ITEM:
466 1d08c280 2021-03-06 op printw("* ");
468 1d08c280 2021-03-06 op case LINE_QUOTE:
469 1d08c280 2021-03-06 op printw("> ");
471 1d08c280 2021-03-06 op case LINE_PRE_START:
472 1d08c280 2021-03-06 op case LINE_PRE_END:
473 1d08c280 2021-03-06 op printw("```");
475 1d08c280 2021-03-06 op case LINE_PRE_CONTENT:
479 1d08c280 2021-03-06 op if (l->line != NULL)
480 1d08c280 2021-03-06 op printw("%s", l->line);
481 1d08c280 2021-03-06 op printw("\n");
485 1d08c280 2021-03-06 op redraw_tab(struct tab *tab)
487 1d08c280 2021-03-06 op struct line *l;
491 1d08c280 2021-03-06 op TAILQ_FOREACH(l, &tab->s->head, lines) {
492 1d08c280 2021-03-06 op print_line(l);
495 1d08c280 2021-03-06 op move(tab->s->curs_y, tab->s->curs_x);
500 5e11c00c 2021-03-02 op ui_init(void)
502 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
509 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
510 5e11c00c 2021-03-02 op keypad(stdscr, TRUE);
512 1d08c280 2021-03-06 op scrollok(stdscr, TRUE);
514 5e11c00c 2021-03-02 op /* non-blocking input */
517 5e11c00c 2021-03-02 op mvprintw(0, 0, "");
519 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
520 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
522 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
523 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
529 5e11c00c 2021-03-02 op ui_on_new_tab(struct tab *tab)
531 5e11c00c 2021-03-02 op struct tab *t;
533 1d08c280 2021-03-06 op if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
536 1d08c280 2021-03-06 op TAILQ_INIT(&tab->s->head);
538 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
539 5e11c00c 2021-03-02 op t->flags &= ~TAB_CURRENT;
541 5e11c00c 2021-03-02 op tab->flags = TAB_CURRENT;
543 5e11c00c 2021-03-02 op /* TODO: redraw the tab list */
544 1d08c280 2021-03-06 op /* TODO: switch to the new tab */
552 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
554 5e11c00c 2021-03-02 op if (!(tab->flags & TAB_CURRENT))
557 1d08c280 2021-03-06 op wrap_page(tab);
558 5e11c00c 2021-03-02 op redraw_tab(tab);