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 43a1b8d0 2021-03-09 op static void load_url_in_tab(struct tab*, const char*);
109 bcb0b073 2021-03-07 op static void new_tab(void);
111 46a9311e 2021-03-08 op typedef void (*interactivefn)(struct tab*);
113 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
114 48e9d457 2021-03-06 op static int body_lines, body_cols;
116 48e9d457 2021-03-06 op static struct event clminibufev;
117 48e9d457 2021-03-06 op static int clminibufev_set;
118 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
119 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
121 bcb0b073 2021-03-07 op static uint32_t tab_counter;
123 1d08c280 2021-03-06 op struct ui_state {
126 48e9d457 2021-03-06 op size_t line_off;
127 48e9d457 2021-03-06 op size_t line_max;
129 8af5e5ed 2021-03-08 op short loading_anim;
130 8af5e5ed 2021-03-08 op short loading_anim_step;
131 8af5e5ed 2021-03-08 op struct event loadingev;
133 1d08c280 2021-03-06 op TAILQ_HEAD(, line) head;
141 19f1448e 2021-03-08 op #define KEY(n) ((struct key){0, n})
142 19f1448e 2021-03-08 op #define CKEY(n) ((struct key){0, (n) & 0x1F})
143 b0ea6f26 2021-03-08 op #define MKEY(n) ((struct key){1, n})
144 b0ea6f26 2021-03-08 op #define CMKEY(n) ((struct key){1, (n) & 0x1F})
146 48e9d457 2021-03-06 op struct binding {
147 19f1448e 2021-03-08 op struct key key;
148 48e9d457 2021-03-06 op interactivefn fn;
149 48e9d457 2021-03-06 op } bindings[] = {
150 19f1448e 2021-03-08 op { CKEY('p'), cmd_previous_line, },
151 19f1448e 2021-03-08 op { CKEY('n'), cmd_next_line, },
152 19f1448e 2021-03-08 op { CKEY('f'), cmd_forward_char, },
153 19f1448e 2021-03-08 op { CKEY('b'), cmd_backward_char, },
155 43a1b8d0 2021-03-09 op { {0,KEY_UP}, cmd_previous_line, },
156 43a1b8d0 2021-03-09 op { {0,KEY_DOWN}, cmd_next_line, },
158 19f1448e 2021-03-08 op { CKEY('L'), cmd_redraw, },
160 19f1448e 2021-03-08 op { KEY('J'), cmd_scroll_down, },
161 19f1448e 2021-03-08 op { KEY('K'), cmd_scroll_up, },
163 19f1448e 2021-03-08 op { KEY('q'), cmd_kill_telescope, },
164 19f1448e 2021-03-08 op { CKEY('m'), cmd_push_button, },
166 19f1448e 2021-03-08 op { {0, 0}, NULL, },
170 1d08c280 2021-03-06 op push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
172 1d08c280 2021-03-06 op struct line *vl;
174 48e9d457 2021-03-06 op tab->s->line_max++;
176 1d08c280 2021-03-06 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
179 1d08c280 2021-03-06 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
184 1d08c280 2021-03-06 op vl->type = l->type;
185 1d08c280 2021-03-06 op if (len != 0)
186 1d08c280 2021-03-06 op memcpy(vl->line, buf, len);
187 1d08c280 2021-03-06 op vl->alt = l->alt;
189 1d08c280 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
190 1d08c280 2021-03-06 op TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
192 1d08c280 2021-03-06 op TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
197 1d08c280 2021-03-06 op empty_vlist(struct tab *tab)
199 1d08c280 2021-03-06 op struct line *l, *t;
201 48e9d457 2021-03-06 op tab->s->line_max = 0;
203 1d08c280 2021-03-06 op TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
204 1d08c280 2021-03-06 op TAILQ_REMOVE(&tab->s->head, l, lines);
205 1d08c280 2021-03-06 op free(l->line);
206 1d08c280 2021-03-06 op /* l->alt references the original line! */
212 48e9d457 2021-03-06 op restore_cursor(struct tab *tab)
214 48e9d457 2021-03-06 op wmove(body, tab->s->curs_y, tab->s->curs_x);
218 46a9311e 2021-03-08 op cmd_previous_line(struct tab *tab)
220 4dd664ce 2021-03-06 op if (--tab->s->curs_y < 0) {
221 4dd664ce 2021-03-06 op tab->s->curs_y = 0;
222 46a9311e 2021-03-08 op cmd_scroll_up(tab);
225 48e9d457 2021-03-06 op restore_cursor(tab);
229 46a9311e 2021-03-08 op cmd_next_line(struct tab *tab)
231 4dd664ce 2021-03-06 op if (++tab->s->curs_y > body_lines-1) {
232 4dd664ce 2021-03-06 op tab->s->curs_y = body_lines-1;
233 46a9311e 2021-03-08 op cmd_scroll_down(tab);
236 48e9d457 2021-03-06 op restore_cursor(tab);
240 46a9311e 2021-03-08 op cmd_forward_char(struct tab *tab)
242 4dd664ce 2021-03-06 op tab->s->curs_x = MIN(body_cols-1, tab->s->curs_x+1);
243 48e9d457 2021-03-06 op restore_cursor(tab);
247 46a9311e 2021-03-08 op cmd_backward_char(struct tab *tab)
249 b2cd5e06 2021-03-06 op tab->s->curs_x = MAX(0, tab->s->curs_x-1);
250 48e9d457 2021-03-06 op restore_cursor(tab);
254 46a9311e 2021-03-08 op cmd_redraw(struct tab *tab)
256 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
260 46a9311e 2021-03-08 op cmd_scroll_up(struct tab *tab)
262 48e9d457 2021-03-06 op struct line *l;
264 48e9d457 2021-03-06 op if (tab->s->line_off == 0)
267 48e9d457 2021-03-06 op l = nth_line(tab, --tab->s->line_off);
268 48e9d457 2021-03-06 op wscrl(body, -1);
269 48e9d457 2021-03-06 op wmove(body, 0, 0);
270 48e9d457 2021-03-06 op print_line(l);
274 46a9311e 2021-03-08 op cmd_scroll_down(struct tab *tab)
276 48e9d457 2021-03-06 op struct line *l;
279 48e9d457 2021-03-06 op if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
282 48e9d457 2021-03-06 op tab->s->line_off++;
283 48e9d457 2021-03-06 op wscrl(body, 1);
285 48e9d457 2021-03-06 op if (tab->s->line_max - tab->s->line_off < body_lines)
288 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off + body_lines-1);
289 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
290 48e9d457 2021-03-06 op print_line(l);
294 46a9311e 2021-03-08 op cmd_kill_telescope(struct tab *tab)
296 1d08c280 2021-03-06 op event_loopbreak();
300 46a9311e 2021-03-08 op cmd_push_button(struct tab *tab)
302 2a4ad912 2021-03-08 op struct line *l;
305 2a4ad912 2021-03-08 op nth = tab->s->line_off + tab->s->curs_y;
306 2a4ad912 2021-03-08 op if (nth > tab->s->line_max)
308 2a4ad912 2021-03-08 op l = nth_line(tab, nth);
309 43a1b8d0 2021-03-09 op if (l->type != LINE_LINK)
312 43a1b8d0 2021-03-09 op load_url_in_tab(tab, l->alt);
316 19f1448e 2021-03-08 op cmd_unbound(struct key k)
318 19f1448e 2021-03-08 op message("%s%c is undefined",
319 b0ea6f26 2021-03-08 op k.meta ? "M-" : "",
323 48e9d457 2021-03-06 op static struct line *
324 48e9d457 2021-03-06 op nth_line(struct tab *tab, size_t n)
326 48e9d457 2021-03-06 op struct line *l;
330 48e9d457 2021-03-06 op TAILQ_FOREACH(l, &tab->s->head, lines) {
336 48e9d457 2021-03-06 op /* unreachable */
340 5e11c00c 2021-03-02 op static struct tab *
341 5e11c00c 2021-03-02 op current_tab(void)
343 5e11c00c 2021-03-02 op struct tab *t;
345 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
346 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
350 5e11c00c 2021-03-02 op /* unreachable */
355 5e11c00c 2021-03-02 op dispatch_stdio(int fd, short ev, void *d)
357 1d08c280 2021-03-06 op struct binding *b;
358 19f1448e 2021-03-08 op struct key key;
361 48e9d457 2021-03-06 op k = wgetch(body);
363 1d08c280 2021-03-06 op if (k == ERR)
366 19f1448e 2021-03-08 op /* look for a M-key */
367 19f1448e 2021-03-08 op if (k == 27) {
368 19f1448e 2021-03-08 op wtimeout(body, 0); /* TODO: make escape-time customizable */
369 19f1448e 2021-03-08 op j = wgetch(body);
370 19f1448e 2021-03-08 op if (j != ERR) {
372 b0ea6f26 2021-03-08 op key.meta = 1;
374 ff3ffcb5 2021-03-08 op key.meta = 0;
377 b0ea6f26 2021-03-08 op key.meta = 0;
381 1d08c280 2021-03-06 op for (b = bindings; b->fn != NULL; ++b) {
382 b0ea6f26 2021-03-08 op if (key.meta == b->key.meta &&
383 19f1448e 2021-03-08 op key.key == b->key.key) {
384 46a9311e 2021-03-08 op b->fn(current_tab());
389 19f1448e 2021-03-08 op cmd_unbound(key);
392 48e9d457 2021-03-06 op restore_cursor(current_tab());
393 48e9d457 2021-03-06 op wrefresh(tabline);
394 48e9d457 2021-03-06 op wrefresh(modeline);
395 48e9d457 2021-03-06 op wrefresh(minibuf);
397 a6d450c1 2021-03-06 op wrefresh(body);
401 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
403 a6d450c1 2021-03-06 op clminibufev_set = 0;
404 a6d450c1 2021-03-06 op werase(minibuf);
405 a6d450c1 2021-03-06 op wrefresh(minibuf);
406 48e9d457 2021-03-06 op wrefresh(body);
410 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
412 1d08c280 2021-03-06 op struct tab *tab;
418 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
420 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
421 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
423 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
424 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
426 48e9d457 2021-03-06 op wresize(body, LINES-3, COLS);
427 48e9d457 2021-03-06 op body_lines = LINES-3;
428 bd9637e9 2021-03-06 op body_cols = COLS;
430 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
432 1d08c280 2021-03-06 op tab = current_tab();
434 1d08c280 2021-03-06 op wrap_page(tab);
435 1d08c280 2021-03-06 op redraw_tab(tab);
439 eb259e66 2021-03-02 op * Helper function for wrap_text. Find the end of the current word
440 eb259e66 2021-03-02 op * and the end of the separator after the word.
443 eb259e66 2021-03-02 op word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
445 eb259e66 2021-03-02 op *endword = s;
446 eb259e66 2021-03-02 op *endword = s;
448 eb259e66 2021-03-02 op if (*s == '\0')
451 eb259e66 2021-03-02 op /* find the end of the current world */
452 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
453 eb259e66 2021-03-02 op if (strchr(sep, *s) != NULL)
457 eb259e66 2021-03-02 op *endword = s;
459 eb259e66 2021-03-02 op /* find the end of the separator */
460 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
461 eb259e66 2021-03-02 op if (strchr(sep, *s) == NULL)
470 1d08c280 2021-03-06 op static inline int
471 1d08c280 2021-03-06 op emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
472 1d08c280 2021-03-06 op const char **line)
474 1d08c280 2021-03-06 op if (!push_line(tab, l, *line, *off - zero))
476 1d08c280 2021-03-06 op *line += *off - zero;
481 eb259e66 2021-03-02 op static inline void
482 eb259e66 2021-03-02 op emitstr(const char **s, size_t len, size_t *off)
486 eb259e66 2021-03-02 op /* printw("%*s", ...) doesn't seem to respect the precision, so... */
487 eb259e66 2021-03-02 op for (i = 0; i < len; ++i)
488 eb259e66 2021-03-02 op addch((*s)[i]);
494 1d08c280 2021-03-06 op * Build a list of visual line by wrapping the given line, assuming
495 1d08c280 2021-03-06 op * that when printed will have a leading prefix prfx.
497 eb259e66 2021-03-02 op * TODO: it considers each byte one cell on the screen!
500 1d08c280 2021-03-06 op wrap_text(struct tab *tab, const char *prfx, struct line *l)
502 eb259e66 2021-03-02 op size_t zero, off, len, split;
503 1d08c280 2021-03-06 op const char *endword, *endspc, *line, *linestart;
505 1d08c280 2021-03-06 op zero = strlen(prfx);
507 1d08c280 2021-03-06 op line = l->line;
508 1d08c280 2021-03-06 op linestart = l->line;
510 1d08c280 2021-03-06 op while (word_boundaries(line, " \t-", &endword, &endspc)) {
511 eb259e66 2021-03-02 op len = endword - line;
512 48e9d457 2021-03-06 op if (off + len >= body_cols) {
513 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
514 48e9d457 2021-03-06 op while (len >= body_cols) {
515 eb259e66 2021-03-02 op /* hard wrap */
516 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
517 48e9d457 2021-03-06 op len -= body_cols-1;
518 48e9d457 2021-03-06 op line += body_cols-1;
521 eb259e66 2021-03-02 op if (len != 0)
526 eb259e66 2021-03-02 op /* print the spaces iff not at bol */
527 eb259e66 2021-03-02 op len = endspc - endword;
528 eb259e66 2021-03-02 op /* line = endspc; */
529 eb259e66 2021-03-02 op if (off != zero) {
530 48e9d457 2021-03-06 op if (off + len >= body_cols) {
531 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
532 1d08c280 2021-03-06 op linestart = endspc;
537 eb259e66 2021-03-02 op line = endspc;
540 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
544 1d08c280 2021-03-06 op hardwrap_text(struct tab *tab, struct line *l)
546 1d08c280 2021-03-06 op size_t off, len;
547 1d08c280 2021-03-06 op const char *linestart;
549 1d08c280 2021-03-06 op len = strlen(l->line);
551 1d08c280 2021-03-06 op linestart = l->line;
553 1d08c280 2021-03-06 op while (len >= COLS) {
554 1d08c280 2021-03-06 op len -= COLS-1;
555 1d08c280 2021-03-06 op off = COLS-1;
556 1d08c280 2021-03-06 op if (!emitline(tab, 0, &off, l, &linestart))
560 ede1f142 2021-03-07 op if (len != 0)
561 ede1f142 2021-03-07 op return emitline(tab, 0, &len, l, &linestart);
567 1d08c280 2021-03-06 op wrap_page(struct tab *tab)
569 1d08c280 2021-03-06 op struct line *l;
571 1d08c280 2021-03-06 op empty_vlist(tab);
573 5e11c00c 2021-03-02 op TAILQ_FOREACH(l, &tab->page.head, lines) {
574 5e11c00c 2021-03-02 op switch (l->type) {
575 5e11c00c 2021-03-02 op case LINE_TEXT:
576 1d08c280 2021-03-06 op wrap_text(tab, "", l);
578 5e11c00c 2021-03-02 op case LINE_LINK:
579 1d08c280 2021-03-06 op wrap_text(tab, "=> ", l);
581 5e11c00c 2021-03-02 op case LINE_TITLE_1:
582 1d08c280 2021-03-06 op wrap_text(tab, "# ", l);
584 5e11c00c 2021-03-02 op case LINE_TITLE_2:
585 1d08c280 2021-03-06 op wrap_text(tab, "## ", l);
587 5e11c00c 2021-03-02 op case LINE_TITLE_3:
588 1d08c280 2021-03-06 op wrap_text(tab, "### ", l);
590 5e11c00c 2021-03-02 op case LINE_ITEM:
591 1d08c280 2021-03-06 op wrap_text(tab, "* ", l);
593 5e11c00c 2021-03-02 op case LINE_QUOTE:
594 1d08c280 2021-03-06 op wrap_text(tab, "> ", l);
596 5e11c00c 2021-03-02 op case LINE_PRE_START:
597 5e11c00c 2021-03-02 op case LINE_PRE_END:
598 1d08c280 2021-03-06 op push_line(tab, l, NULL, 0);
600 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
601 1d08c280 2021-03-06 op hardwrap_text(tab, l);
608 1d08c280 2021-03-06 op static inline void
609 1d08c280 2021-03-06 op print_line(struct line *l)
611 bd9637e9 2021-03-06 op const char *text = l->line;
613 bd9637e9 2021-03-06 op if (text == NULL)
616 1d08c280 2021-03-06 op switch (l->type) {
617 1d08c280 2021-03-06 op case LINE_TEXT:
618 bd9637e9 2021-03-06 op wprintw(body, "%s", text);
620 1d08c280 2021-03-06 op case LINE_LINK:
621 bd9637e9 2021-03-06 op wattron(body, A_UNDERLINE);
622 bd9637e9 2021-03-06 op wprintw(body, "=> %s", text);
623 bd9637e9 2021-03-06 op wattroff(body, A_UNDERLINE);
625 1d08c280 2021-03-06 op case LINE_TITLE_1:
626 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
627 bd9637e9 2021-03-06 op wprintw(body, "# %s", text);
628 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
630 1d08c280 2021-03-06 op case LINE_TITLE_2:
631 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
632 bd9637e9 2021-03-06 op wprintw(body, "## %s", text);
633 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
635 bd9637e9 2021-03-06 op case LINE_TITLE_3:
636 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
637 bd9637e9 2021-03-06 op wprintw(body, "### %s", text);
638 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
640 bd9637e9 2021-03-06 op case LINE_ITEM:
641 bd9637e9 2021-03-06 op wprintw(body, "* %s", text);
643 1d08c280 2021-03-06 op case LINE_QUOTE:
644 bd9637e9 2021-03-06 op wattron(body, A_DIM);
645 bd9637e9 2021-03-06 op wprintw(body, "> %s", text);
646 bd9637e9 2021-03-06 op wattroff(body, A_DIM);
648 1d08c280 2021-03-06 op case LINE_PRE_START:
649 1d08c280 2021-03-06 op case LINE_PRE_END:
650 48e9d457 2021-03-06 op wprintw(body, "```");
652 1d08c280 2021-03-06 op case LINE_PRE_CONTENT:
653 bd9637e9 2021-03-06 op wprintw(body, "%s", text);
659 8af5e5ed 2021-03-08 op redraw_tabline(void)
661 8af5e5ed 2021-03-08 op wclear(tabline);
662 8af5e5ed 2021-03-08 op wbkgd(tabline, A_REVERSE);
663 8af5e5ed 2021-03-08 op mvwprintw(tabline, 0, 0, "TODO: tabs here");
667 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
669 48e9d457 2021-03-06 op int x, y, max_x, max_y;
670 48e9d457 2021-03-06 op const char *mode = "text/gemini-mode";
671 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
673 48e9d457 2021-03-06 op wclear(modeline);
674 48e9d457 2021-03-06 op wattron(modeline, A_REVERSE);
675 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
677 8af5e5ed 2021-03-08 op wprintw(modeline, "-%c %s %s ",
678 4d3785b1 2021-03-09 op spin[tab->s->loading_anim_step], mode, tab->urlstr);
679 48e9d457 2021-03-06 op getyx(modeline, y, x);
680 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
685 48e9d457 2021-03-06 op for (; x < max_x; ++x)
686 48e9d457 2021-03-06 op waddstr(modeline, "-");
690 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
692 48e9d457 2021-03-06 op struct line *l;
695 48e9d457 2021-03-06 op werase(body);
697 48e9d457 2021-03-06 op tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
698 48e9d457 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
702 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off);
703 48e9d457 2021-03-06 op for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
704 48e9d457 2021-03-06 op wmove(body, line, 0);
705 1d08c280 2021-03-06 op print_line(l);
707 48e9d457 2021-03-06 op if (line == body_lines)
711 8af5e5ed 2021-03-08 op redraw_tabline();
712 48e9d457 2021-03-06 op redraw_modeline(tab);
714 48e9d457 2021-03-06 op restore_cursor(tab);
715 48e9d457 2021-03-06 op wrefresh(tabline);
716 48e9d457 2021-03-06 op wrefresh(modeline);
717 48e9d457 2021-03-06 op wrefresh(minibuf);
719 7953dd72 2021-03-07 op wrefresh(body);
723 7953dd72 2021-03-07 op message(const char *fmt, ...)
727 7953dd72 2021-03-07 op va_start(ap, fmt);
729 7953dd72 2021-03-07 op if (clminibufev_set)
730 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
731 7953dd72 2021-03-07 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
732 7953dd72 2021-03-07 op evtimer_add(&clminibufev, &clminibufev_timer);
733 7953dd72 2021-03-07 op clminibufev_set = 1;
735 7953dd72 2021-03-07 op werase(minibuf);
736 7953dd72 2021-03-07 op vw_printw(minibuf, fmt, ap);
738 7953dd72 2021-03-07 op wrefresh(minibuf);
739 48e9d457 2021-03-06 op wrefresh(body);
745 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
747 8af5e5ed 2021-03-08 op if (tab->s->loading_anim)
749 8af5e5ed 2021-03-08 op tab->s->loading_anim = 1;
750 8af5e5ed 2021-03-08 op evtimer_set(&tab->s->loadingev, update_loading_anim, tab);
751 8af5e5ed 2021-03-08 op evtimer_add(&tab->s->loadingev, &loadingev_timer);
755 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
757 8af5e5ed 2021-03-08 op struct tab *tab = d;
759 8af5e5ed 2021-03-08 op tab->s->loading_anim_step = (tab->s->loading_anim_step+1)%4;
761 8af5e5ed 2021-03-08 op redraw_modeline(tab);
762 8af5e5ed 2021-03-08 op wrefresh(modeline);
763 8af5e5ed 2021-03-08 op wrefresh(body);
765 8af5e5ed 2021-03-08 op evtimer_add(&tab->s->loadingev, &loadingev_timer);
769 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
771 8af5e5ed 2021-03-08 op if (!tab->s->loading_anim)
773 8af5e5ed 2021-03-08 op evtimer_del(&tab->s->loadingev);
774 8af5e5ed 2021-03-08 op tab->s->loading_anim = 0;
775 8af5e5ed 2021-03-08 op tab->s->loading_anim_step = 0;
777 43a1b8d0 2021-03-09 op redraw_modeline(tab);
778 43a1b8d0 2021-03-09 op wrefresh(modeline);
779 43a1b8d0 2021-03-09 op wrefresh(body);
783 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
785 43a1b8d0 2021-03-09 op empty_vlist(tab);
786 8af5e5ed 2021-03-08 op message("Loading %s...", url);
787 8af5e5ed 2021-03-08 op start_loading_anim(tab);
788 8af5e5ed 2021-03-08 op load_url(tab, url);
790 43a1b8d0 2021-03-09 op tab->s->curs_x = 0;
791 43a1b8d0 2021-03-09 op tab->s->curs_y = 0;
792 43a1b8d0 2021-03-09 op redraw_tab(tab);
796 bcb0b073 2021-03-07 op new_tab(void)
798 bcb0b073 2021-03-07 op struct tab *tab, *t;
799 bcb0b073 2021-03-07 op const char *url = "about:new";
801 bcb0b073 2021-03-07 op if ((tab = calloc(1, sizeof(*tab))) == NULL)
804 bcb0b073 2021-03-07 op if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
807 bcb0b073 2021-03-07 op TAILQ_INIT(&tab->s->head);
808 bcb0b073 2021-03-07 op TAILQ_FOREACH(t, &tabshead, tabs) {
809 bcb0b073 2021-03-07 op t->flags &= ~TAB_CURRENT;
812 bcb0b073 2021-03-07 op tab->id = tab_counter++;
813 bcb0b073 2021-03-07 op tab->flags = TAB_CURRENT;
815 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
816 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
818 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
820 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
824 bcb0b073 2021-03-07 op event_loopbreak();
828 5e11c00c 2021-03-02 op ui_init(void)
830 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
837 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
839 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
841 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
843 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
845 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
848 48e9d457 2021-03-06 op body_lines = LINES-3;
849 48e9d457 2021-03-06 op body_cols = COLS;
851 43a1b8d0 2021-03-09 op keypad(body, TRUE);
852 48e9d457 2021-03-06 op scrollok(body, TRUE);
854 5e11c00c 2021-03-02 op /* non-blocking input */
855 48e9d457 2021-03-06 op wtimeout(body, 0);
857 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
859 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
860 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
862 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
863 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
871 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
873 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
874 4d3785b1 2021-03-09 op message("Loaded %s", tab->urlstr);
878 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
880 5e11c00c 2021-03-02 op if (!(tab->flags & TAB_CURRENT))
883 1d08c280 2021-03-06 op wrap_page(tab);
884 5e11c00c 2021-03-02 op redraw_tab(tab);