Top-level types and functions

An ANSI terminal library. This library contains various utilities for outputting colors, styles and moving the cursor in an ANSI compatible terminal. The top level ansi namespace contains some frequently used types and functions.

namespace ansi

The root namespace of the ANSI manipulators library.

Contains various utility structures for ANSI related text outputing

enum ansi::Color

The ANSI Terminal Simple Color representation.

Represents the basic ANSI codes for color that are used withing the manipulators

Values:

enumerator Black
enumerator Red
enumerator Green
enumerator Yellow
enumerator Blue
enumerator Magenta
enumerator Cyan
enumerator White
enumerator Default

Resets the color back to it’s original terminal color.

enum ansi::TextModifier

ANSI text graphics modes.

Represents the ANSI text graphics modes for changing the styling of text

Values:

enumerator Bold
enumerator Faint
enumerator Italic
enumerator Underline
enumerator Blink
enumerator Reverse
enumerator Hidden
enumerator Strikethrough
class ansi::ManipulatorFunc

A wrapper for a manipulator lambda.

A type-safe holder for an std::function manipulator lambda. Supports the call operator for use with the global overload of operator<< With std::ostream

Public Functions

inline explicit ManipulatorFunc(fn &&function)

Construct a new ManipulatorFunc from a manipulator compatible lambda.

Parameters

function – The manipulator lambda with the signature of std::ostream&(std::ostream&)

inline auto operator()(std::ostream &os) const -> std::ostream&

Apply the internal lambda to the std::ostream.

Parameters

os – The stream to manipulate

Returns

The manipulated stream

struct ansi::StyleColor

Holds color information compatible with ANSI standards.

Holds various formats of colors for use with other functions/classes to represent ANSI compatible color formats

Public Types

enum ColorType

The format type of the color.

Term256 is for choosing one of the 256 builtin colors, RGB is for choosing the Red, Green, and Blue channels individually (on supported terminals), and ANSI is for the 8 ANSI colors

Values:

enumerator Term256
enumerator RGB
enumerator ANSI

Public Functions

inline StyleColor(Color c)

Constructs a new StyleColor using the provided ANSI color and sets the color type of ANSI.

Parameters

c – The ANSI color to use

inline StyleColor(uint8_t c)

Constructs a new StyleColor using a color number and sets the color type to Term256.

Parameters

c – The color number to use

inline StyleColor(uint8_t r, uint8_t g, uint8_t b)

Constructs a new StyleColor using 3 values for the Red, Green, and Blue color channel and sets the color type to RGB.

Parameters
  • r – The value for the Red channel

  • g – The value for the Green channel

  • b – The value for the Blue channel

inline StyleColor()

Default constructor.

inline StyleColor(const StyleColor &other)

Copy constructor.

Parameters

other – The StyleColor object to copy from

inline StyleColor(StyleColor &&other) noexcept

Move constructor.

Parameters

other – The StyleColor object to move from

inline auto operator=(const StyleColor &rhs) -> StyleColor&

Trivial copy assignment operator.

Parameters

rhs – The StyleColor object to copy from

Returns

The assigned object

inline auto operator=(StyleColor &&rhs) noexcept -> StyleColor&

Trivial move assignment operator.

Parameters

rhs – The StyleColor object to copy from

Returns

The assigned object

inline ~StyleColor()

Discriminated union destructor.

Public Members

enum ansi::StyleColor::ColorType ANSI
ansi::Color ansiColor
std::tuple<uint8_t, uint8_t, uint8_t> rgb
uint8_t term
union ansi::StyleColor::[anonymous] [anonymous]

Holds the color value depending on the color type.

Holds the color number for Term256, the 3 rgb values, or one of the 8 ANSI color values

Note

This union is tagged with the type field of StyleColor

auto ansi::set_raw_tty(int fd) -> void

Sets the terminal to RAW output.

Warning

This should only be called once, if called more than once or from a separate thread it’s undefined behavior

Parameters

fd – The file descriptor of the output to set

auto ansi::reset_tty(int fd) -> void

Reset the terminal to original functionality.

Warning

This should only be called once, if called more than once or from a separate thread it’s undefined behavior

Parameters

fd – The file descriptor of the output to set

auto ansi::format_str(const char *fstr) -> const char*

Returns a cached ANSI format string.

Parses the input string using a tiny format language and returns a cached printf compatible string.

The formatting is as follows:

  • All normal text is treated as text

  • #[ begins a format modifier

  • Inside the format modifier color and text modifiers can be specified (Bold, Red, 220, 20:10:2)

  • Color can be specified as either an ANSI color, a Term256 color or rgb

  • RGB is 3 numbers separated by colons, ie. 220:150:30

  • ] closes the format modifier

  • #$ gets replaced with a s

  • ## escapes the # character

An example:

#include <iostream>
#include <Ansi>

int main() {
    std::cout << ansi::format_str(
        "#[Bold, Underline, Red]Underlined and ##bolded## text"
    ) << std::endl;
}

Parameters

fstr – ANSI format string

Throws

std::runtime_exception – The format string is invalid and cannot be parsed

Returns

A cached printf compatible format string