-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathansi_console_extended.h
More file actions
357 lines (313 loc) · 12.9 KB
/
Copy pathansi_console_extended.h
File metadata and controls
357 lines (313 loc) · 12.9 KB
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
/** ansi_console_extended.h made by William Dawson (MrBisquit on GitHub)
* GitHub: https://github.com/MrBisquit/ansi_console
* File: https://github.com/MrBisquit/ansi_console/tree/main/ansi_console.h
* License: SPDX-License-Identifier: MIT
* See LICENSE file in the project root for full license text.
*/
#pragma once
#include <stdio.h>
#include <stdint.h>
#ifndef CONSOLE_H
/**
Information on ANSI escape codes are available here: https://gist.github.com/ConnerWill/d4b6c776b509add763e17f9f113fd25b
See examples: https://stackoverflow.com/a/54062826
Truecolor: https://en.wikipedia.org/wiki/Color_depth#True_color_.2824-bit.29
Name FG BG
Black 30 40
Red 31 41
Green 32 42
Yellow 33 43
Blue 34 44
Magenta 35 45
Cyan 36 46
White 37 47
Bright Black 90 100
Bright Red 91 101
Bright Green 92 102
Bright Yellow 93 103
Bright Blue 94 104
Bright Magenta 95 105
Bright Cyan 96 106
Bright White 97 107
This file uses the American spelling of colour (color).
*/
#pragma region // Definitions
// Color definitions
// Foreground
#define CONSOLE_FG_BLACK (uint8_t)30
#define CONSOLE_FG_RED (uint8_t)31
#define CONSOLE_FG_GREEN (uint8_t)32
#define CONSOLE_FG_YELLOW (uint8_t)33
#define CONSOLE_FG_BLUE (uint8_t)34
#define CONSOLE_FG_MAGENTA (uint8_t)35
#define CONSOLE_FG_CYAN (uint8_t)36
#define CONSOLE_FG_WHITE (uint8_t)37
#define CONSOLE_FG_BRIGHT_BLACK (uint8_t)90
#define CONSOLE_FG_BRIGHT_RED (uint8_t)91
#define CONSOLE_FG_BRIGHT_GREEN (uint8_t)92
#define CONSOLE_FG_BRIGHT_YELLOW (uint8_t)93
#define CONSOLE_FG_BRIGHT_BLUE (uint8_t)94
#define CONSOLE_FG_BRIGHT_MAGENTA (uint8_t)95
#define CONSOLE_FG_BRIGHT_CYAN (uint8_t)96
#define CONSOLE_FG_BRIGHT_WHITE (uint8_t)97
// Background
#define CONSOLE_BG_BLACK (uint8_t)40
#define CONSOLE_BG_RED (uint8_t)41
#define CONSOLE_BG_GREEN (uint8_t)42
#define CONSOLE_BG_YELLOW (uint8_t)43
#define CONSOLE_BG_BLUE (uint8_t)44
#define CONSOLE_BG_MAGENTA (uint8_t)45
#define CONSOLE_BG_CYAN (uint8_t)46
#define CONSOLE_BG_WHITE (uint8_t)47
#define CONSOLE_BG_BRIGHT_BLACK (uint8_t)100
#define CONSOLE_BG_BRIGHT_RED (uint8_t)101
#define CONSOLE_BG_BRIGHT_GREEN (uint8_t)102
#define CONSOLE_BG_BRIGHT_YELLOW (uint8_t)103
#define CONSOLE_BG_BRIGHT_BLUE (uint8_t)104
#define CONSOLE_BG_BRIGHT_MAGENTA (uint8_t)105
#define CONSOLE_BG_BRIGHT_CYAN (uint8_t)106
#define CONSOLE_BG_BRIGHT_WHITE (uint8_t)107
// Colors/Graphics mode
#define CONSOLE_GRAPHICS_RESET (uint8_t)0
#define CONSOLE_GRAPHICS_BOLD (uint8_t)1
#define CONSOLE_GRAPHICS_DIM (uint8_t)2
#define CONSOLE_GRAPHICS_ITALIC (uint8_t)3
#define CONSOLE_GRAPHICS_UNDERLINE (uint8_t)4
#define CONSOLE_GRAPHICS_BLINKING (uint8_t)5
#define CONSOLE_GRAPHICS_INVERSE_REVERSE (uint8_t)7
#define CONSOLE_GRAPHICS_HIDDEN_INVISIBLE (uint8_t)8
#define CONSOLE_GRAPHICS_STRIKETHROUGH (uint8_t)9
// Reset sequences
#define CONSOLE_GRAPHICS_RESET_BOLD (uint8_t)22
#define CONSOLE_GRAPHICS_RESET_DIM (uint8_t)22
#define CONSOLE_GRAPHICS_RESET_ITALIC (uint8_t)23
#define CONSOLE_GRAPHICS_RESET_UNDERLINE (uint8_t)24
#define CONSOLE_GRAPHICS_RESET_BLINKING (uint8_t)25
#define CONSOLE_GRAPHICS_RESET_INVERSE_REVERSE (uint8_t)27
#define CONSOLE_GRAPHICS_RESET_HIDDEN_INVISIBLE (uint8_t)28
#define CONSOLE_GRAPHICS_RESET_STRIKETHROUGH (uint8_t)29
// Screen modes
#define CONSOLE_MODE_40x25_MONOCHROME (uint8_t)0
#define CONSOLE_MODE_40x25_COLOR (uint8_t)1
#define CONSOLE_MODE_80x25_MONOCHROME (uint8_t)2
#define CONSOLE_MODE_80x25_COLOR (uint8_t)3
#define CONSOLE_MODE_320x200_4_COLOR (uint8_t)4
#define CONSOLE_MODE_320x200_MONOCHROME (uint8_t)5
#define CONSOLE_MODE_640x200_MONOCHROME (uint8_t)6
#define CONSOLE_MODE_LINE_WRAPPING (uint8_t)7 // I have no idea why this is in the middle of here and not at one end
#define CONSOLE_MODE_320x200_COLOR (uint8_t)13
#define CONSOLE_MODE_640x200_16_COLOR (uint8_t)14
#define CONSOLE_MODE_640x350_MONOCHROME (uint8_t)15
#define CONSOLE_MODE_640x350_16_COLOR (uint8_t)16
#define CONSOLE_MODE_640x480_MONOCHROME (uint8_t)17
#define CONSOLE_MODE_640x480_16_COLOR (uint8_t)18
#define CONSOLE_MODE_320x200_256_COLOR (uint8_t)19
#pragma endregion
#pragma region // Colors
/// @brief This can set both the foreground and background color
/// @param color The color (definitions beginning with `CONSOLE_FG` or `CONSOLE_BG`)
void console_set_color(uint8_t color) {
printf("\x1B[%dm", color);
}
/// @brief This can set both the foreground and background colour
/// @param stream The stream to write the ANSI escape code to
/// @param color The color (definitions beginning with `CONSOLE_FG` or `CONSOLE_BG`)
void fconsole_set_color(FILE* stream, uint8_t color) {
fprintf(stream, "\x1B[%dm", color);
}
/// @brief Set the console foreground color with RGB (If your terminal supports Truecolor)
/// @param r Red
/// @param g Green
/// @param b Blue
void console_set_foreground_rgb(uint8_t r, uint8_t g, uint8_t b) {
printf("\x1B[38;2;{%d};{%d};{%d}m", r, g, b);
}
/// @brief Set the console foreground color with RGB (If your terminal supports Truecolor)
/// @param stream The stream to write the ANSI escape code to
/// @param r Red
/// @param g Green
/// @param b Blue
void fconsole_set_foreground_rgb(FILE* stream, uint8_t r, uint8_t g, uint8_t b) {
fprintf(stream, "\x1B[38;2;{%d};{%d};{%d}m", r, g, b);
}
/// @brief Set the console background color with RGB (If your terminal supports Truecolor)
/// @param r Red
/// @param g Green
/// @param b Blue
void console_set_background_rgb(uint8_t r, uint8_t g, uint8_t b) {
printf("\x1B[48;2;{%d};{%d};{%d}m", r, g, b);
}
/// @brief Set the console background color with RGB (If your terminal supports Truecolor)
/// @param stream The stream to write the ANSI escape code to
/// @param r Red
/// @param g Green
/// @param b Blue
void fconsole_set_background_rgb(FILE* stream, uint8_t r, uint8_t g, uint8_t b) {
fprintf(stream, "\x1B[48;2;{%d};{%d};{%d}m", r, g, b);
}
/// @brief Resets console color
void console_reset_color() {
printf("\033[0m");
}
/// @brief Resets console color
/// @param stream The stream to write the ANSI escape code to
void fconsole_reset_color(FILE* stream) {
fprintf(stream, "\033[0m");
}
#pragma endregion
#pragma region // Cursor
/// @brief Resets the console cursor back to (0,0)
void console_reset_cursor() {
printf("\x1B[H");
}
/// @brief Resets the console cursor back to (0,0)
/// @param stream The stream to write the ANSI escape code to
void fconsole_reset_cursor(FILE* stream) {
fprintf(stream, "\x1B[H");
}
/// @brief Moves the console cursor to the specified line and column
/// @param line The line to move the console cursor to
/// @param column The column to move the console cursor to
void console_move_cursor(int line, int column) {
printf("\x1B[%d;%dH", line, column);
printf("\x1B[%d;%df", line, column);
}
/// @brief Moves the console cursor to the specified line and column
/// @param stream The stream to write the ANSI escape code to
/// @param line The line to move the console cursor to
/// @param column The column to move the console cursor to
void fconsole_move_cursor(FILE* stream, int line, int column) {
fprintf(stream, "\x1B[%d;%dH", line, column);
fprintf(stream, "\x1B[%d;%df", line, column);
}
#pragma endregion
#pragma region // Clearing
/// @brief Clears the screen
void console_clear_screen() {
printf("\x1B[2J");
}
/// @brief Clears the screen
/// @param stream The stream to write the ANSI escape code to
void fconsole_clear_screen(FILE* stream) {
fprintf(stream, "\x1B[2J");
}
/// @brief Clears the current line
/// @note You may want to move the cursor to the start of the line with `\r`
void console_clear_line() {
printf("\x1B[2K");
}
/// @brief Clears the current line
/// @note You may want to move the cursor to the start of the line with `\r`
/// @param stream The stream to write the ANSI escape code to
void fconsole_clear_line(FILE* stream) {
fprintf(stream, "\x1B[2K");
}
#pragma endregion
#pragma region // Graphics
/// @brief Sets the graphics mode
/// @param graphics The graphics mode (definitions beginning with `CONSOLE_GRPAHICS`)
void console_graphics_set(uint8_t graphics) {
printf("\x1B[%dm", graphics);
}
/// @brief Sets the graphics mode
/// @param stream The stream to write the ANSI escape code to
/// @param graphics The graphics mode (definitions beginning with `CONSOLE_GRPAHICS`)
void fconsole_graphics_set(FILE* stream, uint8_t graphics) {
fprintf(stream, "\x1B[%dm", graphics);
}
#pragma endregion
#pragma region // Mode
/// @brief Sets the console screen mode
/// @param mode The console screen mode (definitions beginning with `CONSOLE_MODE`)
void console_mode_set(uint8_t mode) {
printf("\x1B[=%dh", mode);
}
/// @brief Sets the console screen mode
/// @param stream The stream to write the ANSI escape code to
/// @param mode The console screen mode (definitions beginning with `CONSOLE_MODE`)
void fconsole_mode_set(FILE* stream, uint8_t mode) {
fprintf(stream, "\x1B[=%dh", mode);
}
/// @brief Resets the console screen mode by using the same values as setting does
/// @param mode The console screen mode (definitions beginning with `CONSOLE_MODE`)
void console_mode_reset(uint8_t mode) {
printf("\x1B[=%dl", mode);
}
/// @brief Resets the console screen mode by using the same values as setting does
/// @param stream The stream to write the ANSI escape code to
/// @param mode The console screen mode (definitions beginning with `CONSOLE_MODE`)
void fconsole_mode_reset(FILE* stream, uint8_t mode) {
fprintf(stream, "\x1B[=%dl", mode);
}
#pragma endregion
#pragma region // Private
// Console private modes
/// @brief Sets the cursor to be invisible
/// @note These are implemented in most terminals, but not all
void console_private_cursor_invisible() {
printf("\x1B[?25l");
}
// Console private modes
/// @brief Sets the cursor to be invisible
/// @note These are implemented in most terminals, but not all
/// @param stream The stream to write the ANSI escape code to
void fconsole_private_cursor_invisible(FILE* stream) {
fprintf(stream, "\x1B[?25l");
}
/// @brief Sets the cursor to be visible
/// @note These are implemented in most terminals, but not all
void console_private_cursor_visible() {
printf("\x1B[?25h");
}
/// @brief Sets the cursor to be visible
/// @note These are implemented in most terminals, but not all
/// @param stream The stream to write the ANSI escape code to
void fconsole_private_cursor_visible(FILE* stream) {
fprintf(stream, "\x1B[?25h");
}
/// @brief Restores the screen
/// @note These are implemented in most terminals, but not all
void console_private_screen_restore() {
printf("\x1B[?47l");
}
/// @brief Restores the screen
/// @note These are implemented in most terminals, but not all
/// @param stream The stream to write the ANSI escape code to
void fconsole_private_screen_restore(FILE* stream) {
fprintf(stream, "\x1B[?47l");
}
/// @brief Saves the screen
/// @note These are implemented in most terminals, but not all
void console_private_screen_save() {
printf("\x1B[?47h");
}
/// @brief Saves the screen
/// @note These are implemented in most terminals, but not all
/// @param stream The stream to write the ANSI escape code to
void fconsole_private_screen_save(FILE* stream) {
fprintf(stream, "\x1B[?47h");
}
/// @brief Enables the alternate buffer
/// @note These are implemented in most terminals, but not all
void console_private_alternate_buffer_enable() {
printf("\x1B[?1049l");
}
/// @brief Enables the alternate buffer
/// @note These are implemented in most terminals, but not all
/// @param stream The stream to write the ANSI escape code to
void fconsole_private_alternate_buffer_enable(FILE* stream) {
fprintf(stream, "\x1B[?1049l");
}
/// @brief Disables the alternate buffer
/// @note These are implemented in most terminals, but not all
void console_private_alternate_buffer_disable() {
printf("\x1B[?1049h");
}
/// @brief Disables the alternate buffer
/// @note These are implemented in most terminals, but not all
/// @param stream The stream to write the ANSI escape code to
void fconsole_private_alternate_buffer_disable(FILE* stream) {
fprintf(stream, "\x1B[?1049h");
}
#pragma endregion
#endif // CONSOLE_H