Coltext is an open-source header-only C++ library for applying colors and styles to strings in terminals.
It introduces a new syntax for in-string ANSI effects (styles, background and foreground colors) parsing. With a few simple rules you won't need to care about remembering escape codes or resetting streams!
The only things you need to use Coltext is C++ compiler and terminal that supports ANSI escape codes.
Currently not supported.
- Download latest release or just
coltext.hpp
file. - Add it to the folder where your code lies.
- Add
#include "coltext.hpp"
to your C++ file. - Compile with
-std=c++17
.
In order to use Coltext features you need to acquire syntax of Coltext and cast to its class.
Effects should start from a #
special symbol, followed by effect's keyword.
Note: if keyword is wrong then Coltext threats it as plain text. NO ERROR TROWN EVER.
These colors are standard 4bit ANSI colors (8 normal colors + 8 bright). How colors look (and whether they are supported) is up to your terminal specification and settings.
color | keywords |
---|---|
black | "black" , "k" |
red | "red" , "r" |
green | "green" , "g" |
yellow | "yellow" , "y" |
blue | "blue" , "b" |
cyan | "cyan" , "c" |
magenta | "magenta" , "m" |
white | "white" , "w" |
Background colors keywords start from big letter. (Example: "Red"
and "R"
)
Their bright versions are accessed by adding "bright_"
to long keyword or "b"
to short one.
(Example: "bright_red"
and "br"
, or "bright_Red"
and "bR"
for background)
Note: acronyms for colors come from CMYK and RGB color models.
If your terminal supports 24bit coloring, you can specify color by these:
#rgb[r;g;b]
for foreground colors
Or
#RGB[r;g;b]
for background colors.
Where r
, g
and b
are numbers in range [0, 255]
.
Note: you can NOT add spaces nor change delimiter between
[]
.
Widly supported styles:
style | keywords |
---|---|
bold | "bold" , "<b>" |
faint | "faint" , "<f>" |
italic | "italic" , "<i>" |
underline | "underline" , "<u>" |
Note:
#
before<
is optional.#<b>
is equivalent to<b>
.
Closing styles by <\>
is currently not supported in Coltext.
See effect scope.
Not frequently used styles:
style | keywords | commentary |
---|---|---|
crossed | "crossed" |
Strikethrough text. May be not supported |
blink | "blink" |
Text starts blinking (slowly) |
reverse | "reverse" |
Reverse background and foreground color |
Usage of those is not so frequent, hence they have no acronym.
Not widely supported styles:
style | keywords |
---|---|
double underline | "double_underline" |
framed | "framed" |
encircled | "encircled" |
overlined | "overlined" |
Note: some of them will work in your terminal, some won't. Deal with it.
Effect scope is controled by 2 ways:
- Use parentheses to get as much as you want. (Example:
#g(text in green) and not in green
) - Use space to get next word. (Example:
#r red and not_red
)
There are only 4 symbols in Coltext you may want to escape: #
, (
, )
and <
.
To escape these symbols use \\
.
Note: actualy you can leave
(
without the escape symbol. It's just made for symmetry.
Examples:
std::cout
<< "\\#red makes text red\n"_col
<< "\\<bold> makes text bold\n"_col
<< "I love green parentheses! #g( \\(\\) )"_col;
In order to use features provided by Coltext, you MUST convert std::string
to Coltext
class.
There are currently 3 ways of doing it:
By constructor:
Coltext ctxt("<b>(Hello, World!)\n");
By dynamic cast:
std::cout << (Coltext) "<b>(Hello, World!)\n";
By literal:
std::cout << "<b>(Hello, World!)\n"_col;
Compile and run tests.cpp
file.
We use SemVer for versioning. For the versions available, see the releases on this repository.
- Earl H. - Initial work - 1410rlH
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
- C++ for the difficulties of coloring text
- CSS for
'#'
special symbol - HTML for style tags
- Ikalnytskyi/termcolor for initial idea