Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
/ pretty Public archive

A simple, lightweight unix console-colouring library for C++.

License

Notifications You must be signed in to change notification settings

jibstack64/pretty

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pretty

License: MIT Downloads Version

A simple, lightweight unix console-colouring library for C++, all in one header file!

ARCHIVED

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.

Colours

C++ map of colours

Includes the large majority of ANSI colour escape sequences. If any are missing, feel free to create an issue.

Use

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

Notes

  • pretty works on Windows systems just as it does on Unix.
  • char arrays were originally going to be used over std::string. This was scrapped because of the speed and superiority of std::string and the fact that the 8 bits in a standard char are not enough to store the content of an ANSI escape sequence, similar to how some Unicode characters cannot be stored in a basic char; std::string handles this, and with great efficiency.

Testing

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.

Future changes

  • dim, bold and normal functions for dimming, brightening and removing escape codes from text.