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
|
// escape.h - ESC escape code macros and functions
//
// Thread safety: Safe for concurrent use (no shared state). The macros
// produce string literals; the functions write to the provided FILE*.
// Portability: ANSI escape codes are not supported on Windows Console API
// (use Windows 10+ VT mode or a translation layer).
#ifndef ESCAPE_H
#define ESCAPE_H
#include "type.h"
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
#define CSI "\x1B["
#define ESC_CURSOR_UP(n) CSI #n "A"
void esc_cursor_up(u32 n);
void esc_fcursor_up(FILE *, u32 n);
#define ESC_CURSOR_DOWN(n) CSI #n "B"
void esc_cursor_down(u32 n);
void esc_fcursor_down(FILE *, u32 n);
#define ESC_CURSOR_LEFT(n) CSI #n "D"
void esc_cursor_left(u32 n);
void esc_fcursor_left(FILE *, u32 n);
#define ESC_CURSOR_RIGHT(n) CSI #n "C"
void esc_cursor_right(u32 n);
void esc_fcursor_right(FILE *, u32 n);
#define ESC_CURSOR_LINE_NEXT(n) CSI #n "E"
void esc_cursor_line_next(u32 n);
void esc_fcursor_line_next(FILE *, u32 n);
#define ESC_CURSOR_PREVIOUS(n) CSI #n "F"
void esc_cursor_previous(u32 n);
void esc_fcursor_previous(FILE *, u32 n);
#define ESC_CURSOR_POSITION(col, row) CSI #row ";" #col "H"
void esc_cursor_position(u32 col, u32 row);
void esc_fcursor_position(FILE *, u32 col, u32 row);
#define ESC_CURSOR_COLUMN_HOME CSI "G"
#define ESC_CLEAR_SCREEN CSI "2J"
void esc_clear_screen(void);
void esc_fclear_screen(FILE *);
#define ESC_CLEAR_SCREEN_START CSI "1J"
void esc_clear_screen_start(void);
void esc_fclear_screen_start(FILE *);
#define ESC_CLEAR_SCREEN_END CSI "0J"
void esc_clear_screen_end(void);
void esc_fclear_screen_end(FILE *);
#define ESC_CLEAR_LINE CSI "2K"
void esc_clear_line(void);
void esc_fclear_line(FILE *);
#define ESC_CLEAR_LINE_START CSI "1K"
void esc_clear_line_start(void);
void esc_fclear_line_start(FILE *);
#define ESC_CLEAR_LINE_END CSI "0K"
void esc_clear_line_end(void);
void esc_fclear_line_end(FILE *);
#define ESC_SCROLL_UP(n) CSI #n "S"
void esc_scroll_up(u32 n);
void esc_fscroll_up(FILE *, u32 n);
#define ESC_SCROLL_DOWN(n) CSI #n "T"
void esc_scroll_down(u32 n);
void esc_fscroll_down(FILE *, u32 n);
#define ESC_SGR "m"
#define ESC_RESET CSI "0" ESC_SGR
#define ESC_BOLD CSI "1" ESC_SGR
#define ESC_DIM CSI "2" ESC_SGR
#define ESC_ITALIC CSI "3" ESC_SGR
#define ESC_UNDERLINE CSI "4" ESC_SGR
#define ESC_BLINK CSI "5" ESC_SGR
#define ESC_REVERSE CSI "7" ESC_SGR
#define ESC_CONCEAL CSI "8" ESC_SGR
#define ESC_REVEAL CSI "28" ESC_SGR
#define ESC_STRIKETHROUGH CSI "9" ESC_SGR
#define ESC_OVERLINE CSI "53" ESC_SGR
#define ESC_COLOR_FG_RGB(r, g, b) CSI "38;2;" #r ";" #g ";" #b ESC_SGR
void esc_color_fg_rgb(u32 r, u32 g, u32 b);
void esc_fcolor_fg_rgb(FILE *, u32 r, u32 g, u32 b);
#define ESC_COLOR_BG_RGB(r, g, b) CSI "48;2;" #r ";" #g ";" #b ESC_SGR
void esc_color_bg_rgb(u32 r, u32 g, u32 b);
void esc_fcolor_bg_rgb(FILE *, u32 r, u32 g, u32 b);
#define ESC_COLOR_FG_256(n) CSI "38;5;" #n ESC_SGR
void esc_color_fg_256(u32 n);
void esc_fcolor_fg_256(FILE *, u32 n);
#define ESC_COLOR_BG_256(n) CSI "48;5;" #n ESC_SGR
void esc_color_bg_256(u32 n);
void esc_fcolor_bg_256(FILE *, u32 n);
#define ESC_COLOR_FG(n) CSI "3" #n ESC_SGR
#define ESC_COLOR_BG(n) CSI "4" #n ESC_SGR
#define ESC_COLOR_FG_BRIGHT(n) CSI "9" #n ESC_SGR
#define ESC_COLOR_BG_BRIGHT(n) CSI "10" #n ESC_SGR
/* Named standard colors (ANSI 0-7). */
#define ESC_COLOR_BLACK ESC_COLOR_FG(0)
#define ESC_COLOR_RED ESC_COLOR_FG(1)
#define ESC_COLOR_GREEN ESC_COLOR_FG(2)
#define ESC_COLOR_YELLOW ESC_COLOR_FG(3)
#define ESC_COLOR_BLUE ESC_COLOR_FG(4)
#define ESC_COLOR_MAGENTA ESC_COLOR_FG(5)
#define ESC_COLOR_CYAN ESC_COLOR_FG(6)
#define ESC_COLOR_WHITE ESC_COLOR_FG(7)
#define ESC_CURSOR_HIDE CSI "?25l"
void esc_cursor_hide(void);
void esc_fcursor_hide(FILE *);
#define ESC_CURSOR_SHOW CSI "?25h"
void esc_cursor_show(void);
void esc_fcursor_show(FILE *);
#define ESC_CURSOR_SAVE CSI "s"
void esc_cursor_save(void);
void esc_fcursor_save(FILE *);
#define ESC_CURSOR_RESTORE CSI "u"
void esc_cursor_restore(void);
void esc_fcursor_restore(FILE *);
#define ESC_BELL "\x07"
void esc_bell(void);
void esc_fbell(FILE *);
#define ESC_ALT_SCREEN_ON CSI "?1049h"
void esc_alt_screen_on(void);
void esc_falt_screen_on(FILE *);
#define ESC_ALT_SCREEN_OFF CSI "?1049l"
void esc_alt_screen_off(void);
void esc_falt_screen_off(FILE *);
#define ESC_BRACKETED_PASTE_ON CSI "?2004h"
void esc_bracketed_paste_on(void);
void esc_fbracketed_paste_on(FILE *);
#define ESC_BRACKETED_PASTE_OFF CSI "?2004l"
void esc_bracketed_paste_off(void);
void esc_fbracketed_paste_off(FILE *);
#define ESC_KITTY_ON CSI ">1u"
void esc_kitty_on(void);
void esc_fkitty_on(FILE *);
#define ESC_KITTY_OFF CSI ">0u"
void esc_kitty_off(void);
void esc_fkitty_off(FILE *);
#ifdef __cplusplus
}
#endif
#endif // ESCAPE_H
#ifdef ESCAPE_IMPLEMENTATION
#include <stdio.h>
void esc_fcursor_up(FILE *f, u32 n) { if (!f) return; fprintf(f, CSI "%uA", (unsigned)n); }
void esc_fcursor_down(FILE *f, u32 n) { if (!f) return; fprintf(f, CSI "%uB", (unsigned)n); }
void esc_fcursor_left(FILE *f, u32 n) { if (!f) return; fprintf(f, CSI "%uD", (unsigned)n); }
void esc_fcursor_right(FILE *f, u32 n) { if (!f) return; fprintf(f, CSI "%uC", (unsigned)n); }
void esc_fcursor_line_next(FILE *f, u32 n) { if (!f) return; fprintf(f, CSI "%uE", (unsigned)n); }
void esc_fcursor_previous(FILE *f, u32 n) { if (!f) return; fprintf(f, CSI "%uF", (unsigned)n); }
void esc_fcursor_position(FILE *f, u32 col, u32 row) {
if (!f) return;
fprintf(f, CSI "%u;%uH", (unsigned)row, (unsigned)col);
}
void esc_fclear_screen(FILE *f) { if (!f) return; fputs(CSI "2J", f); }
void esc_fclear_screen_start(FILE *f) { if (!f) return; fputs(CSI "1J", f); }
void esc_fclear_screen_end(FILE *f) { if (!f) return; fputs(CSI "0J", f); }
void esc_fclear_line(FILE *f) { if (!f) return; fputs(CSI "2K", f); }
void esc_fclear_line_start(FILE *f) { if (!f) return; fputs(CSI "1K", f); }
void esc_fclear_line_end(FILE *f) { if (!f) return; fputs(CSI "0K", f); }
void esc_fscroll_up(FILE *f, u32 n) { if (!f) return; fprintf(f, CSI "%uS", (unsigned)n); }
void esc_fscroll_down(FILE *f, u32 n) { if (!f) return; fprintf(f, CSI "%uT", (unsigned)n); }
void esc_fcolor_fg_rgb(FILE *f, u32 r, u32 g, u32 b) {
if (!f) return;
fprintf(f, CSI "38;2;%u;%u;%u" ESC_SGR, (unsigned)r, (unsigned)g, (unsigned)b);
}
void esc_fcolor_bg_rgb(FILE *f, u32 r, u32 g, u32 b) {
if (!f) return;
fprintf(f, CSI "48;2;%u;%u;%u" ESC_SGR, (unsigned)r, (unsigned)g, (unsigned)b);
}
void esc_fcolor_fg_256(FILE *f, u32 n) {
if (!f) return;
fprintf(f, CSI "38;5;%u" ESC_SGR, (unsigned)n);
}
void esc_fcolor_bg_256(FILE *f, u32 n) {
if (!f) return;
fprintf(f, CSI "48;5;%u" ESC_SGR, (unsigned)n);
}
void esc_fcursor_hide(FILE *f) { if (!f) return; fputs(CSI "?25l", f); }
void esc_fcursor_show(FILE *f) { if (!f) return; fputs(CSI "?25h", f); }
void esc_fcursor_save(FILE *f) { if (!f) return; fputs(CSI "s", f); }
void esc_fcursor_restore(FILE *f) { if (!f) return; fputs(CSI "u", f); }
void esc_fbell(FILE *f) { if (!f) return; fputc('\a', f); }
void esc_falt_screen_on(FILE *f) { if (!f) return; fputs(CSI "?1049h", f); }
void esc_falt_screen_off(FILE *f) { if (!f) return; fputs(CSI "?1049l", f); }
void esc_fbracketed_paste_on(FILE *f) { if (!f) return; fputs(CSI "?2004h", f); }
void esc_fbracketed_paste_off(FILE *f) { if (!f) return; fputs(CSI "?2004l", f); }
void esc_fkitty_on(FILE *f) { if (!f) return; fputs(CSI ">1u", f); }
void esc_fkitty_off(FILE *f) { if (!f) return; fputs(CSI ">0u", f); }
void esc_cursor_up(u32 n) { esc_fcursor_up(stdout, n); }
void esc_cursor_down(u32 n) { esc_fcursor_down(stdout, n); }
void esc_cursor_left(u32 n) { esc_fcursor_left(stdout, n); }
void esc_cursor_right(u32 n) { esc_fcursor_right(stdout, n); }
void esc_cursor_line_next(u32 n) { esc_fcursor_line_next(stdout, n); }
void esc_cursor_previous(u32 n) { esc_fcursor_previous(stdout, n); }
void esc_cursor_position(u32 col, u32 row) {
esc_fcursor_position(stdout, col, row);
}
void esc_clear_screen(void) { esc_fclear_screen(stdout); }
void esc_clear_screen_start(void) { esc_fclear_screen_start(stdout); }
void esc_clear_screen_end(void) { esc_fclear_screen_end(stdout); }
void esc_clear_line(void) { esc_fclear_line(stdout); }
void esc_clear_line_start(void) { esc_fclear_line_start(stdout); }
void esc_clear_line_end(void) { esc_fclear_line_end(stdout); }
void esc_scroll_up(u32 n) { esc_fscroll_up(stdout, n); }
void esc_scroll_down(u32 n) { esc_fscroll_down(stdout, n); }
void esc_color_fg_rgb(u32 r, u32 g, u32 b) {
esc_fcolor_fg_rgb(stdout, r, g, b);
}
void esc_color_bg_rgb(u32 r, u32 g, u32 b) {
esc_fcolor_bg_rgb(stdout, r, g, b);
}
void esc_color_fg_256(u32 n) {
esc_fcolor_fg_256(stdout, n);
}
void esc_color_bg_256(u32 n) {
esc_fcolor_bg_256(stdout, n);
}
void esc_cursor_hide(void) { esc_fcursor_hide(stdout); }
void esc_cursor_show(void) { esc_fcursor_show(stdout); }
void esc_cursor_save(void) { esc_fcursor_save(stdout); }
void esc_cursor_restore(void) { esc_fcursor_restore(stdout); }
void esc_bell(void) { esc_fbell(stdout); }
void esc_alt_screen_on(void) { esc_falt_screen_on(stdout); }
void esc_alt_screen_off(void) { esc_falt_screen_off(stdout); }
void esc_bracketed_paste_on(void) { esc_fbracketed_paste_on(stdout); }
void esc_bracketed_paste_off(void) { esc_fbracketed_paste_off(stdout); }
void esc_kitty_on(void) { esc_fkitty_on(stdout); }
void esc_kitty_off(void) { esc_fkitty_off(stdout); }
#endif // ESCAPE_IMPLEMENTATION
|