A library for working with phylogenetic and population genetic data.
v0.32.0
Style Class Reference

#include <genesis/utils/text/style.hpp>

Detailed Description

Simple text style class for colorized and bold output to a terminal.

This class bundles the following text style properties for output in a terminal:

Those properties can be set using either the respective constructor of this class or using the setter functions. Per default, all of them are empty, meaning that no style manupulation is done.

In order to generate textual output with those styles, operator()() is used. Example:

Style blue( "blue" );
blue.bold( true );
std::cout << blue( "some text" );

Furthermore, the stream operator can be used to get a summary of the properties of a Style object:

LOG_DBG << blue;

will output

Foreground Color: Blue
Background Color:
Bold:             true

to the LOG stream.

Definition at line 81 of file style.hpp.

Public Member Functions

 Style ()=default
 
 Style (bool bold)
 
 Style (const char *foreground_color)
 
 Style (std::string const &foreground_color)
 
 Style (std::string const &foreground_color, bool bold)
 
 Style (std::string const &foreground_color, const char *background_color)
 
 Style (std::string const &foreground_color, std::string const &background_color)
 
 Style (std::string const &foreground_color, std::string const &background_color, bool bold)
 
 Style (Style &&)=default
 
 Style (Style const &)=default
 
 ~Style ()=default
 
std::string background_color () const
 
Stylebackground_color (std::string const &color)
 
bool bold () const
 Return whether the Style uses bold. More...
 
Stylebold (bool value)
 Set whether the Style uses bold. More...
 
bool enabled () const
 Return whether the Style is currently enabled. More...
 
Styleenabled (bool value)
 Set whether the Style is enabled. More...
 
std::string foreground_color () const
 
Styleforeground_color (std::string const &color)
 
std::string operator() (std::string const &text) const
 Operator that returns a text with the current Style applied to it. More...
 
Styleoperator= (Style &&)=default
 
Styleoperator= (Style const &)=default
 
Stylereset ()
 Reset the Style to use not colors and not bold. More...
 
void swap (Style &other)
 
std::string to_bash_string (std::string const &text) const
 Additional output function with the same purpose as operator(). More...
 
std::string to_python_string (std::string const &text) const
 Additional output function with the same purpose as operator(). More...
 

Static Public Member Functions

static std::string get_background_color_value (std::string name)
 Return the color value string for a given background color name. More...
 
static std::string get_foreground_color_value (std::string name)
 Return the color value string for a given foreground color name. More...
 
static bool is_background_color (std::string name)
 Return true iff the given name is a background color name. More...
 
static bool is_foreground_color (std::string name)
 Return true iff the given name is a foreground color name. More...
 

Static Public Attributes

static const std::array< std::pair< std::string, std::string >, 17 > background_colors
 List of all valid background color names and their color strings. More...
 
static const std::array< std::pair< std::string, std::string >, 17 > foreground_colors
 List of all valid foreground color names and their color strings. More...
 

Constructor & Destructor Documentation

◆ Style() [1/10]

Style ( )
default

◆ Style() [2/10]

Style ( std::string const &  foreground_color)
inlineexplicit

Definition at line 91 of file style.hpp.

◆ Style() [3/10]

Style ( const char *  foreground_color)
inlineexplicit

Definition at line 95 of file style.hpp.

◆ Style() [4/10]

Style ( bool  bold)
inlineexplicit

Definition at line 99 of file style.hpp.

◆ Style() [5/10]

Style ( std::string const &  foreground_color,
bool  bold 
)
inline

Definition at line 103 of file style.hpp.

◆ Style() [6/10]

Style ( std::string const &  foreground_color,
std::string const &  background_color 
)
inline

Definition at line 108 of file style.hpp.

◆ Style() [7/10]

Style ( std::string const &  foreground_color,
const char *  background_color 
)
inline

Definition at line 113 of file style.hpp.

◆ Style() [8/10]

Style ( std::string const &  foreground_color,
std::string const &  background_color,
bool  bold 
)
inline

Definition at line 118 of file style.hpp.

◆ ~Style()

~Style ( )
default

◆ Style() [9/10]

Style ( Style const &  )
default

◆ Style() [10/10]

Style ( Style &&  )
default

Member Function Documentation

◆ background_color() [1/2]

std::string background_color ( ) const

Definition at line 163 of file style.cpp.

◆ background_color() [2/2]

Style & background_color ( std::string const &  color)

Definition at line 174 of file style.cpp.

◆ bold() [1/2]

bool bold ( ) const

Return whether the Style uses bold.

