SDOM - Simple SDL Document Object Model
A lightweight, extensible Document Object Model for SDL-based applications.
Loading...
Searching...
No Matches
CLR Class Reference

Static container for ANSI escape sequences and formatting helpers. More...

#include <SDOM_CLR.hpp>

Static Public Member Functions

static void get_terminal_size (int &width, int &height)
 Gets the current terminal size (width and height).
 
static void exposeToLua (sol::state &lua)
 Expose CLR constants and helper functions to a Lua state.
 
Utility Member Functions

Functions for generating ANSI escape sequences for terminal formatting.

static const std::string fg (Byte color)
 Returns ANSI escape sequence to set 8-bit foreground color.
 
static const std::string bg (Byte color)
 Returns ANSI escape sequence to set 8-bit background color.
 
static const std::string fg_rgb (Byte r, Byte g, Byte b)
 Returns ANSI escape sequence to set 24-bit (RGB) foreground color.
 
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 corresponding ANSI 24-bit foreground escape sequence.
 
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 corresponding ANSI 24-bit background escape sequence.
 
static const std::string bg_rgb (Byte r, Byte g, Byte b)
 Returns ANSI escape sequence to set 24-bit (RGB) background color.
 
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.
 
static const std::string save_cursor ()
 Returns ANSI escape sequence to save the current cursor position.
 
static const std::string restore_cursor ()
 Returns ANSI escape sequence to restore a previously saved cursor position.
 
static void write_at (Byte row, Byte col, const std::string &s)
 Fast helper to write a string at a given terminal position.
 
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)
 
static bool get_cursor_pos (int &row, int &col)
 Query the terminal for the current cursor position (row, col).
 
static const std::string erase_in_display (Byte n=0)
 Returns ANSI escape sequence to clear part of the screen.
 
static const std::string erase_in_line (Byte n=0)
 Returns ANSI escape sequence to erase part of the current line.
 
Formatting Helper Functions

Utility functions for indentation, hex formatting, and padding.

static std::string indent_push ()
 Increases indentation level and returns corresponding spaces.
 
static std::string indent_pop ()
 Decreases indentation level and returns corresponding spaces.
 
static std::string indent ()
 Returns spaces for the current indentation level.
 
static std::string hex (Uint32 n, Uint8 d)
 Converts an integer to a zero-padded hexadecimal string.
 
static std::string pad (std::string text, Uint8 d)
 Pads a string with spaces to a specified length.
 

Static Public Attributes

Type Set Constants

ANSI escape sequences for text formatting attributes.

static const std::string RESET = "\e[0m"
 ANSI escape sequence to reset formatting.
 
static const std::string NORMAL = "\e[0m"
 ANSI escape sequence to reset all attributes.
 
static const std::string RETURN = "\e[0m\n"
 ANSI escape sequence to reset all attributes and add a newline.
 
static const std::string BOLD = "\e[1m"
 ANSI escape sequence to set bold text.
 
static const std::string DIM = "\e[2m"
 ANSI escape sequence to set dim text.
 
static const std::string ITALIC = "\e[3m"
 ANSI escape sequence to set italic text.
 
static const std::string UNDERLINE = "\e[4m"
 ANSI escape sequence to set underline text.
 
static const std::string BLINKING = "\e[5m"
 ANSI escape sequence to set blinking text.
 
static const std::string REVERSED = "\e[7m"
 ANSI escape sequence to set reversed text.
 
static const std::string CONCEALED = "\e[8m"
 ANSI escape sequence to set concealed text.
 
static const std::string STRIKE = "\e[9m"
 ANSI escape sequence to set strikethrough text.
 
static const std::string ERASE_LINE = "\e[2K\r"
 ANSI escape sequence to erase the entire current line and return cursor to start of line.
 
Foreground Color Constants

ANSI escape sequences for foreground text colors.

static const std::string BLACK = "\e[0;30m"
 ANSI escape sequence for black foreground.
 
static const std::string RED = "\e[0;31m"
 ANSI escape sequence for red foreground.
 
static const std::string GREEN = "\e[0;32m"
 ANSI escape sequence for green foreground.
 
static const std::string BROWN = "\e[0;33m"
 ANSI escape sequence for brown foreground.
 
static const std::string BLUE = "\e[0;34m"
 ANSI escape sequence for blue foreground.
 
