C++ Utilities 5.24.7
Useful C++ classes and routines such as argument parser, IO and conversion utilities
Loading...
Searching...
No Matches
Enumerations | Functions | Variables
CppUtilities::EscapeCodes Namespace Reference

Encapsulates functions for formatted terminal output using ANSI escape codes. More...

Enumerations

enum class  Color : char {
  Black = '0' , Red , Green , Yellow ,
  Blue , Purple , Cyan , White
}
 
enum class  ColorContext : char { Foreground = '3' , Background = '4' }
 
enum class  TextAttribute : char {
  Reset = '0' , Bold = '1' , Dim = '2' , Italic = '3' ,
  Underscore = '4' , Blink = '5' , ReverseVideo = '7' , Concealed = '8' ,
  Strikethrough = '9'
}
 
enum class  Direction : char { Up = 'A' , Down = 'B' , Forward = 'C' , Backward = 'D' }
 
enum class  Phrases {
  Error , Warning , End , PlainMessage ,
  SuccessMessage , SubMessage , ErrorMessage , WarningMessage ,
  EndFlush , Info , Override , SubError ,
  SubWarning , InfoMessage
}
 The Phrases enum contains standard phrases which can be printed to any std::ostream and obtained as strings via EscapeCodes::phraseString() and EscapeCodes::formattedPhraseString(). More...
 

Functions

void setStyle (std::ostream &stream, TextAttribute displayAttribute=TextAttribute::Reset)
 
void setStyle (std::ostream &stream, Color color, ColorContext context=ColorContext::Foreground, TextAttribute displayAttribute=TextAttribute::Reset)
 
void setStyle (std::ostream &stream, Color foregroundColor, Color backgroundColor, TextAttribute displayAttribute=TextAttribute::Reset)
 
void resetStyle (std::ostream &stream)
 
void setCursor (std::ostream &stream, unsigned int row=0, unsigned int col=0)
 
void moveCursor (std::ostream &stream, unsigned int cells, Direction direction)
 
void saveCursor (std::ostream &stream)
 
void restoreCursor (std::ostream &stream)
 
void eraseDisplay (std::ostream &stream)
 
void eraseLine (std::ostream &stream)
 
std::ostream & operator<< (std::ostream &stream, TextAttribute displayAttribute)
 
constexpr auto color (Color foreground, Color background, TextAttribute displayAttribute=TextAttribute::Reset)
 
constexpr auto color (Color foreground, ColorContext context, TextAttribute displayAttribute=TextAttribute::Reset)
 
template<typename TupleType , Traits::EnableIfAny< std::is_same< TupleType, std::tuple< Color, Color, TextAttribute > >, std::is_same< TupleType, std::tuple< Color, ColorContext, TextAttribute > > > * = nullptr>
std::ostream & operator<< (std::ostream &stream, TupleType displayAttribute)
 
CPP_UTILITIES_EXPORT std::ostream & operator<< (std::ostream &stream, Phrases phrase)
 Prints the specified phrase in a formatted manner using ANSI escape codes.
 
CPP_UTILITIES_EXPORT std::string_view phraseString (Phrases phrase)
 Returns a string for the specified phrase without formatting.
 
CPP_UTILITIES_EXPORT std::string_view formattedPhraseString (Phrases phrase)
 Returns a string for the specified phrase which is formatted using ANSI escape codes.
 

Variables

CPP_UTILITIES_EXPORT bool enabled
 Controls whether the functions inside the EscapeCodes namespace actually make use of escape codes.
 

Detailed Description

Encapsulates functions for formatted terminal output using ANSI escape codes.

Enumeration Type Documentation

◆ Color

Enumerator
Black 
Red 
Green 
Yellow 
Blue 
Purple 
Cyan 
White 

Definition at line 16 of file ansiescapecodes.h.

◆ ColorContext

Enumerator
Foreground 
Background 

Definition at line 18 of file ansiescapecodes.h.

◆ Direction

Enumerator
Up 
Down 
Forward 
Backward 

Definition at line 32 of file ansiescapecodes.h.

◆ Phrases

The Phrases enum contains standard phrases which can be printed to any std::ostream and obtained as strings via EscapeCodes::phraseString() and EscapeCodes::formattedPhraseString().

Example: std::cerr << Phrases::Error << "Something bad happened." << Phrases::End

Enumerator
Error 

bold, red "Error: "

Warning 

bold, yellow "Warning: "

End 

resets the style

PlainMessage 

bold, 4 spaces " "

SuccessMessage 

bold, green "==> "

SubMessage 

bold, green " -> "

ErrorMessage 

bold, red "==> ERROR: "

WarningMessage 

bold, yellow "==> WARNING: "

EndFlush 

resets the style and flushes the stream

Info 

bold, blue "Info: "

Override 

erases the current line

SubError 

bold, red " -> ERROR: "

SubWarning 

bold, yellow " -> WARNING: "

InfoMessage 

bold, white "==> "

Definition at line 138 of file ansiescapecodes.h.

◆ TextAttribute

