AnsiColour.toSequence

Creates an ANSI SGR command that either sets the foreground, or the background (isBg) to the colour stored inside of this AnsiColour.

Please note that the CSI (ANSI_CSI/\033[) and the SGR marker (ANSI_COLOUR_END/m) are not included in this output.

Notes: Any characters inside of buffer that are not covered by the returned slice, are left unmodified.

If this colour hasn't been initialised or assigned a value, then the returned value is simply null.

struct AnsiColour
@safe @nogc nothrow const
char[]
toSequence
(
ref return char[MAX_CHARS_NEEDED] buffer
)

Parameters

buffer char[MAX_CHARS_NEEDED]

The statically allocated buffer used to store the result of this function.

Return Value

Type: char[]

A slice into buffer that contains the output of this function.

Examples

char[AnsiColour.MAX_CHARS_NEEDED] buffer;

void test(string expected, AnsiColour colour)
{
    const slice = colour.toSequence(buffer);
    assert(slice == expected);
}

test("32",               AnsiColour(Ansi4BitColour.green));
test("42",               AnsiColour(Ansi4BitColour.green, IsBgColour.yes));
test("38;5;1",           AnsiColour(Ansi8BitColour(1)));
test("48;5;1",           AnsiColour(Ansi8BitColour(1), IsBgColour.yes));
test("38;2;255;255;255", AnsiColour(255, 255, 255));
test("48;2;255;128;64",  AnsiColour(255, 128, 64, IsBgColour.yes));

Meta