SDOM - Simple SDL Document Object Model
A lightweight, extensible Document Object Model for SDL-based applications.
Loading...
Searching...
No Matches
SDOM_CLR.hpp
Go to the documentation of this file.
1/*** SDOM_CLR.hpp ***********
2 * _____ _ _____ _
3 * / ____| | | __ \ | |
4 * | | | | | |__) | | |__ _ __ _ __
5 * | | | | | _ / | '_ \| '_ \| '_ \
6 * | |____| |____| | \ \ _ | | | | |_) | |_) |
7 * \_____|______|_| \_\ (_) |_| |_| .__/| .__/
8 * | | | |
9 * |_| |_|
10 *
11 * Defines a set of ANSI escape sequences to colorize and format text in the terminal.
12 *
13 * Static container object. Cannot be instantiated.
14 * May require periodic fflush(stdout).
15 * See: https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-sequences
16 *
17 ***********/
18
33#pragma once
34
35#include <string>
36#include <SDL3/SDL.h>
37// Expose CLR helpers to embedded Lua states
38#include <sol/sol.hpp>
39#include <cstdio>
40
41// simple types for 8-bit archetecture
42#ifndef Byte
43 #define Byte Uint8
44#endif
45#ifndef Word
46 #define Word Uint16
47#endif
48#ifndef DWord
49 #define DWord Uint32
50#endif
51
52// cross-platform stuff for Linux, macos, and windows
53#if defined(_WIN32)
54 #define WIN32_LEAN_AND_MEAN
55 #define VC_EXTRALEAN
56 #include <Windows.h>
57#elif defined(__linux__)
58 #include <sys/ioctl.h>
59#endif // Windows/Linux
60
67class CLR
68{
69private:
70 CLR(); // private constructor
71 ~CLR(); // private destructor
72 CLR(const CLR&) = delete; // delete the copy constructor
73 CLR(CLR&&) = delete; // delete the move constructor
74 CLR& operator=(const CLR&) = delete; // delete the copy assignment operator
75 CLR& operator=(CLR&&) = delete; // delete the move assignment operator
76
77
78public:
79
91 inline static const std::string fg(Byte color)
92 { return "\e[38;5;" + std::to_string(color) + "m"; }
93
99 inline static const std::string bg(Byte color)
100 { return "\e[48;5;" + std::to_string(color) + "m"; }
101
109 inline static const std::string fg_rgb(Byte r, Byte g, Byte b)
110 { return "\e[38;2;" + std::to_string(r) + ";" + std::to_string(g) + ";" + std::to_string(b) + "m"; }
111
118 inline static const std::string fg_color(Byte clr) {
119 // extract bitfields
120 Uint8 r3 = (clr >> 5) & 0x7; // 3 bits
121 Uint8 g3 = (clr >> 2) & 0x7; // 3 bits
122 Uint8 b2 = clr & 0x3; // 2 bits
123
124 // expand to 0..255 (rounding)
125 Uint8 r8 = (Uint8)((r3 * 255 + 3) / 7);
126 Uint8 g8 = (Uint8)((g3 * 255 + 3) / 7);
127 Uint8 b8 = (Uint8)((b2 * 255 + 1) / 3);
128
129 return fg_rgb(r8, g8, b8);
130 }
131
138 inline static const std::string bg_color(Byte clr) {
139 // extract bitfields
140 Uint8 r3 = (clr >> 5) & 0x7; // 3 bits
141 Uint8 g3 = (clr >> 2) & 0x7; // 3 bits
142 Uint8 b2 = clr & 0x3; // 2 bits
143
144 // expand to 0..255 (rounding)
145 Uint8 r8 = (Uint8)((r3 * 255 + 3) / 7);
146 Uint8 g8 = (Uint8)((g3 * 255 + 3) / 7);
147 Uint8 b8 = (Uint8)((b2 * 255 + 1) / 3);
148
149 return bg_rgb(r8, g8, b8);
150 }
151
159 inline static const std::string bg_rgb(Byte r, Byte g, Byte b)
160 { return "\e[48;2;" + std::to_string(r) + ";" + std::to_string(g) + ";" + std::to_string(b) + "m"; }
161
168 inline static const std::string set_cursor_pos(Byte row=1, Byte col=1)
169 { return "\e[" + std::to_string(row) + ";" + std::to_string(col) + "H"; }
170
175 inline static const std::string save_cursor() { return "\e[s"; }
176
180 inline static const std::string restore_cursor() { return "\e[u"; }
181
189 inline static void write_at(Byte row, Byte col, const std::string& s) {
190 std::fwrite(save_cursor().c_str(), 1, save_cursor().size(), stdout);
191 std::fwrite(set_cursor_pos(row, col).c_str(), 1, set_cursor_pos(row, col).size(), stdout);
192 std::fwrite(s.c_str(), 1, s.size(), stdout);
193 std::fwrite(restore_cursor().c_str(), 1, restore_cursor().size(), stdout);
194 std::fflush(stdout);
195 }
196
197 // Render debug text into the current SDL renderer at (x,y).
198 // Uses SDL_RenderDebugText when available. Returns true on success.
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);
201
210 static bool get_cursor_pos(int& row, int& col);
211
222 inline static const std::string erase_in_display(Byte n=0)
223 { return "\e[" + std::to_string(n) + "J"; }
224
234 inline static const std::string erase_in_line(Byte n=0)
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";
251 inline static const std::string UNDERLINE = "\e[4m";
252 inline static const std::string BLINKING = "\e[5m";
253 inline static const std::string REVERSED = "\e[7m";
254 inline static const std::string CONCEALED = "\e[8m";
255 inline static const std::string STRIKE = "\e[9m";
256 inline static const std::string ERASE_LINE = "\e[2K\r";
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";
283 inline static const std::string LT_MAGENTA = fg_rgb(255, 105, 180);
284 inline static const std::string LT_ORANGE = fg_rgb(255, 165, 79);
285
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";
300 inline static const std::string BG_PURPLE = "\e[0;45m";
301 inline static const std::string BG_CYAN = "\e[0;46m";
302 inline static const std::string BG_GREY = "\e[0;47m";
303 inline static const std::string BG_DEFAULT = "\e[0;49m";
304 inline static const std::string BG_DARK = "\e[0;100m";
305 inline static const std::string BG_LT_RED = "\e[0;101m";
306 inline static const std::string BG_LT_GREEN = "\e[0;102m";
307 inline static const std::string BG_YELLOW = "\e[0;103m";
308 inline static const std::string BG_LT_BLUE = "\e[0;104m";
309 inline static const std::string BG_PINK = "\e[0;105m";
310 inline static const std::string BG_LT_CYAN = "\e[0;106m";
311 inline static const std::string BG_WHITE = "\e[0;107m";
329 inline static void get_terminal_size(int& width, int& height) {
330 #if defined(_WIN32)
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__)
336 struct winsize w;
337 ioctl(fileno(stdout), TIOCGWINSZ, &w);
338 width = (int)(w.ws_col);
339 height = (int)(w.ws_row);
340 #endif // Windows/Linux
341 }
342
343
354 inline static std::string indent_push() {
355 return std::string(indent_level++ * 2, ' ');
356 }
357
362 inline static std::string indent_pop() {
363 return std::string((indent_level-- > 0 ? indent_level : 0) * 2, ' ');
364 }
365
370 inline static std::string indent() { return std::string(indent_level * 2, ' '); }
371
378 inline static std::string hex(Uint32 n, Uint8 d)
379 {
380 std::string s(d, '0');
381 for (int i = d - 1; i >= 0; i--, n >>= 4)
382 s[i] = "0123456789ABCDEF"[n & 0xF];
383 return s;
384 };
385
392 inline static std::string pad(std::string text, Uint8 d)
393 {
394 std::string ret = text;
395 while (ret.length()<d) {
396 ret += " ";
397 }
398 return ret;
399 };
408 static void exposeToLua(sol::state& lua);
409
410private:
411 inline static int indent_level = 0;
412
413};
#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