Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 /*
18 * Ncurses UI for telescope.
19 *
20 *
21 * Text wrapping
22 * =============
23 *
24 * There's a simple text wrapping algorithm.
25 *
26 * 1. if it's a line in a pre-formatted block:
27 * a. hard wrap.
28 * b. repeat
29 * 2. there is enough room for the next word?
30 * a. yes: render it
31 * b. no: break the current line.
32 * i. while there isn't enough space to draw the current
33 * word, hard-wrap it
34 * ii. draw the remainder of the current word (can be the
35 * the entirely)
36 * 3. render the spaces after the word
37 * a. but if there is not enough room break the line and
38 * forget them
39 * 4. repeat
40 *
41 *
42 * Text scrolling
43 * ==============
44 *
45 * ncurses allows you to scroll a window, but when a line goes out of
46 * the visible area it's forgotten. We keep a list of formatted lines
47 * (``visual lines'') that we know fits in the window, and draw them.
48 * This way is easy to scroll: just call wscrl and then render the
49 * first/last line!
50 *
51 * This means that on every resize we have to clear our list of lines
52 * and re-render everything. A clever approach would be to do this
53 * ``on-demand''.
54 *
55 * TODO: make the text formatting on-demand.
56 *
57 */
59 #include <telescope.h>
61 #include <ctype.h>
62 #include <curses.h>
63 #include <event.h>
64 #include <locale.h>
65 #include <signal.h>
66 #include <stdarg.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <unistd.h>
71 #define TAB_CURRENT 0x1
73 #define MIN(a, b) ((a) < (b) ? (a) : (b))
74 #define MAX(a, b) ((a) > (b) ? (a) : (b))
76 struct kmap;
78 static struct event stdioev, winchev;
80 static int kbd(const char*);
81 static void kmap_define_key(struct kmap*, const char*, void(*)(struct tab*));
82 static void load_default_keys(void);
83 static int push_line(struct tab*, const struct line*, const char*, size_t);
84 static void empty_vlist(struct tab*);
85 static void restore_cursor(struct tab *);
87 static void cmd_previous_line(struct tab*);
88 static void cmd_next_line(struct tab*);
89 static void cmd_forward_char(struct tab*);
90 static void cmd_backward_char(struct tab*);
91 static void cmd_redraw(struct tab*);
92 static void cmd_scroll_line_down(struct tab*);
93 static void cmd_scroll_line_up(struct tab*);
94 static void cmd_scroll_up(struct tab*);
95 static void cmd_scroll_down(struct tab*);
96 static void cmd_kill_telescope(struct tab*);
97 static void cmd_push_button(struct tab*);
98 static void cmd_execute_extended_command(struct tab*);
100 static void global_key_unbound(void);
102 static void cmd_mini_del(struct tab*);
103 static void cmd_mini_forward_char(struct tab*);
104 static void cmd_mini_backward_char(struct tab*);
105 static void cmd_mini_move_end_of_line(struct tab*);
106 static void cmd_mini_move_beginning_of_line(struct tab*);
107 static void cmd_mini_kill_line(struct tab*);
108 static void cmd_mini_abort();
109 static void cmd_mini_complete_and_exit();
111 static void eecmd_self_insert(void);
112 static void eecmd_select(void);
114 static struct line *nth_line(struct tab*, size_t);
115 static struct tab *current_tab(void);
116 static void dispatch_stdio(int, short, void*);
117 static void handle_clear_minibuf(int, short, void*);
118 static void handle_resize(int, short, void*);
119 static int word_bourdaries(const char*, const char*, const char**, const char**);
120 static void wrap_text(struct tab*, const char*, struct line*);
121 static int hardwrap_text(struct tab*, struct line*);
122 static int wrap_page(struct tab*);
123 static void print_line(struct line*);
124 static void redraw_tabline(void);
125 static void redraw_modeline(struct tab*);
126 static void redraw_minibuffer(void);
127 static void redraw_tab(struct tab*);
128 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
129 static void start_loading_anim(struct tab*);
130 static void update_loading_anim(int, short, void*);
131 static void stop_loading_anim(struct tab*);
132 static void load_url_in_tab(struct tab*, const char*);
133 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void));
134 static void exit_minibuffer(void);
135 static void new_tab(void);
137 static struct { int meta, key; } thiskey;
139 static WINDOW *tabline, *body, *modeline, *minibuf;
140 static int body_lines, body_cols;
142 static struct event clminibufev;
143 static int clminibufev_set;
144 static struct timeval clminibufev_timer = { 5, 0 };
145 static struct timeval loadingev_timer = { 0, 250000 };
147 static uint32_t tab_counter;
149 struct ui_state {
150 int curs_x;
151 int curs_y;
152 size_t line_off;
153 size_t line_max;
155 short loading_anim;
156 short loading_anim_step;
157 struct event loadingev;
159 TAILQ_HEAD(, line) head;
160 };
162 #define CTRL(n) ((n)&0x1F)
164 struct keytable {
165 char *p;
166 int k;
167 } keytable[] = {
168 { "<up>", KEY_UP },
169 { "<down>", KEY_DOWN },
170 { "<left>", KEY_LEFT },
171 { "<right>", KEY_RIGHT },
172 { "<prior>", KEY_PPAGE },
173 { "<next>", KEY_NPAGE },
174 { "<home>", KEY_HOME },
175 { "<end>", KEY_END },
176 /* ... */
177 { "del", KEY_BACKSPACE },
178 { "esc", 27 },
179 { "space", ' ' },
180 { "spc", ' ' },
181 { "enter", CTRL('m') },
182 { "ret", CTRL('m' )},
183 { "tab", CTRL('i') },
184 /* ... */
185 { NULL, 0 },
186 };
188 struct kmap {
189 TAILQ_HEAD(map, keymap) m;
190 void (*unhandled_input)(void);
191 };
193 struct kmap global_map,
194 minibuffer_map,
195 *current_map,
196 *base_map;
198 struct keymap {
199 int meta;
200 int key;
201 struct kmap map;
202 void (*fn)(struct tab*);
204 TAILQ_ENTRY(keymap) keymaps;
205 };
207 static int in_minibuffer;
209 static struct {
210 char *curmesg;
212 char buf[1025];
213 size_t off, len;
214 char prompt[16];
215 void (*donefn)(void);
216 void (*abortfn)(void);
217 } ministate;
219 static int
220 kbd(const char *key)
222 struct keytable *t;
224 for (t = keytable; t->p != NULL; ++t) {
225 if (has_prefix(key, t->p))
226 return t->k;
229 return *key;
232 static const char *
233 unkbd(int k)
235 struct keytable *t;
237 for (t = keytable; t->p != NULL; ++t) {
238 if (k == t->k)
239 return t->p;
242 return NULL;
245 static void
246 kmap_define_key(struct kmap *map, const char *key, void (*fn)(struct tab*))
248 int ctrl, meta, k;
249 struct keymap *entry;
251 again:
252 if ((ctrl = has_prefix(key, "C-")))
253 key += 2;
254 if ((meta = has_prefix(key, "M-")))
255 key += 2;
256 if (*key == '\0')
257 _exit(1);
258 k = kbd(key);
260 if (ctrl)
261 k = CTRL(k);
263 /* skip key & spaces */
264 while (*key != '\0' && !isspace(*key))
265 ++key;
266 while (*key != '\0' && isspace(*key))
267 ++key;
269 TAILQ_FOREACH(entry, &map->m, keymaps) {
270 if (entry->meta == meta && entry->key == k) {
271 if (*key == '\0') {
272 entry->fn = fn;
273 return;
275 map = &entry->map;
276 goto again;
280 if ((entry = calloc(1, sizeof(*entry))) == NULL)
281 abort();
283 entry->meta = meta;
284 entry->key = k;
285 TAILQ_INIT(&entry->map.m);
287 if (TAILQ_EMPTY(&map->m))
288 TAILQ_INSERT_HEAD(&map->m, entry, keymaps);
289 else
290 TAILQ_INSERT_TAIL(&map->m, entry, keymaps);
292 if (*key != '\0') {
293 map = &entry->map;
294 goto again;
297 entry->fn = fn;
300 static inline void
301 global_set_key(const char *key, void (*fn)(struct tab*))
303 kmap_define_key(&global_map, key, fn);
306 static inline void
307 minibuffer_set_key(const char *key, void (*fn)(struct tab*))
309 kmap_define_key(&minibuffer_map, key, fn);
312 static void
313 load_default_keys(void)
315 /* === global map === */
317 /* emacs */
318 global_set_key("C-p", cmd_previous_line);
319 global_set_key("C-n", cmd_next_line);
320 global_set_key("C-f", cmd_forward_char);
321 global_set_key("C-b", cmd_backward_char);
323 global_set_key("M-v", cmd_scroll_up);
324 global_set_key("C-v", cmd_scroll_down);
326 global_set_key("C-x C-c", cmd_kill_telescope);
328 global_set_key("M-x", cmd_execute_extended_command);
330 /* vi/vi-like */
331 global_set_key("k", cmd_previous_line);
332 global_set_key("j", cmd_next_line);
333 global_set_key("l", cmd_forward_char);
334 global_set_key("h", cmd_backward_char);
336 global_set_key("K", cmd_scroll_line_up);
337 global_set_key("J", cmd_scroll_line_down);
339 /* tmp */
340 global_set_key("q", cmd_kill_telescope);
342 global_set_key(":", cmd_execute_extended_command);
344 /* cua */
345 global_set_key("<up>", cmd_previous_line);
346 global_set_key("<down>", cmd_next_line);
347 global_set_key("<right>", cmd_forward_char);
348 global_set_key("<left>", cmd_backward_char);
349 global_set_key("<prior>", cmd_scroll_up);
350 global_set_key("<next>", cmd_scroll_down);
352 /* "ncurses standard" */
353 global_set_key("C-l", cmd_redraw);
355 /* global */
356 global_set_key("C-m", cmd_push_button);
358 /* === minibuffer map === */
359 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
360 minibuffer_set_key("C-g", cmd_mini_abort);
361 minibuffer_set_key("esc", cmd_mini_abort);
362 minibuffer_set_key("del", cmd_mini_del);
364 minibuffer_set_key("C-f", cmd_mini_forward_char);
365 minibuffer_set_key("C-b", cmd_mini_backward_char);
366 minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
367 minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
368 minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
369 minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
370 minibuffer_set_key("C-k", cmd_mini_kill_line);
373 static int
374 push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
376 struct line *vl;
378 tab->s->line_max++;
380 if ((vl = calloc(1, sizeof(*vl))) == NULL)
381 return 0;
383 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
384 free(vl);
385 return 0;
388 vl->type = l->type;
389 if (len != 0)
390 memcpy(vl->line, buf, len);
391 vl->alt = l->alt;
393 if (TAILQ_EMPTY(&tab->s->head))
394 TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
395 else
396 TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
397 return 1;
400 static void
401 empty_vlist(struct tab *tab)
403 struct line *l, *t;
405 tab->s->line_max = 0;
407 TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
408 TAILQ_REMOVE(&tab->s->head, l, lines);
409 free(l->line);
410 /* l->alt references the original line! */
411 free(l);
415 static void
416 restore_cursor(struct tab *tab)
418 wmove(body, tab->s->curs_y, tab->s->curs_x);
421 static void
422 cmd_previous_line(struct tab *tab)
424 if (--tab->s->curs_y < 0) {
425 tab->s->curs_y = 0;
426 cmd_scroll_line_up(tab);
429 restore_cursor(tab);
432 static void
433 cmd_next_line(struct tab *tab)
435 if (++tab->s->curs_y > body_lines-1) {
436 tab->s->curs_y = body_lines-1;
437 cmd_scroll_line_down(tab);
440 restore_cursor(tab);
443 static void
444 cmd_forward_char(struct tab *tab)
446 tab->s->curs_x = MIN(body_cols-1, tab->s->curs_x+1);
447 restore_cursor(tab);
450 static void
451 cmd_backward_char(struct tab *tab)
453 tab->s->curs_x = MAX(0, tab->s->curs_x-1);
454 restore_cursor(tab);
457 static void
458 cmd_redraw(struct tab *tab)
460 handle_resize(0, 0, NULL);
463 static void
464 cmd_scroll_line_up(struct tab *tab)
466 struct line *l;
468 if (tab->s->line_off == 0)
469 return;
471 l = nth_line(tab, --tab->s->line_off);
472 wscrl(body, -1);
473 wmove(body, 0, 0);
474 print_line(l);
477 static void
478 cmd_scroll_line_down(struct tab *tab)
480 struct line *l;
481 size_t n;
483 if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
484 return;
486 tab->s->line_off++;
487 wscrl(body, 1);
489 if (tab->s->line_max - tab->s->line_off < body_lines)
490 return;
492 l = nth_line(tab, tab->s->line_off + body_lines-1);
493 wmove(body, body_lines-1, 0);
494 print_line(l);
497 static void
498 cmd_scroll_up(struct tab *tab)
500 size_t off;
502 off = body_lines+1;
504 for (; off > 0; --off)
505 cmd_scroll_line_up(tab);
508 static void
509 cmd_scroll_down(struct tab *tab)
511 ssize_t off;
513 off = tab->s->line_off + body_lines;
514 off = MIN(tab->s->line_max, off);
516 for (; off >= 0; --off)
517 cmd_scroll_line_down(tab);
520 static void
521 cmd_kill_telescope(struct tab *tab)
523 event_loopbreak();
526 static void
527 cmd_push_button(struct tab *tab)
529 struct line *l;
530 size_t nth;
532 nth = tab->s->line_off + tab->s->curs_y;
533 if (nth > tab->s->line_max)
534 return;
535 l = nth_line(tab, nth);
536 if (l->type != LINE_LINK)
537 return;
539 load_url_in_tab(tab, l->alt);
542 static void
543 cmd_execute_extended_command(struct tab *tab)
545 size_t len;
547 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer);
549 len = sizeof(ministate.prompt);
550 strlcpy(ministate.prompt, "", len);
552 if (thiskey.meta)
553 strlcat(ministate.prompt, "M-", len);
555 strlcat(ministate.prompt, keyname(thiskey.key), len);
556 strlcat(ministate.prompt, " ", len);
559 static void
560 global_key_unbound(void)
562 const char *keyname;
563 char tmp[2] = {0, 0};
565 if ((keyname = unkbd(thiskey.key)) == NULL) {
566 message("%s%c is undefined",
567 thiskey.meta ? "M-" : "",
568 thiskey.key);
569 return;
572 message("%s%s is undefined",
573 thiskey.meta ? "M-" : "",
574 keyname);
577 static void
578 cmd_mini_del(struct tab *tab)
580 if (ministate.len == 0 || ministate.off == 0)
581 return;
583 memmove(&ministate.buf[ministate.off-1],
584 &ministate.buf[ministate.off],
585 ministate.len - ministate.off + 1);
586 ministate.off--;
587 ministate.len--;
590 static void
591 cmd_mini_forward_char(struct tab *tab)
593 if (ministate.off == ministate.len)
594 return;
595 ministate.off++;
598 static void
599 cmd_mini_backward_char(struct tab *tab)
601 if (ministate.off == 0)
602 return;
603 ministate.off--;
606 static void
607 cmd_mini_move_end_of_line(struct tab *tab)
609 ministate.off = ministate.len;
612 static void
613 cmd_mini_move_beginning_of_line(struct tab *tab)
615 ministate.off = 0;
618 static void
619 cmd_mini_kill_line(struct tab *tab)
621 if (ministate.off == ministate.len)
622 return;
623 ministate.buf[ministate.off] = '\0';
624 ministate.len -= ministate.off;
627 static void
628 cmd_mini_abort(struct tab *tab)
630 ministate.abortfn();
633 static void
634 cmd_mini_complete_and_exit(struct tab *tab)
636 ministate.donefn();
639 static void
640 eecmd_self_insert(void)
642 if (thiskey.meta || isspace(thiskey.key) ||
643 !isgraph(thiskey.key)) {
644 global_key_unbound();
645 return;
648 if (ministate.len == sizeof(ministate.buf) -1)
649 return;
651 /* TODO: utf8 handling! */
653 memmove(&ministate.buf[ministate.off+1],
654 &ministate.buf[ministate.off],
655 ministate.len - ministate.off + 1);
656 ministate.buf[ministate.off] = thiskey.key;
657 ministate.off++;
658 ministate.len++;
661 static void
662 eecmd_select(void)
664 exit_minibuffer();
665 message("TODO: try to execute %s", ministate.buf);
668 static struct line *
669 nth_line(struct tab *tab, size_t n)
671 struct line *l;
672 size_t i;
674 i = 0;
675 TAILQ_FOREACH(l, &tab->s->head, lines) {
676 if (i == n)
677 return l;
678 i++;
681 /* unreachable */
682 abort();
685 static struct tab *
686 current_tab(void)
688 struct tab *t;
690 TAILQ_FOREACH(t, &tabshead, tabs) {
691 if (t->flags & TAB_CURRENT)
692 return t;
695 /* unreachable */
696 abort();
699 static void
700 dispatch_stdio(int fd, short ev, void *d)
702 struct keymap *k;
704 thiskey.key = wgetch(body);
705 if (thiskey.key == ERR)
706 return;
707 if (thiskey.key == 27) {
708 /* TODO: make escape-time customizable */
710 thiskey.meta = 1;
711 thiskey.key = wgetch(body);
712 if (thiskey.key == ERR)
713 thiskey.key = 27;
714 } else
715 thiskey.meta = 0;
717 TAILQ_FOREACH(k, &current_map->m, keymaps) {
718 if (k->meta == thiskey.meta &&
719 k->key == thiskey.key) {
720 if (k->fn == NULL)
721 current_map = &k->map;
722 else {
723 current_map = base_map;
724 k->fn(current_tab());
726 goto done;
730 current_map->unhandled_input();
731 current_map = base_map;
733 done:
734 redraw_minibuffer();
735 restore_cursor(current_tab());
736 wrefresh(tabline);
737 wrefresh(modeline);
739 if (in_minibuffer) {
740 wrefresh(body);
741 wrefresh(minibuf);
742 } else {
743 wrefresh(minibuf);
744 wrefresh(body);
748 static void
749 handle_clear_minibuf(int fd, short ev, void *d)
751 clminibufev_set = 0;
753 free(ministate.curmesg);
754 ministate.curmesg = NULL;
756 redraw_minibuffer();
757 if (in_minibuffer) {
758 wrefresh(body);
759 wrefresh(minibuf);
760 } else {
761 wrefresh(minibuf);
762 wrefresh(body);
766 static void
767 handle_resize(int sig, short ev, void *d)
769 struct tab *tab;
771 endwin();
772 refresh();
773 clear();
775 /* move and resize the windows, in reverse order! */
777 mvwin(minibuf, LINES-1, 0);
778 wresize(minibuf, 1, COLS);
780 mvwin(modeline, LINES-2, 0);
781 wresize(modeline, 1, COLS);
783 wresize(body, LINES-3, COLS);
784 body_lines = LINES-3;
785 body_cols = COLS;
787 wresize(tabline, 1, COLS);
789 tab = current_tab();
791 wrap_page(tab);
792 redraw_tab(tab);
795 /*
796 * Helper function for wrap_text. Find the end of the current word
797 * and the end of the separator after the word.
798 */
799 static int
800 word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
802 *endword = s;
803 *endword = s;
805 if (*s == '\0')
806 return 0;
808 /* find the end of the current world */
809 for (; *s != '\0'; ++s) {
810 if (strchr(sep, *s) != NULL)
811 break;
814 *endword = s;
816 /* find the end of the separator */
817 for (; *s != '\0'; ++s) {
818 if (strchr(sep, *s) == NULL)
819 break;
822 *endspc = s;
824 return 1;
827 static inline int
828 emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
829 const char **line)
831 if (!push_line(tab, l, *line, *off - zero))
832 return 0;
833 *line += *off - zero;
834 *off = zero;
835 return 1;
838 static inline void
839 emitstr(const char **s, size_t len, size_t *off)
841 size_t i;
843 /* printw("%*s", ...) doesn't seem to respect the precision, so... */
844 for (i = 0; i < len; ++i)
845 addch((*s)[i]);
846 *off += len;
847 *s += len;
850 /*
851 * Build a list of visual line by wrapping the given line, assuming
852 * that when printed will have a leading prefix prfx.
854 * TODO: it considers each byte one cell on the screen!
855 */
856 static void
857 wrap_text(struct tab *tab, const char *prfx, struct line *l)
859 size_t zero, off, len, split;
860 const char *endword, *endspc, *line, *linestart;
862 zero = strlen(prfx);
863 off = zero;
864 line = l->line;
865 linestart = l->line;
867 while (word_boundaries(line, " \t-", &endword, &endspc)) {
868 len = endword - line;
869 if (off + len >= body_cols) {
870 emitline(tab, zero, &off, l, &linestart);
871 while (len >= body_cols) {
872 /* hard wrap */
873 emitline(tab, zero, &off, l, &linestart);
874 len -= body_cols-1;
875 line += body_cols-1;
878 if (len != 0)
879 off += len;
880 } else
881 off += len;
883 /* print the spaces iff not at bol */
884 len = endspc - endword;
885 /* line = endspc; */
886 if (off != zero) {
887 if (off + len >= body_cols) {
888 emitline(tab, zero, &off, l, &linestart);
889 linestart = endspc;
890 } else
891 off += len;
894 line = endspc;
897 emitline(tab, zero, &off, l, &linestart);
900 static int
901 hardwrap_text(struct tab *tab, struct line *l)
903 size_t off, len;
904 const char *linestart;
906 len = strlen(l->line);
907 off = 0;
908 linestart = l->line;
910 while (len >= COLS) {
911 len -= COLS-1;
912 off = COLS-1;
913 if (!emitline(tab, 0, &off, l, &linestart))
914 return 0;
917 if (len != 0)
918 return emitline(tab, 0, &len, l, &linestart);
920 return 1;
923 static int
924 wrap_page(struct tab *tab)
926 struct line *l;
928 empty_vlist(tab);
930 TAILQ_FOREACH(l, &tab->page.head, lines) {
931 switch (l->type) {
932 case LINE_TEXT:
933 wrap_text(tab, "", l);
934 break;
935 case LINE_LINK:
936 wrap_text(tab, "=> ", l);
937 break;
938 case LINE_TITLE_1:
939 wrap_text(tab, "# ", l);
940 break;
941 case LINE_TITLE_2:
942 wrap_text(tab, "## ", l);
943 break;
944 case LINE_TITLE_3:
945 wrap_text(tab, "### ", l);
946 break;
947 case LINE_ITEM:
948 wrap_text(tab, "* ", l);
949 break;
950 case LINE_QUOTE:
951 wrap_text(tab, "> ", l);
952 break;
953 case LINE_PRE_START:
954 case LINE_PRE_END:
955 push_line(tab, l, NULL, 0);
956 break;
957 case LINE_PRE_CONTENT:
958 hardwrap_text(tab, l);
959 break;
962 return 1;
965 static inline void
966 print_line(struct line *l)
968 const char *text = l->line;
970 if (text == NULL)
971 text = "";
973 switch (l->type) {
974 case LINE_TEXT:
975 wprintw(body, "%s", text);
976 break;
977 case LINE_LINK:
978 wattron(body, A_UNDERLINE);
979 wprintw(body, "=> %s", text);
980 wattroff(body, A_UNDERLINE);
981 return;
982 case LINE_TITLE_1:
983 wattron(body, A_BOLD);
984 wprintw(body, "# %s", text);
985 wattroff(body, A_BOLD);
986 return;
987 case LINE_TITLE_2:
988 wattron(body, A_BOLD);
989 wprintw(body, "## %s", text);
990 wattroff(body, A_BOLD);
991 return;
992 case LINE_TITLE_3:
993 wattron(body, A_BOLD);
994 wprintw(body, "### %s", text);
995 wattroff(body, A_BOLD);
996 return;
997 case LINE_ITEM:
998 wprintw(body, "* %s", text);
999 return;
1000 case LINE_QUOTE:
1001 wattron(body, A_DIM);
1002 wprintw(body, "> %s", text);
1003 wattroff(body, A_DIM);
1004 return;
1005 case LINE_PRE_START:
1006 case LINE_PRE_END:
1007 wprintw(body, "```");
1008 return;
1009 case LINE_PRE_CONTENT:
1010 wprintw(body, "%s", text);
1011 return;
1015 static void
1016 redraw_tabline(void)
1018 wclear(tabline);
1019 wbkgd(tabline, A_REVERSE);
1020 mvwprintw(tabline, 0, 0, "TODO: tabs here");
1023 static void
1024 redraw_modeline(struct tab *tab)
1026 int x, y, max_x, max_y;
1027 const char *mode = "text/gemini-mode";
1028 const char *spin = "-\\|/";
1030 wclear(modeline);
1031 wattron(modeline, A_REVERSE);
1032 wmove(modeline, 0, 0);
1034 wprintw(modeline, "-%c %s %s ",
1035 spin[tab->s->loading_anim_step], mode, tab->urlstr);
1036 getyx(modeline, y, x);
1037 getmaxyx(modeline, max_y, max_x);
1039 (void)y;
1040 (void)max_y;
1042 for (; x < max_x; ++x)
1043 waddstr(modeline, "-");
1046 static void
1047 redraw_minibuffer(void)
1049 size_t skip = 0, off = 0;
1051 wclear(minibuf);
1052 if (!in_minibuffer)
1053 goto message;
1055 off = strlen(ministate.prompt);
1057 while (ministate.off - skip > COLS / 2) {
1058 skip += MIN(ministate.off/4, 1);
1061 mvwprintw(minibuf, 0, 0, "%s%s", ministate.prompt,
1062 ministate.buf + skip);
1064 message:
1065 if (ministate.curmesg != NULL) {
1066 if (in_minibuffer)
1067 wprintw(minibuf, " [%s]", ministate.curmesg);
1068 else
1069 wprintw(minibuf, "%s", ministate.curmesg);
1072 wmove(minibuf, 0, off + ministate.off - skip);
1075 static void
1076 redraw_tab(struct tab *tab)
1078 struct line *l;
1079 int line;
1081 werase(body);
1083 tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
1084 if (TAILQ_EMPTY(&tab->s->head))
1085 return;
1087 line = 0;
1088 l = nth_line(tab, tab->s->line_off);
1089 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
1090 wmove(body, line, 0);
1091 print_line(l);
1092 line++;
1093 if (line == body_lines)
1094 break;
1097 redraw_tabline();
1098 redraw_modeline(tab);
1099 redraw_minibuffer();
1101 restore_cursor(tab);
1102 wrefresh(tabline);
1103 wrefresh(modeline);
1105 if (in_minibuffer) {
1106 wrefresh(body);
1107 wrefresh(minibuf);
1108 } else {
1109 wrefresh(minibuf);
1110 wrefresh(body);
1114 static void
1115 message(const char *fmt, ...)
1117 va_list ap;
1119 if (clminibufev_set)
1120 evtimer_del(&clminibufev);
1121 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1122 evtimer_add(&clminibufev, &clminibufev_timer);
1123 clminibufev_set = 1;
1125 va_start(ap, fmt);
1126 /* TODO: what to do if the allocation fails here? */
1127 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1128 ministate.curmesg = NULL;
1129 va_end(ap);
1131 redraw_minibuffer();
1133 if (in_minibuffer) {
1134 wrefresh(body);
1135 wrefresh(minibuf);
1136 } else {
1137 wrefresh(minibuf);
1138 wrefresh(body);
1142 static void
1143 start_loading_anim(struct tab *tab)
1145 if (tab->s->loading_anim)
1146 return;
1147 tab->s->loading_anim = 1;
1148 evtimer_set(&tab->s->loadingev, update_loading_anim, tab);
1149 evtimer_add(&tab->s->loadingev, &loadingev_timer);
1152 static void
1153 update_loading_anim(int fd, short ev, void *d)
1155 struct tab *tab = d;
1157 tab->s->loading_anim_step = (tab->s->loading_anim_step+1)%4;
1159 redraw_modeline(tab);
1160 wrefresh(modeline);
1162 wrefresh(body);
1163 if (in_minibuffer)
1164 wrefresh(minibuf);
1166 evtimer_add(&tab->s->loadingev, &loadingev_timer);
1169 static void
1170 stop_loading_anim(struct tab *tab)
1172 if (!tab->s->loading_anim)
1173 return;
1174 evtimer_del(&tab->s->loadingev);
1175 tab->s->loading_anim = 0;
1176 tab->s->loading_anim_step = 0;
1178 redraw_modeline(tab);
1180 wrefresh(modeline);
1181 wrefresh(body);
1182 if (in_minibuffer)
1183 wrefresh(minibuf);
1186 static void
1187 load_url_in_tab(struct tab *tab, const char *url)
1189 empty_vlist(tab);
1190 message("Loading %s...", url);
1191 start_loading_anim(tab);
1192 load_url(tab, url);
1194 tab->s->curs_x = 0;
1195 tab->s->curs_y = 0;
1196 redraw_tab(tab);
1199 static void
1200 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1201 void (*abortfn)(void))
1203 in_minibuffer = 1;
1204 base_map = &minibuffer_map;
1205 current_map = &minibuffer_map;
1207 base_map->unhandled_input = self_insert_fn;
1209 ministate.donefn = donefn;
1210 ministate.abortfn = abortfn;
1211 memset(ministate.buf, 0, sizeof(ministate.buf));
1212 ministate.off = 0;
1213 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1216 static void
1217 exit_minibuffer(void)
1219 wclear(minibuf);
1221 in_minibuffer = 0;
1222 base_map = &global_map;
1223 current_map = &global_map;
1226 static void
1227 new_tab(void)
1229 struct tab *tab, *t;
1230 const char *url = "about:new";
1232 if ((tab = calloc(1, sizeof(*tab))) == NULL)
1233 goto err;
1235 if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
1236 goto err;
1238 TAILQ_INIT(&tab->s->head);
1239 TAILQ_FOREACH(t, &tabshead, tabs) {
1240 t->flags &= ~TAB_CURRENT;
1243 tab->id = tab_counter++;
1244 tab->flags = TAB_CURRENT;
1246 if (TAILQ_EMPTY(&tabshead))
1247 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1248 else
1249 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1251 load_url_in_tab(tab, url);
1252 return;
1254 err:
1255 event_loopbreak();
1258 int
1259 ui_init(void)
1261 setlocale(LC_ALL, "");
1263 TAILQ_INIT(&global_map.m);
1264 global_map.unhandled_input = global_key_unbound;
1266 TAILQ_INIT(&minibuffer_map.m);
1268 base_map = &global_map;
1269 current_map = &global_map;
1270 load_default_keys();
1272 initscr();
1273 raw();
1274 noecho();
1276 nonl();
1277 intrflush(stdscr, FALSE);
1279 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1280 return 0;
1281 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1282 return 0;
1283 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1284 return 0;
1285 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1286 return 0;
1288 body_lines = LINES-3;
1289 body_cols = COLS;
1291 keypad(body, TRUE);
1292 scrollok(body, TRUE);
1294 /* non-blocking input */
1295 wtimeout(body, 0);
1297 mvwprintw(body, 0, 0, "");
1299 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1300 event_add(&stdioev, NULL);
1302 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1303 signal_add(&winchev, NULL);
1305 new_tab();
1307 return 1;
1310 void
1311 ui_on_tab_loaded(struct tab *tab)
1313 stop_loading_anim(tab);
1314 message("Loaded %s", tab->urlstr);
1317 void
1318 ui_on_tab_refresh(struct tab *tab)
1320 if (!(tab->flags & TAB_CURRENT))
1321 return;
1323 wrap_page(tab);
1324 redraw_tab(tab);
1327 void
1328 ui_end(void)
1330 endwin();