static const std::string PURPLE = "\e[0;35m"
 ANSI escape sequence for purple foreground.
 
static const std::string MAGENTA = "\e[0;35m"
 ANSI escape sequence for magenta foreground.
 
static const std::string CYAN = "\e[0;36m"
 ANSI escape sequence for cyan foreground.
 
static const std::string GREY = "\e[0;37m"
 ANSI escape sequence for grey foreground.
 
static const std::string DARK = "\e[1;30m"
 ANSI escape sequence for dark grey foreground.
 
static const std::string ORANGE = "\e[1;31m"
 ANSI escape sequence for orange foreground.
 
static const std::string LT_GRN = "\e[1;32m"
 ANSI escape sequence for light green foreground.
 
static const std::string YELLOW = "\e[1;33m"
 ANSI escape sequence for yellow foreground.
 
static const std::string LT_BLUE = "\e[1;34m"
 ANSI escape sequence for light blue foreground.
 
static const std::string PINK = "\e[1;35m"
 ANSI escape sequence for pink foreground.
 
static const std::string LT_CYAN = "\e[1;36m"
 ANSI escape sequence for light cyan foreground.
 
static const std::string WHITE = "\e[1;37m"
 ANSI escape sequence for white foreground.
 
static const std::string LT_MAGENTA = fg_rgb(255, 105, 180)
 ANSI escape sequence for light magenta (hot pink) foreground.
 
static const std::string LT_ORANGE = fg_rgb(255, 165, 79)
 ANSI escape sequence for light orange foreground.
 
Background Color Constants

ANSI escape sequences for background text colors.

static const std::string BG_BLACK = "\e[0;40m"
 ANSI escape sequence for black background.
 
static const std::string BG_RED = "\e[0;41m"
 ANSI escape sequence for red background.
 
static const std::string BG_GREEN = "\e[0;42m"
 ANSI escape sequence for green background.
 
static const std::string BG_BROWN = "\e[0;43m"
 ANSI escape sequence for brown background.
 
static const std::string BG_BLUE = "\e[0;44m"
 ANSI escape sequence for blue background.
 
static const std::string BG_PURPLE = "\e[0;45m"
 ANSI escape sequence for purple background.
 
static const std::string BG_CYAN = "\e[0;46m"
 ANSI escape sequence for cyan background.
 
static const std::string BG_GREY = "\e[0;47m"
 ANSI escape sequence for grey background.
 
static const std::string BG_DEFAULT = "\e[0;49m"
 ANSI escape sequence for default background.
 
static const std::string BG_DARK = "\e[0;100m"
 ANSI escape sequence for dark grey background.
 
static const std::string BG_LT_RED = "\e[0;101m"
 ANSI escape sequence for light red background.
 
static const std::string BG_LT_GREEN = "\e[0;102m"
 ANSI escape sequence for light green background.
 
static const std::string BG_YELLOW = "\e[0;103m"
 ANSI escape sequence for yellow background.
 
static const std::string BG_LT_BLUE = "\e[0;104m"
 ANSI escape sequence for light blue background.
 
static const std::string BG_PINK = "\e[0;105m"
 ANSI escape sequence for pink background.
 
static const std::string BG_LT_CYAN = "\e[0;106m"
 ANSI escape sequence for light cyan background.
 
static const std::string BG_WHITE = "\e[0;107m"
 ANSI escape sequence for white background.
 

Detailed Description

Static container for ANSI escape sequences and formatting helpers.

Provides color and formatting utilities for terminal output. Cannot be instantiated. All members are static. See individual groups for details.

Member Function Documentation

◆ bg()

static const std::string CLR::bg ( Byte  color)
inlinestatic

Returns ANSI escape sequence to set 8-bit background color.

Parameters
color8-bit color value (0-255).
Returns
ANSI escape sequence as a string.

◆ bg_color()

static const std::string CLR::bg_color ( Byte  clr)
inlinestatic

Map a single 8-bit index into an RGB color using the bit-pattern {r:3,g:3,b:2} and return the corresponding ANSI 24-bit background escape sequence.

Parameters
clr8-bit index where bits 7..5 = r (3 bits), 4..2 = g (3 bits), 1..0 = b (2 bits)
Returns
ANSI escape sequence for 24-bit background color.

◆ bg_rgb()

