OpenMS
Colorizer Class Reference

Color and style the fonts shown on cout/cerr (or other streams) More...

#include <OpenMS/CONCEPT/Colorizer.h>

Collaboration diagram for Colorizer:
[legend]

Classes

struct  ColorWithUndo_
 

Public Member Functions

 Colorizer (const ConsoleColor color)
 Constructor. More...
 
 Colorizer ()=delete
 Constructor (deleted) More...
 
 Colorizer (const Colorizer &rhs)=delete
 Copy constructor (deleted) More...
 
 ~Colorizer ()=default
 Destructor. More...
 
Colorizeroperator() ()
 
template<typename T >
Colorizeroperator() (T s)
 
Colorizerundo ()
 
ColorizerundoAll ()
 prepare this colorizer to reset the console to its default colors/style. More...
 

Static Public Member Functions

static bool isTTY (const std::ostream &stream)
 

Protected Member Functions

void outputToStream_ (std::ostream &o_stream)
 write the content of input_ to the stream More...
 
const std::stringstream & getInternalChars_ () const
 
void setInternalChars_ (const std::string &data)
 

Static Protected Member Functions

static void colorStream_ (std::ostream &stream, const char *ANSI_command)
 color the stream using the given color More...
 

Protected Attributes

const ConsoleColor color_
 color of the stream (const; set in C'tor) More...
 
bool undo_ = true
 
bool undo_all_ = true
 
bool undos_only = false
 
const char * color_undo_all_ = "\033[0m"
 resets all attributes to default More...
 
std::stringstream input_
 

Static Protected Attributes

static constexpr const std::array< ColorWithUndo_, 9 > colors_
 ANSI colors/styles, corresponding to values of enum ConsoleColor. More...
 

Friends

class IndentedStream
 
std::ostream & operator<< (std::ostream &o_stream, Colorizer &col)
 stream insertion, e.g. std::cout << red() << "this is red" << red.undo(); More...
 

Detailed Description

Color and style the fonts shown on cout/cerr (or other streams)

Allows to color the console fonts' foreground color (making the current color brighter, or setting a new color) and/or add an underline. There are predefined Colorizer objects for your convenience, e.g. 'red' or 'underline'. They are named identically to the possible values of enum ConsoleColor. Multiple styles can be combined (with limitations for nesting colors, see below). To undo a color/style, simply call its '.undo()' function. To undo all modifications and return to a normal console color/style, call 'undoAll()'.

e.g.

std::cout << "normal text" << red() << "this is red" << underline() << "red and underlined" << red.undo() << "just underlined" << underline.undo();
Colorizer & undo()
Colorizer red
Colorizer underline

You can also modify a single item and immediately return to the previous setting by passing the item to the bracket operator, e.g.

std::cout << "normal text << red("this part is red") << " back to normal here";

Undo() has limitations and does not support nesting of colors, i.e. does not remember the previous color, but resets to default

std::cout << red() << "this is red" << "blue() << "this is blue" << blue.undo() << " not red! but normal";
Colorizer blue

Redirecting cout/cerr streams
If std::cout or std::cerr are redirected to a file, then Colorizer will detect this and not emit any color/style information. This is to avoid the ANSI color codes showing up in the file instead of being filtered out by the console's color process.

Note: all OS's we know (Windows, Linux, MacOS) only have a single color configuration for the whole console/terminal, independent of streams. I.e. if you apply a permanent color to std::cout then all subsequent output to std::cerr will also be colored! (unless std::cout is redirected to a file, then coloring std::cout will have no effect, not even when printing to the console using std::cerr).


Class Documentation

◆ OpenMS::Colorizer::ColorWithUndo_

struct OpenMS::Colorizer::ColorWithUndo_

holds ANSI codes to switch to a certain color or undo this effect The undo may not be a perfect fit when nesting, e.g. 'cout << red() << blue() << blue.undo()' will not yield red, but the default color

Collaboration diagram for Colorizer::ColorWithUndo_:
[legend]
Class Members
const char * disable ANSO code to undo the color/style.
const char * enable ANSI code to activate the color/style.

Constructor & Destructor Documentation

◆ Colorizer() [1/3]

Colorizer ( const ConsoleColor  color)

Constructor.

◆ Colorizer() [2/3]

Colorizer ( )
delete

Constructor (deleted)

◆ Colorizer() [3/3]

Colorizer ( const Colorizer rhs)
delete

Copy constructor (deleted)

If you really wanted to, you can implement a copy c'tor, but there seems little use

◆ ~Colorizer()

~Colorizer ( )
default

Destructor.

Member Function Documentation

◆ colorStream_()

static void colorStream_ ( std::ostream &  stream,
const char *  ANSI_command 
)
staticprotected

color the stream using the given color

◆ getInternalChars_()

const std::stringstream& getInternalChars_ ( ) const
inlineprotected

internal methods used by friends to manipulate the internal data (if present) This avoids exposing the stream coloring itself (which is easy to get wrong)

◆ isTTY()

static bool isTTY ( const std::ostream &  stream)
static

A wrapper for POSIX 'isatty()' or the equivalent on Windows. Checks if the stream is written to the terminal/console or being redirected to a file/NULL/nul (i.e. not visible). This only works for std::cout and std::cerr. Passing any other stream will always return 'false'.

◆ operator()() [1/2]

Colorizer& operator() ( )
inline

bracket operator to prepare this colorizer object to color the next stream it is applied to with this object's color until it is reset somewhere downstream. e.g. 'cerr << red() << "this is red" << " this too ..." << red.undo();'

◆ operator()() [2/2]

Colorizer& operator() ( s)
inline

Consume s, convert it to a string and insert this string with color into the next stream this Colorizer is applied to. Reset the stream right away. e.g. 'cerr << red("make this red!") << " this is not red ";

◆ outputToStream_()

void outputToStream_ ( std::ostream &  o_stream)
protected

write the content of input_ to the stream

◆ setInternalChars_()

void setInternalChars_ ( const std::string &  data)
inlineprotected

internal methods used by friends to manipulate the internal data (if present) This avoids exposing the stream coloring itself (which is easy to get wrong)

◆ undo()

Colorizer& undo ( )

prepare this colorizer to undo the effect of this Colorizer object to the next stream it is applied to e.g. 'cerr << red.undo() << "not red anymore"'

◆ undoAll()

Colorizer& undoAll ( )

prepare this colorizer to reset the console to its default colors/style.

Friends And Related Function Documentation

◆ IndentedStream

friend class IndentedStream
friend

◆ operator<<

std::ostream& operator<< ( std::ostream &  o_stream,
Colorizer col 
)
friend

stream insertion, e.g. std::cout << red() << "this is red" << red.undo();

stream operator for Colorizers, which will (depending on the state of col) add color to the o_stream, and print some internal string, e.g. 'cout << red("this is red")' or 'cout << red() << "stream stays red until dooms day";' or reset the color e.g. 'cout << red.undo() << "not red anymore"'

Member Data Documentation

◆ color_

const ConsoleColor color_
protected

color of the stream (const; set in C'tor)

◆ color_undo_all_

const char* color_undo_all_ = "\033[0m"
protected

resets all attributes to default

◆ colors_

constexpr const std::array<ColorWithUndo_, 9> colors_
inlinestaticconstexprprotected
Initial value:
{
ColorWithUndo_ {"\033[91m", "\033[39m"},
ColorWithUndo_ {"\033[92m", "\033[39m"},
ColorWithUndo_ {"\033[93m", "\033[39m"},
ColorWithUndo_ {"\033[94m", "\033[39m"},
ColorWithUndo_ {"\033[95m", "\033[39m"},
ColorWithUndo_ {"\033[96m", "\033[39m"},
ColorWithUndo_ {"\033[4m", "\033[24m"},
ColorWithUndo_ {"\033[1m", "\033[22m"},
ColorWithUndo_ {"\033[7m", "\033[27m"},
}

ANSI colors/styles, corresponding to values of enum ConsoleColor.

◆ input_

std::stringstream input_
protected

internal string buffer when using operator()(T data) This data will be colored

◆ undo_

bool undo_ = true
protected

clear the color/style of this Colorizer from a stream (usually cout/cerr) upon the next call of 'std::ostream& operator<<(std::ostream& o_stream, OpenMS::Colorizer& col)'

◆ undo_all_

bool undo_all_ = true
protected

clear all color/styles from a stream (usually cout/cerr) upon the next call of 'std::ostream& operator<<(std::ostream& o_stream, OpenMS::Colorizer& col)'

◆ undos_only

bool undos_only = false
protected

optimization to prevent coloring a stream, print nothing and then immediately uncoloring is, e.g. when calling c.undo() or c.undoAll()