Enumerator
Reset 
Bold 
Dim 
Italic 
Underscore 
Blink 
ReverseVideo 
Concealed 
Strikethrough 

Definition at line 20 of file ansiescapecodes.h.

Function Documentation

◆ color() [1/2]

constexpr auto CppUtilities::EscapeCodes::color ( Color foreground,
Color background,
TextAttribute displayAttribute = TextAttribute::Reset )
constexpr

Definition at line 113 of file ansiescapecodes.h.

◆ color() [2/2]

constexpr auto CppUtilities::EscapeCodes::color ( Color foreground,
ColorContext context,
TextAttribute displayAttribute = TextAttribute::Reset )
constexpr

Definition at line 118 of file ansiescapecodes.h.

◆ eraseDisplay()

void CppUtilities::EscapeCodes::eraseDisplay ( std::ostream & stream)
inline

Definition at line 93 of file ansiescapecodes.h.

◆ eraseLine()

void CppUtilities::EscapeCodes::eraseLine ( std::ostream & stream)
inline

Definition at line 100 of file ansiescapecodes.h.

◆ formattedPhraseString()

std::string_view CppUtilities::EscapeCodes::formattedPhraseString ( Phrases phrase)

Returns a string for the specified phrase which is formatted using ANSI escape codes.

Remarks
This function is still experimental. It might be modified in an incompatible way or even removed in the next minor or patch release.

Definition at line 159 of file ansiescapecodes.cpp.

◆ moveCursor()

void CppUtilities::EscapeCodes::moveCursor ( std::ostream & stream,
unsigned int cells,
Direction direction )
inline

Definition at line 72 of file ansiescapecodes.h.

◆ operator<<() [1/3]

std::ostream & CppUtilities::EscapeCodes::operator<< ( std::ostream & stream,
Phrases phrase )

Prints the specified phrase in a formatted manner using ANSI escape codes.

Definition at line 33 of file ansiescapecodes.cpp.

◆ operator<<() [2/3]

std::ostream & CppUtilities::EscapeCodes::operator<< ( std::ostream & stream,
TextAttribute displayAttribute )
inline

Definition at line 107 of file ansiescapecodes.h.

◆ operator<<() [3/3]

template<typename TupleType , Traits::EnableIfAny< std::is_same< TupleType, std::tuple< Color, Color, TextAttribute > >, std::is_same< TupleType, std::tuple< Color, ColorContext, TextAttribute > > > * = nullptr>
std::ostream & CppUtilities::EscapeCodes::operator<< ( std::ostream & stream,
TupleType displayAttribute )
inline

Definition at line 126 of file ansiescapecodes.h.

◆ phraseString()

std::string_view CppUtilities::EscapeCodes::phraseString ( Phrases phrase)

Returns a string for the specified phrase without formatting.

Definition at line 120 of file ansiescapecodes.cpp.

◆ resetStyle()

void CppUtilities::EscapeCodes::resetStyle ( std::ostream & stream)
inline

Definition at line 58 of file ansiescapecodes.h.

◆ restoreCursor()

void CppUtilities::EscapeCodes::restoreCursor ( std::ostream & stream)
inline

Definition at line 86 of file ansiescapecodes.h.

◆ saveCursor()

void CppUtilities::EscapeCodes::saveCursor ( std::ostream & stream)
inline

Definition at line 79 of file ansiescapecodes.h.

◆ setCursor()

void CppUtilities::EscapeCodes::setCursor ( std::ostream & stream,
unsigned int row = 0,
unsigned int col = 0 )
inline

Definition at line 65 of file ansiescapecodes.h.

◆ setStyle() [1/3]

void CppUtilities::EscapeCodes::setStyle ( std::ostream & stream,
Color color,
ColorContext context = ColorContext::Foreground,
TextAttribute displayAttribute = TextAttribute::Reset )
inline

Definition at line 41 of file ansiescapecodes.h.

◆ setStyle() [2/3]

void CppUtilities::EscapeCodes::setStyle ( std::ostream & stream,
Color foregroundColor,
Color backgroundColor,
TextAttribute displayAttribute = TextAttribute::Reset )
inline

Definition at line 49 of file ansiescapecodes.h.

◆ setStyle() [3/3]

void CppUtilities::EscapeCodes::setStyle ( std::ostream & stream,
TextAttribute displayAttribute = TextAttribute::Reset )
inline

Definition at line 34 of file ansiescapecodes.h.

Variable Documentation

◆ enabled

bool CppUtilities::EscapeCodes::enabled
Initial value:
=
false

Controls whether the functions inside the EscapeCodes namespace actually make use of escape codes.

This allows to disable use of escape codes when not appropriate.

The default value can be configured at build time by setting the CMake variable ENABLE_ESCAPE_CODES_BY_DEFAULT. The "default for the default" is true. However, the default is overridden with the value of the environment variable ENABLE_ESCAPE_CODES when instantiating a NoColorArgument (if ENABLE_ESCAPE_CODES is present).

See also
NoColorArgument

Definition at line 22 of file ansiescapecodes.cpp.