static const std::string CLR::bg_rgb ( Byte  r,
Byte  g,
Byte  b 
)
inlinestatic

Returns ANSI escape sequence to set 24-bit (RGB) background color.

Parameters
rRed component (0-255).
gGreen component (0-255).
bBlue component (0-255).
Returns
ANSI escape sequence as a string.

◆ draw_debug_text()

bool CLR::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 
)
static

◆ erase_in_display()

static const std::string CLR::erase_in_display ( Byte  n = 0)
inlinestatic

Returns ANSI escape sequence to clear part of the screen.

  • If n is 0 (or missing), clears from cursor to end of screen.
  • If n is 1, clears from cursor to beginning of the screen.
  • If n is 2, clears entire screen.
  • If n is 3, deletes all lines saved in the scrollback buffer.
    Parameters
    nMode selector (0-3).
    Returns
    ANSI escape sequence as a string.

◆ erase_in_line()

static const std::string CLR::erase_in_line ( Byte  n = 0)
inlinestatic

Returns ANSI escape sequence to erase part of the current line.

  • If n is 0 (or missing), clears from cursor to end of the line.
  • If n is 1, clears from cursor to beginning of the line.
  • If n is 2, clears entire line.
    Parameters
    nMode selector (0-2).
    Returns
    ANSI escape sequence as a string.

◆ exposeToLua()

void CLR::exposeToLua ( sol::state &  lua)
static

Expose CLR constants and helper functions to a Lua state.

Parameters
luaThe lua state to populate with a CLR table.
Note
Implementation lives in src/SDOM_CLR.cpp to avoid duplicate inline definitions across translation units.

◆ fg()

static const std::string CLR::fg ( Byte  color)
inlinestatic

Returns ANSI escape sequence to set 8-bit foreground color.

Parameters
color8-bit color value (0-255).
Returns
ANSI escape sequence as a string.

◆ fg_color()

static const std::string CLR::fg_color ( Byte  clr)
inlinestatic

Map a single 8-bit index into an RGB color using the bit-pattern {r:3,g:3,b:2} and return the corresponding ANSI 24-bit foreground escape sequence.

Parameters
clr8-bit index where bits 7..5 = r (3 bits), 4..2 = g (3 bits), 1..0 = b (2 bits)
Returns
ANSI escape sequence for 24-bit foreground color.

◆ fg_rgb()

static const std::string CLR::fg_rgb ( Byte  r,
Byte  g,
Byte  b 
)
inlinestatic

Returns ANSI escape sequence to set 24-bit (RGB) foreground color.

Parameters
rRed component (0-255).
gGreen component (0-255).
bBlue component (0-255).
Returns
ANSI escape sequence as a string.

◆ get_cursor_pos()

bool CLR::get_cursor_pos ( int &  row,
int &  col 
)
static

Query the terminal for the current cursor position (row, col).

