-
Notifications
You must be signed in to change notification settings - Fork 1
/
ansi.cpp
executable file
·583 lines (523 loc) · 13 KB
/
ansi.cpp
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/***
ansi.cpp
Support for printing strings containing ANSI control characters in the terminal, and
have them do the right thing. Color, attributes, and cursor control are supported.
Most terminals support this functionality natively, but the terminal shipped with Windows is
a notable exception. These functions give support for control codes even in Windows terminals.
Currently only built into libcodehappy in native Windows environments.
Copyright (c) 2014-2022 C. M. Street
***/
#ifdef PDCURSES_OK
#define CHAR_ESC 0033
#define CHAR_CSI 0233
static bool __is_termcode(const char* w) {
if (*w == CHAR_ESC)
return(true);
if (*w == CHAR_CSI)
return(true);
return(false);
}
static bool __curses_init = false;
#define COLOR_PAIR_INDEX(fg, bg) (((bg << 3) + (fg)) + 1)
void ensure_curses_lib(void) {
if (__curses_init)
return;
initscr();
raw();
start_color();
attrset(A_NORMAL);
init_pair(COLOR_PAIR_INDEX(COLOR_WHITE, COLOR_BLACK), COLOR_WHITE, COLOR_BLACK);
attron(COLOR_PAIR(COLOR_PAIR_INDEX(COLOR_WHITE, COLOR_BLACK)));
keypad(stdscr, TRUE);
__curses_init = true;
}
void curses_lib_done(void) {
if (!__curses_init)
return;
endwin();
__curses_init = false;
}
static bool __is_final_ch(const char w) {
return is_between(w, '@', '~');
}
static void __clear_line_between(int col1, int col2, int rw) {
int i;
for (i = col1; i <= col2; ++i) {
move(rw, i);
addch(' ');
}
}
static void __clear_lines(int rw1, int rw2, int max_x) {
int i, j;
for (j = rw1; j <= rw2; ++j) {
for (i = 0; i <= max_x; ++i) {
move(j, i);
addch(' ');
}
}
}
static void __clear_to_end(void) {
int x, y, sx, sy;
getyx(stdscr, y, x);
getmaxyx(stdscr, sy, sx);
__clear_line_between(x, sx - 1, y);
__clear_lines(y + 1, sy - 1, sx - 1);
move(y, x);
}
static void __clear_to_start(void) {
int x, y, sx, sy;
getyx(stdscr, y, x);
getmaxyx(stdscr, sy, sx);
__clear_line_between(0, x, y);
__clear_lines(0, y - 1, sx - 1);
move(y, x);
}
static void __cursor_move_delta(int dx, int dy) {
int x, y, sx, sy;
getyx(stdscr, y, x);
getmaxyx(stdscr, sy, sx);
x += dx;
y += dy;
x = CLAMP(x, 0, sx - 1);
y = CLAMP(y, 0, sy - 1);
move(y, x);
}
#define MISSING_ARG 0xFFFFFFFFUL
#define HI_INTENSITY (8)
static void __set_sgr_parameters(u32* iargs, u32 cargs) {
int i;
const u32 colors[16] =
{COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW,
COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE,
COLOR_BLACK + HI_INTENSITY, COLOR_RED + HI_INTENSITY, COLOR_GREEN + HI_INTENSITY, COLOR_YELLOW + HI_INTENSITY,
COLOR_BLUE + HI_INTENSITY, COLOR_MAGENTA + HI_INTENSITY, COLOR_CYAN + HI_INTENSITY, COLOR_WHITE + HI_INTENSITY };
// we start with COLOR_WHITE on COLOR_BLACK
static u32 __fg_clr = 7;
static u32 __bg_clr = 0;
for (i = 0; i < cargs; ++i) {
if (MISSING_ARG == iargs[i])
continue;
switch (iargs[i]) {
case 0:
/* reset / normal */
attrset(A_NORMAL);
break;
case 1:
/* bold / hi-intensity */
attroff(A_DIM);
attron(A_BOLD);
break;
case 2:
/* faint / lo-intensity */
attroff(A_BOLD);
attron(A_DIM);
break;
case 3:
/* italic / inverse video */
attron(A_REVERSE);
break;
case 4:
/* underline: single */
attron(A_UNDERLINE);
break;
case 5:
case 6:
/* blink (slow) and blink (rapid): for our purposes, the same code */
attron(A_BLINK);
break;
case 7:
/* image: negative (for our purposes, inverse video) */
attron(A_REVERSE);
break;
case 8:
/* conceal (we'll implement as A_INVIS) */
attron(A_INVIS);
break;
case 9:
/* crossed-out (strikethrough) */
// Not supported
break;
/* case 10-19, set terminal font -- not supported */
/* case 20, Fraktur -- not supported */
case 21:
/* bold: off or underline: double (we support bold: off) */
attroff(A_BOLD);
break;
case 22:
/* normal color or intensity */
attrset(A_NORMAL);
break;
case 23:
/* italic/Fraktur: off, implemented here as inverse video: off */
attroff(A_REVERSE);
break;
case 24:
/* underline: none */
attroff(A_UNDERLINE);
break;
case 25:
/* blink off */
attroff(A_BLINK);
break;
/* case 26: this code is reserved, not supported */
case 27:
/* image: positive, implemented here as inverse video: off */
attroff(A_REVERSE);
break;
case 28:
/* reveal */
attroff(A_INVIS);
break;
/* case 29: strike-through off, "not crossed out", not supported */
case 30:
case 31:
case 32:
case 33:
case 34:
case 35:
case 36:
case 37:
/* set text color */
attroff(A_BOLD);
attroff(COLOR_PAIR(COLOR_PAIR_INDEX(colors[__fg_clr], colors[__bg_clr])));
__fg_clr = (iargs[i] - 30);
init_pair(COLOR_PAIR_INDEX(colors[__fg_clr], colors[__bg_clr]), colors[__fg_clr], colors[__bg_clr]);
attron(COLOR_PAIR(COLOR_PAIR_INDEX(colors[__fg_clr], colors[__bg_clr])));
break;
/* case 38: reserved, sometimes used for extended/RGB foreground color, not supported -- see code 48 */
// TODO: might be neat to have some kind of support for these two codes
case 39:
/* default text color -- implementation defined according to standard. Implemented here as a reset code, basically. */
attrset(A_NORMAL);
break;
case 40:
case 41:
case 42:
case 43:
case 44:
case 45:
case 46:
case 47:
/* set background color */
attroff(A_BOLD);
attroff(COLOR_PAIR(COLOR_PAIR_INDEX(colors[__fg_clr], colors[__bg_clr])));
__bg_clr = (iargs[i] - 40);
init_pair(COLOR_PAIR_INDEX(colors[__fg_clr], colors[__bg_clr]), colors[__fg_clr], colors[__bg_clr]);
attron(COLOR_PAIR(COLOR_PAIR_INDEX(colors[__fg_clr], colors[__bg_clr])));
break;
/* case 48: reserved, sometimes used for extended/RGB background color, not supported -- see code 38 */
case 49:
/* default background color -- implementation defined according to standard. Implemented here as a reset code, basically. */
attrset(A_NORMAL);
break;
case 90:
case 91:
case 92:
case 93:
case 94:
case 95:
case 96:
case 97:
/* set text color, hi-intensity -- not standard but commonly provided */
// Note: this adds HI_INTENSITY to get the bright colors -- this might not be portable, on some
// terminals this might activate blinking, etc. The #ifdef-ed out code below uses the bold attribute instead
// to indicate hi-intensity colors, which might be more portable.
#if 0
attron(A_BOLD);
attroff(COLOR_PAIR(COLOR_PAIR_INDEX(colors[__fg_clr], colors[__bg_clr])));
__fg_clr = (iargs[i] - 90);
init_pair(COLOR_PAIR_INDEX(colors[__fg_clr], colors[__bg_clr]), colors[__fg_clr], colors[__bg_clr]);
attron(COLOR_PAIR(COLOR_PAIR_INDEX(colors[__fg_clr], colors[__bg_clr])));
#else
attroff(COLOR_PAIR(COLOR_PAIR_INDEX(colors[__fg_clr], colors[__bg_clr])));
__fg_clr = (iargs[i] - 90) + HI_INTENSITY;
init_pair(COLOR_PAIR_INDEX(colors[__fg_clr], colors[__bg_clr]), colors[__fg_clr], colors[__bg_clr]);
attron(COLOR_PAIR(COLOR_PAIR_INDEX(colors[__fg_clr], colors[__bg_clr])));
#endif
break;
case 100:
case 101:
case 102:
case 103:
case 104:
case 105:
case 106:
case 107:
/* set background color, hi-intensity -- not standard but commonly provided */
// note comment above about HI_INTENSITY
attroff(COLOR_PAIR(COLOR_PAIR_INDEX(colors[__fg_clr], colors[__bg_clr])));
__bg_clr = (iargs[i] - 100) + HI_INTENSITY;
init_pair(COLOR_PAIR_INDEX(colors[__fg_clr], colors[__bg_clr]), colors[__fg_clr], colors[__bg_clr]);
attron(COLOR_PAIR(COLOR_PAIR_INDEX(colors[__fg_clr], colors[__bg_clr])));
break;
/*
The remaining SGR codes are reserved or not supported:
50 - reserved
51 - framed
52 - encircled
53 - overlined
54 - not framed or encircled
55 - not overlined
56-59 - reserved
60-65 - ideogram codes
*/
}
}
}
static const char* __exec_csi(const char* str) {
const char* w, *arg;
bool has_args;
u32 iargs[8] = {MISSING_ARG, MISSING_ARG, MISSING_ARG, MISSING_ARG,
MISSING_ARG, MISSING_ARG, MISSING_ARG, MISSING_ARG};
u32 x, y;
u32 sx, sy;
u32 cargs = 0;
static u32 save_x = 0, save_y = 0;
w = str;
forever {
if (!(*w))
return w; // ???
if (__is_final_ch(*w))
break;
++w;
}
/* determine integer args, if any */
has_args = (w != str);
if (has_args)
{
arg = str;
while (arg < w)
{
if (*arg == ';')
{
iargs[cargs++] = MISSING_ARG;
++arg;
continue;
}
else
{
u32 ag;
ag = atoi(arg);
iargs[cargs++] = ag;
while (arg < w)
{
if (*arg == ';')
break;
++arg;
}
++arg;
}
}
}
if (MISSING_ARG == iargs[0] && MISSING_ARG == iargs[1])
has_args = false;
// convenience macros for the first two args
#define i1 iargs[0]
#define i2 iargs[1]
switch (*w)
{
case 'A':
/* cursor up */
__cursor_move_delta(0, has_args ? -i1 : -1);
break;
case 'B':
/* cursor down */
__cursor_move_delta(0, has_args ? i1 : 1);
break;
case 'C':
/* cursor forward */
__cursor_move_delta(has_args ? i1 : 1, 0);
break;
case 'D':
/* cursor back */
__cursor_move_delta(has_args ? -i1 : -1, 0);
break;
case 'E':
/* cursor next line */
getyx(stdscr, y, x);
if (has_args)
y += i1;
else
++y;
x = 0;
move(y, x);
break;
case 'F':
/* cursor previous line */
getyx(stdscr, y, x);
if (has_args)
y -= i1;
else
--y;
x = 0;
if (y < 0)
y = 0;
move(y, x);
break;
case 'G':
/* cursor horizontal absolute */
// TODO: is this 0-based or 1-based? I'm treating the argument as 1-based.
getyx(stdscr, y, x);
if (has_args)
x = i1 - 1;
assert(x >= 0);
move(y, x);
break;
case 'H':
/* cursor position */
// this IS 1-based
getyx(stdscr, y, x);
if (i1 == MISSING_ARG)
y = 0;
else
y = i1 - 1;
if (MISSING_ARG == i2)
x = 0;
else
x = i2 - 1;
move(y, x);
break;
case 'J':
/* erase display */
if (0 == i1 || MISSING_ARG == i1)
{
// clear from the cursor to the end of the screen
__clear_to_end();
}
else if (1 == i1)
{
// clear from the cursor to the start of the screen
__clear_to_start();
}
else if (i1 == 2)
{
// clear the entire screen and move to upper-left corner
erase();
move(0, 0);
}
break;
case 'K':
/* erase in line */
getyx(stdscr, y, x);
getmaxyx(stdscr, sy, sx);
if (0 == i1 || MISSING_ARG == i1)
// clear from cursor to end of line
__clear_line_between(x, sx - 1, y);
else if (1 == i1)
// clear from cursor to beginning of line
__clear_line_between(0, x, y);
else if (2 == i1)
// clear entire line
__clear_lines(y, y, sx - 1);
move(y, x);
break;
case 'S':
/* scroll up */
if (MISSING_ARG == i1)
scroll(stdscr);
else
scrl(i1);
break;
case 'T':
/* scroll down */
// TODO: implement this feature. The single argument, if present, is the number of lines to scroll.
assert(false);
break;
case 'f':
/* horizontal and vertical position */
move((i1 == MISSING_ARG) ? 0 : i1 - 1, (i2 == MISSING_ARG) ? 0 : i2 - 1);
break;
case 'm':
/* select graphic rendition (SGR) */
__set_sgr_parameters(iargs, cargs);
break;
case 's':
/* save cursor position */
getyx(stdscr, save_y, save_x);
break;
case 'u':
/* restore cursor position */
move(save_y, save_x);
break;
case '?':
/* supported DECTCEM codes: ?25l: hide the cursor, ?25h: shows the cursor */
++w;
if (!strncmp(w, "25", 2))
{
w += 2;
switch (*w)
{
case 'l':
/* hide the cursor */
curs_set(0);
break;
case 'h':
/* show the cursor */
curs_set(1);
break;
}
}
break;
/*** Not implemented:
5i AUX port on
4i AUX port off
6(n) device status report
***/
}
#undef i2
#undef i1
return(w + 1);
}
static const char* __exec_termcode(const char* str)
{
bool is_csi;
if (*str == CHAR_CSI)
{
is_csi = true;
++str;
}
else if (*str == CHAR_ESC && *(str + 1) == '[')
{
is_csi = true;
str += 2;
}
if (is_csi)
{
// CSI escape codes
return __exec_csi(str);
}
else
{
// TODO: non-CSI escape codes -- none of these are currently supported
/***
Some that might be useful to add:
ESC N, ESC O: these indicate the following single character is from an
alternate character set (like IBM-PC extended characters)
ESC ]: Operating system command. Need to find a reference for what
these codes indicate.
***/
++str;
}
return(str);
}
void ansi_puts(const char* str)
{
ensure_curses_lib();
forever
{
if (!*str)
break;
if (__is_termcode(str))
{
str = __exec_termcode(str);
}
else
{
addch(*str);
++str;
}
}
refresh();
}
#endif // PDCURSES_OK
/* end ansi.cpp */