OpenMS
|
Color and style the fonts shown on cout/cerr (or other streams) More...
#include <OpenMS/CONCEPT/Colorizer.h>
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... | |
Colorizer & | operator() () |
template<typename T > | |
Colorizer & | operator() (T s) |
Colorizer & | undo () |
Colorizer & | undoAll () |
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... | |
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.
You can also modify a single item and immediately return to the previous setting by passing the item to the bracket operator, e.g.
Undo() has limitations and does not support nesting of colors, i.e. does not remember the previous color, but resets to default
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).
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
Class Members | ||
---|---|---|
const char * | disable | ANSO code to undo the color/style. |
const char * | enable | ANSI code to activate the color/style. |
Colorizer | ( | const ConsoleColor | color | ) |
Constructor.
|
delete |
Constructor (deleted)
Copy constructor (deleted)
If you really wanted to, you can implement a copy c'tor, but there seems little use
|
default |
Destructor.
|
staticprotected |
color the stream
using the given color
|
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)
|
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'.
|
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();'
|
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 ";
|
protected |
write the content of input_ to the stream
|
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)
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"'
Colorizer& undoAll | ( | ) |
prepare this colorizer to reset the console to its default colors/style.
|
friend |
|
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"'
|
protected |
color of the stream (const; set in C'tor)
|
protected |
resets all attributes to default
|
inlinestaticconstexprprotected |
ANSI colors/styles, corresponding to values of enum ConsoleColor.
|
protected |
internal string buffer when using operator()(T data) This data will be colored
|
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)'
|
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)'
|
protected |
optimization to prevent coloring a stream, print nothing and then immediately uncoloring is, e.g. when calling c.undo() or c.undoAll()