54 #define WIN32_LEAN_AND_MEAN
57#elif defined(__linux__)
58 #include <sys/ioctl.h>
74 CLR& operator=(
const CLR&) =
delete;
75 CLR& operator=(
CLR&&) =
delete;
91 inline static const std::string
fg(
Byte color)
92 {
return "\e[38;5;" + std::to_string(color) +
"m"; }
99 inline static const std::string
bg(
Byte color)
100 {
return "\e[48;5;" + std::to_string(color) +
"m"; }
110 {
return "\e[38;2;" + std::to_string(r) +
";" + std::to_string(g) +
";" + std::to_string(b) +
"m"; }
120 Uint8 r3 = (clr >> 5) & 0x7;
121 Uint8 g3 = (clr >> 2) & 0x7;
122 Uint8 b2 = clr & 0x3;
125 Uint8 r8 = (Uint8)((r3 * 255 + 3) / 7);
126 Uint8 g8 = (Uint8)((g3 * 255 + 3) / 7);
127 Uint8 b8 = (Uint8)((b2 * 255 + 1) / 3);
129 return fg_rgb(r8, g8, b8);
140 Uint8 r3 = (clr >> 5) & 0x7;
141 Uint8 g3 = (clr >> 2) & 0x7;
142 Uint8 b2 = clr & 0x3;
145 Uint8 r8 = (Uint8)((r3 * 255 + 3) / 7);
146 Uint8 g8 = (Uint8)((g3 * 255 + 3) / 7);
147 Uint8 b8 = (Uint8)((b2 * 255 + 1) / 3);
149 return bg_rgb(r8, g8, b8);
160 {
return "\e[48;2;" + std::to_string(r) +
";" + std::to_string(g) +
";" + std::to_string(b) +
"m"; }
169 {
return "\e[" + std::to_string(row) +
";" + std::to_string(col) +
"H"; }
192 std::fwrite(s.c_str(), 1, s.size(), stdout);
199 static bool draw_debug_text(
const std::string& text,
int x,
int y,
int ptsize = 12,
200 Uint8 r = 255, Uint8 g = 255, Uint8 b = 255, Uint8 a = 255);
223 {
return "\e[" + std::to_string(n) +
"J"; }
235 {
return "\e[" + std::to_string(n) +
"K"; }
245 inline static const std::string
RESET =
"\e[0m";
246 inline static const std::string
NORMAL =
"\e[0m";
247 inline static const std::string
RETURN =
"\e[0m\n";
248 inline static const std::string
BOLD =
"\e[1m";
249 inline static const std::string
DIM =
"\e[2m";
250 inline static const std::string
ITALIC =
"\e[3m";
252 inline static const std::string
BLINKING =
"\e[5m";
253 inline static const std::string
REVERSED =
"\e[7m";
255 inline static const std::string
STRIKE =
"\e[9m";
266 inline static const std::string
BLACK =
"\e[0;30m";
267 inline static const std::string
RED =
"\e[0;31m";
268 inline static const std::string
GREEN =
"\e[0;32m";
269 inline static const std::string
BROWN =
"\e[0;33m";
270 inline static const std::string
BLUE =
"\e[0;34m";
271 inline static const std::string
PURPLE =
"\e[0;35m";
272 inline static const std::string
MAGENTA =
"\e[0;35m";
273 inline static const std::string
CYAN =
"\e[0;36m";
274 inline static const std::string
GREY =
"\e[0;37m";
275 inline static const std::string
DARK =
"\e[1;30m";
276 inline static const std::string
ORANGE =
"\e[1;31m";
277 inline static const std::string
LT_GRN =
"\e[1;32m";
278 inline static const std::string
YELLOW =
"\e[1;33m";
279 inline static const std::string
LT_BLUE =
"\e[1;34m";
280 inline static const std::string
PINK =
"\e[1;35m";
281 inline static const std::string
LT_CYAN =
"\e[1;36m";
282 inline static const std::string
WHITE =
"\e[1;37m";
295 inline static const std::string
BG_BLACK =
"\e[0;40m";
296 inline static const std::string
BG_RED =
"\e[0;41m";
297 inline static const std::string
BG_GREEN =
"\e[0;42m";
298 inline static const std::string
BG_BROWN =
"\e[0;43m";
299 inline static const std::string
BG_BLUE =
"\e[0;44m";
301 inline static const std::string
BG_CYAN =
"\e[0;46m";
302 inline static const std::string
BG_GREY =
"\e[0;47m";
304 inline static const std::string
BG_DARK =
"\e[0;100m";
305 inline static const std::string
BG_LT_RED =
"\e[0;101m";
307 inline static const std::string
BG_YELLOW =
"\e[0;103m";
309 inline static const std::string
BG_PINK =
"\e[0;105m";
311 inline static const std::string
BG_WHITE =
"\e[0;107m";
331 CONSOLE_SCREEN_BUFFER_INFO csbi;
332 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
333 width = (int)(csbi.srWindow.Right-csbi.srWindow.Left+1);
334 height = (int)(csbi.srWindow.Bottom-csbi.srWindow.Top+1);
335 #elif defined(__linux__) || defined(__APPLE__)
337 ioctl(fileno(stdout), TIOCGWINSZ, &w);
338 width = (int)(w.ws_col);
339 height = (int)(w.ws_row);
355 return std::string(indent_level++ * 2,
' ');
363 return std::string((indent_level-- > 0 ? indent_level : 0) * 2,
' ');
370 inline static std::string
indent() {
return std::string(indent_level * 2,
' '); }
378 inline static std::string
hex(Uint32 n, Uint8 d)
380 std::string s(d,
'0');
381 for (
int i = d - 1; i >= 0; i--, n >>= 4)
382 s[i] =
"0123456789ABCDEF"[n & 0xF];
392 inline static std::string
pad(std::string text, Uint8 d)
394 std::string ret = text;
395 while (ret.length()<d) {
411 inline static int indent_level = 0;
#define Byte
Definition SDOM_CLR.hpp:43
Static container for ANSI escape sequences and formatting helpers.
Definition SDOM_CLR.hpp:68
static const std::string BOLD
ANSI escape sequence to set bold text.
Definition SDOM_CLR.hpp:248
static const std::string BLACK
ANSI escape sequence for black foreground.
Definition SDOM_CLR.hpp:266
static const std::string BG_BLUE
ANSI escape sequence for blue background.
Definition SDOM_CLR.hpp:299
static std::string indent_push()
Increases indentation level and returns corresponding spaces.
Definition SDOM_CLR.hpp:354
static const std::string BG_CYAN
ANSI escape sequence for cyan background.
Definition SDOM_CLR.hpp:301
static const std::string BG_GREY
ANSI escape sequence for grey background.
Definition SDOM_CLR.hpp:302
static const std::string LT_MAGENTA
ANSI escape sequence for light magenta (hot pink) foreground.
Definition SDOM_CLR.hpp:283
static std::string pad(std::string text, Uint8 d)
Pads a string with spaces to a specified length.
Definition SDOM_CLR.hpp:392
static const std::string save_cursor()
Returns ANSI escape sequence to save the current cursor position.
Definition SDOM_CLR.hpp:175
static const std::string BG_WHITE
ANSI escape sequence for white background.
Definition SDOM_CLR.hpp:311
static const std::string BLUE
ANSI escape sequence for blue foreground.
Definition SDOM_CLR.hpp:270
static bool get_cursor_pos(int &row, int &col)
Query the terminal for the current cursor position (row, col).
Definition SDOM_CLR.cpp:120
static std::string indent()
Returns spaces for the current indentation level.
Definition SDOM_CLR.hpp:370
static const std::string BROWN
ANSI escape sequence for brown foreground.
Definition SDOM_CLR.hpp:269
static const std::string bg(Byte color)
Returns ANSI escape sequence to set 8-bit background color.
Definition SDOM_CLR.hpp:99
static const std::string RED
ANSI escape sequence for red foreground.
Definition SDOM_CLR.hpp:267
static const std::string fg_rgb(Byte r, Byte g, Byte b)
Returns ANSI escape sequence to set 24-bit (RGB) foreground color.
Definition SDOM_CLR.hpp:109
static const std::string GREEN
ANSI escape sequence for green foreground.
Definition SDOM_CLR.hpp:268
static const std::string BG_PURPLE
ANSI escape sequence for purple background.
Definition SDOM_CLR.hpp:300
static const std::string REVERSED
ANSI escape sequence to set reversed text.
Definition SDOM_CLR.hpp:253
static const std::string BG_BLACK
ANSI escape sequence for black background.
Definition SDOM_CLR.hpp:295
static const std::string RESET
ANSI escape sequence to reset formatting.
Definition SDOM_CLR.hpp:245
static const std::string BG_LT_BLUE
ANSI escape sequence for light blue background.
Definition SDOM_CLR.hpp:308
static const std::string LT_ORANGE
ANSI escape sequence for light orange foreground.
Definition SDOM_CLR.hpp:284
static const std::string erase_in_line(Byte n=0)
Returns ANSI escape sequence to erase part of the current line.
Definition SDOM_CLR.hpp:234
static const std::string bg_color(Byte clr)
Map a single 8-bit index into an RGB color using the bit-pattern {r:3,g:3,b:2} and return the corresp...
Definition SDOM_CLR.hpp:138
static const std::string BLINKING
ANSI escape sequence to set blinking text.
Definition SDOM_CLR.hpp:252
static const std::string set_cursor_pos(Byte row=1, Byte col=1)
Returns ANSI escape sequence to move the cursor to a specific position.
Definition SDOM_CLR.hpp:168
static const std::string BG_RED
ANSI escape sequence for red background.
Definition SDOM_CLR.hpp:296
static void get_terminal_size(int &width, int &height)
Gets the current terminal size (width and height).
Definition SDOM_CLR.hpp:329
static const std::string STRIKE
ANSI escape sequence to set strikethrough text.
Definition SDOM_CLR.hpp:255
static const std::string LT_CYAN
ANSI escape sequence for light cyan foreground.
Definition SDOM_CLR.hpp:281
static const std::string fg_color(Byte clr)
Map a single 8-bit index into an RGB color using the bit-pattern {r:3,g:3,b:2} and return the corresp...
Definition SDOM_CLR.hpp:118
static const std::string BG_PINK
ANSI escape sequence for pink background.
Definition SDOM_CLR.hpp:309
static const std::string RETURN
ANSI escape sequence to reset all attributes and add a newline.
Definition SDOM_CLR.hpp:247
static std::string hex(Uint32 n, Uint8 d)
Converts an integer to a zero-padded hexadecimal string.
Definition SDOM_CLR.hpp:378
static bool draw_debug_text(const std::string &text, int x, int y, int ptsize=12, Uint8 r=255, Uint8 g=255, Uint8 b=255, Uint8 a=255)
Definition SDOM_CLR.cpp:184
static const std::string bg_rgb(Byte r, Byte g, Byte b)
Returns ANSI escape sequence to set 24-bit (RGB) background color.
Definition SDOM_CLR.hpp:159
static void exposeToLua(sol::state &lua)
Expose CLR constants and helper functions to a Lua state.
Definition SDOM_CLR.cpp:12
static const std::string BG_LT_RED
ANSI escape sequence for light red background.
Definition SDOM_CLR.hpp:305
static const std::string BG_LT_GREEN
ANSI escape sequence for light green background.
Definition SDOM_CLR.hpp:306
static void write_at(Byte row, Byte col, const std::string &s)
Fast helper to write a string at a given terminal position.
Definition SDOM_CLR.hpp:189
static const std::string YELLOW
ANSI escape sequence for yellow foreground.
Definition SDOM_CLR.hpp:278
static const std::string restore_cursor()
Returns ANSI escape sequence to restore a previously saved cursor position.
Definition SDOM_CLR.hpp:180
static const std::string PURPLE
ANSI escape sequence for purple foreground.
Definition SDOM_CLR.hpp:271
static const std::string BG_GREEN
ANSI escape sequence for green background.
Definition SDOM_CLR.hpp:297
static const std::string BG_DEFAULT
ANSI escape sequence for default background.
Definition SDOM_CLR.hpp:303
static const std::string LT_GRN
ANSI escape sequence for light green foreground.
Definition SDOM_CLR.hpp:277
static std::string indent_pop()
Decreases indentation level and returns corresponding spaces.
Definition SDOM_CLR.hpp:362
static const std::string ORANGE
ANSI escape sequence for orange foreground.
Definition SDOM_CLR.hpp:276
static const std::string WHITE
ANSI escape sequence for white foreground.
Definition SDOM_CLR.hpp:282
static const std::string PINK
ANSI escape sequence for pink foreground.
Definition SDOM_CLR.hpp:280
static const std::string LT_BLUE
ANSI escape sequence for light blue foreground.
Definition SDOM_CLR.hpp:279
static const std::string UNDERLINE
ANSI escape sequence to set underline text.
Definition SDOM_CLR.hpp:251
static const std::string fg(Byte color)
Returns ANSI escape sequence to set 8-bit foreground color.
Definition SDOM_CLR.hpp:91
static const std::string CYAN
ANSI escape sequence for cyan foreground.
Definition SDOM_CLR.hpp:273
static const std::string ERASE_LINE
ANSI escape sequence to erase the entire current line and return cursor to start of line.
Definition SDOM_CLR.hpp:256
static const std::string BG_YELLOW
ANSI escape sequence for yellow background.
Definition SDOM_CLR.hpp:307
static const std::string DARK
ANSI escape sequence for dark grey foreground.
Definition SDOM_CLR.hpp:275
static const std::string DIM
ANSI escape sequence to set dim text.
Definition SDOM_CLR.hpp:249
static const std::string NORMAL
ANSI escape sequence to reset all attributes.
Definition SDOM_CLR.hpp:246
static const std::string ITALIC
ANSI escape sequence to set italic text.
Definition SDOM_CLR.hpp:250
static const std::string BG_BROWN
ANSI escape sequence for brown background.
Definition SDOM_CLR.hpp:298
static const std::string erase_in_display(Byte n=0)
Returns ANSI escape sequence to clear part of the screen.
Definition SDOM_CLR.hpp:222
static const std::string BG_DARK
ANSI escape sequence for dark grey background.
Definition SDOM_CLR.hpp:304
static const std::string MAGENTA
ANSI escape sequence for magenta foreground.
Definition SDOM_CLR.hpp:272
static const std::string BG_LT_CYAN
ANSI escape sequence for light cyan background.
Definition SDOM_CLR.hpp:310
static const std::string GREY
ANSI escape sequence for grey foreground.
Definition SDOM_CLR.hpp:274
static const std::string CONCEALED
ANSI escape sequence to set concealed text.
Definition SDOM_CLR.hpp:254