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.
17 5e11c00c 2021-03-02 op #include <telescope.h>
19 5e11c00c 2021-03-02 op #include <curses.h>
20 5e11c00c 2021-03-02 op #include <event.h>
21 5e11c00c 2021-03-02 op #include <locale.h>
22 5e11c00c 2021-03-02 op #include <signal.h>
23 eb259e66 2021-03-02 op #include <stdlib.h>
24 eb259e66 2021-03-02 op #include <string.h>
26 5e11c00c 2021-03-02 op #define TAB_CURRENT 0x1
28 eb259e66 2021-03-02 op #define CTRL(x) ((x)&0x1F)
30 5e11c00c 2021-03-02 op static struct event stdioev, winchev;
32 5e11c00c 2021-03-02 op static struct tab *current_tab(void);
33 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
34 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
35 eb259e66 2021-03-02 op static int word_bourdaries(const char*, const char*, const char**, const char**);
36 eb259e66 2021-03-02 op static void wrap_text(const char*, const char*, const char*, const char*);
37 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
39 5e11c00c 2021-03-02 op static struct tab *
40 5e11c00c 2021-03-02 op current_tab(void)
42 5e11c00c 2021-03-02 op struct tab *t;
44 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
45 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
49 5e11c00c 2021-03-02 op /* unreachable */
54 5e11c00c 2021-03-02 op dispatch_stdio(int fd, short ev, void *d)
63 5e11c00c 2021-03-02 op if (c == 'q') {
64 5e11c00c 2021-03-02 op event_loopbreak();
68 eb259e66 2021-03-02 op if (c == CTRL('L')) {
70 eb259e66 2021-03-02 op redraw_tab(current_tab());
74 5e11c00c 2021-03-02 op printw("You typed %c\n", c);
79 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
85 5e11c00c 2021-03-02 op redraw_tab(current_tab());
89 eb259e66 2021-03-02 op * Helper function for wrap_text. Find the end of the current word
90 eb259e66 2021-03-02 op * and the end of the separator after the word.
93 eb259e66 2021-03-02 op word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
98 eb259e66 2021-03-02 op if (*s == '\0')
101 eb259e66 2021-03-02 op /* find the end of the current world */
102 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
103 eb259e66 2021-03-02 op if (strchr(sep, *s) != NULL)
107 eb259e66 2021-03-02 op *endword = s;
109 eb259e66 2021-03-02 op /* find the end of the separator */
110 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
111 eb259e66 2021-03-02 op if (strchr(sep, *s) == NULL)
120 eb259e66 2021-03-02 op static inline void
121 eb259e66 2021-03-02 op emitline(const char *prfx, size_t zero, size_t *off)
123 eb259e66 2021-03-02 op printw("\n%s", prfx);
127 eb259e66 2021-03-02 op static inline void
128 eb259e66 2021-03-02 op emitstr(const char **s, size_t len, size_t *off)
132 eb259e66 2021-03-02 op /* printw("%*s", ...) doesn't seem to respect the precision, so... */
133 eb259e66 2021-03-02 op for (i = 0; i < len; ++i)
134 eb259e66 2021-03-02 op addch((*s)[i]);
140 eb259e66 2021-03-02 op * Wrap the text, prefixing the first line with prfx1 and the
141 eb259e66 2021-03-02 op * following with prfx2, and breaking on characters in the breakon set.
142 eb259e66 2021-03-02 op * The idea is pretty simple: if there is enough space, write the next
143 eb259e66 2021-03-02 op * word; if we are at the start of a line and there's not enough
144 eb259e66 2021-03-02 op * space, hard-split it.
146 eb259e66 2021-03-02 op * TODO: it considers each byte one cell on the screen!
147 eb259e66 2021-03-02 op * TODO: assume strlen(prfx1) == strlen(prfx2)
150 eb259e66 2021-03-02 op wrap_text(const char *prfx1, const char *prfx2, const char *line, const char *breakon)
152 eb259e66 2021-03-02 op size_t zero, off, len, split;
153 eb259e66 2021-03-02 op const char *endword, *endspc;
155 eb259e66 2021-03-02 op printw("%s", prfx1);
156 eb259e66 2021-03-02 op zero = strlen(prfx1);
159 eb259e66 2021-03-02 op while (word_boundaries(line, breakon, &endword, &endspc)) {
160 eb259e66 2021-03-02 op len = endword - line;
161 eb259e66 2021-03-02 op if (off + len < COLS) {
162 eb259e66 2021-03-02 op emitstr(&line, len, &off);
164 eb259e66 2021-03-02 op emitline(prfx2, zero, &off);
165 eb259e66 2021-03-02 op while (len >= COLS) {
166 eb259e66 2021-03-02 op /* hard wrap */
167 eb259e66 2021-03-02 op printw("%*s", COLS-1, line);
168 eb259e66 2021-03-02 op emitline(prfx2, zero, &off);
169 eb259e66 2021-03-02 op len -= COLS-1;
170 eb259e66 2021-03-02 op line += COLS-1;
173 eb259e66 2021-03-02 op if (len != 0)
174 eb259e66 2021-03-02 op emitstr(&line, len, &off);
177 eb259e66 2021-03-02 op /* print the spaces iff not at bol */
178 eb259e66 2021-03-02 op len = endspc - endword;
179 eb259e66 2021-03-02 op /* line = endspc; */
180 eb259e66 2021-03-02 op if (off != zero) {
181 ffa1604d 2021-03-02 op if (off + len >= COLS)
182 eb259e66 2021-03-02 op emitline(prfx2, zero, &off);
184 eb259e66 2021-03-02 op emitstr(&line, len, &off);
187 eb259e66 2021-03-02 op line = endspc;
190 eb259e66 2021-03-02 op printw("\n");
194 5e11c00c 2021-03-02 op redraw_tab(struct tab *tab)
196 5e11c00c 2021-03-02 op struct line *l;
197 eb259e66 2021-03-02 op const char *sep = " \t";
201 5e11c00c 2021-03-02 op TAILQ_FOREACH(l, &tab->page.head, lines) {
202 5e11c00c 2021-03-02 op switch (l->type) {
203 5e11c00c 2021-03-02 op case LINE_TEXT:
204 eb259e66 2021-03-02 op wrap_text("", "", l->line, sep);
206 5e11c00c 2021-03-02 op case LINE_LINK:
207 eb259e66 2021-03-02 op wrap_text("=> ", " ", l->line, sep);
209 5e11c00c 2021-03-02 op case LINE_TITLE_1:
210 eb259e66 2021-03-02 op wrap_text("# ", " ", l->line, sep);
212 5e11c00c 2021-03-02 op case LINE_TITLE_2:
213 eb259e66 2021-03-02 op wrap_text("## ", " ", l->line, sep);
215 5e11c00c 2021-03-02 op case LINE_TITLE_3:
216 eb259e66 2021-03-02 op wrap_text("### ", " ", l->line, sep);
218 5e11c00c 2021-03-02 op case LINE_ITEM:
219 eb259e66 2021-03-02 op wrap_text("* ", " ", l->line, sep);
221 5e11c00c 2021-03-02 op case LINE_QUOTE:
222 eb259e66 2021-03-02 op wrap_text("> ", "> ", l->line, sep);
224 5e11c00c 2021-03-02 op case LINE_PRE_START:
225 5e11c00c 2021-03-02 op case LINE_PRE_END:
226 5e11c00c 2021-03-02 op printw("```\n");
228 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
229 eb259e66 2021-03-02 op printw("%s\n", l->line);
238 5e11c00c 2021-03-02 op ui_init(void)
240 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
247 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
248 5e11c00c 2021-03-02 op keypad(stdscr, TRUE);
250 5e11c00c 2021-03-02 op /* non-blocking input */
253 5e11c00c 2021-03-02 op mvprintw(0, 0, "");
255 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
256 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
258 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
259 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
265 5e11c00c 2021-03-02 op ui_on_new_tab(struct tab *tab)
267 5e11c00c 2021-03-02 op struct tab *t;
269 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
270 5e11c00c 2021-03-02 op t->flags &= ~TAB_CURRENT;
273 5e11c00c 2021-03-02 op tab->flags = TAB_CURRENT;
275 5e11c00c 2021-03-02 op /* TODO: redraw the tab list */
279 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
281 5e11c00c 2021-03-02 op if (!(tab->flags & TAB_CURRENT))
284 5e11c00c 2021-03-02 op redraw_tab(tab);