2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
18 * Ncurses UI for telescope.
23 * ncurses allows you to scroll a window, but when a line goes out of
24 * the visible area it's forgotten. We keep a list of formatted lines
25 * (``visual lines'') that we know fits in the window, and draw them.
27 * This means that on every resize we have to clear our list of lines
28 * and re-render everything. A clever approach would be to do this
29 * ``on-demand'', but it's still missing.
44 #include "minibuffer.h"
45 #include "telescope.h"
49 static struct event stdioev, winchev;
51 static void restore_curs_x(struct buffer *);
53 static struct vline *nth_line(struct buffer*, size_t);
54 static int readkey(void);
55 static void dispatch_stdio(int, short, void*);
56 static void handle_clear_echoarea(int, short, void*);
57 static void handle_resize(int, short, void*);
58 static void handle_resize_nodelay(int, short, void*);
59 static void rearrange_windows(void);
60 static int wrap_page(struct buffer*, int);
61 static void line_prefix_and_text(struct vline *, char *, size_t, const char **, const char **);
62 static void print_vline(int, int, WINDOW*, struct vline*);
63 static void redraw_tabline(void);
64 static void redraw_window(WINDOW*, int, int, int, struct buffer*);
65 static void redraw_help(void);
66 static void redraw_body(struct tab*);
67 static void redraw_modeline(struct tab*);
68 static void redraw_minibuffer(void);
69 static void do_redraw_echoarea(void);
70 static void do_redraw_minibuffer(void);
71 static void do_redraw_minibuffer_compl(void);
72 static void place_cursor(int);
73 static void redraw_tab(struct tab*);
74 static void emit_help_item(char*, void*);
75 static void rec_compute_help(struct kmap*, char*, size_t);
76 static void recompute_help(void);
77 static void update_loading_anim(int, short, void*);
78 static void stop_loading_anim(struct tab*);
81 * Used to know when we're finished loading.
85 static int should_rearrange_windows;
89 struct thiskey thiskey;
90 struct tab *current_tab;
92 static struct event resizeev;
93 static struct timeval resize_timer = { 0, 250000 };
95 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
97 int body_lines, body_cols;
100 static struct buffer helpwin;
101 static int help_lines, help_cols;
103 static int side_window;
105 static struct event clechoev;
106 static struct timeval clechoev_timer = { 5, 0 };
107 static struct timeval loadingev_timer = { 0, 250000 };
109 static uint32_t tab_counter;
111 static char keybuf[64];
113 struct kmap global_map,
121 update_x_offset(void)
123 if (olivetti_mode && fill_column < body_cols)
124 x_offset = (body_cols - fill_column)/2;
130 save_excursion(struct excursion *place, struct buffer *buffer)
132 place->curs_x = buffer->curs_x;
133 place->curs_y = buffer->curs_y;
134 place->line_off = buffer->line_off;
135 place->current_line = buffer->current_line;
136 place->cpoff = buffer->cpoff;
140 restore_excursion(struct excursion *place, struct buffer *buffer)
142 buffer->curs_x = place->curs_x;
143 buffer->curs_y = place->curs_y;
144 buffer->line_off = place->line_off;
145 buffer->current_line = place->current_line;
146 buffer->cpoff = place->cpoff;
150 restore_curs_x(struct buffer *buffer)
155 vl = buffer->current_line;
156 if (vl == NULL || vl->line == NULL)
157 buffer->curs_x = buffer->cpoff = 0;
158 else if (vl->parent->data != NULL)
159 buffer->curs_x = utf8_snwidth(vl->parent->data+1, buffer->cpoff) + 1;
161 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
163 buffer->curs_x += x_offset;
168 if (vl->parent->data != NULL)
169 buffer->curs_x += utf8_swidth_between(vl->parent->line, vl->parent->data);
171 prfx = line_prefixes[vl->parent->type].prfx1;
172 buffer->curs_x += utf8_swidth(prfx);
177 global_key_unbound(void)
179 message("%s is undefined", keybuf);
186 return &ministate.buffer;
187 return ¤t_tab->buffer;
195 if ((thiskey.key = wgetch(body)) == ERR)
198 thiskey.meta = thiskey.key == 27;
200 thiskey.key = wgetch(body);
201 if (thiskey.key == ERR || thiskey.key == 27) {
208 if ((unsigned int)thiskey.key < UINT8_MAX) {
210 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
212 if ((thiskey.key = wgetch(body)) == ERR) {
213 message("Error decoding user input");
223 dispatch_stdio(int fd, short ev, void *d)
229 /* TODO: schedule a redraw? */
236 if (keybuf[0] != '\0')
237 strlcat(keybuf, " ", sizeof(keybuf));
239 strlcat(keybuf, "M-", sizeof(keybuf));
240 if (thiskey.cp != 0) {
241 utf8_encode(thiskey.cp, tmp);
242 strlcat(keybuf, tmp, sizeof(keybuf));
243 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
244 strlcat(keybuf, keyname, sizeof(keybuf));
246 tmp[0] = thiskey.key;
247 strlcat(keybuf, tmp, sizeof(keybuf));
250 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
251 if (k->meta == thiskey.meta &&
252 k->key == thiskey.key) {
254 current_map = &k->map;
256 current_map = base_map;
257 strlcpy(keybuf, "", sizeof(keybuf));
258 k->fn(current_buffer());
264 if (current_map->unhandled_input != NULL)
265 current_map->unhandled_input();
267 global_key_unbound();
269 strlcpy(keybuf, "", sizeof(keybuf));
270 current_map = base_map;
276 if (should_rearrange_windows)
278 redraw_tab(current_tab);
282 handle_clear_echoarea(int fd, short ev, void *d)
284 free(ministate.curmesg);
285 ministate.curmesg = NULL;
292 handle_resize(int sig, short ev, void *d)
294 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
295 event_del(&resizeev);
297 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
298 evtimer_add(&resizeev, &resize_timer);
302 handle_resize_nodelay(int s, short ev, void *d)
312 rearrange_windows(void)
316 should_rearrange_windows = 0;
320 if ((too_small = lines < 15)) {
322 printw("Window too small.");
327 /* move and resize the windows, in reverse order! */
329 if (in_minibuffer == MB_COMPREAD) {
330 mvwin(minibuffer, lines-10, 0);
331 wresize(minibuffer, 10, COLS);
334 wrap_page(&ministate.compl.buffer, COLS);
337 mvwin(echoarea, --lines, 0);
338 wresize(echoarea, 1, COLS);
340 mvwin(modeline, --lines, 0);
341 wresize(modeline, 1, COLS);
343 body_lines = --lines;
347 help_cols = 0.3 * COLS;
350 wresize(help, help_lines, help_cols);
352 wrap_page(&helpwin, help_cols);
354 body_cols = COLS - help_cols - 1;
355 mvwin(body, 1, help_cols);
360 wresize(body, body_lines, body_cols);
362 wresize(tabline, 1, COLS);
364 wrap_page(¤t_tab->buffer, body_cols);
365 redraw_tab(current_tab);
369 wrap_page(struct buffer *buffer, int width)
372 const struct line *top_orig, *orig;
377 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
378 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
380 buffer->top_line = NULL;
381 buffer->current_line = NULL;
383 buffer->force_redraw = 1;
385 buffer->line_off = 0;
389 TAILQ_FOREACH(l, &buffer->page.head, lines) {
390 prfx = line_prefixes[l->type].prfx1;
401 wrap_text(buffer, prfx, l, MIN(fill_column, width));
403 case LINE_PRE_CONTENT:
405 pre_width = MIN(fill_column, width);
408 hardwrap_text(buffer, l, pre_width);
411 case LINE_COMPL_CURRENT:
412 wrap_one(buffer, prfx, l, width);
416 if (top_orig == l && buffer->top_line == NULL) {
417 buffer->line_off = buffer->line_max-1;
418 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
421 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
422 if (vl == NULL || vl->parent != orig)
424 buffer->top_line = vl;
429 if (orig == l && buffer->current_line == NULL) {
430 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
433 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
434 if (vl == NULL || vl->parent != orig)
436 buffer->current_line = vl;
441 if (buffer->current_line == NULL)
442 buffer->current_line = TAILQ_FIRST(&buffer->head);
444 if (buffer->top_line == NULL)
445 buffer->top_line = buffer->current_line;
451 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
452 const char **prfx_ret, const char **text_ret)
454 int type, i, cont, width;
457 if ((*text_ret = vl->line) == NULL)
460 cont = vl->flags & L_CONTINUATION;
461 type = vl->parent->type;
463 *prfx_ret = line_prefixes[type].prfx1;
465 *prfx_ret = line_prefixes[type].prfx2;
467 space = vl->parent->data;
468 if (!emojify_link || type != LINE_LINK || space == NULL)
473 width = utf8_swidth_between(vl->parent->line, space);
474 for (i = 0; i < width + 1; ++i)
475 strlcat(buf, " ", len);
477 strlcpy(buf, *text_ret, len);
478 if ((t = strchr(buf, ' ')) != NULL)
480 strlcat(buf, " ", len);
483 *text_ret += (space - vl->parent->line) + 1;
490 * Core part of the rendering. It prints a vline starting from the
491 * current cursor position. Printing a vline consists of skipping
492 * `off' columns (for olivetti-mode), print the correct prefix (which
493 * may be the emoji in case of emojified links-lines), printing the
494 * text itself, filling until width - off and filling off columns
498 print_vline(int off, int width, WINDOW *window, struct vline *vl)
501 * Believe me or not, I've seen emoji ten code points long!
502 * That means, to stay large, 4*10 bytes + NUL.
504 char emojibuf[41] = {0};
505 const char *text, *prfx;
509 f = &line_faces[vl->parent->type];
511 /* unused, set by getyx */
514 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
516 wattr_on(window, body_face.left, NULL);
517 for (i = 0; i < off; i++)
519 wattr_off(window, body_face.left, NULL);
521 wattr_on(window, f->prefix, NULL);
522 wprintw(window, "%s", prfx);
523 wattr_off(window, f->prefix, NULL);
525 wattr_on(window, f->text, NULL);
526 wprintw(window, "%s", text);
527 wattr_off(window, f->text, NULL);
533 wattr_on(window, f->trail, NULL);
534 for (i = 0; i < left - off; ++i)
536 wattr_off(window, f->trail, NULL);
538 wattr_on(window, body_face.right, NULL);
539 for (i = 0; i < off; i++)
541 wattr_off(window, body_face.right, NULL);
549 size_t toskip, ots, tabwidth, space, x;
550 int current, y, truncated, pair;
556 /* unused, but setted by a getyx */
559 tabwidth = sizeof(buf)+1;
563 TAILQ_FOREACH(tab, &tabshead, tabs) {
565 if (tab == current_tab)
569 if (toskip * tabwidth < space)
574 while (toskip != 0 &&
575 (ots - toskip+1) * tabwidth < space)
580 wattr_on(tabline, tab_face.background, NULL);
581 wprintw(tabline, toskip == 0 ? " " : "<");
582 wattr_off(tabline, tab_face.background, NULL);
585 TAILQ_FOREACH(tab, &tabshead, tabs) {
593 getyx(tabline, y, x);
594 if (x + sizeof(buf)+2 >= (size_t)COLS)
597 current = tab == current_tab;
599 if (*(title = tab->buffer.page.title) == '\0')
600 title = tab->hist_cur->h;
602 if (tab->flags & TAB_URGENT)
603 strlcpy(buf, "!", sizeof(buf));
605 strlcpy(buf, " ", sizeof(buf));
607 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
608 /* truncation happens */
609 strlcpy(&buf[sizeof(buf)-4], "...", 4);
611 /* pad with spaces */
612 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
616 pair = current ? tab_face.current : tab_face.tab;
617 wattr_on(tabline, pair, NULL);
618 wprintw(tabline, "%s", buf);
619 wattr_off(tabline, pair, NULL);
621 wattr_on(tabline, tab_face.background, NULL);
622 if (TAILQ_NEXT(tab, tabs) != NULL)
623 wprintw(tabline, "│");
624 wattr_off(tabline, tab_face.background, NULL);
627 wattr_on(tabline, tab_face.background, NULL);
628 for (; x < (size_t)COLS; ++x)
629 waddch(tabline, ' ');
631 mvwprintw(tabline, 0, COLS-1, ">");
632 wattr_off(tabline, tab_face.background, NULL);
636 * Compute the first visible line around vl. Try to search forward
637 * until the end of the buffer; if a visible line is not found, search
638 * backward. Return NULL if no viable line was found.
641 adjust_line(struct vline *vl, struct buffer *buffer)
648 if (!(vl->parent->flags & L_HIDDEN))
653 t != NULL && t->parent->flags & L_HIDDEN;
654 t = TAILQ_NEXT(t, vlines))
660 /* search backward */
662 t != NULL && t->parent->flags & L_HIDDEN;
663 t = TAILQ_PREV(t, vhead, vlines))
670 redraw_window(WINDOW *win, int off, int height, int width, struct buffer *buffer)
675 restore_curs_x(buffer);
678 * TODO: ignoring buffer->force_update and always
679 * re-rendering. In theory we can recompute the y position
680 * without a re-render, and optimize here. It's not the only
681 * optimisation possible here, wscrl wolud also be an
689 if (TAILQ_EMPTY(&buffer->head))
692 if (buffer->top_line == NULL)
693 buffer->top_line = TAILQ_FIRST(&buffer->head);
695 buffer->top_line = adjust_line(buffer->top_line, buffer);
696 if (buffer->top_line == NULL)
699 buffer->current_line = adjust_line(buffer->current_line, buffer);
703 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
704 if (vl->parent->flags & L_HIDDEN)
708 print_vline(off, width, win, vl);
710 if (vl == buffer->current_line)
722 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
723 if (vl == buffer->current_line)
725 if (vl->parent->flags & L_HIDDEN)
728 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
735 buffer->last_line_off = buffer->line_off;
736 buffer->force_redraw = 0;
738 wmove(win, buffer->curs_y, buffer->curs_x);
744 redraw_window(help, 0, help_lines, help_cols, &helpwin);
748 redraw_body(struct tab *tab)
750 static struct tab *last_tab;
753 tab->buffer.force_redraw =1;
756 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
760 trust_status_char(enum trust_state ts)
763 case TS_UNKNOWN: return 'u';
764 case TS_UNTRUSTED: return '!';
765 case TS_TEMP_TRUSTED: return '!';
766 case TS_TRUSTED: return 'v';
767 case TS_VERIFIED: return 'V';
773 redraw_modeline(struct tab *tab)
776 int x, y, max_x, max_y;
777 const char *mode = tab->buffer.page.name;
778 const char *spin = "-\\|/";
781 wattr_on(modeline, modeline_face.background, NULL);
782 wmove(modeline, 0, 0);
784 wprintw(modeline, "-%c%c %s ",
785 spin[tab->loading_anim_step],
786 trust_status_char(tab->trust),
787 mode == NULL ? "(none)" : mode);
789 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0
790 / tab->buffer.line_max;
792 if (tab->buffer.line_max <= (size_t)body_lines)
793 wprintw(modeline, "All ");
794 else if (tab->buffer.line_off == 0)
795 wprintw(modeline, "Top ");
796 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
797 wprintw(modeline, "Bottom ");
799 wprintw(modeline, "%.0f%% ", pct);
801 wprintw(modeline, "%d/%d %s ",
802 tab->buffer.line_off + tab->buffer.curs_y,
803 tab->buffer.line_max,
806 getyx(modeline, y, x);
807 getmaxyx(modeline, max_y, max_x);
812 for (; x < max_x; ++x)
813 waddstr(modeline, "-");
815 wattr_off(modeline, modeline_face.background, NULL);
819 redraw_minibuffer(void)
821 wattr_on(echoarea, minibuffer_face.background, NULL);
825 do_redraw_minibuffer();
827 do_redraw_echoarea();
829 if (in_minibuffer == MB_COMPREAD)
830 do_redraw_minibuffer_compl();
832 wattr_off(echoarea, minibuffer_face.background, NULL);
836 do_redraw_echoarea(void)
840 if (ministate.curmesg != NULL)
841 wprintw(echoarea, "%s", ministate.curmesg);
842 else if (*keybuf != '\0')
843 waddstr(echoarea, keybuf);
845 /* If nothing else, show the URL at point */
846 vl = current_tab->buffer.current_line;
847 if (vl != NULL && vl->parent->type == LINE_LINK)
848 waddstr(echoarea, vl->parent->alt);
853 do_redraw_minibuffer(void)
855 size_t off_y, off_x = 0;
856 const char *start, *c;
858 /* unused, set by getyx */
861 wmove(echoarea, 0, 0);
863 if (in_minibuffer == MB_COMPREAD)
864 wprintw(echoarea, "(%2d) ",
865 ministate.compl.buffer.line_max);
867 wprintw(echoarea, "%s", ministate.prompt);
868 if (ministate.hist_cur != NULL)
869 wprintw(echoarea, "(%zu/%zu) ",
870 ministate.hist_off + 1,
871 ministate.history->len);
873 getyx(echoarea, off_y, off_x);
875 start = ministate.hist_cur != NULL
876 ? ministate.hist_cur->h
878 c = utf8_nth(ministate.buffer.current_line->line,
879 ministate.buffer.cpoff);
880 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
881 start = utf8_next_cp(start);
884 waddstr(echoarea, start);
886 if (ministate.curmesg != NULL)
887 wprintw(echoarea, " [%s]", ministate.curmesg);
889 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
893 do_redraw_minibuffer_compl(void)
895 redraw_window(minibuffer, 0, 10, body_cols,
896 &ministate.compl.buffer);
900 * Place the cursor in the right ncurses window. If soft is 1, use
901 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
905 place_cursor(int soft)
907 int (*touch)(WINDOW *);
910 touch = wnoutrefresh;
924 redraw_tab(struct tab *tab)
936 redraw_modeline(tab);
939 wnoutrefresh(tabline);
940 wnoutrefresh(modeline);
942 if (in_minibuffer == MB_COMPREAD)
943 wnoutrefresh(minibuffer);
950 dprintf(1, "\033]0;%s - Telescope\a",
951 current_tab->buffer.page.title);
955 emit_help_item(char *prfx, void *fn)
960 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
966 if ((l = calloc(1, sizeof(*l))) == NULL)
972 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
974 if (TAILQ_EMPTY(&helpwin.page.head))
975 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
977 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
981 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
987 TAILQ_FOREACH(k, &keymap->m, keymaps) {
988 strlcpy(p, prfx, sizeof(p));
990 strlcat(p, " ", sizeof(p));
992 strlcat(p, "M-", sizeof(p));
993 if ((kn = unkbd(k->key)) != NULL)
994 strlcat(p, kn, sizeof(p));
996 strlcat(p, keyname(k->key), sizeof(p));
999 rec_compute_help(&k->map, p, sizeof(p));
1001 emit_help_item(p, k->fn);
1006 recompute_help(void)
1010 empty_vlist(&helpwin);
1011 empty_linelist(&helpwin);
1012 rec_compute_help(current_map, p, sizeof(p));
1013 wrap_page(&helpwin, help_cols);
1017 vmessage(const char *fmt, va_list ap)
1019 if (evtimer_pending(&clechoev, NULL))
1020 evtimer_del(&clechoev);
1022 free(ministate.curmesg);
1023 ministate.curmesg = NULL;
1026 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
1027 evtimer_add(&clechoev, &clechoev_timer);
1029 /* TODO: what to do if the allocation fails here? */
1030 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1031 ministate.curmesg = NULL;
1034 redraw_minibuffer();
1039 message(const char *fmt, ...)
1049 start_loading_anim(struct tab *tab)
1051 if (tab->loading_anim)
1053 tab->loading_anim = 1;
1054 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1055 evtimer_add(&tab->loadingev, &loadingev_timer);
1059 update_loading_anim(int fd, short ev, void *d)
1061 struct tab *tab = d;
1063 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1065 if (tab == current_tab) {
1066 redraw_modeline(tab);
1073 evtimer_add(&tab->loadingev, &loadingev_timer);
1077 stop_loading_anim(struct tab *tab)
1079 if (!tab->loading_anim)
1081 evtimer_del(&tab->loadingev);
1082 tab->loading_anim = 0;
1083 tab->loading_anim_step = 0;
1085 if (tab != current_tab)
1088 redraw_modeline(tab);
1097 load_url_in_tab(struct tab *tab, const char *url)
1104 message("Loading %s...", url);
1105 start_loading_anim(tab);
1112 switch_to_tab(struct tab *tab)
1115 tab->flags &= ~TAB_URGENT;
1117 if (operating && tab->flags & TAB_LAZY)
1118 load_url_in_tab(tab, tab->hist_cur->h);
1124 return tab_counter++;
1128 new_tab(const char *url)
1132 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1138 TAILQ_INIT(&tab->hist.head);
1140 TAILQ_INIT(&tab->buffer.head);
1142 tab->id = tab_new_id();
1144 tab->flags |= TAB_LAZY;
1147 if (TAILQ_EMPTY(&tabshead))
1148 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1150 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1152 load_url_in_tab(tab, url);
1157 ui_print_colors(void)
1159 int colors = 0, pairs = 0, can_change = 0;
1160 int columns = 16, lines, color, i, j;
1165 use_default_colors();
1168 pairs = COLOR_PAIRS;
1169 can_change = can_change_color();
1173 printf("Term info:\n");
1174 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1175 getenv("TERM"), colors, pairs, can_change);
1179 printf("No color support\n");
1183 printf("Available colors:\n\n");
1184 lines = (colors - 1) / columns + 1;
1186 for (i = 0; i < lines; ++i) {
1187 for (j = 0; j < columns; ++j, ++color) {
1188 printf("\033[0;38;5;%dm %03d", color, color);
1201 setlocale(LC_ALL, "");
1203 TAILQ_INIT(&eecmd_history.head);
1204 TAILQ_INIT(&ir_history.head);
1205 TAILQ_INIT(&lu_history.head);
1207 ministate.line.type = LINE_TEXT;
1208 ministate.vline.parent = &ministate.line;
1209 ministate.buffer.current_line = &ministate.vline;
1211 /* initialize help window */
1212 TAILQ_INIT(&helpwin.head);
1214 base_map = &global_map;
1215 current_map = &global_map;
1219 if (enable_colors) {
1222 use_default_colors();
1227 config_apply_style();
1232 intrflush(stdscr, FALSE);
1234 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1236 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1238 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1240 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1242 if ((minibuffer = newwin(1, COLS, LINES-1, 0)) == NULL)
1244 if ((help = newwin(1, 1, 1, 0)) == NULL)
1247 body_lines = LINES-3;
1250 wbkgd(body, body_face.body);
1251 wbkgd(echoarea, minibuffer_face.background);
1256 scrollok(body, FALSE);
1258 /* non-blocking input */
1261 mvwprintw(body, 0, 0, "");
1264 * Dummy so libevent2 won't complain that no event_base is set
1265 * when checking event_pending for the first time
1267 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
1268 evtimer_add(&clechoev, &clechoev_timer);
1269 evtimer_set(&resizeev, handle_resize, NULL);
1270 evtimer_add(&resizeev, &resize_timer);
1272 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1273 event_add(&stdioev, NULL);
1275 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1276 signal_add(&winchev, NULL);
1285 switch_to_tab(current_tab);
1286 redraw_tab(current_tab);
1292 ui_on_tab_loaded(struct tab *tab)
1294 stop_loading_anim(tab);
1295 message("Loaded %s", tab->hist_cur->h);
1303 ui_on_tab_refresh(struct tab *tab)
1305 wrap_page(&tab->buffer, body_cols);
1306 if (tab == current_tab)
1309 tab->flags |= TAB_URGENT;
1319 ui_toggle_side_window(void)
1321 side_window = !side_window;
1326 * ugly hack, but otherwise the window doesn't get updated
1327 * until I call rearrange_windows a second time (e.g. via
1328 * C-l). I will be happy to know why something like this is
1331 rearrange_windows();
1332 rearrange_windows();
1336 ui_schedule_redraw(void)
1338 should_rearrange_windows = 1;
1342 ui_require_input(struct tab *tab, int hide)
1344 /* TODO: hard-switching to another tab is ugly */
1347 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1348 &ir_history, NULL, NULL);
1349 strlcpy(ministate.prompt, "Input required: ",
1350 sizeof(ministate.prompt));
1355 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1358 yornp(prompt, fn, data);
1359 redraw_tab(current_tab);
1363 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1366 completing_read(prompt, fn, data);
1367 redraw_tab(current_tab);
1375 kill(getpid(), SIGSTOP);
1379 rearrange_windows();