Blame


1 5e11c00c 2021-03-02 op /*
2 5e11c00c 2021-03-02 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 5e11c00c 2021-03-02 op *
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.
7 5e11c00c 2021-03-02 op *
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.
15 5e11c00c 2021-03-02 op */
16 5e11c00c 2021-03-02 op
17 5e11c00c 2021-03-02 op #include <telescope.h>
18 5e11c00c 2021-03-02 op
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>
25 5e11c00c 2021-03-02 op
26 5e11c00c 2021-03-02 op #define TAB_CURRENT 0x1
27 5e11c00c 2021-03-02 op
28 eb259e66 2021-03-02 op #define CTRL(x) ((x)&0x1F)
29 eb259e66 2021-03-02 op
30 5e11c00c 2021-03-02 op static struct event stdioev, winchev;
31 5e11c00c 2021-03-02 op
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*);
38 5e11c00c 2021-03-02 op
39 5e11c00c 2021-03-02 op static struct tab *
40 5e11c00c 2021-03-02 op current_tab(void)
41 5e11c00c 2021-03-02 op {
42 5e11c00c 2021-03-02 op struct tab *t;
43 5e11c00c 2021-03-02 op
44 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
45 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
46 5e11c00c 2021-03-02 op return t;
47 5e11c00c 2021-03-02 op }
48 5e11c00c 2021-03-02 op
49 5e11c00c 2021-03-02 op /* unreachable */
50 5e11c00c 2021-03-02 op abort();
51 5e11c00c 2021-03-02 op }
52 5e11c00c 2021-03-02 op
53 5e11c00c 2021-03-02 op static void
54 5e11c00c 2021-03-02 op dispatch_stdio(int fd, short ev, void *d)
55 5e11c00c 2021-03-02 op {
56 5e11c00c 2021-03-02 op int c;
57 5e11c00c 2021-03-02 op
58 5e11c00c 2021-03-02 op c = getch();
59 5e11c00c 2021-03-02 op
60 5e11c00c 2021-03-02 op if (c == ERR)
61 5e11c00c 2021-03-02 op return;
62 5e11c00c 2021-03-02 op
63 5e11c00c 2021-03-02 op if (c == 'q') {
64 5e11c00c 2021-03-02 op event_loopbreak();
65 5e11c00c 2021-03-02 op return;
66 5e11c00c 2021-03-02 op }
67 5e11c00c 2021-03-02 op
68 eb259e66 2021-03-02 op if (c == CTRL('L')) {
69 eb259e66 2021-03-02 op clear();
70 eb259e66 2021-03-02 op redraw_tab(current_tab());
71 eb259e66 2021-03-02 op return;
72 eb259e66 2021-03-02 op }
73 eb259e66 2021-03-02 op
74 5e11c00c 2021-03-02 op printw("You typed %c\n", c);
75 5e11c00c 2021-03-02 op refresh();
76 5e11c00c 2021-03-02 op }
77 5e11c00c 2021-03-02 op
78 5e11c00c 2021-03-02 op static void
79 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
80 5e11c00c 2021-03-02 op {
81 5e11c00c 2021-03-02 op endwin();
82 5e11c00c 2021-03-02 op refresh();
83 5e11c00c 2021-03-02 op clear();
84 5e11c00c 2021-03-02 op
85 5e11c00c 2021-03-02 op redraw_tab(current_tab());
86 5e11c00c 2021-03-02 op }
87 5e11c00c 2021-03-02 op
88 eb259e66 2021-03-02 op /*
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.
91 eb259e66 2021-03-02 op */
92 eb259e66 2021-03-02 op static int
93 eb259e66 2021-03-02 op word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
94 eb259e66 2021-03-02 op {
95 eb259e66 2021-03-02 op *endword = s;
96 eb259e66 2021-03-02 op *endword = s;
97 eb259e66 2021-03-02 op
98 eb259e66 2021-03-02 op if (*s == '\0')
99 eb259e66 2021-03-02 op return 0;
100 eb259e66 2021-03-02 op
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)
104 eb259e66 2021-03-02 op break;
105 eb259e66 2021-03-02 op }
106 eb259e66 2021-03-02 op
107 eb259e66 2021-03-02 op *endword = s;
108 eb259e66 2021-03-02 op
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)
112 eb259e66 2021-03-02 op break;
113 eb259e66 2021-03-02 op }
114 eb259e66 2021-03-02 op
115 eb259e66 2021-03-02 op *endspc = s;
116 eb259e66 2021-03-02 op
117 eb259e66 2021-03-02 op return 1;
118 eb259e66 2021-03-02 op }
119 eb259e66 2021-03-02 op
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)
122 eb259e66 2021-03-02 op {
123 eb259e66 2021-03-02 op printw("\n%s", prfx);
124 eb259e66 2021-03-02 op *off = zero;
125 eb259e66 2021-03-02 op }
126 eb259e66 2021-03-02 op
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)
129 eb259e66 2021-03-02 op {
130 eb259e66 2021-03-02 op size_t i;
131 eb259e66 2021-03-02 op
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]);
135 eb259e66 2021-03-02 op *off += len;
136 eb259e66 2021-03-02 op *s += len;
137 eb259e66 2021-03-02 op }
138 eb259e66 2021-03-02 op
139 eb259e66 2021-03-02 op /*
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.
145 eb259e66 2021-03-02 op *
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)
148 eb259e66 2021-03-02 op */
149 5e11c00c 2021-03-02 op static void
150 eb259e66 2021-03-02 op wrap_text(const char *prfx1, const char *prfx2, const char *line, const char *breakon)
151 eb259e66 2021-03-02 op {
152 eb259e66 2021-03-02 op size_t zero, off, len, split;
153 eb259e66 2021-03-02 op const char *endword, *endspc;
154 eb259e66 2021-03-02 op
155 eb259e66 2021-03-02 op printw("%s", prfx1);
156 eb259e66 2021-03-02 op zero = strlen(prfx1);
157 eb259e66 2021-03-02 op off = zero;
158 eb259e66 2021-03-02 op
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);
163 eb259e66 2021-03-02 op } else {
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;
171 eb259e66 2021-03-02 op }
172 eb259e66 2021-03-02 op
173 eb259e66 2021-03-02 op if (len != 0)
174 eb259e66 2021-03-02 op emitstr(&line, len, &off);
175 eb259e66 2021-03-02 op }
176 eb259e66 2021-03-02 op
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);
183 eb259e66 2021-03-02 op else
184 eb259e66 2021-03-02 op emitstr(&line, len, &off);
185 eb259e66 2021-03-02 op }
186 eb259e66 2021-03-02 op
187 eb259e66 2021-03-02 op line = endspc;
188 eb259e66 2021-03-02 op }
189 eb259e66 2021-03-02 op
190 eb259e66 2021-03-02 op printw("\n");
191 eb259e66 2021-03-02 op }
192 eb259e66 2021-03-02 op
193 eb259e66 2021-03-02 op static void
194 5e11c00c 2021-03-02 op redraw_tab(struct tab *tab)
195 5e11c00c 2021-03-02 op {
196 5e11c00c 2021-03-02 op struct line *l;
197 eb259e66 2021-03-02 op const char *sep = " \t";
198 5e11c00c 2021-03-02 op
199 5e11c00c 2021-03-02 op erase();
200 5e11c00c 2021-03-02 op
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);
205 5e11c00c 2021-03-02 op break;
206 5e11c00c 2021-03-02 op case LINE_LINK:
207 eb259e66 2021-03-02 op wrap_text("=> ", " ", l->line, sep);
208 5e11c00c 2021-03-02 op break;
209 5e11c00c 2021-03-02 op case LINE_TITLE_1:
210 eb259e66 2021-03-02 op wrap_text("# ", " ", l->line, sep);
211 5e11c00c 2021-03-02 op break;
212 5e11c00c 2021-03-02 op case LINE_TITLE_2:
213 eb259e66 2021-03-02 op wrap_text("## ", " ", l->line, sep);
214 5e11c00c 2021-03-02 op break;
215 5e11c00c 2021-03-02 op case LINE_TITLE_3:
216 eb259e66 2021-03-02 op wrap_text("### ", " ", l->line, sep);
217 5e11c00c 2021-03-02 op break;
218 5e11c00c 2021-03-02 op case LINE_ITEM:
219 eb259e66 2021-03-02 op wrap_text("* ", " ", l->line, sep);
220 5e11c00c 2021-03-02 op break;
221 5e11c00c 2021-03-02 op case LINE_QUOTE:
222 eb259e66 2021-03-02 op wrap_text("> ", "> ", l->line, sep);
223 5e11c00c 2021-03-02 op break;
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");
227 5e11c00c 2021-03-02 op break;
228 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
229 eb259e66 2021-03-02 op printw("%s\n", l->line);
230 5e11c00c 2021-03-02 op break;
231 5e11c00c 2021-03-02 op }
232 5e11c00c 2021-03-02 op }
233 5e11c00c 2021-03-02 op
234 5e11c00c 2021-03-02 op refresh();
235 5e11c00c 2021-03-02 op }
236 5e11c00c 2021-03-02 op
237 5e11c00c 2021-03-02 op int
238 5e11c00c 2021-03-02 op ui_init(void)
239 5e11c00c 2021-03-02 op {
240 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
241 5e11c00c 2021-03-02 op
242 5e11c00c 2021-03-02 op initscr();
243 15e1b108 2021-03-02 op raw();
244 5e11c00c 2021-03-02 op noecho();
245 5e11c00c 2021-03-02 op
246 5e11c00c 2021-03-02 op nonl();
247 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
248 5e11c00c 2021-03-02 op keypad(stdscr, TRUE);
249 5e11c00c 2021-03-02 op
250 5e11c00c 2021-03-02 op /* non-blocking input */
251 5e11c00c 2021-03-02 op timeout(0);
252 5e11c00c 2021-03-02 op
253 5e11c00c 2021-03-02 op mvprintw(0, 0, "");
254 5e11c00c 2021-03-02 op
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);
257 5e11c00c 2021-03-02 op
258 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
259 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
260 5e11c00c 2021-03-02 op
261 5e11c00c 2021-03-02 op return 1;
262 5e11c00c 2021-03-02 op }
263 5e11c00c 2021-03-02 op
264 5e11c00c 2021-03-02 op void
265 5e11c00c 2021-03-02 op ui_on_new_tab(struct tab *tab)
266 5e11c00c 2021-03-02 op {
267 5e11c00c 2021-03-02 op struct tab *t;
268 5e11c00c 2021-03-02 op
269 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
270 5e11c00c 2021-03-02 op t->flags &= ~TAB_CURRENT;
271 5e11c00c 2021-03-02 op }
272 5e11c00c 2021-03-02 op
273 5e11c00c 2021-03-02 op tab->flags = TAB_CURRENT;
274 5e11c00c 2021-03-02 op
275 5e11c00c 2021-03-02 op /* TODO: redraw the tab list */
276 5e11c00c 2021-03-02 op }
277 5e11c00c 2021-03-02 op
278 5e11c00c 2021-03-02 op void
279 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
280 5e11c00c 2021-03-02 op {
281 5e11c00c 2021-03-02 op if (!(tab->flags & TAB_CURRENT))
282 5e11c00c 2021-03-02 op return;
283 5e11c00c 2021-03-02 op
284 5e11c00c 2021-03-02 op redraw_tab(tab);
285 5e11c00c 2021-03-02 op }
286 5e11c00c 2021-03-02 op
287 5e11c00c 2021-03-02 op void
288 5e11c00c 2021-03-02 op ui_end(void)
289 5e11c00c 2021-03-02 op {
290 5e11c00c 2021-03-02 op endwin();
291 5e11c00c 2021-03-02 op }
292 5e11c00c 2021-03-02 op