AnsiSectionRange

An InputRange that turns an array of Chars into a range of AnsiSections.

This isn't overly useful on its own, and is mostly so other ranges can be built on top of this.

Notes: Please see AnsiSectionBase.value's documentation comment, as it explains that certain characters of an ANSI sequence are omitted from the final output (the starting "\033[" and the ending 'm' specifically).

Limitations: To prevent the need for allocations or possibly buggy behaviour regarding a reusable buffer, this range can only work directly on arrays, and not any generic char range.

@safe
struct AnsiSectionRange (
Char
) if (
isSomeChar!Char
) {}

Constructors

this
this(Char[] input)

Creates an AnsiSectionRange from the given input.

Members

Functions

empty
bool empty()
front
AnsiSectionBase!Char front()
popFront
void popFront()

Parses the next section.

Examples

import std.array : array;

const onlyText = "Hello, World!";
const onlyAnsi = "\033[30m\033[0m";
const mixed    = "\033[30;1;2mHello, \033[0mWorld!";

void test(string input, AnsiSection[] expectedSections)
{
    import std.algorithm : equal;
    import std.format    : format;

    auto range = input.asAnsiSections();
    assert(range.equal(expectedSections), "Expected:\n%s\nGot:\n%s".format(expectedSections, range));
}

test(onlyText, [AnsiSection(AnsiSectionType.text, "Hello, World!")]);
test(onlyAnsi, [AnsiSection(AnsiSectionType.escapeSequence, "30"), AnsiSection(AnsiSectionType.escapeSequence, "0")]);
test(mixed,
[
    AnsiSection(AnsiSectionType.escapeSequence, "30;1;2"),
    AnsiSection(AnsiSectionType.text,           "Hello, "),
    AnsiSection(AnsiSectionType.escapeSequence, "0"),
    AnsiSection(AnsiSectionType.text,           "World!")
]);

assert(mixed.asAnsiSections.array.length == 4);

Meta