mapColoursImpl

Maps mIRC effect colour tokens to terminal ones, or strip them entirely. Now with less regex.

Pass Yes.strip as strip to map colours to nothing, removing colouring.

This function requires version Colours to map colours, but doesn't if just to strip.

private @safe pure nothrow
string
mapColoursImpl
(
Flag!"strip" strip = No.strip
)

Parameters

strip

Whether or not to strip colours or to map them.

line string

String line with IRC colours to translate.

fgFallback TerminalForeground

Foreground code to reset to after colour-default tokens.

bgFallback TerminalBackground

Background code to reset to after colour-default tokens.

Return Value

Type: string

The passed line, now with terminal colouring, or completely without.

Examples

alias I = IRCControlCharacter;
alias TF = TerminalForeground;
alias TB = TerminalBackground;

{
    immutable line = "This is " ~ I.colour ~ "4all red!" ~ I.colour ~ " while this is not.";
    immutable mapped = mapColours(line, TF.default_, TB.default_);
    assert((mapped == "This is \033[91mall red!\033[39;49m while this is not."), mapped);
}
{
    immutable line = "This time there's" ~ I.colour ~ "6 no ending token, only magenta.";
    immutable mapped = mapColours(line, TF.default_, TB.default_);
    assert((mapped == "This time there's\033[35m no ending token, only magenta.\033[39;49m"), mapped);
}
{
    immutable line = I.colour ~ "1,0You" ~ I.colour ~ "0,4Tube" ~ I.colour ~ " asdf";
    immutable mapped = mapColours(line, TF.default_, TB.default_);
    assert((mapped == "\033[90;107mYou\033[97;41mTube\033[39;49m asdf"), mapped);
}
{
    immutable line = I.colour ~ "17,0You" ~ I.colour ~ "0,21Tube" ~ I.colour ~ " asdf";
    immutable mapped = mapColours(line, TF.default_, TB.default_);
    assert((mapped == "\033[90;107mYou\033[97;41mTube\033[39;49m asdf"), mapped);
}
{
    immutable line = I.colour ~ "17,0You" ~ I.colour ~ "0,2" ~ I.colour;
    immutable mapped = mapColours(line, TF.default_, TB.default_);
    assert((mapped == "\033[90;107mYou\033[97;44m\033[39;49m"), mapped);
}
{
    immutable line = I.colour ~ "";
    immutable mapped = mapColours(line, TF.default_, TB.default_);
    assert((mapped == "\033[39;49m"), mapped);
}