AsAnsiTextRange

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

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

Behaviour: This range will only return text that isn't 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
AnsiText front()
popFront
void popFront()

Parses the next sections.

Parameters

R

The range of AnsiSections.

Examples

// Even this test is copy-pasted, I'm so lazy today T.T

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 text = input.asAnsiTexts.array;
assert(
    text.length == 2, 
    "Expected length of %s not %s\n%s".format(2, text.length, text)
);

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

assert(text[0].fg      == style1.fg);
assert(text[0].bg      == style1.bg);
assert(text[0].flags   == style1.flags);
assert(text[0].rawText == "Hello");

style2.bgRef.isBg = IsBgColour.yes; // AnsiText is a bit better at keeping this value set to `yes` than `AnsiChar`.
assert(text[1].fg      == style2.fg);
assert(text[1].bg      == style2.bg);
assert(text[1].flags   == style2.flags);
assert(text[1].rawText == "World");

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

See Also

asAnsiTexts for easy creation of this struct.

Meta