20#define TERM_ESCAPTE_CHAR "\x1b["
26 const char *_empty_str =
"";
27 const char *_esc_char = TERM_ESCAPTE_CHAR;
28 const char *_reset = TERM_ESCAPTE_CHAR
"0m";
29 const char *_bold = TERM_ESCAPTE_CHAR
"1m";
30 const char *_faint = TERM_ESCAPTE_CHAR
"2m";
31 const char *_underline = TERM_ESCAPTE_CHAR
"4m";
32 const char *_blink = TERM_ESCAPTE_CHAR
"5m";
33 const char *_col8b_black = TERM_ESCAPTE_CHAR
"30m";
34 const char *_col8b_red = TERM_ESCAPTE_CHAR
"31m";
35 const char *_col8b_green = TERM_ESCAPTE_CHAR
"32m";
36 const char *_col8b_yellow = TERM_ESCAPTE_CHAR
"33m";
37 const char *_col8b_blue = TERM_ESCAPTE_CHAR
"34m";
38 const char *_col8b_magenta = TERM_ESCAPTE_CHAR
"35m";
39 const char *_col8b_cyan = TERM_ESCAPTE_CHAR
"36m";
40 const char *_col8b_white = TERM_ESCAPTE_CHAR
"37m";
77 namespace colors_256 {
81 std::string build(
int mode, std::uint8_t index) {
82 if (color_level_value < ColorLevel::ANSI256) {
85 return std::string(TERM_ESCAPTE_CHAR) + std::to_string(mode) +
";5;"
86 + std::to_string(index) +
"m";
90 std::string
foreground(std::uint8_t index) {
return build(38, index); }
91 std::string
background(std::uint8_t index) {
return build(48, index); }
94 namespace colors_24b {
98 std::string build(
int mode, std::uint8_t r, std::uint8_t g, std::uint8_t b) {
99 if (color_level_value < ColorLevel::TrueColor) {
102 return std::string(TERM_ESCAPTE_CHAR) + std::to_string(mode) +
";2;"
103 + std::to_string(r) +
";" + std::to_string(g) +
";" + std::to_string(b)
108 std::string
foreground(std::uint8_t r, std::uint8_t g, std::uint8_t b) {
109 return build(38, r, g, b);
111 std::string
background(std::uint8_t r, std::uint8_t g, std::uint8_t b) {
112 return build(48, r, g, b);
Terminal color escape sequences and enable/disable control.
ColorLevel
The possible levels of terminal color support.
@ NoColor
No color support, plain ASCII output only.
@ Basic
ANSI/16 colors (basic SGR codes), \x1b[3Xm.
ColorLevel color_level()
Query the currently detected/forced terminal color support level.
void set_color_level(ColorLevel level)
Set the terminal color support level.
void enable_colors()
Enable colors.
void disable_colors()
Disable all colors.
bool are_colors_enabled()
Query whether terminal color output is currently enabled.
std::string foreground(std::uint8_t r, std::uint8_t g, std::uint8_t b)
Escape sequence to set the given RGB foreground text color.
std::string background(std::uint8_t r, std::uint8_t g, std::uint8_t b)
Escape sequence to set the given RGB background color.
std::string background(std::uint8_t index)
Escape sequence to set the given palette index as background color.
std::string foreground(std::uint8_t index)
Escape sequence to set the given palette index as foreground text color.
const char * red()
Escape sequence to set red text color.
const char * green()
Escape sequence to set green text color.
const char * black()
Escape sequence to set black text color.
const char * white()
Escape sequence to set white text color.
const char * magenta()
Escape sequence to set magenta (pink) text color.
const char * cyan()
Escape sequence to set cyan text color.
const char * blue()
Escape sequence to set blue text color.
const char * yellow()
Escape sequence to set yellow text color.
const char * blink()
escape sequence to set blinking text formatting.
const char * reset()
escape sequence to reset the terminal text formatting.
const char * bold()
escape sequence to set bold text formatting.
const char * underline()
escape sequence to set underlined text formatting.
const char * faint()
escape sequence to set faint (dim) text formatting.