AsAnsiCharRange

An InputRange that converts a range of AnsiSections into a range of AnsiChars.

TLDR; If you have a piece of ANSI-encoded text, and you want to easily step through character by character, keeping the ANSI info, then this range is for you.

Notes: This struct is @nogc, except for when it throws exceptions.

Behaviour: This range will only return characters that are not part of an ANSI sequence, which should hopefully end up only being visible ones.

For example, a string containing nothing but ANSI sequences won't produce any values.

Constructors

this
this(R range)

Creates a new instance of this struct, using range as the range of sections.

Members

Functions

empty
bool empty()
front
AnsiChar front()
popFront
void popFront()

Parses the next sections.

Parameters

R

The range of AnsiSections.

Examples

import std.array  : array;
import std.format : format;

const input = "Hello".ansi.fg(Ansi4BitColour.green).bg(20).bold.toString()
            ~ "World".ansi.fg(255, 0, 255).italic.toString();

const chars = input.asAnsiChars.array;
assert(
    chars.length == "HelloWorld".length, 
    "Expected length of %s not %s\n%s".format("HelloWorld".length, chars.length, chars)
);

// Styling for both sections
const style1 = AnsiChar(AnsiColour(Ansi4BitColour.green), AnsiColour(20, IsBgColour.yes), AnsiTextFlags.bold);
const style2 = AnsiChar(AnsiColour(255, 0, 255), AnsiColour.init, AnsiTextFlags.italic);

foreach(i, ch; chars)
{
    AnsiChar style = (i < 5) ? style1 : style2;
    style.value    = ch.value;

    assert(ch == style, "Char #%s doesn't match.\nExpected: %s\nGot: %s".format(i, style, ch));
}

assert("".asAnsiChars.array.length == 0);

See Also

asAnsiChars for easy creation of this struct.

Meta