-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathansi_console.hpp
More file actions
150 lines (130 loc) · 4.26 KB
/
Copy pathansi_console.hpp
File metadata and controls
150 lines (130 loc) · 4.26 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
/** ansi_console.hpp 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.hpp
* 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).
*/
namespace Console {
enum Color {
Black = 30,
Red = 31,
Green = 32,
Yellow = 33,
Blue = 34,
Magenta = 35,
Cyan = 36,
White = 37,
Bright_Black = 90,
Bright_Red = 91,
Bright_Green = 92,
Bright_Yellow = 93,
Bright_Blue = 94,
Bright_Magenta = 95,
Bright_Cyan = 96,
Bright_White = 97
};
enum GraphicsMode {
Reset = 0,
Bold = 1,
Dim = 2,
Italic = 3,
Underline = 4,
Blinking = 5,
Reverse = 7,
Invisible = 8,
Strikethrough = 9
};
enum ScreenMode {
_40x25_MONOCHROME = 0,
_40x25_COLOR = 1,
_80x25_MONOCHROME = 2,
_80x25_COLOR = 3,
_320x200_4_COLOR = 4,
_320x200_MONOCHROME = 5,
_640x200_MONOCHROME = 6,
_LINE_WRAPPING = 7, // I have no idea why this is in the middle of here and not at one end
_320x200_COLOR = 13,
_640x200_16_COLOR = 14,
_640x350_MONOCHROME = 15,
_640x350_16_COLOR = 16,
_640x480_MONOCHROME = 17,
_640x480_16_COLOR = 18,
_320x200_256_COLOR = 19
};
/// @brief Sets the foreground color
/// @param color The color
void set_foreground_color(Color color) {
printf("\x1B[%dm", (uint8_t)color);
}
/// @brief Sets the background color
/// @param color The color
void set_background_color(Color color) {
printf("\x1B[%dm", ((uint8_t)color) + 10);
}
/// @brief Sets the foreground color with RGB (If your terminal supports Truecolor)
/// @param r Red
/// @param g Green
/// @param b Blue
void set_foreground_rgb(uint8_t r, uint8_t g, uint8_t b) {
printf("\x1B[38;2;{%d};{%d};{%d}m", r, g, b);
}
/// @brief Sets the background color with RGB (If your terminal supports Truecolor)
/// @param r Red
/// @param g Green
/// @param b Blue
void set_background_rgb(uint8_t r, uint8_t g, uint8_t b) {
printf("\x1B[48;2;{%d};{%d};{%d}m", r, g, b);
}
/// @brief Resets console color
void reset_color() {
printf("\033[0m");
}
/// @brief Sets the graphics mode
/// @param mode The graphics mode
void graphics_set(GraphicsMode mode) {
printf("\x1B[%dm", (uint8_t)mode);
}
/// @brief Reset the graphics node
/// @param mode The graphics mode
void graphics_reset(GraphicsMode mode) {
if(mode == GraphicsMode::Dim) {
printf("\x1B[22m");
return;
}
printf("\x1B[%dm", ((uint8_t)mode) + 21);
}
void mode_set(ScreenMode mode) {
printf("\x1B[=%dh", (uint8_t)mode);
}
void mode_reset(ScreenMode mode) {
printf("\x1B[=%dl", (uint8_t)mode);
}
}
#endif // CONSOLE_H