I made this project as a precursor to developing a more established understanding of C++ and lower-level languages in general - it is very poorly written and there are plenty of sleeker alternatives.
Includes the large majority of ANSI colour escape sequences. If any are missing, feel free to create an issue.
A list of colour names can be found below Colours, or, alternatively, just at the top of the
pretty.hpp
header file.
Pretty goes under the pty
namespace.
Pretty adds the reset escape sequence automically, meaning no more boilerplate CLR + str + RESET
!
Say you wanted to go basic, a hello world script, for example:
#include <iostream>
// include pretty from whatever directory you store your includes in
#include "pretty.hpp"
int main() {
std::cout << pty::paint("Hello world!", "green") << std::endl;
// simple as that!
}
Or maybe you are working on a deadline project, and need a reusable set of sequences to avoid typing pty::paint(...)
repeatedly:
// here we create the colour set, providing it as many different values as we need
pty::ColourSet clSet({"green", "bold"});
// using this colour set is easy and efficient
std::cout << clSet.apply("This is much faster!") << std::endl;
The paint
function can also be used in the same way as the ColourSet
constructor.
std::cout << pty::paint("I love C++!", {"yellow", "dim"}) << std::endl;
// alternatively with an array
std::vector<const char *> clrs = {"yellow", "bluebg", "underlined"};
std::cout << pty::paint("I love C!", clrs) << std::endl;
Need to save coloured text for later, or maybe remove ANSI sequences from a string?
std::string coloured = pty::paint("I'll need this later!", {"blue", "yellowbg"});
// use the 'normal(...)' function to "normalise" a string (remove ANSI escape sequences)
std::cout << pty::normal(coloured) << std::endl;
// output will be plain, uncoloured text
- pretty works on Windows systems just as it does on Unix.
char
arrays were originally going to be used overstd::string
. This was scrapped because of the speed and superiority ofstd::string
and the fact that the 8 bits in a standardchar
are not enough to store the content of an ANSI escape sequence, similar to how some Unicode characters cannot be stored in a basicchar
;std::string
handles this, and with great efficiency.
pretty_tests.cpp
contains code for testing and checking the functionality of changes made in pretty.hpp
. This is to be updated and used to verify every major addition to pretty.hpp
.
-
dim
,bold
andnormal
functions for dimming, brightening and removing escape codes from text.