TesC (Text escape Colorizer) is a tiny C++17 library to decorate console output with ANSI codes-based ostream manipulators
- Download the library source;
#includethetesc.hppfile in your C++ project;- Enjoy!
⚠️ Note that the library requires C++17 support
Output decoration is carried out using manipulators. The library provides three of them:
tesc::color— to change current fore-/background colors;tesc::font— to change current font style settings;tesc::reset— to revert all settings to its default values.
To change output text colors (applies to the following output only) use the tesc::color manipulator with corresponding parameters like this:
// For pretty usage
using namespace tesc;
std::cout << color{ bright(face::white) | back::red } << "Error badge";Several parameters for a manipulator are joining with each other via operator |. Regarding colors, you can join background (enum back) and foreground (enum face) colors only. If you didn't specify one of them, the corresponding color stays the same as before.
Enumerations tesc::back and tesc::face contains the following color codes:
| Name | FG Code | BG code |
|---|---|---|
black |
30 | 40 |
red |
31 | 41 |
green |
32 | 42 |
yellow |
33 | 43 |
blue |
34 | 44 |
magenta |
35 | 45 |
cyan |
36 | 46 |
white |
37 | 47 |
Brighter colors can be obtained using the tesc::bright(...) function (see example above). It adds 60 to the code of a given color. For example, bright(back::blue) returns the color with code 104.
TesC may be used to set bold, italic or/and underlined text style. Use the tesc::font manipulator with the following tesc::style settings:
| Name | ANSI Code | Enum values |
|---|---|---|
normal |
22; 23; 24 | 0 |
bold |
1; 23; 24 | 1 |
italic |
3; 22; 24 | 2 |
underline |
4; 22; 23 | 4 |
As for tesc::color, these parameters are joinable as well. Note that if you haven't used some of settings, they will be automatically cancelled. For example:
// The best way is to use it locally (in function body)
using namespace tesc;
std::cout << font{ style::bold | style::underline } << "Important!"
<< font{ style::italic } << "Italic only";Use the tesc::reset manipulator (like std::endl) to revert all color and font style settings to its default values:
std::cout << color{ face::black | bright(back::white) } << font{ style::bold } << "Stylized text"
<< reset << "Ordinary style";
// ^^^^^
// Just put it like thisSince applying decoration stores as static, they can be saved for future. Use these static member functions to get their components:
- Foreground text color —
tesc::color::get_face(); - Background text color —
tesc::color::get_back(); - Current font style —
tesc::font::get_style().
You can restore font style settings after reset-ing by use the default constructing form
std::cout << font{ style::italic } << "Italic" << reset << "Normal" << font{} << "Italic";or by explicit restoration, as: font{ font::get_style() }. In addition to modifying, it is possible to check which font style properties are set — just use the tesc::font::test_style() function:
std::cout << std::boolalpha << font{ style::italic }
<< font::test_style(style::italic) // prints `true`
<< font::test_style(style::bold | style::italic); // prints `false`See the LICENSE file for license rights and limitations (MIT).
