Color#

ANSI escape sequence related functions and facilities.

Info

To use declarations from this page, include the <essence/color.hpp> header.

They are found within the essence::ansi namespace.

using sequence = std::string_view#

A distinctly-named type for predefined ansi escape sequences.

Escape sequence constants#

Constant

Resetter

Description

none

N/A

Reset all formatting

bold

reset::bold

Bolden text

dim

reset::dim

Dim the text

italic

reset::italic

Italicize text

underline

reset::underline

Underline the text

blink

reset::blink

Periodically blink between hidden and visible

hidden

reset::hidden

Make text invisible

strike

reset::strike

Add a strike-through line to the text

fg::<color>

fg::reset

Colorize foreground text, where <color> is one of black, red, green, yellow, blue, magenta, cyan or white

bg::<color>

bg::reset

Colorize background of cells, where <color> is one of black, red, green, yellow, blue, magenta, cyan or white

fg::bright::<color>

fg::reset

Colorize foreground text with a bright variant of <color>, where <color> is one of black, red, green, yellow, blue, magenta, cyan or white

bg::bright::<color>

bg::reset

Colorize background of cells with a bright variant of <color>, where <color> is one of black, red, green, yellow, blue, magenta, cyan or white

std::regex escape_rx#

A regex useful for capturing standard ansi escape sequences.

void strip(std::string &in)#

Strip ansi escape sequences from a string (in-place).

Utilizes the escape_rx regex to perform the strip.

std::string cstrip(std::string_view in)#

Strip ansi escape sequences from a string (copying).

Utilizes the escape_rx regex to perform the strip.

template<typename T>
concept Appendable#

A type that can be appended to a message instance.

Valid Expressions

  • T, when T is a std::string

  • T, when T is a std::string_view

  • T, when T is a const char*

  • T, when T is a sequence

  • T, when T is a char

template<typename T>
concept StringLike#

A string-like type.

Valid Expressions

  • T, when T is convertible to an std::string_view

  • T, when T is a char

class message#

A wrapped std::string facilitating easy colorization and formatting.

Example

#include <essence/io.hpp>
#include <essence/color.hpp>

int main()
{
    using namespace essence::ansi;

    // print ' Hello! ' with black text on a green background, in a bold and italic face.
    message m = fg::black|bg::green|italic|bold|" Hello! "|none;

    io::printn(m);

    return 0;
}
message::message(std::string s)#

Construct a message from a std::string. The constructor may also be called with a const char*, std::string_view, const std::string& or a sequence.

template<Appendable T>
message &append(T &t)#

Append an Appendable type to a message. May also be called with a constant T reference.

template<StringLike... Args>
requires (sizeof...(Args) >= 2)
message &append(Args&&... args)#

Append multiple StringLike types to a message.

Example

using namespace essence::ansi;
message m;

m.append(fg::black, bg::green, bold, italic, " Hello ", none);
message &newline()#

Append a newline character to the end of the message.

message &clear()#

Empty the contents of the underlying std::string.

message &strip()#

Remove all ansi escape sequences from the underlying std::string using the ansi::strip function and ansi::escape_rx regex.

message &reset()#

Append the format clearing sequence to the end of the message

message &text(std::string txt)#

Set the contents of the underlying std::string

std::string str() const#

Get a copy of the string content.

template<Appendable T>
friend message operator+(const message &lhs, T rhs)#

Addition operator for Appendable types.

friend message operator+(message &lhs, message &rhs)#

Construct a new message from lhs and rhs

friend std::ostream &operator<<(std::ostream &os, const message &self)#

Stream overload for std::cout and the like.

operator std::string() const#

Implicit conversion operator to produce a std::string.

message &operator+=(message &lhs, std::string rhs)#

Addition-equal overload to append a std::string to a message.

std::string &operator|(std::string &lhs, sequence rhs)#

Bitwise-or overload to append a sequence to a string.

std::string &operator|=(std::string &lhs, sequence rhs)#

Bitwise-or assignment overload to append a sequence to a string.

std::string operator+(sequence a, sequence b)#

Addition overload which constructs a string from two sequences.

std::string &operator+=(std::string &lhs, sequence rhs)#

Addition-assignment overload to append a sequence to a string.

std::string operator|(sequence a, sequence b)#

Bitwise-or overload to construct a string from two sequences.

std::string operator|(std::string &&lhs, sequence rhs)#

Bitwise-or overload to construct a string from an rvalue string and a sequence.

std::string operator|(std::string &&lhs, const char *rhs)#

Bitwise-or overload to construct a string from an rvalue string and a const char*.