1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
|
#define _DEFAULT_SOURCE
#include "display.h"
#include "buffer.h"
#include "timers.h"
#include "utf8.h"
#include <assert.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#define ESC 0x1b
struct display {
struct termios term;
struct termios orig_term;
uint32_t width;
uint32_t height;
};
enum render_cmd_type {
RenderCommand_DrawText = 0,
RenderCommand_PushFormat = 1,
RenderCommand_Repeat = 2,
RenderCommand_ClearFormat = 3,
RenderCommand_SetShowWhitespace = 4,
RenderCommand_DrawList = 5,
};
struct render_command {
enum render_cmd_type type;
union render_cmd_data {
struct draw_text_cmd *draw_txt;
struct push_fmt_cmd *push_fmt;
struct repeat_cmd *repeat;
struct show_ws_cmd *show_ws;
struct draw_list_cmd *draw_list;
} data;
};
struct draw_text_cmd {
uint32_t col;
uint32_t row;
uint8_t *data;
uint32_t len;
};
struct push_fmt_cmd {
uint8_t fmt[64];
uint32_t len;
};
struct repeat_cmd {
uint32_t col;
uint32_t row;
uint32_t c;
uint32_t nrepeat;
};
struct show_ws_cmd {
bool show;
};
struct draw_list_cmd {
struct command_list *list;
};
struct command_list {
struct render_command *cmds;
uint64_t ncmds;
uint64_t capacity;
uint32_t xoffset;
uint32_t yoffset;
uint32_t tab_width;
void *(*allocator)(size_t);
char name[16];
struct command_list *next_list;
};
struct winsize getsize(void) {
struct winsize ws;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_row == 0 ||
ws.ws_col == 0) {
int fd = open("/dev/tty", O_RDONLY);
if (fd != -1) {
ioctl(fd, TIOCGWINSZ, &ws);
close(fd);
}
}
return ws;
}
struct display *display_create(void) {
struct winsize ws = getsize();
// save old settings
struct termios orig_term;
if (tcgetattr(0, &orig_term) < 0) {
return NULL;
}
// set terminal to raw mode
struct termios term = orig_term;
cfmakeraw(&term);
if (tcsetattr(0, TCSADRAIN, &term) < 0) {
return NULL;
}
struct display *d = calloc(1, sizeof(struct display));
d->orig_term = orig_term;
d->term = term;
d->height = ws.ws_row;
d->width = ws.ws_col;
return d;
}
void display_resize(struct display *display) {
struct winsize sz = getsize();
display->width = sz.ws_col;
display->height = sz.ws_row;
}
void display_destroy(struct display *display) {
// reset old terminal mode
tcsetattr(0, TCSADRAIN, &display->orig_term);
free(display);
}
uint32_t display_width(struct display *display) { return display->width; }
uint32_t display_height(struct display *display) { return display->height; }
void putch(uint8_t c) { putc(c, stdout); }
static void apply_fmt(uint8_t *fmt_stack, uint32_t fmt_stack_len) {
if (fmt_stack == NULL || fmt_stack_len == 0) {
return;
}
for (uint32_t i = 0; i < fmt_stack_len; ++i) {
putc(fmt_stack[i], stdout);
}
putc('m', stdout);
}
void putch_ws(uint8_t c, bool show_whitespace, uint8_t *fmt_stack,
uint32_t fmt_stack_len, uint32_t tab_width) {
if (show_whitespace && c == '\t') {
fprintf(stdout, "\x1b[90m→%*s\x1b[39m", tab_width > 0 ? tab_width - 1 : 0,
"");
apply_fmt(fmt_stack, fmt_stack_len);
} else if (show_whitespace && c == ' ') {
fputs("\x1b[90m·\x1b[39m", stdout);
apply_fmt(fmt_stack, fmt_stack_len);
} else {
putch(c);
}
}
void putbytes(uint8_t *line_bytes, uint32_t line_length, bool show_whitespace,
uint8_t *fmt_stack, uint32_t fmt_stack_len, uint32_t tab_width) {
for (uint32_t bytei = 0; bytei < line_length; ++bytei) {
putch_ws(line_bytes[bytei], show_whitespace, fmt_stack, fmt_stack_len,
tab_width);
}
}
void put_ansiparm(int n) {
int q = n / 10;
if (q != 0) {
int r = q / 10;
if (r != 0) {
putc((r % 10) + '0', stdout);
}
putc((q % 10) + '0', stdout);
}
putc((n % 10) + '0', stdout);
}
void display_move_cursor(struct display *display, uint32_t row, uint32_t col) {
(void)display;
putc(ESC, stdout);
putc('[', stdout);
put_ansiparm(row + 1);
putc(';', stdout);
put_ansiparm(col + 1);
putc('H', stdout);
}
void display_clear(struct display *display) {
display_move_cursor(display, 0, 0);
putc(ESC, stdout);
putc('[', stdout);
putc('J', stdout);
}
struct command_list *command_list_create(uint32_t initial_capacity,
void *(*allocator)(size_t),
uint32_t xoffset, uint32_t yoffset,
uint32_t tab_width, const char *name) {
struct command_list *command_list = allocator(sizeof(struct command_list));
command_list->capacity = initial_capacity;
command_list->ncmds = 0;
command_list->xoffset = xoffset;
command_list->yoffset = yoffset;
command_list->next_list = NULL;
command_list->tab_width = tab_width;
strncpy(command_list->name, name, 15);
command_list->cmds =
allocator(sizeof(struct render_command) * initial_capacity);
command_list->allocator = allocator;
return command_list;
}
struct render_command *add_command(struct command_list *list,
enum render_cmd_type tp) {
struct command_list *l = list;
struct command_list *n = l->next_list;
// scan through lists for one with capacity
while (l->ncmds == l->capacity && n != NULL) {
l = n;
n = l->next_list;
}
if (l->ncmds == l->capacity && n == NULL) {
l->next_list = command_list_create(l->capacity, l->allocator, l->xoffset,
l->yoffset, l->tab_width, l->name);
l = l->next_list;
}
struct render_command *cmd = &l->cmds[l->ncmds];
cmd->type = tp;
switch (tp) {
case RenderCommand_DrawText:
cmd->data.draw_txt = l->allocator(sizeof(struct draw_text_cmd));
break;
case RenderCommand_Repeat:
cmd->data.repeat = l->allocator(sizeof(struct repeat_cmd));
break;
case RenderCommand_PushFormat:
cmd->data.push_fmt = l->allocator(sizeof(struct push_fmt_cmd));
break;
case RenderCommand_SetShowWhitespace:
cmd->data.show_ws = l->allocator(sizeof(struct show_ws_cmd));
break;
case RenderCommand_ClearFormat:
break;
case RenderCommand_DrawList:
cmd->data.draw_list = l->allocator(sizeof(struct draw_list_cmd));
break;
default:
assert(false);
}
++l->ncmds;
return cmd;
}
void command_list_draw_text(struct command_list *list, uint32_t col,
uint32_t row, uint8_t *data, uint32_t len) {
struct draw_text_cmd *cmd =
add_command(list, RenderCommand_DrawText)->data.draw_txt;
cmd->data = data;
cmd->col = col;
cmd->row = row;
cmd->len = len;
}
void command_list_draw_text_copy(struct command_list *list, uint32_t col,
uint32_t row, uint8_t *data, uint32_t len) {
uint8_t *bytes = (uint8_t *)list->allocator(len);
memcpy(bytes, data, len);
command_list_draw_text(list, col, row, bytes, len);
}
void command_list_draw_repeated(struct command_list *list, uint32_t col,
uint32_t row, uint32_t c, uint32_t nrepeat) {
struct repeat_cmd *cmd = add_command(list, RenderCommand_Repeat)->data.repeat;
cmd->col = col;
cmd->row = row;
cmd->c = c;
cmd->nrepeat = nrepeat;
}
void command_list_draw_command_list(struct command_list *list,
struct command_list *to_draw) {
struct draw_list_cmd *cmd =
add_command(list, RenderCommand_DrawList)->data.draw_list;
cmd->list = to_draw;
}
void command_list_set_index_color_fg(struct command_list *list,
uint8_t color_idx) {
struct push_fmt_cmd *cmd =
add_command(list, RenderCommand_PushFormat)->data.push_fmt;
if (color_idx < 8) {
cmd->len = snprintf((char *)cmd->fmt, 64, "%d", 30 + color_idx);
} else if (color_idx < 16) {
cmd->len = snprintf((char *)cmd->fmt, 64, "%d", 90 + color_idx - 8);
} else {
cmd->len = snprintf((char *)cmd->fmt, 64, "38;5;%d", color_idx);
}
}
void command_list_set_color_fg(struct command_list *list, uint8_t red,
uint8_t green, uint8_t blue) {
struct push_fmt_cmd *cmd =
add_command(list, RenderCommand_PushFormat)->data.push_fmt;
cmd->len = snprintf((char *)cmd->fmt, 64, "38;2;%d;%d;%d", red, green, blue);
}
void command_list_set_index_color_bg(struct command_list *list,
uint8_t color_idx) {
struct push_fmt_cmd *cmd =
add_command(list, RenderCommand_PushFormat)->data.push_fmt;
if (color_idx < 8) {
cmd->len = snprintf((char *)cmd->fmt, 64, "%d", 40 + color_idx);
} else if (color_idx < 16) {
cmd->len = snprintf((char *)cmd->fmt, 64, "%d", 100 + color_idx - 8);
} else {
cmd->len = snprintf((char *)cmd->fmt, 64, "48;5;%d", color_idx);
}
}
void command_list_set_color_bg(struct command_list *list, uint8_t red,
uint8_t green, uint8_t blue) {
struct push_fmt_cmd *cmd =
add_command(list, RenderCommand_PushFormat)->data.push_fmt;
cmd->len = snprintf((char *)cmd->fmt, 64, "48;2;%d;%d;%d", red, green, blue);
}
void command_list_set_inverted_colors(struct command_list *list) {
struct push_fmt_cmd *cmd =
add_command(list, RenderCommand_PushFormat)->data.push_fmt;
cmd->fmt[0] = '7';
cmd->len = 1;
}
void command_list_reset_color(struct command_list *list) {
add_command(list, RenderCommand_ClearFormat);
}
void command_list_set_show_whitespace(struct command_list *list, bool show) {
add_command(list, RenderCommand_SetShowWhitespace)->data.show_ws->show = show;
}
void display_render(struct display *display,
struct command_list *command_list) {
struct command_list *cl = command_list;
static char name[32] = {0};
snprintf(name, 31, "display.cl.%s", cl->name);
struct timer *render_timer = timer_start(name);
uint8_t fmt_stack[256] = {0};
fmt_stack[0] = ESC;
fmt_stack[1] = '[';
fmt_stack[2] = '0';
uint32_t fmt_stack_len = 3;
bool show_whitespace_state = false;
while (cl != NULL) {
for (uint64_t cmdi = 0; cmdi < cl->ncmds; ++cmdi) {
struct render_command *cmd = &cl->cmds[cmdi];
switch (cmd->type) {
case RenderCommand_DrawText: {
struct draw_text_cmd *txt_cmd = cmd->data.draw_txt;
display_move_cursor(display, txt_cmd->row + cl->yoffset,
txt_cmd->col + cl->xoffset);
apply_fmt(fmt_stack, fmt_stack_len);
putbytes(txt_cmd->data, txt_cmd->len, show_whitespace_state, fmt_stack,
fmt_stack_len, cl->tab_width);
break;
}
case RenderCommand_Repeat: {
struct repeat_cmd *repeat_cmd = cmd->data.repeat;
display_move_cursor(display, repeat_cmd->row + cl->yoffset,
repeat_cmd->col + cl->xoffset);
apply_fmt(fmt_stack, fmt_stack_len);
struct utf8_codepoint_iterator iter =
create_utf8_codepoint_iterator((uint8_t *)&repeat_cmd->c, 4, 0);
struct codepoint *codepoint = utf8_next_codepoint(&iter);
if (codepoint != NULL) {
for (uint32_t i = 0; i < repeat_cmd->nrepeat; ++i) {
putbytes((uint8_t *)&repeat_cmd->c, codepoint->nbytes,
show_whitespace_state, fmt_stack, fmt_stack_len,
cl->tab_width);
}
}
break;
}
case RenderCommand_PushFormat: {
struct push_fmt_cmd *fmt_cmd = cmd->data.push_fmt;
fmt_stack[fmt_stack_len] = ';';
++fmt_stack_len;
memcpy(fmt_stack + fmt_stack_len, fmt_cmd->fmt, fmt_cmd->len);
fmt_stack_len += fmt_cmd->len;
break;
}
case RenderCommand_ClearFormat:
fmt_stack_len = 3;
break;
case RenderCommand_SetShowWhitespace:
show_whitespace_state = cmd->data.show_ws->show;
break;
case RenderCommand_DrawList:
display_render(display, cmd->data.draw_list->list);
break;
}
}
cl = cl->next_list;
}
timer_stop(render_timer);
}
void hide_cursor(void) {
putc(ESC, stdout);
putc('[', stdout);
putc('?', stdout);
putc('2', stdout);
putc('5', stdout);
putc('l', stdout);
}
void show_cursor(void) {
putc(ESC, stdout);
putc('[', stdout);
putc('?', stdout);
putc('2', stdout);
putc('5', stdout);
putc('h', stdout);
}
void display_begin_render(struct display *display) {
(void)display;
hide_cursor();
}
void display_end_render(struct display *display) {
(void)display;
show_cursor();
fflush(stdout);
}
|