expandIRCTags

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.

Parameters

line T

String line to expand IRC tags of.

Return Value

Type: T

The passed line but with tags expanded to formatting and colouring.

Examples

// Old
enum pattern = "Quote %s #%s saved.";
immutable message = plugin.state.settings.colouredOutgoing ?
    pattern.format(id.ircColourByHash(Yes.extendedOutgoingColours), 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 = "hellohello";
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 = "hellokameloso";
89     assert((expanded == expected), expanded);
90 }
91 {
92     immutable line = "hello<3,4>hello<c>hello"d;
93     immutable expanded = line.expandIRCTags;
94     immutable expected = "hello"d ~ I.colour ~ "03,04hello"d ~ I.colour ~ "hello"d;
95     assert((expanded == expected), expanded.to!string);
96 }
97 /*{
98     immutable line = "hello<h>kameloso<h>hello"w;
99     immutable expanded = line.expandIRCTags;
100     immutable expected = "hello"w ~ I.colour ~ "01kameloso"w ~ I.colour ~ "hello"w;
101     assert((expanded == expected), expanded.to!string);
102 }*/
103 {
104     immutable line = "Quote <h>zorael<h> #<b>5<b> saved.";
105     immutable expanded = line.expandIRCTags;
106     enum pattern = "Quote %s #%s saved.";
107     immutable expected = pattern.format(
108         "zorael".ircColourByHash(Yes.extendedOutgoingColours),
109         "5".ircBold);
110     assert((expanded == expected), expanded);
111 }
112 {
113     immutable line = "Stopwatch stopped after <b>5 seconds<b>.";
114     immutable expanded = line.expandIRCTags;
115     enum pattern = "Stopwatch stopped after %s.";
116     immutable expected = pattern.format("5 seconds".ircBold);
117     assert((expanded == expected), expanded);
118 }
119 {
120     immutable line = "<h>hirrsteff<h> was already <b>whitelist<b> in #garderoben.";
121     immutable expanded = line.expandIRCTags;
122     enum pattern = "%s was already %s in #garderoben.";
123     immutable expected = pattern.format(
124         "hirrsteff".ircColourByHash(Yes.extendedOutgoingColours),
125         "whitelist".ircBold);
126     assert((expanded == expected), expanded);
127 }
128 {
129     immutable line = `hello\<h>hello<h>hello<h>hello`;
130     immutable expanded = line.expandIRCTags;
131     immutable expected = text(
132         "hello<h>hello",
133         "hello".ircColourByHash(Yes.extendedOutgoingColours),
134         "hello");
135     assert((expanded == expected), expanded);
136 }

Meta