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 7953dd72 2021-03-07 op #include <stdarg.h>
66 eb259e66 2021-03-02 op #include <stdlib.h>
67 eb259e66 2021-03-02 op #include <string.h>
69 5e11c00c 2021-03-02 op #define TAB_CURRENT 0x1
71 1d08c280 2021-03-06 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
72 1d08c280 2021-03-06 op #define MAX(a, b) ((a) > (b) ? (a) : (b))
76 5e11c00c 2021-03-02 op static struct event stdioev, winchev;
78 1d08c280 2021-03-06 op static int push_line(struct tab*, const struct line*, const char*, size_t);
79 1d08c280 2021-03-06 op static void empty_vlist(struct tab*);
80 48e9d457 2021-03-06 op static void restore_cursor(struct tab *);
81 46a9311e 2021-03-08 op static void cmd_previous_line(struct tab*);
82 46a9311e 2021-03-08 op static void cmd_next_line(struct tab*);
83 46a9311e 2021-03-08 op static void cmd_forward_char(struct tab*);
84 46a9311e 2021-03-08 op static void cmd_backward_char(struct tab*);
85 46a9311e 2021-03-08 op static void cmd_redraw(struct tab*);
86 46a9311e 2021-03-08 op static void cmd_scroll_down(struct tab*);
87 46a9311e 2021-03-08 op static void cmd_scroll_up(struct tab*);
88 46a9311e 2021-03-08 op static void cmd_kill_telescope(struct tab*);
89 46a9311e 2021-03-08 op static void cmd_push_button(struct tab*);
90 19f1448e 2021-03-08 op static void cmd_unbound(struct key);
91 48e9d457 2021-03-06 op static struct line *nth_line(struct tab*, size_t);
92 5e11c00c 2021-03-02 op static struct tab *current_tab(void);
93 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
94 a6d450c1 2021-03-06 op static void handle_clear_minibuf(int, short, void*);
95 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
96 eb259e66 2021-03-02 op static int word_bourdaries(const char*, const char*, const char**, const char**);
97 1d08c280 2021-03-06 op static void wrap_text(struct tab*, const char*, struct line*);
98 1d08c280 2021-03-06 op static int hardwrap_text(struct tab*, struct line*);
99 1d08c280 2021-03-06 op static int wrap_page(struct tab*);
100 48e9d457 2021-03-06 op static void print_line(struct line*);
101 8af5e5ed 2021-03-08 op static void redraw_tabline(void);
102 8af5e5ed 2021-03-08 op static void redraw_modeline(struct tab*);
103 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
104 7953dd72 2021-03-07 op static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
105 8af5e5ed 2021-03-08 op static void start_loading_anim(struct tab*);
106 8af5e5ed 2021-03-08 op static void update_loading_anim(int, short, void*);
107 8af5e5ed 2021-03-08 op static void stop_loading_anim(struct tab*);
108 bcb0b073 2021-03-07 op static void new_tab(void);
110 46a9311e 2021-03-08 op typedef void (*interactivefn)(struct tab*);
112 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
113 48e9d457 2021-03-06 op static int body_lines, body_cols;
115 48e9d457 2021-03-06 op static struct event clminibufev;
116 48e9d457 2021-03-06 op static int clminibufev_set;
117 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
118 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
120 bcb0b073 2021-03-07 op static uint32_t tab_counter;
122 1d08c280 2021-03-06 op struct ui_state {
125 48e9d457 2021-03-06 op size_t line_off;
126 48e9d457 2021-03-06 op size_t line_max;
128 8af5e5ed 2021-03-08 op short loading_anim;
129 8af5e5ed 2021-03-08 op short loading_anim_step;
130 8af5e5ed 2021-03-08 op struct event loadingev;
132 1d08c280 2021-03-06 op TAILQ_HEAD(, line) head;
140 19f1448e 2021-03-08 op #define KEY(n) ((struct key){0, n})
141 19f1448e 2021-03-08 op #define CKEY(n) ((struct key){0, (n) & 0x1F})
142 b0ea6f26 2021-03-08 op #define MKEY(n) ((struct key){1, n})
143 b0ea6f26 2021-03-08 op #define CMKEY(n) ((struct key){1, (n) & 0x1F})
145 48e9d457 2021-03-06 op struct binding {
146 19f1448e 2021-03-08 op struct key key;
147 48e9d457 2021-03-06 op interactivefn fn;
148 48e9d457 2021-03-06 op } bindings[] = {
149 19f1448e 2021-03-08 op { CKEY('p'), cmd_previous_line, },
150 19f1448e 2021-03-08 op { CKEY('n'), cmd_next_line, },
151 19f1448e 2021-03-08 op { CKEY('f'), cmd_forward_char, },
152 19f1448e 2021-03-08 op { CKEY('b'), cmd_backward_char, },
154 19f1448e 2021-03-08 op { CKEY('L'), cmd_redraw, },
156 19f1448e 2021-03-08 op { KEY('J'), cmd_scroll_down, },
157 19f1448e 2021-03-08 op { KEY('K'), cmd_scroll_up, },
159 19f1448e 2021-03-08 op { KEY('q'), cmd_kill_telescope, },
160 19f1448e 2021-03-08 op { CKEY('m'), cmd_push_button, },
162 19f1448e 2021-03-08 op { {0, 0}, NULL, },
166 1d08c280 2021-03-06 op push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
168 1d08c280 2021-03-06 op struct line *vl;
170 48e9d457 2021-03-06 op tab->s->line_max++;
172 1d08c280 2021-03-06 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
175 1d08c280 2021-03-06 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
180 1d08c280 2021-03-06 op vl->type = l->type;
181 1d08c280 2021-03-06 op if (len != 0)
182 1d08c280 2021-03-06 op memcpy(vl->line, buf, len);
183 1d08c280 2021-03-06 op vl->alt = l->alt;
185 1d08c280 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
186 1d08c280 2021-03-06 op TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
188 1d08c280 2021-03-06 op TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
193 1d08c280 2021-03-06 op empty_vlist(struct tab *tab)
195 1d08c280 2021-03-06 op struct line *l, *t;
197 48e9d457 2021-03-06 op tab->s->line_max = 0;
199 1d08c280 2021-03-06 op TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
200 1d08c280 2021-03-06 op TAILQ_REMOVE(&tab->s->head, l, lines);
201 1d08c280 2021-03-06 op free(l->line);
202 1d08c280 2021-03-06 op /* l->alt references the original line! */
208 48e9d457 2021-03-06 op restore_cursor(struct tab *tab)
210 48e9d457 2021-03-06 op wmove(body, tab->s->curs_y, tab->s->curs_x);
214 46a9311e 2021-03-08 op cmd_previous_line(struct tab *tab)
216 4dd664ce 2021-03-06 op if (--tab->s->curs_y < 0) {
217 4dd664ce 2021-03-06 op tab->s->curs_y = 0;
218 46a9311e 2021-03-08 op cmd_scroll_up(tab);
221 48e9d457 2021-03-06 op restore_cursor(tab);
225 46a9311e 2021-03-08 op cmd_next_line(struct tab *tab)
227 4dd664ce 2021-03-06 op if (++tab->s->curs_y > body_lines-1) {
228 4dd664ce 2021-03-06 op tab->s->curs_y = body_lines-1;
229 46a9311e 2021-03-08 op cmd_scroll_down(tab);
232 48e9d457 2021-03-06 op restore_cursor(tab);
236 46a9311e 2021-03-08 op cmd_forward_char(struct tab *tab)
238 4dd664ce 2021-03-06 op tab->s->curs_x = MIN(body_cols-1, tab->s->curs_x+1);
239 48e9d457 2021-03-06 op restore_cursor(tab);
243 46a9311e 2021-03-08 op cmd_backward_char(struct tab *tab)
245 b2cd5e06 2021-03-06 op tab->s->curs_x = MAX(0, tab->s->curs_x-1);
246 48e9d457 2021-03-06 op restore_cursor(tab);
250 46a9311e 2021-03-08 op cmd_redraw(struct tab *tab)
252 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
256 46a9311e 2021-03-08 op cmd_scroll_up(struct tab *tab)
258 48e9d457 2021-03-06 op struct line *l;
260 48e9d457 2021-03-06 op if (tab->s->line_off == 0)
263 48e9d457 2021-03-06 op l = nth_line(tab, --tab->s->line_off);
264 48e9d457 2021-03-06 op wscrl(body, -1);
265 48e9d457 2021-03-06 op wmove(body, 0, 0);
266 48e9d457 2021-03-06 op print_line(l);
270 46a9311e 2021-03-08 op cmd_scroll_down(struct tab *tab)
272 48e9d457 2021-03-06 op struct line *l;
275 48e9d457 2021-03-06 op if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
278 48e9d457 2021-03-06 op tab->s->line_off++;
279 48e9d457 2021-03-06 op wscrl(body, 1);
281 48e9d457 2021-03-06 op if (tab->s->line_max - tab->s->line_off < body_lines)
284 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off + body_lines-1);
285 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
286 48e9d457 2021-03-06 op print_line(l);
290 46a9311e 2021-03-08 op cmd_kill_telescope(struct tab *tab)
292 1d08c280 2021-03-06 op event_loopbreak();
296 46a9311e 2021-03-08 op cmd_push_button(struct tab *tab)
298 2a4ad912 2021-03-08 op struct line *l;
301 2a4ad912 2021-03-08 op nth = tab->s->line_off + tab->s->curs_y;
302 2a4ad912 2021-03-08 op if (nth > tab->s->line_max)
304 2a4ad912 2021-03-08 op l = nth_line(tab, nth);
305 2a4ad912 2021-03-08 op message("Enter on line: \"%s\"", l->line ? l->line : "");
309 19f1448e 2021-03-08 op cmd_unbound(struct key k)
311 19f1448e 2021-03-08 op message("%s%c is undefined",
312 b0ea6f26 2021-03-08 op k.meta ? "M-" : "",
316 48e9d457 2021-03-06 op static struct line *
317 48e9d457 2021-03-06 op nth_line(struct tab *tab, size_t n)
319 48e9d457 2021-03-06 op struct line *l;
323 48e9d457 2021-03-06 op TAILQ_FOREACH(l, &tab->s->head, lines) {
329 48e9d457 2021-03-06 op /* unreachable */
333 5e11c00c 2021-03-02 op static struct tab *
334 5e11c00c 2021-03-02 op current_tab(void)
336 5e11c00c 2021-03-02 op struct tab *t;
338 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
339 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
343 5e11c00c 2021-03-02 op /* unreachable */
348 5e11c00c 2021-03-02 op dispatch_stdio(int fd, short ev, void *d)
350 1d08c280 2021-03-06 op struct binding *b;
351 19f1448e 2021-03-08 op struct key key;
354 48e9d457 2021-03-06 op k = wgetch(body);
356 1d08c280 2021-03-06 op if (k == ERR)
359 19f1448e 2021-03-08 op /* look for a M-key */
360 19f1448e 2021-03-08 op if (k == 27) {
361 19f1448e 2021-03-08 op wtimeout(body, 0); /* TODO: make escape-time customizable */
362 19f1448e 2021-03-08 op j = wgetch(body);
363 19f1448e 2021-03-08 op if (j != ERR) {
365 b0ea6f26 2021-03-08 op key.meta = 1;
367 ff3ffcb5 2021-03-08 op key.meta = 0;
370 b0ea6f26 2021-03-08 op key.meta = 0;
374 1d08c280 2021-03-06 op for (b = bindings; b->fn != NULL; ++b) {
375 b0ea6f26 2021-03-08 op if (key.meta == b->key.meta &&
376 19f1448e 2021-03-08 op key.key == b->key.key) {
377 46a9311e 2021-03-08 op b->fn(current_tab());
382 19f1448e 2021-03-08 op cmd_unbound(key);
385 48e9d457 2021-03-06 op restore_cursor(current_tab());
386 48e9d457 2021-03-06 op wrefresh(tabline);
387 48e9d457 2021-03-06 op wrefresh(modeline);
388 48e9d457 2021-03-06 op wrefresh(minibuf);
390 a6d450c1 2021-03-06 op wrefresh(body);
394 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
396 a6d450c1 2021-03-06 op clminibufev_set = 0;
397 a6d450c1 2021-03-06 op werase(minibuf);
398 a6d450c1 2021-03-06 op wrefresh(minibuf);
399 48e9d457 2021-03-06 op wrefresh(body);
403 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
405 1d08c280 2021-03-06 op struct tab *tab;
411 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
413 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
414 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
416 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
417 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
419 48e9d457 2021-03-06 op wresize(body, LINES-3, COLS);
420 48e9d457 2021-03-06 op body_lines = LINES-3;
421 bd9637e9 2021-03-06 op body_cols = COLS;
423 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
425 1d08c280 2021-03-06 op tab = current_tab();
427 1d08c280 2021-03-06 op wrap_page(tab);
428 1d08c280 2021-03-06 op redraw_tab(tab);
432 eb259e66 2021-03-02 op * Helper function for wrap_text. Find the end of the current word
433 eb259e66 2021-03-02 op * and the end of the separator after the word.
436 eb259e66 2021-03-02 op word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
438 eb259e66 2021-03-02 op *endword = s;
439 eb259e66 2021-03-02 op *endword = s;
441 eb259e66 2021-03-02 op if (*s == '\0')
444 eb259e66 2021-03-02 op /* find the end of the current world */
445 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
446 eb259e66 2021-03-02 op if (strchr(sep, *s) != NULL)
450 eb259e66 2021-03-02 op *endword = s;
452 eb259e66 2021-03-02 op /* find the end of the separator */
453 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
454 eb259e66 2021-03-02 op if (strchr(sep, *s) == NULL)
463 1d08c280 2021-03-06 op static inline int
464 1d08c280 2021-03-06 op emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
465 1d08c280 2021-03-06 op const char **line)
467 1d08c280 2021-03-06 op if (!push_line(tab, l, *line, *off - zero))
469 1d08c280 2021-03-06 op *line += *off - zero;
474 eb259e66 2021-03-02 op static inline void
475 eb259e66 2021-03-02 op emitstr(const char **s, size_t len, size_t *off)
479 eb259e66 2021-03-02 op /* printw("%*s", ...) doesn't seem to respect the precision, so... */
480 eb259e66 2021-03-02 op for (i = 0; i < len; ++i)
481 eb259e66 2021-03-02 op addch((*s)[i]);
487 1d08c280 2021-03-06 op * Build a list of visual line by wrapping the given line, assuming
488 1d08c280 2021-03-06 op * that when printed will have a leading prefix prfx.
490 eb259e66 2021-03-02 op * TODO: it considers each byte one cell on the screen!
493 1d08c280 2021-03-06 op wrap_text(struct tab *tab, const char *prfx, struct line *l)
495 eb259e66 2021-03-02 op size_t zero, off, len, split;
496 1d08c280 2021-03-06 op const char *endword, *endspc, *line, *linestart;
498 1d08c280 2021-03-06 op zero = strlen(prfx);
500 1d08c280 2021-03-06 op line = l->line;
501 1d08c280 2021-03-06 op linestart = l->line;
503 1d08c280 2021-03-06 op while (word_boundaries(line, " \t-", &endword, &endspc)) {
504 eb259e66 2021-03-02 op len = endword - line;
505 48e9d457 2021-03-06 op if (off + len >= body_cols) {
506 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
507 48e9d457 2021-03-06 op while (len >= body_cols) {
508 eb259e66 2021-03-02 op /* hard wrap */
509 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
510 48e9d457 2021-03-06 op len -= body_cols-1;
511 48e9d457 2021-03-06 op line += body_cols-1;
514 eb259e66 2021-03-02 op if (len != 0)
519 eb259e66 2021-03-02 op /* print the spaces iff not at bol */
520 eb259e66 2021-03-02 op len = endspc - endword;
521 eb259e66 2021-03-02 op /* line = endspc; */
522 eb259e66 2021-03-02 op if (off != zero) {
523 48e9d457 2021-03-06 op if (off + len >= body_cols) {
524 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
525 1d08c280 2021-03-06 op linestart = endspc;
530 eb259e66 2021-03-02 op line = endspc;
533 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
537 1d08c280 2021-03-06 op hardwrap_text(struct tab *tab, struct line *l)
539 1d08c280 2021-03-06 op size_t off, len;
540 1d08c280 2021-03-06 op const char *linestart;
542 1d08c280 2021-03-06 op len = strlen(l->line);
544 1d08c280 2021-03-06 op linestart = l->line;
546 1d08c280 2021-03-06 op while (len >= COLS) {
547 1d08c280 2021-03-06 op len -= COLS-1;
548 1d08c280 2021-03-06 op off = COLS-1;
549 1d08c280 2021-03-06 op if (!emitline(tab, 0, &off, l, &linestart))
553 ede1f142 2021-03-07 op if (len != 0)
554 ede1f142 2021-03-07 op return emitline(tab, 0, &len, l, &linestart);
560 1d08c280 2021-03-06 op wrap_page(struct tab *tab)
562 1d08c280 2021-03-06 op struct line *l;
564 1d08c280 2021-03-06 op empty_vlist(tab);
566 5e11c00c 2021-03-02 op TAILQ_FOREACH(l, &tab->page.head, lines) {
567 5e11c00c 2021-03-02 op switch (l->type) {
568 5e11c00c 2021-03-02 op case LINE_TEXT:
569 1d08c280 2021-03-06 op wrap_text(tab, "", l);
571 5e11c00c 2021-03-02 op case LINE_LINK:
572 1d08c280 2021-03-06 op wrap_text(tab, "=> ", l);
574 5e11c00c 2021-03-02 op case LINE_TITLE_1:
575 1d08c280 2021-03-06 op wrap_text(tab, "# ", l);
577 5e11c00c 2021-03-02 op case LINE_TITLE_2:
578 1d08c280 2021-03-06 op wrap_text(tab, "## ", l);
580 5e11c00c 2021-03-02 op case LINE_TITLE_3:
581 1d08c280 2021-03-06 op wrap_text(tab, "### ", l);
583 5e11c00c 2021-03-02 op case LINE_ITEM:
584 1d08c280 2021-03-06 op wrap_text(tab, "* ", l);
586 5e11c00c 2021-03-02 op case LINE_QUOTE:
587 1d08c280 2021-03-06 op wrap_text(tab, "> ", l);
589 5e11c00c 2021-03-02 op case LINE_PRE_START:
590 5e11c00c 2021-03-02 op case LINE_PRE_END:
591 1d08c280 2021-03-06 op push_line(tab, l, NULL, 0);
593 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
594 1d08c280 2021-03-06 op hardwrap_text(tab, l);
601 1d08c280 2021-03-06 op static inline void
602 1d08c280 2021-03-06 op print_line(struct line *l)
604 bd9637e9 2021-03-06 op const char *text = l->line;
606 bd9637e9 2021-03-06 op if (text == NULL)
609 1d08c280 2021-03-06 op switch (l->type) {
610 1d08c280 2021-03-06 op case LINE_TEXT:
611 bd9637e9 2021-03-06 op wprintw(body, "%s", text);
613 1d08c280 2021-03-06 op case LINE_LINK:
614 bd9637e9 2021-03-06 op wattron(body, A_UNDERLINE);
615 bd9637e9 2021-03-06 op wprintw(body, "=> %s", text);
616 bd9637e9 2021-03-06 op wattroff(body, A_UNDERLINE);
618 1d08c280 2021-03-06 op case LINE_TITLE_1:
619 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
620 bd9637e9 2021-03-06 op wprintw(body, "# %s", text);
621 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
623 1d08c280 2021-03-06 op case LINE_TITLE_2:
624 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
625 bd9637e9 2021-03-06 op wprintw(body, "## %s", text);
626 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
628 bd9637e9 2021-03-06 op case LINE_TITLE_3:
629 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
630 bd9637e9 2021-03-06 op wprintw(body, "### %s", text);
631 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
633 bd9637e9 2021-03-06 op case LINE_ITEM:
634 bd9637e9 2021-03-06 op wprintw(body, "* %s", text);
636 1d08c280 2021-03-06 op case LINE_QUOTE:
637 bd9637e9 2021-03-06 op wattron(body, A_DIM);
638 bd9637e9 2021-03-06 op wprintw(body, "> %s", text);
639 bd9637e9 2021-03-06 op wattroff(body, A_DIM);
641 1d08c280 2021-03-06 op case LINE_PRE_START:
642 1d08c280 2021-03-06 op case LINE_PRE_END:
643 48e9d457 2021-03-06 op wprintw(body, "```");
645 1d08c280 2021-03-06 op case LINE_PRE_CONTENT:
646 bd9637e9 2021-03-06 op wprintw(body, "%s", text);
652 8af5e5ed 2021-03-08 op redraw_tabline(void)
654 8af5e5ed 2021-03-08 op wclear(tabline);
655 8af5e5ed 2021-03-08 op wbkgd(tabline, A_REVERSE);
656 8af5e5ed 2021-03-08 op mvwprintw(tabline, 0, 0, "TODO: tabs here");
660 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
662 48e9d457 2021-03-06 op int x, y, max_x, max_y;
663 48e9d457 2021-03-06 op const char *mode = "text/gemini-mode";
664 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
666 48e9d457 2021-03-06 op wclear(modeline);
667 48e9d457 2021-03-06 op wattron(modeline, A_REVERSE);
668 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
670 8af5e5ed 2021-03-08 op wprintw(modeline, "-%c %s %s ",
671 8af5e5ed 2021-03-08 op spin[tab->s->loading_anim_step], mode, tab->url);
672 48e9d457 2021-03-06 op getyx(modeline, y, x);
673 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
678 48e9d457 2021-03-06 op for (; x < max_x; ++x)
679 48e9d457 2021-03-06 op waddstr(modeline, "-");
683 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
685 48e9d457 2021-03-06 op struct line *l;
688 48e9d457 2021-03-06 op werase(body);
690 48e9d457 2021-03-06 op tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
691 48e9d457 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
695 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off);
696 48e9d457 2021-03-06 op for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
697 48e9d457 2021-03-06 op wmove(body, line, 0);
698 1d08c280 2021-03-06 op print_line(l);
700 48e9d457 2021-03-06 op if (line == body_lines)
704 8af5e5ed 2021-03-08 op redraw_tabline();
705 48e9d457 2021-03-06 op redraw_modeline(tab);
707 48e9d457 2021-03-06 op restore_cursor(tab);
708 48e9d457 2021-03-06 op wrefresh(tabline);
709 48e9d457 2021-03-06 op wrefresh(modeline);
710 48e9d457 2021-03-06 op wrefresh(minibuf);
712 7953dd72 2021-03-07 op wrefresh(body);
716 7953dd72 2021-03-07 op message(const char *fmt, ...)
720 7953dd72 2021-03-07 op va_start(ap, fmt);
722 7953dd72 2021-03-07 op if (clminibufev_set)
723 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
724 7953dd72 2021-03-07 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
725 7953dd72 2021-03-07 op evtimer_add(&clminibufev, &clminibufev_timer);
726 7953dd72 2021-03-07 op clminibufev_set = 1;
728 7953dd72 2021-03-07 op werase(minibuf);
729 7953dd72 2021-03-07 op vw_printw(minibuf, fmt, ap);
731 7953dd72 2021-03-07 op wrefresh(minibuf);
732 48e9d457 2021-03-06 op wrefresh(body);
738 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
740 8af5e5ed 2021-03-08 op if (tab->s->loading_anim)
742 8af5e5ed 2021-03-08 op tab->s->loading_anim = 1;
743 8af5e5ed 2021-03-08 op evtimer_set(&tab->s->loadingev, update_loading_anim, tab);
744 8af5e5ed 2021-03-08 op evtimer_add(&tab->s->loadingev, &loadingev_timer);
748 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
750 8af5e5ed 2021-03-08 op struct tab *tab = d;
752 8af5e5ed 2021-03-08 op tab->s->loading_anim_step = (tab->s->loading_anim_step+1)%4;
754 8af5e5ed 2021-03-08 op redraw_modeline(tab);
755 8af5e5ed 2021-03-08 op wrefresh(modeline);
756 8af5e5ed 2021-03-08 op wrefresh(body);
758 8af5e5ed 2021-03-08 op evtimer_add(&tab->s->loadingev, &loadingev_timer);
762 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
764 8af5e5ed 2021-03-08 op if (!tab->s->loading_anim)
766 8af5e5ed 2021-03-08 op evtimer_del(&tab->s->loadingev);
767 8af5e5ed 2021-03-08 op tab->s->loading_anim = 0;
768 8af5e5ed 2021-03-08 op tab->s->loading_anim_step = 0;
772 8af5e5ed 2021-03-08 op ui_load_url_in_tab(struct tab *tab, const char *url)
774 8af5e5ed 2021-03-08 op message("Loading %s...", url);
775 8af5e5ed 2021-03-08 op start_loading_anim(tab);
776 8af5e5ed 2021-03-08 op load_url(tab, url);
780 bcb0b073 2021-03-07 op new_tab(void)
782 bcb0b073 2021-03-07 op struct tab *tab, *t;
783 bcb0b073 2021-03-07 op const char *url = "about:new";
785 bcb0b073 2021-03-07 op if ((tab = calloc(1, sizeof(*tab))) == NULL)
788 bcb0b073 2021-03-07 op if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
791 bcb0b073 2021-03-07 op TAILQ_INIT(&tab->s->head);
792 bcb0b073 2021-03-07 op TAILQ_FOREACH(t, &tabshead, tabs) {
793 bcb0b073 2021-03-07 op t->flags &= ~TAB_CURRENT;
796 bcb0b073 2021-03-07 op tab->id = tab_counter++;
797 bcb0b073 2021-03-07 op tab->flags = TAB_CURRENT;
799 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
800 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
802 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
804 8af5e5ed 2021-03-08 op ui_load_url_in_tab(tab, url);
808 bcb0b073 2021-03-07 op event_loopbreak();
812 5e11c00c 2021-03-02 op ui_init(void)
814 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
821 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
822 5e11c00c 2021-03-02 op keypad(stdscr, TRUE);
824 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
826 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
828 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
830 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
833 48e9d457 2021-03-06 op body_lines = LINES-3;
834 48e9d457 2021-03-06 op body_cols = COLS;
836 48e9d457 2021-03-06 op scrollok(body, TRUE);
838 5e11c00c 2021-03-02 op /* non-blocking input */
839 48e9d457 2021-03-06 op wtimeout(body, 0);
841 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
843 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
844 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
846 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
847 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
855 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
857 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
858 8af5e5ed 2021-03-08 op message("Loaded %s", tab->url);
862 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
864 5e11c00c 2021-03-02 op if (!(tab->flags & TAB_CURRENT))
867 1d08c280 2021-03-06 op wrap_page(tab);
868 5e11c00c 2021-03-02 op redraw_tab(tab);