String line to expand IRC tags of.
The passed line but with tags expanded to formatting and colouring.
// Old enum pattern = "Quote %s #%s saved."; immutable message = plugin.state.settings.colouredOutgoing ? pattern.format(id.ircColourByHash(extendedOutgoingColours: true), index.ircBold) : pattern.format(id, index); privmsg(plugin.state, event.channel, event.sender.nickname. message); // New enum newPattern = "Quote <h>%s<h> #<b>%d<b> saved."; immutable newMessage = newPattern.format(id, index); privmsg(plugin.state, event.channel, event.sender.nickname, newMessage);
1 import dialect.common : I = IRCControlCharacter; 2 import std.conv : text, to; 3 import std.format : format; 4 5 { 6 immutable line = "hello"; 7 immutable expanded = line.expandIRCTags; 8 assert((expanded is line), expanded); 9 } 10 { 11 immutable line = string.init; 12 immutable expanded = line.expandIRCTags; 13 assert(expanded is null); 14 } 15 { 16 immutable line = "hello<b>hello<b>hello"; 17 immutable expanded = line.expandIRCTags; 18 immutable expected = "hello" ~ I.bold ~ "hello" ~ I.bold ~ "hello"; 19 assert((expanded == expected), expanded); 20 } 21 { 22 immutable line = "hello<1>HELLO<c>hello"; 23 immutable expanded = line.expandIRCTags; 24 immutable expected = "hello" ~ I.colour ~ "01HELLO" ~ I.colour ~ "hello"; 25 assert((expanded == expected), expanded); 26 } 27 { 28 immutable line = "hello<3,4>hello<c>hello"; 29 immutable expanded = line.expandIRCTags; 30 immutable expected = "hello" ~ I.colour ~ "03,04hello" ~ I.colour ~ "hello"; 31 assert((expanded == expected), expanded); 32 } 33 { 34 immutable line = "hello<99,99<b>hiho</>"; 35 immutable expanded = line.expandIRCTags; 36 immutable expected = "hello<99,99" ~ I.bold ~ "hiho" ~ I.reset; 37 assert((expanded == expected), expanded); 38 } 39 { 40 immutable line = "hello<99,99><b>hiho</>"; 41 immutable expanded = line.expandIRCTags; 42 immutable expected = "hello" ~ I.colour ~ "99,99" ~ I.bold ~ "hiho" ~ I.reset; 43 assert((expanded == expected), expanded); 44 } 45 { 46 immutable line = "hello<99,999><b>hiho</>hey"; 47 immutable expanded = line.expandIRCTags; 48 immutable expected = "hello<99,999>" ~ I.bold ~ "hiho" ~ I.reset ~ "hey"; 49 assert((expanded == expected), expanded); 50 } 51 { 52 immutable line = `hello\<1,2>hiho`; 53 immutable expanded = line.expandIRCTags; 54 immutable expected = `hello<1,2>hiho`; 55 assert((expanded == expected), expanded); 56 } 57 { 58 immutable line = `hello\\<1,2>hiho`; 59 immutable expanded = line.expandIRCTags; 60 immutable expected = `hello\` ~ I.colour ~ "01,02hiho"; 61 assert((expanded == expected), expanded); 62 } 63 { 64 immutable line = "hello<"; 65 immutable expanded = line.expandIRCTags; 66 assert((expanded is line), expanded); 67 } 68 { 69 immutable line = "hello<<<<"; 70 immutable expanded = line.expandIRCTags; 71 assert((expanded is line), expanded); 72 } 73 { 74 immutable line = "hello<x>hello<z>"; 75 immutable expanded = line.expandIRCTags; 76 immutable expected = "hello<x>hello<z>"; 77 assert((expanded == expected), expanded); 78 } 79 { 80 immutable line = "hello<h>kameloso<h>hello"; 81 immutable expanded = line.expandIRCTags; 82 immutable expected = "hello" ~ I.colour ~ "23kameloso" ~ I.colour ~ "hello"; 83 assert((expanded == expected), expanded); 84 } 85 { 86 immutable line = "hello<h>kameloso"; 87 immutable expanded = line.expandIRCTags; 88 immutable expected = "hello<h>kameloso"; 89 assert((expanded == expected), expanded); 90 } 91 { 92 immutable line = "hello<1,>kameloso"; 93 immutable expanded = line.expandIRCTags; 94 immutable expected = "hello<1,>kameloso"; 95 assert((expanded == expected), expanded); 96 } 97 { 98 immutable line = "hello<1,"; 99 immutable expanded = line.expandIRCTags; 100 immutable expected = "hello<1,"; 101 assert((expanded == expected), expanded); 102 } 103 { 104 immutable line = "hello<3,4>hello<c>hello"d; 105 immutable expanded = line.expandIRCTags; 106 immutable expected = "hello"d ~ I.colour ~ "03,04hello"d ~ I.colour ~ "hello"d; 107 assert((expanded == expected), expanded.to!string); 108 } 109 /*{ 110 immutable line = "hello<h>kameloso<h>hello"w; 111 immutable expanded = line.expandIRCTags; 112 immutable expected = "hello"w ~ I.colour ~ "01kameloso"w ~ I.colour ~ "hello"w; 113 assert((expanded == expected), expanded.to!string); 114 }*/ 115 { 116 immutable line = "Quote <h>zorael<h> #<b>5<b> saved."; 117 immutable expanded = line.expandIRCTags; 118 enum pattern = "Quote %s #%s saved."; 119 immutable expected = pattern.format( 120 "zorael".ircColourByHash(extendedOutgoingColours: true), 121 "5".ircBold); 122 assert((expanded == expected), expanded); 123 } 124 { 125 immutable line = "Stopwatch stopped after <b>5 seconds<b>."; 126 immutable expanded = line.expandIRCTags; 127 enum pattern = "Stopwatch stopped after %s."; 128 immutable expected = pattern.format("5 seconds".ircBold); 129 assert((expanded == expected), expanded); 130 } 131 { 132 immutable line = "<h>hirrsteff<h> was already <b>whitelist<b> in #garderoben."; 133 immutable expanded = line.expandIRCTags; 134 enum pattern = "%s was already %s in #garderoben."; 135 immutable expected = pattern.format( 136 "hirrsteff".ircColourByHash(extendedOutgoingColours: true), 137 "whitelist".ircBold); 138 assert((expanded == expected), expanded); 139 } 140 { 141 immutable line = `hello\<h>hello<h>hello<h>hello`; 142 immutable expanded = line.expandIRCTags; 143 immutable expected = text( 144 "hello<h>hello", 145 "hello".ircColourByHash(extendedOutgoingColours: true), 146 "hello"); 147 assert((expanded == expected), expanded); 148 }
Slightly more complicated, but essentially string-replaces <tags> in an outgoing IRC string with correlating formatting using IRCControlCharacters in their syntax. Overload that does not take a strip Flag.
<tags> are the lowercase first letter of all IRCControlCharacter members; <b> for IRCControlCharacter.bold, <c> for IRCControlCharacter.colour, <i> for IRCControlCharacter.italics, <u> for IRCControlCharacter.underlined, and the magic </> for IRCControlCharacter.reset,
An additional <h> tag is also introduced, which invokes ircColourByHash on the content between two of them.
If the line is not valid UTF, it is sanitised and the expansion retried.