File: utilities/ansi_colors.hxx
Dependencies: none
Shared ANSI escape-code constants and TTY-aware color wrappers. Used internally by logger, benchmarking, and testing.
bool ansi::enabled() // true when stdout is a real TTY (result is cached)ansi::codes::reset
ansi::codes::red
ansi::codes::green
ansi::codes::yellow
ansi::codes::cyan
ansi::codes::blue
ansi::codes::magenta
ansi::codes::white
ansi::codes::bold
ansi::codes::dim
// ...and bright variantsAll codes are constexpr const char* ANSI escape sequences.
std::string ansi::green(std::string_view s)
std::string ansi::red(std::string_view s)
std::string ansi::yellow(std::string_view s)
std::string ansi::cyan(std::string_view s)
std::string ansi::bold(std::string_view s)
std::string ansi::dim(std::string_view s)Each wrapper returns the string wrapped in the appropriate escape codes when enabled() is true, or returns the string unchanged otherwise.
#include "ansi_colors.hxx"
// Check TTY
if (ansi::enabled()) {
std::cout << ansi::green("OK") << "\n";
}
// Always safe — no-op when not a TTY
std::cout << ansi::red("Error: ") << message << "\n";
// Use raw codes directly
std::cout << ansi::codes::bold << "Important" << ansi::codes::reset << "\n";enabled() calls isatty() once and caches the result — subsequent calls are a simple boolean load. The color wrapper functions perform one branch and one string allocation when colors are on, and return the input string unchanged when off.
Use raw ansi::codes::* constants in tight loops to avoid any allocation.