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 48e9d457 2021-03-06 op static void restore_cursor(struct tab *);
80 48e9d457 2021-03-06 op static void cmd_previous_line(int);
81 48e9d457 2021-03-06 op static void cmd_next_line(int);
82 48e9d457 2021-03-06 op static void cmd_forward_char(int);
83 48e9d457 2021-03-06 op static void cmd_backward_char(int);
84 48e9d457 2021-03-06 op static void cmd_redraw(int);
85 48e9d457 2021-03-06 op static void cmd_scroll_down(int);
86 48e9d457 2021-03-06 op static void cmd_scroll_up(int);
87 48e9d457 2021-03-06 op static void cmd_kill_telescope(int);
88 48e9d457 2021-03-06 op static struct line *nth_line(struct tab*, size_t);
89 5e11c00c 2021-03-02 op static struct tab *current_tab(void);
90 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
91 a6d450c1 2021-03-06 op static void handle_clear_minibuf(int, short, void*);
92 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
93 eb259e66 2021-03-02 op static int word_bourdaries(const char*, const char*, const char**, const char**);
94 1d08c280 2021-03-06 op static void wrap_text(struct tab*, const char*, struct line*);
95 1d08c280 2021-03-06 op static int hardwrap_text(struct tab*, struct line*);
96 1d08c280 2021-03-06 op static int wrap_page(struct tab*);
97 48e9d457 2021-03-06 op static void print_line(struct line*);
98 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
100 1d08c280 2021-03-06 op typedef void (*interactivefn)(int);
102 1d08c280 2021-03-06 op static void cmd_unbound(int);
104 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
105 48e9d457 2021-03-06 op static int body_lines, body_cols;
107 48e9d457 2021-03-06 op static struct event clminibufev;
108 48e9d457 2021-03-06 op static int clminibufev_set;
109 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
111 1d08c280 2021-03-06 op struct ui_state {
114 48e9d457 2021-03-06 op size_t line_off;
115 48e9d457 2021-03-06 op size_t line_max;
117 1d08c280 2021-03-06 op TAILQ_HEAD(, line) head;
120 48e9d457 2021-03-06 op struct binding {
122 48e9d457 2021-03-06 op interactivefn fn;
123 48e9d457 2021-03-06 op } bindings[] = {
124 48e9d457 2021-03-06 op { CTRL('p'), cmd_previous_line, },
125 48e9d457 2021-03-06 op { CTRL('n'), cmd_next_line, },
126 48e9d457 2021-03-06 op { CTRL('f'), cmd_forward_char, },
127 48e9d457 2021-03-06 op { CTRL('b'), cmd_backward_char, },
129 48e9d457 2021-03-06 op { CTRL('L'), cmd_redraw, },
131 48e9d457 2021-03-06 op { 'J', cmd_scroll_down, },
132 48e9d457 2021-03-06 op { 'K', cmd_scroll_up, },
134 48e9d457 2021-03-06 op { 'q', cmd_kill_telescope, },
136 48e9d457 2021-03-06 op { 0, NULL, },
140 1d08c280 2021-03-06 op push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
142 1d08c280 2021-03-06 op struct line *vl;
144 48e9d457 2021-03-06 op tab->s->line_max++;
146 1d08c280 2021-03-06 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
149 1d08c280 2021-03-06 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
154 1d08c280 2021-03-06 op vl->type = l->type;
155 1d08c280 2021-03-06 op if (len != 0)
156 1d08c280 2021-03-06 op memcpy(vl->line, buf, len);
157 1d08c280 2021-03-06 op vl->alt = l->alt;
159 1d08c280 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
160 1d08c280 2021-03-06 op TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
162 1d08c280 2021-03-06 op TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
167 1d08c280 2021-03-06 op empty_vlist(struct tab *tab)
169 1d08c280 2021-03-06 op struct line *l, *t;
171 48e9d457 2021-03-06 op tab->s->line_max = 0;
173 1d08c280 2021-03-06 op TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
174 1d08c280 2021-03-06 op TAILQ_REMOVE(&tab->s->head, l, lines);
175 1d08c280 2021-03-06 op free(l->line);
176 1d08c280 2021-03-06 op /* l->alt references the original line! */
182 48e9d457 2021-03-06 op restore_cursor(struct tab *tab)
184 48e9d457 2021-03-06 op wmove(body, tab->s->curs_y, tab->s->curs_x);
188 1d08c280 2021-03-06 op cmd_previous_line(int k)
190 1d08c280 2021-03-06 op struct tab *tab;
192 1d08c280 2021-03-06 op tab = current_tab();
193 1d08c280 2021-03-06 op tab->s->curs_y = MAX(0, tab->s->curs_y-1);
194 48e9d457 2021-03-06 op restore_cursor(tab);
198 1d08c280 2021-03-06 op cmd_next_line(int k)
200 1d08c280 2021-03-06 op struct tab *tab;
202 1d08c280 2021-03-06 op tab = current_tab();
203 b2cd5e06 2021-03-06 op tab->s->curs_y = MIN(LINES-1, tab->s->curs_y+1);
204 48e9d457 2021-03-06 op restore_cursor(tab);
208 1d08c280 2021-03-06 op cmd_forward_char(int k)
210 b2cd5e06 2021-03-06 op struct tab *tab;
212 b2cd5e06 2021-03-06 op tab = current_tab();
213 b2cd5e06 2021-03-06 op tab->s->curs_x = MIN(COLS-1, tab->s->curs_x+1);
214 48e9d457 2021-03-06 op restore_cursor(tab);
218 1d08c280 2021-03-06 op cmd_backward_char(int k)
220 b2cd5e06 2021-03-06 op struct tab *tab;
222 b2cd5e06 2021-03-06 op tab = current_tab();
223 b2cd5e06 2021-03-06 op tab->s->curs_x = MAX(0, tab->s->curs_x-1);
224 48e9d457 2021-03-06 op restore_cursor(tab);
228 1d08c280 2021-03-06 op cmd_redraw(int k)
230 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
234 48e9d457 2021-03-06 op cmd_scroll_up(int k)
236 48e9d457 2021-03-06 op struct tab *tab;
237 48e9d457 2021-03-06 op struct line *l;
239 48e9d457 2021-03-06 op tab = current_tab();
240 48e9d457 2021-03-06 op if (tab->s->line_off == 0)
243 48e9d457 2021-03-06 op l = nth_line(tab, --tab->s->line_off);
244 48e9d457 2021-03-06 op wscrl(body, -1);
245 48e9d457 2021-03-06 op wmove(body, 0, 0);
246 48e9d457 2021-03-06 op print_line(l);
250 48e9d457 2021-03-06 op cmd_scroll_down(int k)
252 48e9d457 2021-03-06 op struct tab *tab;
253 48e9d457 2021-03-06 op struct line *l;
256 48e9d457 2021-03-06 op tab = current_tab();
258 48e9d457 2021-03-06 op if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
261 48e9d457 2021-03-06 op tab->s->line_off++;
262 48e9d457 2021-03-06 op wscrl(body, 1);
264 48e9d457 2021-03-06 op if (tab->s->line_max - tab->s->line_off < body_lines)
267 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off + body_lines-1);
268 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
269 48e9d457 2021-03-06 op print_line(l);
273 1d08c280 2021-03-06 op cmd_kill_telescope(int k)
275 1d08c280 2021-03-06 op event_loopbreak();
279 1d08c280 2021-03-06 op cmd_unbound(int k)
281 a6d450c1 2021-03-06 op if (clminibufev_set)
282 a6d450c1 2021-03-06 op evtimer_del(&clminibufev);
283 a6d450c1 2021-03-06 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
284 a6d450c1 2021-03-06 op evtimer_add(&clminibufev, &clminibufev_timer);
286 b1738d2e 2021-03-06 op werase(minibuf);
287 48e9d457 2021-03-06 op wprintw(minibuf, "Unknown key %c", k);
288 48e9d457 2021-03-06 op restore_cursor(current_tab());
291 48e9d457 2021-03-06 op static struct line *
292 48e9d457 2021-03-06 op nth_line(struct tab *tab, size_t n)
294 48e9d457 2021-03-06 op struct line *l;
298 48e9d457 2021-03-06 op TAILQ_FOREACH(l, &tab->s->head, lines) {
304 48e9d457 2021-03-06 op /* unreachable */
308 5e11c00c 2021-03-02 op static struct tab *
309 5e11c00c 2021-03-02 op current_tab(void)
311 5e11c00c 2021-03-02 op struct tab *t;
313 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
314 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
318 5e11c00c 2021-03-02 op /* unreachable */
323 5e11c00c 2021-03-02 op dispatch_stdio(int fd, short ev, void *d)
325 1d08c280 2021-03-06 op struct binding *b;
328 48e9d457 2021-03-06 op k = wgetch(body);
330 1d08c280 2021-03-06 op if (k == ERR)
333 1d08c280 2021-03-06 op for (b = bindings; b->fn != NULL; ++b) {
334 1d08c280 2021-03-06 op if (k == b->key) {
340 1d08c280 2021-03-06 op cmd_unbound(k);
343 48e9d457 2021-03-06 op restore_cursor(current_tab());
344 48e9d457 2021-03-06 op wrefresh(tabline);
345 48e9d457 2021-03-06 op wrefresh(modeline);
346 48e9d457 2021-03-06 op wrefresh(minibuf);
348 a6d450c1 2021-03-06 op wrefresh(body);
352 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
354 a6d450c1 2021-03-06 op clminibufev_set = 0;
355 a6d450c1 2021-03-06 op werase(minibuf);
356 a6d450c1 2021-03-06 op wrefresh(minibuf);
357 48e9d457 2021-03-06 op wrefresh(body);
361 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
363 1d08c280 2021-03-06 op struct tab *tab;
369 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
371 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
372 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
374 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
375 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
377 48e9d457 2021-03-06 op wresize(body, LINES-3, COLS);
378 48e9d457 2021-03-06 op body_lines = LINES-3;
379 48e9d457 2021-03-06 op body_cols = COLS-1;
381 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
383 1d08c280 2021-03-06 op tab = current_tab();
385 1d08c280 2021-03-06 op wrap_page(tab);
386 1d08c280 2021-03-06 op redraw_tab(tab);
390 eb259e66 2021-03-02 op * Helper function for wrap_text. Find the end of the current word
391 eb259e66 2021-03-02 op * and the end of the separator after the word.
394 eb259e66 2021-03-02 op word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
396 eb259e66 2021-03-02 op *endword = s;
397 eb259e66 2021-03-02 op *endword = s;
399 eb259e66 2021-03-02 op if (*s == '\0')
402 eb259e66 2021-03-02 op /* find the end of the current world */
403 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
404 eb259e66 2021-03-02 op if (strchr(sep, *s) != NULL)
408 eb259e66 2021-03-02 op *endword = s;
410 eb259e66 2021-03-02 op /* find the end of the separator */
411 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
412 eb259e66 2021-03-02 op if (strchr(sep, *s) == NULL)
421 1d08c280 2021-03-06 op static inline int
422 1d08c280 2021-03-06 op emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
423 1d08c280 2021-03-06 op const char **line)
425 1d08c280 2021-03-06 op if (!push_line(tab, l, *line, *off - zero))
427 1d08c280 2021-03-06 op *line += *off - zero;
432 eb259e66 2021-03-02 op static inline void
433 eb259e66 2021-03-02 op emitstr(const char **s, size_t len, size_t *off)
437 eb259e66 2021-03-02 op /* printw("%*s", ...) doesn't seem to respect the precision, so... */
438 eb259e66 2021-03-02 op for (i = 0; i < len; ++i)
439 eb259e66 2021-03-02 op addch((*s)[i]);
445 1d08c280 2021-03-06 op * Build a list of visual line by wrapping the given line, assuming
446 1d08c280 2021-03-06 op * that when printed will have a leading prefix prfx.
448 eb259e66 2021-03-02 op * TODO: it considers each byte one cell on the screen!
451 1d08c280 2021-03-06 op wrap_text(struct tab *tab, const char *prfx, struct line *l)
453 eb259e66 2021-03-02 op size_t zero, off, len, split;
454 1d08c280 2021-03-06 op const char *endword, *endspc, *line, *linestart;
456 1d08c280 2021-03-06 op zero = strlen(prfx);
458 1d08c280 2021-03-06 op line = l->line;
459 1d08c280 2021-03-06 op linestart = l->line;
461 1d08c280 2021-03-06 op while (word_boundaries(line, " \t-", &endword, &endspc)) {
462 eb259e66 2021-03-02 op len = endword - line;
463 48e9d457 2021-03-06 op if (off + len >= body_cols) {
464 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
465 48e9d457 2021-03-06 op while (len >= body_cols) {
466 eb259e66 2021-03-02 op /* hard wrap */
467 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
468 48e9d457 2021-03-06 op len -= body_cols-1;
469 48e9d457 2021-03-06 op line += body_cols-1;
472 eb259e66 2021-03-02 op if (len != 0)
477 eb259e66 2021-03-02 op /* print the spaces iff not at bol */
478 eb259e66 2021-03-02 op len = endspc - endword;
479 eb259e66 2021-03-02 op /* line = endspc; */
480 eb259e66 2021-03-02 op if (off != zero) {
481 48e9d457 2021-03-06 op if (off + len >= body_cols) {
482 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
483 1d08c280 2021-03-06 op linestart = endspc;
488 eb259e66 2021-03-02 op line = endspc;
491 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
495 1d08c280 2021-03-06 op hardwrap_text(struct tab *tab, struct line *l)
497 1d08c280 2021-03-06 op size_t off, len;
498 1d08c280 2021-03-06 op const char *linestart;
500 1d08c280 2021-03-06 op len = strlen(l->line);
502 1d08c280 2021-03-06 op linestart = l->line;
504 1d08c280 2021-03-06 op while (len >= COLS) {
505 1d08c280 2021-03-06 op len -= COLS-1;
506 1d08c280 2021-03-06 op off = COLS-1;
507 1d08c280 2021-03-06 op if (!emitline(tab, 0, &off, l, &linestart))
515 1d08c280 2021-03-06 op wrap_page(struct tab *tab)
517 1d08c280 2021-03-06 op struct line *l;
519 1d08c280 2021-03-06 op empty_vlist(tab);
521 5e11c00c 2021-03-02 op TAILQ_FOREACH(l, &tab->page.head, lines) {
522 5e11c00c 2021-03-02 op switch (l->type) {
523 5e11c00c 2021-03-02 op case LINE_TEXT:
524 1d08c280 2021-03-06 op wrap_text(tab, "", l);
526 5e11c00c 2021-03-02 op case LINE_LINK:
527 1d08c280 2021-03-06 op wrap_text(tab, "=> ", l);
529 5e11c00c 2021-03-02 op case LINE_TITLE_1:
530 1d08c280 2021-03-06 op wrap_text(tab, "# ", l);
532 5e11c00c 2021-03-02 op case LINE_TITLE_2:
533 1d08c280 2021-03-06 op wrap_text(tab, "## ", l);
535 5e11c00c 2021-03-02 op case LINE_TITLE_3:
536 1d08c280 2021-03-06 op wrap_text(tab, "### ", l);
538 5e11c00c 2021-03-02 op case LINE_ITEM:
539 1d08c280 2021-03-06 op wrap_text(tab, "* ", l);
541 5e11c00c 2021-03-02 op case LINE_QUOTE:
542 1d08c280 2021-03-06 op wrap_text(tab, "> ", l);
544 5e11c00c 2021-03-02 op case LINE_PRE_START:
545 5e11c00c 2021-03-02 op case LINE_PRE_END:
546 1d08c280 2021-03-06 op push_line(tab, l, NULL, 0);
548 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
549 1d08c280 2021-03-06 op hardwrap_text(tab, l);
556 1d08c280 2021-03-06 op static inline void
557 1d08c280 2021-03-06 op print_line(struct line *l)
559 1d08c280 2021-03-06 op switch (l->type) {
560 1d08c280 2021-03-06 op case LINE_TEXT:
562 1d08c280 2021-03-06 op case LINE_LINK:
563 48e9d457 2021-03-06 op wprintw(body, "=> ");
565 1d08c280 2021-03-06 op case LINE_TITLE_1:
566 48e9d457 2021-03-06 op wprintw(body, "# ");
568 1d08c280 2021-03-06 op case LINE_TITLE_2:
569 48e9d457 2021-03-06 op wprintw(body, "## ");
571 1d08c280 2021-03-06 op case LINE_TITLE_3:
572 48e9d457 2021-03-06 op wprintw(body, "### ");
574 1d08c280 2021-03-06 op case LINE_ITEM:
575 48e9d457 2021-03-06 op wprintw(body, "* ");
577 1d08c280 2021-03-06 op case LINE_QUOTE:
578 48e9d457 2021-03-06 op wprintw(body, "> ");
580 1d08c280 2021-03-06 op case LINE_PRE_START:
581 1d08c280 2021-03-06 op case LINE_PRE_END:
582 48e9d457 2021-03-06 op wprintw(body, "```");
584 1d08c280 2021-03-06 op case LINE_PRE_CONTENT:
588 1d08c280 2021-03-06 op if (l->line != NULL)
589 48e9d457 2021-03-06 op wprintw(body, "%s", l->line);
593 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
595 48e9d457 2021-03-06 op int x, y, max_x, max_y;
596 48e9d457 2021-03-06 op const char *url = "TODO:url";
597 48e9d457 2021-03-06 op const char *mode = "text/gemini-mode";
599 48e9d457 2021-03-06 op wclear(modeline);
600 48e9d457 2021-03-06 op wattron(modeline, A_REVERSE);
601 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
603 48e9d457 2021-03-06 op wprintw(modeline, "-- %s %s ", mode, url);
604 48e9d457 2021-03-06 op getyx(modeline, y, x);
605 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
610 48e9d457 2021-03-06 op for (; x < max_x; ++x)
611 48e9d457 2021-03-06 op waddstr(modeline, "-");
615 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
617 48e9d457 2021-03-06 op struct line *l;
620 48e9d457 2021-03-06 op werase(body);
622 48e9d457 2021-03-06 op tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
623 48e9d457 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
627 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off);
628 48e9d457 2021-03-06 op for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
629 48e9d457 2021-03-06 op wmove(body, line, 0);
630 1d08c280 2021-03-06 op print_line(l);
632 48e9d457 2021-03-06 op if (line == body_lines)
636 48e9d457 2021-03-06 op redraw_modeline(tab);
638 48e9d457 2021-03-06 op restore_cursor(tab);
639 48e9d457 2021-03-06 op wrefresh(tabline);
640 48e9d457 2021-03-06 op wrefresh(modeline);
641 48e9d457 2021-03-06 op wrefresh(minibuf);
643 48e9d457 2021-03-06 op wrefresh(body);
647 5e11c00c 2021-03-02 op ui_init(void)
649 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
656 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
657 5e11c00c 2021-03-02 op keypad(stdscr, TRUE);
659 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
661 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
663 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
665 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
668 48e9d457 2021-03-06 op body_lines = LINES-3;
669 48e9d457 2021-03-06 op body_cols = COLS;
671 48e9d457 2021-03-06 op scrollok(body, TRUE);
673 5e11c00c 2021-03-02 op /* non-blocking input */
674 48e9d457 2021-03-06 op wtimeout(body, 0);
676 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
678 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
679 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
681 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
682 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
688 5e11c00c 2021-03-02 op ui_on_new_tab(struct tab *tab)
690 5e11c00c 2021-03-02 op struct tab *t;
692 1d08c280 2021-03-06 op if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
695 1d08c280 2021-03-06 op TAILQ_INIT(&tab->s->head);
697 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
698 5e11c00c 2021-03-02 op t->flags &= ~TAB_CURRENT;
700 5e11c00c 2021-03-02 op tab->flags = TAB_CURRENT;
702 5e11c00c 2021-03-02 op /* TODO: redraw the tab list */
703 1d08c280 2021-03-06 op /* TODO: switch to the new tab */
705 48e9d457 2021-03-06 op wmove(body, 0, 0);
711 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
713 5e11c00c 2021-03-02 op if (!(tab->flags & TAB_CURRENT))
716 1d08c280 2021-03-06 op wrap_page(tab);
717 5e11c00c 2021-03-02 op redraw_tab(tab);