CPGen
C++ project generator
Loading...
Searching...
No Matches
Namespaces | Functions
Utils.hpp File Reference

TUI utility functions: ANSI colour helpers, input reading, and string metrics. More...

#include "Misc/Defs.hpp"
#include <array>
#include <cwchar>
#include <string>
#include <unordered_map>
#include <poll.h>
#include <unistd.h>
Include dependency graph for Utils.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

namespace  Utils
 TUI utility functions and constants.
 
namespace  Utils::Colours
 ANSI SGR (Select Graphic Rendition) code constants and escape sequence builder.
 

Functions

std::string Utils::Colours::esc (const char *code)
 Build a complete ANSI SGR escape sequence from a code string.
 
bool Utils::stdinReady (int timeout_ms)
 Poll stdin for available data within a timeout.
 
std::string Utils::stripAnsi (const std::string &s)
 Remove all ANSI escape sequences from a string.
 
size_t Utils::visualWidth (const std::string &s)
 Compute the visual (display) column width of a UTF-8 string.
 
Defs::Key Utils::handleEscapeKey ()
 Decode an escape sequence whose leading ESC byte has already been consumed.
 
Defs::Key Utils::readKey ()
 Read a single keypress from stdin (terminal must be in raw mode).
 

Variables

Standard foreground colours
constexpr const char * Utils::Colours::FG_BLACK = "30"
 Black foreground.
 
constexpr const char * Utils::Colours::FG_RED = "31"
 Red foreground.
 
constexpr const char * Utils::Colours::FG_GREEN = "32"
 Green foreground.
 
constexpr const char * Utils::Colours::FG_YELLOW = "33"
 Yellow foreground.
 
constexpr const char * Utils::Colours::FG_BLUE = "34"
 Blue foreground.
 
constexpr const char * Utils::Colours::FG_MAGENTA = "35"
 Magenta foreground.
 
constexpr const char * Utils::Colours::FG_CYAN = "36"
 Cyan foreground.
 
constexpr const char * Utils::Colours::FG_WHITE = "37"
 White foreground.
 
Bright / light foreground colours
constexpr const char * Utils::Colours::FG_BBLACK
 Bright black (dark grey) foreground.
 
constexpr const char * Utils::Colours::FG_BRED = "91"
 Bright red foreground.
 
constexpr const char * Utils::Colours::FG_BGREEN = "92"
 Bright green foreground.
 
constexpr const char * Utils::Colours::FG_BYELLOW = "93"
 Bright yellow foreground.
 
constexpr const char * Utils::Colours::FG_BBLUE = "94"
 Bright blue foreground.
 
constexpr const char * Utils::Colours::FG_BMAGENTA = "95"
 Bright magenta foreground.
 
constexpr const char * Utils::Colours::FG_BCYAN = "96"
 Bright cyan foreground.
 
constexpr const char * Utils::Colours::FG_BWHITE = "97"
 Bright white foreground.
 
Standard background colours
constexpr const char * Utils::Colours::BG_BLACK = "40"
 Black background.
 
constexpr const char * Utils::Colours::BG_RED = "41"
 Red background.
 
constexpr const char * Utils::Colours::BG_GREEN = "42"
 Green background.
 
constexpr const char * Utils::Colours::BG_YELLOW = "43"
 Yellow background.
 
constexpr const char * Utils::Colours::BG_BLUE = "44"
 Blue background.
 
constexpr const char * Utils::Colours::BG_MAGENTA = "45"
 Magenta background.
 
constexpr const char * Utils::Colours::BG_CYAN = "46"
 Cyan background.
 
constexpr const char * Utils::Colours::BG_WHITE = "47"
 White background.
 
Bright / light background colours
constexpr const char * Utils::Colours::BG_BBLACK = "100"
 Bright black background.
 
constexpr const char * Utils::Colours::BG_BRED = "101"
 Bright red background.
 
constexpr const char * Utils::Colours::BG_BGREEN = "102"
 Bright green background.
 
constexpr const char * Utils::Colours::BG_BYELLOW = "103"
 Bright yellow background.
 
constexpr const char * Utils::Colours::BG_BBLUE = "104"
 Bright blue background.
 
constexpr const char * Utils::Colours::BG_BMAGENTA
 Bright magenta background.
 
constexpr const char * Utils::Colours::BG_BCYAN = "106"
 Bright cyan background.
 
constexpr const char * Utils::Colours::BG_BWHITE = "107"
 Bright white background.
 
Text style modifiers
constexpr const char * Utils::Colours::RESET = "0"
 Reset all attributes.
 
constexpr const char * Utils::Colours::BOLD = "1"
 Bold / increased intensity.
 
constexpr const char * Utils::Colours::DIM = "2"
 Dim / decreased intensity.
 
constexpr const char * Utils::Colours::ITALIC = "3"
 Italic.
 
constexpr const char * Utils::Colours::UNDERLINE = "4"
 Underline.
 
constexpr const char * Utils::Colours::BLINK = "5"
 Slow blink.
 
constexpr const char * Utils::Colours::INVERSE = "7"
 Reverse video (swap FG/BG).
 
constexpr const char * Utils::Colours::HIDDEN = "8"
 Conceal / invisible.
 
constexpr const char * Utils::Colours::STRIKETHRU = "9"
 Strikethrough.
 

Detailed Description

TUI utility functions: ANSI colour helpers, input reading, and string metrics.

Author
Darleanow

All functions in this file are inline and live in header-only namespaces so they can be included by any TUI translation unit without ODR issues.

Colour usage

Build an ANSI escape sequence with Utils::Colours::esc and combine it with Utils::Colours::RESET to restore the default style:

+ "hello"
constexpr const char * FG_GREEN
Green foreground.
Definition Utils.hpp:54
std::string esc(const char *code)
Build a complete ANSI SGR escape sequence from a code string.
Definition Utils.hpp:122
constexpr const char * RESET
Reset all attributes.
Definition Utils.hpp:102