Pretty Diagnostics
Create your own pretty diagnostics
Loading...
Searching...
No Matches
pretty_diagnostics::color Namespace Reference

Data Structures

struct  SyledText
 Holds a piece of text together with one or more ANSI style codes. More...
 

Enumerations

enum class  Code {
  Reset = 0 , Bold = 1 , Dim = 2 , Italic = 3 ,
  Underline = 4 , Blink = 5 , Reverse = 7 , Hidden = 8 ,
  Strikethrough = 9 , FgBlack = 30 , FgRed = 31 , FgGreen = 32 ,
  FgYellow = 33 , FgBlue = 34 , FgMagenta = 35 , FgCyan = 36 ,
  FgWhite = 37 , FgDefault = 39 , BgBlack = 40 , BgRed = 41 ,
  BgGreen = 42 , BgYellow = 43 , BgBlue = 44 , BgMagenta = 45 ,
  BgCyan = 46 , BgWhite = 47 , BgDefault = 49 , FgBrightBlack = 90 ,
  FgBrightRed = 91 , FgBrightGreen = 92 , FgBrightYellow = 93 , FgBrightBlue = 94 ,
  FgBrightMagenta = 95 , FgBrightCyan = 96 , FgBrightWhite = 97 , BgBrightBlack = 100 ,
  BgBrightRed = 101 , BgBrightGreen = 102 , BgBrightYellow = 103 , BgBrightBlue = 104 ,
  BgBrightMagenta = 105 , BgBrightCyan = 106 , BgBrightWhite = 107
}
 ANSI escape codes for text styling and coloring. More...
 

Functions

std::ostream & operator<< (std::ostream &os, Code code)
 Outputs a single ANSI escape code to the given stream. More...
 
template<std::size_t N>
std::ostream & operator<< (std::ostream &os, const SyledText< N > &text)
 Outputs styled text to the given stream. More...
 
template<typename... Codes>
requires (std::conjunction_v<std::is_same<Codes, Code>...>)
SyledText< sizeof...(Codes)> STYLE (std::string_view text, Codes... codes)
 Creates a styled text object from a text and a set of ANSI codes. More...
 
int color_enabled_index ()
 Returns a unique std::ios_base::xalloc index used to store per-stream color enable state. More...
 
void set_color_enabled (std::ostream &os, bool enabled)
 Enables or disables color output for a specific output stream. More...
 
bool is_color_enabled (std::ostream &os)
 Checks whether color output is enabled for the given stream. More...
 
bool is_colorable (const std::ostream &os)
 Checks whether the given output stream supports colored output. More...
 
void auto_enable_color (std::ostream &os)
 Enables or disables color output for a stream based on its capability. More...
 

Enumeration Type Documentation

◆ Code

ANSI escape codes for text styling and coloring.

Enumerator
Reset 
Bold 
Dim 
Italic 
Underline 
Blink 
Reverse 
Hidden 
Strikethrough 
FgBlack 
FgRed 
FgGreen 
FgYellow 
FgBlue 
FgMagenta 
FgCyan 
FgWhite 
FgDefault 
BgBlack 
BgRed 
BgGreen 
BgYellow 
BgBlue 
BgMagenta 
BgCyan 
BgWhite 
BgDefault 
FgBrightBlack 
FgBrightRed 
FgBrightGreen 
FgBrightYellow 
FgBrightBlue 
FgBrightMagenta 
FgBrightCyan 
FgBrightWhite 
BgBrightBlack 
BgBrightRed 
BgBrightGreen 
BgBrightYellow 
BgBrightBlue 
BgBrightMagenta 
BgBrightCyan 
BgBrightWhite 

Function Documentation

◆ auto_enable_color()

void pretty_diagnostics::color::auto_enable_color ( std::ostream &  os)

Enables or disables color output for a stream based on its capability.

Calls is_colorable(os) and enables color output if supported, otherwise disables it.

Parameters
osOutput stream to initialize

◆ color_enabled_index()

int pretty_diagnostics::color::color_enabled_index ( )

Returns a unique std::ios_base::xalloc index used to store per-stream color enable state.

Returns
Index used for accessing the stream-local color flag

◆ is_color_enabled()

bool pretty_diagnostics::color::is_color_enabled ( std::ostream &  os)

Checks whether color output is enabled for the given stream.

Parameters
osOutput stream to query
Returns
True if color output is enabled, false otherwise

◆ is_colorable()

bool pretty_diagnostics::color::is_colorable ( const std::ostream &  os)

Checks whether the given output stream supports colored output.

Performs a platform-specific best-effort check to determine whether the stream is connected to a terminal capable of displaying ANSI escape codes.

Parameters
osOutput stream to check
Returns
True if the stream likely supports color output

◆ operator<<() [1/2]

std::ostream & pretty_diagnostics::color::operator<< ( std::ostream &  os,
Code  code 
)

Outputs a single ANSI escape code to the given stream.

If color output is disabled for the stream, this operation produces no output

Parameters
osTarget output stream
codeANSI style or color code to emit
Returns
Reference to the given stream

◆ operator<<() [2/2]

template<std::size_t N>
std::ostream & pretty_diagnostics::color::operator<< ( std::ostream &  os,
const SyledText< N > &  text 
)

Outputs styled text to the given stream.

All style codes are emitted in order, followed by the text and a final reset code to restore default formatting

Template Parameters
NNumber of ANSI codes applied to the text
Parameters
osTarget output stream
textStyled text to be written
Returns
Reference to the given stream

◆ set_color_enabled()

void pretty_diagnostics::color::set_color_enabled ( std::ostream &  os,
bool  enabled 
)

Enables or disables color output for a specific output stream.

When disabled, no ANSI escape codes will be emitted for that stream

Parameters
osOutput stream to modify
enabledWhether color output should be enabled

◆ STYLE()

template<typename... Codes>
requires (std::conjunction_v<std::is_same<Codes, Code>...>)
SyledText< sizeof...(Codes)> pretty_diagnostics::color::STYLE ( std::string_view  text,
Codes...  codes 
)

Creates a styled text object from a text and a set of ANSI codes.

This helper enables concise and expressive syntax for applying multiple styles to a single text segment

Parameters
textText to be styled
codesANSI style and color codes to apply
Returns
A SyledText instance containing the provided styles and text