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 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
92 eb259e66 2021-03-02 op static int word_bourdaries(const char*, const char*, const char**, const char**);
93 1d08c280 2021-03-06 op static void wrap_text(struct tab*, const char*, struct line*);
94 1d08c280 2021-03-06 op static int hardwrap_text(struct tab*, struct line*);
95 1d08c280 2021-03-06 op static int wrap_page(struct tab*);
96 48e9d457 2021-03-06 op static void print_line(struct line*);
97 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
99 1d08c280 2021-03-06 op typedef void (*interactivefn)(int);
101 1d08c280 2021-03-06 op static void cmd_unbound(int);
103 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
104 48e9d457 2021-03-06 op static int body_lines, body_cols;
106 48e9d457 2021-03-06 op static struct event clminibufev;
107 48e9d457 2021-03-06 op static int clminibufev_set;
109 1d08c280 2021-03-06 op struct ui_state {
112 48e9d457 2021-03-06 op size_t line_off;
113 48e9d457 2021-03-06 op size_t line_max;
115 1d08c280 2021-03-06 op TAILQ_HEAD(, line) head;
118 48e9d457 2021-03-06 op struct binding {
120 48e9d457 2021-03-06 op interactivefn fn;
121 48e9d457 2021-03-06 op } bindings[] = {
122 48e9d457 2021-03-06 op { CTRL('p'), cmd_previous_line, },
123 48e9d457 2021-03-06 op { CTRL('n'), cmd_next_line, },
124 48e9d457 2021-03-06 op { CTRL('f'), cmd_forward_char, },
125 48e9d457 2021-03-06 op { CTRL('b'), cmd_backward_char, },
127 48e9d457 2021-03-06 op { CTRL('L'), cmd_redraw, },
129 48e9d457 2021-03-06 op { 'J', cmd_scroll_down, },
130 48e9d457 2021-03-06 op { 'K', cmd_scroll_up, },
132 48e9d457 2021-03-06 op { 'q', cmd_kill_telescope, },
134 48e9d457 2021-03-06 op { 0, NULL, },
138 1d08c280 2021-03-06 op push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
140 1d08c280 2021-03-06 op struct line *vl;
142 48e9d457 2021-03-06 op tab->s->line_max++;
144 1d08c280 2021-03-06 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
147 1d08c280 2021-03-06 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
152 1d08c280 2021-03-06 op vl->type = l->type;
153 1d08c280 2021-03-06 op if (len != 0)
154 1d08c280 2021-03-06 op memcpy(vl->line, buf, len);
155 1d08c280 2021-03-06 op vl->alt = l->alt;
157 1d08c280 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
158 1d08c280 2021-03-06 op TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
160 1d08c280 2021-03-06 op TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
165 1d08c280 2021-03-06 op empty_vlist(struct tab *tab)
167 1d08c280 2021-03-06 op struct line *l, *t;
169 48e9d457 2021-03-06 op tab->s->line_max = 0;
171 1d08c280 2021-03-06 op TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
172 1d08c280 2021-03-06 op TAILQ_REMOVE(&tab->s->head, l, lines);
173 1d08c280 2021-03-06 op free(l->line);
174 1d08c280 2021-03-06 op /* l->alt references the original line! */
180 48e9d457 2021-03-06 op restore_cursor(struct tab *tab)
182 48e9d457 2021-03-06 op wmove(body, tab->s->curs_y, tab->s->curs_x);
186 1d08c280 2021-03-06 op cmd_previous_line(int k)
188 1d08c280 2021-03-06 op struct tab *tab;
190 1d08c280 2021-03-06 op tab = current_tab();
191 1d08c280 2021-03-06 op tab->s->curs_y = MAX(0, tab->s->curs_y-1);
192 48e9d457 2021-03-06 op restore_cursor(tab);
196 1d08c280 2021-03-06 op cmd_next_line(int k)
198 1d08c280 2021-03-06 op struct tab *tab;
200 1d08c280 2021-03-06 op tab = current_tab();
201 b2cd5e06 2021-03-06 op tab->s->curs_y = MIN(LINES-1, tab->s->curs_y+1);
202 48e9d457 2021-03-06 op restore_cursor(tab);
206 1d08c280 2021-03-06 op cmd_forward_char(int k)
208 b2cd5e06 2021-03-06 op struct tab *tab;
210 b2cd5e06 2021-03-06 op tab = current_tab();
211 b2cd5e06 2021-03-06 op tab->s->curs_x = MIN(COLS-1, tab->s->curs_x+1);
212 48e9d457 2021-03-06 op restore_cursor(tab);
216 1d08c280 2021-03-06 op cmd_backward_char(int k)
218 b2cd5e06 2021-03-06 op struct tab *tab;
220 b2cd5e06 2021-03-06 op tab = current_tab();
221 b2cd5e06 2021-03-06 op tab->s->curs_x = MAX(0, tab->s->curs_x-1);
222 48e9d457 2021-03-06 op restore_cursor(tab);
226 1d08c280 2021-03-06 op cmd_redraw(int k)
229 1d08c280 2021-03-06 op redraw_tab(current_tab());
233 48e9d457 2021-03-06 op cmd_scroll_up(int k)
235 48e9d457 2021-03-06 op struct tab *tab;
236 48e9d457 2021-03-06 op struct line *l;
238 48e9d457 2021-03-06 op tab = current_tab();
239 48e9d457 2021-03-06 op if (tab->s->line_off == 0)
242 48e9d457 2021-03-06 op l = nth_line(tab, --tab->s->line_off);
243 48e9d457 2021-03-06 op wscrl(body, -1);
244 48e9d457 2021-03-06 op wmove(body, 0, 0);
245 48e9d457 2021-03-06 op print_line(l);
249 48e9d457 2021-03-06 op cmd_scroll_down(int k)
251 48e9d457 2021-03-06 op struct tab *tab;
252 48e9d457 2021-03-06 op struct line *l;
255 48e9d457 2021-03-06 op tab = current_tab();
257 48e9d457 2021-03-06 op if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
260 48e9d457 2021-03-06 op tab->s->line_off++;
261 48e9d457 2021-03-06 op wscrl(body, 1);
263 48e9d457 2021-03-06 op if (tab->s->line_max - tab->s->line_off < body_lines)
266 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off + body_lines-1);
267 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
268 48e9d457 2021-03-06 op print_line(l);
272 1d08c280 2021-03-06 op cmd_kill_telescope(int k)
274 1d08c280 2021-03-06 op event_loopbreak();
278 1d08c280 2021-03-06 op cmd_unbound(int k)
280 48e9d457 2021-03-06 op wclear(minibuf);
281 48e9d457 2021-03-06 op wprintw(minibuf, "Unknown key %c", k);
282 48e9d457 2021-03-06 op restore_cursor(current_tab());
285 48e9d457 2021-03-06 op static struct line *
286 48e9d457 2021-03-06 op nth_line(struct tab *tab, size_t n)
288 48e9d457 2021-03-06 op struct line *l;
292 48e9d457 2021-03-06 op TAILQ_FOREACH(l, &tab->s->head, lines) {
298 48e9d457 2021-03-06 op /* unreachable */
302 5e11c00c 2021-03-02 op static struct tab *
303 5e11c00c 2021-03-02 op current_tab(void)
305 5e11c00c 2021-03-02 op struct tab *t;
307 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
308 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
312 5e11c00c 2021-03-02 op /* unreachable */
317 5e11c00c 2021-03-02 op dispatch_stdio(int fd, short ev, void *d)
319 1d08c280 2021-03-06 op struct binding *b;
322 48e9d457 2021-03-06 op k = wgetch(body);
324 1d08c280 2021-03-06 op if (k == ERR)
327 1d08c280 2021-03-06 op for (b = bindings; b->fn != NULL; ++b) {
328 1d08c280 2021-03-06 op if (k == b->key) {
334 1d08c280 2021-03-06 op cmd_unbound(k);
337 48e9d457 2021-03-06 op restore_cursor(current_tab());
338 48e9d457 2021-03-06 op wrefresh(tabline);
339 48e9d457 2021-03-06 op wrefresh(modeline);
340 48e9d457 2021-03-06 op wrefresh(minibuf);
342 48e9d457 2021-03-06 op wrefresh(body);
346 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
348 1d08c280 2021-03-06 op struct tab *tab;
354 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
356 48e9d457 2021-03-06 op mvwin(minibuf, LINES-2, 0);
357 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
359 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
360 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
362 48e9d457 2021-03-06 op wresize(body, LINES-3, COLS);
363 48e9d457 2021-03-06 op body_lines = LINES-3;
364 48e9d457 2021-03-06 op body_cols = COLS-1;
366 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
368 1d08c280 2021-03-06 op tab = current_tab();
370 1d08c280 2021-03-06 op wrap_page(tab);
371 1d08c280 2021-03-06 op redraw_tab(tab);
375 eb259e66 2021-03-02 op * Helper function for wrap_text. Find the end of the current word
376 eb259e66 2021-03-02 op * and the end of the separator after the word.
379 eb259e66 2021-03-02 op word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
381 eb259e66 2021-03-02 op *endword = s;
382 eb259e66 2021-03-02 op *endword = s;
384 eb259e66 2021-03-02 op if (*s == '\0')
387 eb259e66 2021-03-02 op /* find the end of the current world */
388 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
389 eb259e66 2021-03-02 op if (strchr(sep, *s) != NULL)
393 eb259e66 2021-03-02 op *endword = s;
395 eb259e66 2021-03-02 op /* find the end of the separator */
396 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
397 eb259e66 2021-03-02 op if (strchr(sep, *s) == NULL)
406 1d08c280 2021-03-06 op static inline int
407 1d08c280 2021-03-06 op emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
408 1d08c280 2021-03-06 op const char **line)
410 1d08c280 2021-03-06 op if (!push_line(tab, l, *line, *off - zero))
412 1d08c280 2021-03-06 op *line += *off - zero;
417 eb259e66 2021-03-02 op static inline void
418 eb259e66 2021-03-02 op emitstr(const char **s, size_t len, size_t *off)
422 eb259e66 2021-03-02 op /* printw("%*s", ...) doesn't seem to respect the precision, so... */
423 eb259e66 2021-03-02 op for (i = 0; i < len; ++i)
424 eb259e66 2021-03-02 op addch((*s)[i]);
430 1d08c280 2021-03-06 op * Build a list of visual line by wrapping the given line, assuming
431 1d08c280 2021-03-06 op * that when printed will have a leading prefix prfx.
433 eb259e66 2021-03-02 op * TODO: it considers each byte one cell on the screen!
436 1d08c280 2021-03-06 op wrap_text(struct tab *tab, const char *prfx, struct line *l)
438 eb259e66 2021-03-02 op size_t zero, off, len, split;
439 1d08c280 2021-03-06 op const char *endword, *endspc, *line, *linestart;
441 1d08c280 2021-03-06 op zero = strlen(prfx);
443 1d08c280 2021-03-06 op line = l->line;
444 1d08c280 2021-03-06 op linestart = l->line;
446 1d08c280 2021-03-06 op while (word_boundaries(line, " \t-", &endword, &endspc)) {
447 eb259e66 2021-03-02 op len = endword - line;
448 48e9d457 2021-03-06 op if (off + len >= body_cols) {
449 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
450 48e9d457 2021-03-06 op while (len >= body_cols) {
451 eb259e66 2021-03-02 op /* hard wrap */
452 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
453 48e9d457 2021-03-06 op len -= body_cols-1;
454 48e9d457 2021-03-06 op line += body_cols-1;
457 eb259e66 2021-03-02 op if (len != 0)
462 eb259e66 2021-03-02 op /* print the spaces iff not at bol */
463 eb259e66 2021-03-02 op len = endspc - endword;
464 eb259e66 2021-03-02 op /* line = endspc; */
465 eb259e66 2021-03-02 op if (off != zero) {
466 48e9d457 2021-03-06 op if (off + len >= body_cols) {
467 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
468 1d08c280 2021-03-06 op linestart = endspc;
473 eb259e66 2021-03-02 op line = endspc;
476 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
480 1d08c280 2021-03-06 op hardwrap_text(struct tab *tab, struct line *l)
482 1d08c280 2021-03-06 op size_t off, len;
483 1d08c280 2021-03-06 op const char *linestart;
485 1d08c280 2021-03-06 op len = strlen(l->line);
487 1d08c280 2021-03-06 op linestart = l->line;
489 1d08c280 2021-03-06 op while (len >= COLS) {
490 1d08c280 2021-03-06 op len -= COLS-1;
491 1d08c280 2021-03-06 op off = COLS-1;
492 1d08c280 2021-03-06 op if (!emitline(tab, 0, &off, l, &linestart))
500 1d08c280 2021-03-06 op wrap_page(struct tab *tab)
502 1d08c280 2021-03-06 op struct line *l;
504 1d08c280 2021-03-06 op empty_vlist(tab);
506 5e11c00c 2021-03-02 op TAILQ_FOREACH(l, &tab->page.head, lines) {
507 5e11c00c 2021-03-02 op switch (l->type) {
508 5e11c00c 2021-03-02 op case LINE_TEXT:
509 1d08c280 2021-03-06 op wrap_text(tab, "", l);
511 5e11c00c 2021-03-02 op case LINE_LINK:
512 1d08c280 2021-03-06 op wrap_text(tab, "=> ", l);
514 5e11c00c 2021-03-02 op case LINE_TITLE_1:
515 1d08c280 2021-03-06 op wrap_text(tab, "# ", l);
517 5e11c00c 2021-03-02 op case LINE_TITLE_2:
518 1d08c280 2021-03-06 op wrap_text(tab, "## ", l);
520 5e11c00c 2021-03-02 op case LINE_TITLE_3:
521 1d08c280 2021-03-06 op wrap_text(tab, "### ", l);
523 5e11c00c 2021-03-02 op case LINE_ITEM:
524 1d08c280 2021-03-06 op wrap_text(tab, "* ", l);
526 5e11c00c 2021-03-02 op case LINE_QUOTE:
527 1d08c280 2021-03-06 op wrap_text(tab, "> ", l);
529 5e11c00c 2021-03-02 op case LINE_PRE_START:
530 5e11c00c 2021-03-02 op case LINE_PRE_END:
531 1d08c280 2021-03-06 op push_line(tab, l, NULL, 0);
533 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
534 1d08c280 2021-03-06 op hardwrap_text(tab, l);
541 1d08c280 2021-03-06 op static inline void
542 1d08c280 2021-03-06 op print_line(struct line *l)
544 1d08c280 2021-03-06 op switch (l->type) {
545 1d08c280 2021-03-06 op case LINE_TEXT:
547 1d08c280 2021-03-06 op case LINE_LINK:
548 48e9d457 2021-03-06 op wprintw(body, "=> ");
550 1d08c280 2021-03-06 op case LINE_TITLE_1:
551 48e9d457 2021-03-06 op wprintw(body, "# ");
553 1d08c280 2021-03-06 op case LINE_TITLE_2:
554 48e9d457 2021-03-06 op wprintw(body, "## ");
556 1d08c280 2021-03-06 op case LINE_TITLE_3:
557 48e9d457 2021-03-06 op wprintw(body, "### ");
559 1d08c280 2021-03-06 op case LINE_ITEM:
560 48e9d457 2021-03-06 op wprintw(body, "* ");
562 1d08c280 2021-03-06 op case LINE_QUOTE:
563 48e9d457 2021-03-06 op wprintw(body, "> ");
565 1d08c280 2021-03-06 op case LINE_PRE_START:
566 1d08c280 2021-03-06 op case LINE_PRE_END:
567 48e9d457 2021-03-06 op wprintw(body, "```");
569 1d08c280 2021-03-06 op case LINE_PRE_CONTENT:
573 1d08c280 2021-03-06 op if (l->line != NULL)
574 48e9d457 2021-03-06 op wprintw(body, "%s", l->line);
578 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
580 48e9d457 2021-03-06 op int x, y, max_x, max_y;
581 48e9d457 2021-03-06 op const char *url = "TODO:url";
582 48e9d457 2021-03-06 op const char *mode = "text/gemini-mode";
584 48e9d457 2021-03-06 op wclear(modeline);
585 48e9d457 2021-03-06 op wattron(modeline, A_REVERSE);
586 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
588 48e9d457 2021-03-06 op wprintw(modeline, "-- %s %s ", mode, url);
589 48e9d457 2021-03-06 op getyx(modeline, y, x);
590 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
595 48e9d457 2021-03-06 op for (; x < max_x; ++x)
596 48e9d457 2021-03-06 op waddstr(modeline, "-");
600 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
602 48e9d457 2021-03-06 op struct line *l;
605 48e9d457 2021-03-06 op werase(body);
607 48e9d457 2021-03-06 op tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
608 48e9d457 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
612 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off);
613 48e9d457 2021-03-06 op for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
614 48e9d457 2021-03-06 op wmove(body, line, 0);
615 1d08c280 2021-03-06 op print_line(l);
617 48e9d457 2021-03-06 op if (line == body_lines)
621 48e9d457 2021-03-06 op redraw_modeline(tab);
623 48e9d457 2021-03-06 op restore_cursor(tab);
624 48e9d457 2021-03-06 op wrefresh(tabline);
625 48e9d457 2021-03-06 op wrefresh(modeline);
626 48e9d457 2021-03-06 op wrefresh(minibuf);
628 48e9d457 2021-03-06 op wrefresh(body);
632 5e11c00c 2021-03-02 op ui_init(void)
634 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
641 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
642 5e11c00c 2021-03-02 op keypad(stdscr, TRUE);
644 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
646 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
648 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
650 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
653 48e9d457 2021-03-06 op body_lines = LINES-3;
654 48e9d457 2021-03-06 op body_cols = COLS;
656 48e9d457 2021-03-06 op scrollok(body, TRUE);
658 5e11c00c 2021-03-02 op /* non-blocking input */
659 48e9d457 2021-03-06 op wtimeout(body, 0);
661 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
663 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
664 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
666 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
667 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
673 5e11c00c 2021-03-02 op ui_on_new_tab(struct tab *tab)
675 5e11c00c 2021-03-02 op struct tab *t;
677 1d08c280 2021-03-06 op if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
680 1d08c280 2021-03-06 op TAILQ_INIT(&tab->s->head);
682 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
683 5e11c00c 2021-03-02 op t->flags &= ~TAB_CURRENT;
685 5e11c00c 2021-03-02 op tab->flags = TAB_CURRENT;
687 5e11c00c 2021-03-02 op /* TODO: redraw the tab list */
688 1d08c280 2021-03-06 op /* TODO: switch to the new tab */
690 48e9d457 2021-03-06 op wmove(body, 0, 0);
696 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
698 5e11c00c 2021-03-02 op if (!(tab->flags & TAB_CURRENT))
701 1d08c280 2021-03-06 op wrap_page(tab);
702 5e11c00c 2021-03-02 op redraw_tab(tab);