Definition at line 125 of file style.cpp.

◆ bold() [2/2]

Style & bold ( bool  value)

Set whether the Style uses bold.

The function returns the Style object itself, in order to allow a fluent interface.

Definition at line 135 of file style.cpp.

◆ enabled() [1/2]

bool enabled ( ) const

Return whether the Style is currently enabled.

Definition at line 103 of file style.cpp.

◆ enabled() [2/2]

Style & enabled ( bool  value)

Set whether the Style is enabled.

If set to false, no style attributes are outputted when the Style is applied to a text with operator()(). Default is true.

The function returns the Style object itself, in order to allow a fluent interface.

Definition at line 116 of file style.cpp.

◆ foreground_color() [1/2]

std::string foreground_color ( ) const

Definition at line 141 of file style.cpp.

◆ foreground_color() [2/2]

Style & foreground_color ( std::string const &  color)

Definition at line 152 of file style.cpp.

◆ get_background_color_value()

std::string get_background_color_value ( std::string  name)
static

Return the color value string for a given background color name.

See Style::foreground_colors for background valid color names. If the name is invalid, the function throws an std::out_of_range exception.

Definition at line 332 of file style.cpp.

◆ get_foreground_color_value()

std::string get_foreground_color_value ( std::string  name)
static

Return the color value string for a given foreground color name.

See Style::foreground_colors for valid foreground color names. If the name is invalid, the function throws an std::out_of_range exception.

Definition at line 281 of file style.cpp.

◆ is_background_color()

bool is_background_color ( std::string  name)
static

Return true iff the given name is a background color name.

Definition at line 321 of file style.cpp.

◆ is_foreground_color()

bool is_foreground_color ( std::string  name)
static

Return true iff the given name is a foreground color name.

Definition at line 270 of file style.cpp.

◆ operator()()

std::string operator() ( std::string const &  text) const

Operator that returns a text with the current Style applied to it.

This is most helpful for outputting styled text to a terminal. See the Style class description for an example.

Definition at line 220 of file style.cpp.

◆ operator=() [1/2]

Style& operator= ( Style &&  )
default

◆ operator=() [2/2]

Style& operator= ( Style const &  )
default

◆ reset()

Style & reset ( )

Reset the Style to use not colors and not bold.

Definition at line 91 of file style.cpp.

◆ swap()

void swap ( Style other)
inline

Definition at line 132 of file style.hpp.

◆ to_bash_string()

std::string to_bash_string ( std::string const &  text) const

Additional output function with the same purpose as operator().

It uses the notation \033 for the escape symbol, which is best interpreted by bash. Usually, there is no need to use this function. The operator() should just work fine.

Definition at line 231 of file style.cpp.

◆ to_python_string()

std::string to_python_string ( std::string const &  text) const

Additional output function with the same purpose as operator().

It uses the notation \x1b for the escape symbol, which is best interpreted by python. Usually, there is no need to use this function. The operator() should just work fine.

Definition at line 249 of file style.cpp.

Member Data Documentation

◆ background_colors

const std::array< std::pair< std::string, std::string >, 17 > background_colors
static
Initial value:
= {{
{ "Default", "49" },
{ "Black", "40" },
{ "Red", "41" },
{ "Green", "42" },
{ "Yellow", "43" },
{ "Blue", "44" },
{ "Magenta", "45" },
{ "Cyan", "46" },
{ "LightGray", "47" },
{ "DarkGray", "100" },
{ "LightRed", "101" },
{ "LightGreen", "102" },
{ "LightYellow", "103" },
{ "LightBlue", "104" },
{ "LightMagenta", "105" },
{ "LightCyan", "106" },
{ "White", "107" }
}}

List of all valid background color names and their color strings.

Definition at line 179 of file style.hpp.

◆ foreground_colors

const std::array< std::pair< std::string, std::string >, 17 > foreground_colors
static
Initial value:
= {{
{ "Default", "39" },
{ "Black", "30" },
{ "Red", "31" },
{ "Green", "32" },
{ "Yellow", "33" },
{ "Blue", "34" },
{ "Magenta", "35" },
{ "Cyan", "36" },
{ "LightGray", "37" },
{ "DarkGray", "90" },
{ "LightRed", "91" },
{ "LightGreen", "92" },
{ "LightYellow", "93" },
{ "LightBlue", "94" },
{ "LightMagenta", "95" },
{ "LightCyan", "96" },
{ "White", "97" }
}}

List of all valid foreground color names and their color strings.

Definition at line 178 of file style.hpp.


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