termcolor.cxx
v2.2024.8
Published
Termcolor is a header-only C++ library for printing colored messages to the terminal. Written just for fun with a help of the Force; Ihor Kalnytskyi (2013).
Maintainers
Readme
Termcolor

Termcolor is a header-only C++ library for printing colored messages to the terminal. Written just for fun with a help of the Force. Termcolor uses ANSI color formatting, so you can use it on every system that supports such terminals (most *nix systems, including Linux and macOS).
Note
On Windows, the Windows API is used instead of escape codes but some limitations are applied (not everything is supported). That's why it's recommended to enable virtual terminal processing mode and set the
TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCESmacro to trick termcolor into using ANSI color codes.
It’s licensed under the BSD (3-clause) License. That basically means: do whatever you want as long as the copyright sticks around.
Installation
Run:
$ npm i termcolor.cxxAnd then include termcolor.hpp as follows:
// main.cxx
#include <termcolor.hpp> // or, termcolor/termcolor.hpp
int main() { /* ... */ }Finally, compile while adding the path node_modules/termcolor.cxx to your compiler's include paths.
$ clang++ -I./node_modules/termcolor.cxx main.cxx # or, use g++
$ g++ -I./node_modules/termcolor.cxx main.cxxYou may also use a simpler approach with the cpoach tool, which automatically adds the necessary include paths of all the installed dependencies for your project.
$ cpoach clang++ main.cxx # or, use g++
$ cpoach g++ main.cxxYou can also use vcpkg to install the library:
$ vcpkg install termcolorOr if you are on macOS, you can use Homebrew:
$ brew install termcolorFor up-to-date information about existing packages, refer to the following picture:
How to use?
It’s very easy to use. The idea is built upon C++ stream manipulators. A typical “Hello World” application looks like this:
#include <iostream>
#include <termcolor/termcolor.hpp>
int main(int /*argc*/, char** /*argv*/)
{
std::cout << termcolor::red << "Hello, "; // 16 colors
std::cout << termcolor::color<100> << "Colorful "; // 256 colors
std::cout << termcolor::color<211, 54, 130> << "World!"; // true colors
std::cout << std::endl;
return 0;
}The application above prints a string using different colors. There is one caveat though: you must not forget to reset colors, otherwise they will be applied to other prints as well.
std::cout << termcolor::red << "Hello, Colorful World!" << std::endl;
std::cout << "I'm RED too!" << std::endl;The correct version of the code above should look like this:
std::cout << termcolor::red << "Hello, Colorful World!" << termcolor::reset << std::endl;
std::cout << termcolor::reset << "Here I'm!" << std::endl;By default, Termcolor ignores any colors for non-TTY streams (e.g. std::stringstream), so the following snippet:
std::stringstream ss;
ss << termcolor::red << "unicorn";
std::cout << ss.str();will print “unicorn” using the default color, not red.
To change this behavior, use the termcolor::colorize manipulator, which enforces colors no matter what.
What manipulators are supported?
The manipulators are divided into four groups:
- foreground, which changes text color
- background, which changes text background color
- attributes, which change text style (bold, underline, etc.)
- control, which change termcolor’s behavior
There are also color manipulators for 16 colors, 256 colors, and true colors palettes.
Note
While
termcolorsupports true color, the terminal emulator you use must also support true color. Please ensure it is supported before filing an issue.
Foreground manipulators
16 colors
termcolor::greytermcolor::redtermcolor::greentermcolor::yellowtermcolor::bluetermcolor::magentatermcolor::cyantermcolor::whitetermcolor::bright_greytermcolor::bright_redtermcolor::bright_greentermcolor::bright_yellowtermcolor::bright_bluetermcolor::bright_magentatermcolor::bright_cyantermcolor::bright_white
256 colors
termcolor::color<256_COLOR_CODE>
True colors
termcolor::color<RED, GREEN, BLUE>
Background manipulators
16 colors
termcolor::on_greytermcolor::on_redtermcolor::on_greentermcolor::on_yellowtermcolor::on_bluetermcolor::on_magentatermcolor::on_cyantermcolor::on_whitetermcolor::on_bright_greytermcolor::on_bright_redtermcolor::on_bright_greentermcolor::on_bright_yellowtermcolor::on_bright_bluetermcolor::on_bright_magentatermcolor::on_bright_cyantermcolor::on_bright_white
256 colors
termcolor::on_color<256_COLOR_CODE>
True colors
termcolor::on_color<RED, GREEN, BLUE>
Attribute manipulators
(Windows API does not support these manipulators except for underline)
termcolor::boldtermcolor::darktermcolor::italictermcolor::underlinetermcolor::blinktermcolor::reversetermcolor::concealedtermcolor::crossed
Control manipulators
(Windows API does not support these manipulators)
termcolor::colorizetermcolor::nocolorize
Caveats
On Windows, due to internal usage of
<windows.h>, the global namespace may be polluted withmin/maxmacros. If such an effect is undesirable, consider using:#define NOMINMAX #include <termcolor.hpp>

