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
|
// 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_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_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_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
#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_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_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_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);
}
#endif // ESCAPE_IMPLEMENTATION
|