Parameters
rowOut parameter for 1-based row index.
colOut parameter for 1-based column index.
Returns
true if cursor position was successfully queried; false otherwise.
Note
On POSIX this uses \x1b[6n (CPR) against /dev/tty and requires the terminal to support the Cursor Position Report sequence. On Windows it queries the console API.

◆ get_terminal_size()

static void CLR::get_terminal_size ( int &  width,
int &  height 
)
inlinestatic

Gets the current terminal size (width and height).

This function is not called within an output stream. It should be called directly. Example usage:

int width = 0, height = 0;
CLR::get_terminal_size(width, height);
std::cout << "width=" << width << ", height=" << height << std::endl;
static void get_terminal_size(int &width, int &height)
Gets the current terminal size (width and height).
Definition SDOM_CLR.hpp:329
Note
Only works on supported platforms (Windows, Linux, macOS).
Parameters
widthReference to store the terminal width.
heightReference to store the terminal height.

◆ hex()

static std::string CLR::hex ( Uint32  n,
Uint8  d 
)
inlinestatic

Converts an integer to a zero-padded hexadecimal string.

Parameters
nNumber to convert.
dNumber of hex digits.
Returns
Hexadecimal string.

◆ indent()

static std::string CLR::indent ( )
inlinestatic

Returns spaces for the current indentation level.

Returns
String of spaces for the current indentation level.

◆ indent_pop()

static std::string CLR::indent_pop ( )
inlinestatic

Decreases indentation level and returns corresponding spaces.

Returns
String of spaces for the new indentation level.

◆ indent_push()

static std::string CLR::indent_push ( )
inlinestatic

Increases indentation level and returns corresponding spaces.

Returns
String of spaces for the new indentation level.

◆ pad()

static std::string CLR::pad ( std::string  text,
Uint8  d 
)
inlinestatic

Pads a string with spaces to a specified length.

Parameters
textInput string.
dDesired length.
Returns
Padded string.

◆ restore_cursor()

static const std::string CLR::restore_cursor ( )
inlinestatic

Returns ANSI escape sequence to restore a previously saved cursor position.

◆ save_cursor()

static const std::string CLR::save_cursor ( )
inlinestatic

Returns ANSI escape sequence to save the current cursor position.

Note
Many terminals support ESC[s to save and ESC[u to restore.

◆ set_cursor_pos()

static const std::string CLR::set_cursor_pos ( Byte  row = 1,
Byte  col = 1 
)
inlinestatic

Returns ANSI escape sequence to move the cursor to a specific position.

Parameters
rowRow number (1-based).
colColumn number (1-based).
Returns
ANSI escape sequence as a string.

◆ write_at()

static void CLR::write_at ( Byte  row,
Byte  col,
const std::string &  s 
)
inlinestatic

Fast helper to write a string at a given terminal position.

Parameters
row1-based row
col1-based column
sThe string to write
Note
This writes directly to stdout and flushes; use sparingly.

Member Data Documentation

◆ BG_BLACK

const std::string CLR::BG_BLACK = "\e[0;40m"
inlinestatic

ANSI escape sequence for black background.

◆ BG_BLUE

const std::string CLR::BG_BLUE = "\e[0;44m"
inlinestatic

ANSI escape sequence for blue background.

◆ BG_BROWN

const std::string CLR::BG_BROWN = "\e[0;43m"
inlinestatic

ANSI escape sequence for brown background.

◆ BG_CYAN

const std::string CLR::BG_CYAN = "\e[0;46m"
inlinestatic

ANSI escape sequence for cyan background.

◆ BG_DARK

const std::string CLR::BG_DARK = "\e[0;100m"
inlinestatic

ANSI escape sequence for dark grey background.

◆ BG_DEFAULT

const std::string CLR::BG_DEFAULT = "\e[0;49m"
inlinestatic

ANSI escape sequence for default background.

◆ BG_GREEN

const std::string CLR::BG_GREEN = "\e[0;42m"
inlinestatic

ANSI escape sequence for green background.

◆ BG_GREY

const std::string CLR::BG_GREY = "\e[0;47m"
inlinestatic

ANSI escape sequence for grey background.

◆ BG_LT_BLUE

const std::string CLR::BG_LT_BLUE = "\e[0;104m"
inlinestatic

ANSI escape sequence for light blue background.

◆ BG_LT_CYAN

const std::string CLR::BG_LT_CYAN = "\e[0;106m"
inlinestatic

ANSI escape sequence for light cyan background.

◆ BG_LT_GREEN

const std::string CLR::BG_LT_GREEN = "\e[0;102m"
inlinestatic

ANSI escape sequence for light green background.

◆ BG_LT_RED

const std::string CLR::BG_LT_RED = "\e[0;101m"
inlinestatic

ANSI escape sequence for light red background.

◆ BG_PINK

const std::string CLR::BG_PINK = "\e[0;105m"
inlinestatic

ANSI escape sequence for pink background.

◆ BG_PURPLE

const std::string CLR::BG_PURPLE = "\e[0;45m"
inlinestatic

ANSI escape sequence for purple background.

◆ BG_RED

const std::string CLR::BG_RED = "\e[0;41m"
inlinestatic

ANSI escape sequence for red background.

◆ BG_WHITE

const std::string CLR::BG_WHITE = "\e[0;107m"
inlinestatic

ANSI escape sequence for white background.

◆ BG_YELLOW

const std::string CLR::BG_YELLOW = "\e[0;103m"
inlinestatic

ANSI escape sequence for yellow background.

◆ BLACK

const std::string CLR::BLACK = "\e[0;30m"
inlinestatic

ANSI escape sequence for black foreground.

◆ BLINKING

const std::string CLR::BLINKING = "\e[5m"
inlinestatic

ANSI escape sequence to set blinking text.

◆ BLUE

const std::string CLR::BLUE = "\e[0;34m"
inlinestatic

ANSI escape sequence for blue foreground.

◆ BOLD

const std::string CLR::BOLD = "\e[1m"
inlinestatic

ANSI escape sequence to set bold text.

◆ BROWN

const std::string CLR::BROWN = "\e[0;33m"
inlinestatic

ANSI escape sequence for brown foreground.

◆ CONCEALED

const std::string CLR::CONCEALED = "\e[8m"
inlinestatic

ANSI escape sequence to set concealed text.

◆ CYAN

const std::string CLR::CYAN = "\e[0;36m"
inlinestatic

ANSI escape sequence for cyan foreground.

◆ DARK

const std::string CLR::DARK = "\e[1;30m"
inlinestatic

ANSI escape sequence for dark grey foreground.

◆ DIM

const std::string CLR::DIM = "\e[2m"
inlinestatic

ANSI escape sequence to set dim text.

◆ ERASE_LINE

const std::string CLR::ERASE_LINE = "\e[2K\r"
inlinestatic

ANSI escape sequence to erase the entire current line and return cursor to start of line.

◆ GREEN

const std::string CLR::GREEN = "\e[0;32m"
inlinestatic

ANSI escape sequence for green foreground.

◆ GREY

const std::string CLR::GREY = "\e[0;37m"
inlinestatic

ANSI escape sequence for grey foreground.

◆ ITALIC

const std::string CLR::ITALIC = "\e[3m"
inlinestatic

ANSI escape sequence to set italic text.

◆ LT_BLUE

const std::string CLR::LT_BLUE = "\e[1;34m"
inlinestatic

ANSI escape sequence for light blue foreground.

◆ LT_CYAN

const std::string CLR::LT_CYAN = "\e[1;36m"
inlinestatic

ANSI escape sequence for light cyan foreground.

◆ LT_GRN

const std::string CLR::LT_GRN = "\e[1;32m"
inlinestatic

ANSI escape sequence for light green foreground.

◆ LT_MAGENTA

const std::string CLR::LT_MAGENTA = fg_rgb(255, 105, 180)
inlinestatic

ANSI escape sequence for light magenta (hot pink) foreground.

◆ LT_ORANGE

const std::string CLR::LT_ORANGE = fg_rgb(255, 165, 79)
inlinestatic

ANSI escape sequence for light orange foreground.

◆ MAGENTA

const std::string CLR::MAGENTA = "\e[0;35m"
inlinestatic

ANSI escape sequence for magenta foreground.

◆ NORMAL

const std::string CLR::NORMAL = "\e[0m"
inlinestatic

ANSI escape sequence to reset all attributes.

◆ ORANGE

const std::string CLR::ORANGE = "\e[1;31m"
inlinestatic

ANSI escape sequence for orange foreground.

◆ PINK

const std::string CLR::PINK = "\e[1;35m"
inlinestatic

ANSI escape sequence for pink foreground.

◆ PURPLE

const std::string CLR::PURPLE = "\e[0;35m"
inlinestatic

ANSI escape sequence for purple foreground.

◆ RED

const std::string CLR::RED = "\e[0;31m"
inlinestatic

ANSI escape sequence for red foreground.

◆ RESET

const std::string CLR::RESET = "\e[0m"
inlinestatic

ANSI escape sequence to reset formatting.

◆ RETURN

const std::string CLR::RETURN = "\e[0m\n"
inlinestatic

ANSI escape sequence to reset all attributes and add a newline.

◆ REVERSED

const std::string CLR::REVERSED = "\e[7m"
inlinestatic

ANSI escape sequence to set reversed text.

◆ STRIKE

const std::string CLR::STRIKE = "\e[9m"
inlinestatic

ANSI escape sequence to set strikethrough text.

◆ UNDERLINE

const std::string CLR::UNDERLINE = "\e[4m"
inlinestatic

ANSI escape sequence to set underline text.

◆ WHITE

const std::string CLR::WHITE = "\e[1;37m"
inlinestatic

ANSI escape sequence for white foreground.

◆ YELLOW

const std::string CLR::YELLOW = "\e[1;33m"
inlinestatic

ANSI escape sequence for yellow foreground.


The documentation for this class was generated from the following files: