kameloso.plugins.printer.formatting

Implementation of Printer plugin functionality that concerns formatting. For internal use.

The IRCEvent-annotated handlers must be in the same module as the PrinterPlugin, but these implementation functions can be offloaded here to limit module size a bit.

Members

Functions

containsNickname
auto containsNickname(string haystack, string needle)

Searches a string for a substring that isn't surrounded by characters that can be part of a nickname. This can detect a nickname in a string without getting false positives from similar nicknames.

formatMessageColoured
void formatMessageColoured(PrinterPlugin plugin, Sink sink, IRCEvent event, bool bellOnMention, bool bellOnError)

Formats an IRCEvent into an output range sink, coloured.

withoutTypePrefix
auto withoutTypePrefix(string typestring)

Slices away any type prefixes from the string of a IRCEvent.Type.

Examples

1 import kameloso.plugins.common : IRCPluginState;
2 import std.array : Appender;
3 
4 Appender!(char[]) sink;
5 
6 IRCPluginState state;
7 state.server.daemon = IRCServer.Daemon.twitch;
8 PrinterPlugin plugin = new PrinterPlugin(state);
9 
10 IRCEvent event;
11 
12 with (event.sender)
13 {
14     nickname = "nickname";
15     address = "127.0.0.1";
16     version(TwitchSupport) displayName = "Nickname";
17     account = "n1ckn4m3";
18     class_ = IRCUser.Class.whitelist;
19 }
20 
21 event.type = IRCEvent.Type.JOIN;
22 event.channel = "#channel";
23 
24 {
25     formatMessageMonochrome(plugin, sink, event, bellOnMention: false, bellOnError: false);
26     immutable joinLine = sink.data[11..$].idup;
27     version(TwitchSupport) string nickstring = "Nickname";
28     else string nickstring = "nickname";
29     //nickstring ~= "/whitelist";
30     immutable expected = "[join] [#channel] " ~ nickstring;
31     assert((joinLine == expected), joinLine);
32     sink.clear();
33 }
34 
35 event.type = IRCEvent.Type.CHAN;
36 event.content = "Harbl snarbl";
37 
38 {
39     formatMessageMonochrome(plugin, sink, event, bellOnMention: false, bellOnError: false);
40     immutable chanLine = sink.data[11..$].idup;
41     version(TwitchSupport) string nickstring = "Nickname";
42     else string nickstring = "nickname";
43     //nickstring ~= "/whitelist";
44     immutable expected = "[chan] [#channel] " ~ nickstring ~ `: "Harbl snarbl"`;
45     assert((chanLine == expected), chanLine);
46     sink.clear();
47 }
48 
49 plugin.printerSettings.classNames = true;
50 event.sender.badges = "broadcaster/0,moderator/1,subscriber/9";
51 event.sender.class_ = IRCUser.Class.staff;
52 //colour = "#3c507d";
53 
54 version(TwitchSupport)
55 {{
56     formatMessageMonochrome(plugin, sink, event, bellOnMention: false, bellOnError: false);
57     immutable twitchLine = sink.data[11..$].idup;
58     version(TwitchSupport) string nickstring = "Nickname";
59     else string nickstring = "nickname";
60     nickstring ~= "/staff";
61     immutable expected = "[chan] [#channel] " ~ nickstring ~
62         ` [broadcaster/0,moderator/1,subscriber/9]: "Harbl snarbl"`;
63     assert((twitchLine == expected), twitchLine);
64     sink.clear();
65     event.sender.badges = string.init;
66 }}
67 
68 plugin.printerSettings.accountNames = true;
69 plugin.state.server.daemon = IRCServer.Daemon.inspircd;
70 event.sender.class_ = IRCUser.Class.anyone;
71 event.type = IRCEvent.Type.ACCOUNT;
72 event.channel = string.init;
73 event.content = string.init;
74 //event.sender.account = "n1ckn4m3";
75 event.aux[0] = "n1ckn4m3";
76 
77 {
78     formatMessageMonochrome(plugin, sink, event, bellOnMention: false, bellOnError: false);
79     immutable accountLine = sink.data[11..$].idup;
80     version(TwitchSupport) string nickstring = "Nickname";
81     else string nickstring = "nickname";
82     nickstring ~= "/anyone";
83     nickstring ~= " (n1ckn4m3)";
84     immutable expected = "[account] " ~ nickstring ~ " (n1ckn4m3)";
85     assert((accountLine == expected), accountLine);
86     sink.clear();
87 }
88 
89 event.errors = "DANGER WILL ROBINSON";
90 event.content = "Blah balah";
91 event.num = 666;
92 event.count[0] = -42;
93 event.count[1] = 123;
94 event.count[5] = 420;
95 event.aux[0] = string.init;
96 event.aux[1] = "aux1";
97 event.aux[4] = "aux5";
98 event.type = IRCEvent.Type.ERROR;
99 
100 {
101     formatMessageMonochrome(plugin, sink, event, bellOnMention: false, bellOnError: false);
102     immutable errorLine = sink.data[11..$].idup;
103     version(TwitchSupport) string nickstring = "Nickname";
104     else string nickstring = "nickname";
105     nickstring ~= "/anyone";
106     nickstring ~= " (n1ckn4m3)";
107     immutable expected = "[error] " ~ nickstring ~ `: "Blah balah" (aux1) (aux5) ` ~
108         "{-42} {123} {420} [#666] ! DANGER WILL ROBINSON !";
109     assert((errorLine == expected), errorLine);
110     sink.clear();
111 }
112 
113 plugin.printerSettings.classNames = false;
114 event.type = IRCEvent.Type.CHAN;
115 event.channel = "#nickname";
116 event.num = 0;
117 event.count = typeof(IRCEvent.count).init;
118 event.aux = typeof(IRCEvent.aux).init;
119 event.errors = string.init;
120 
121 {
122     formatMessageMonochrome(plugin, sink, event, bellOnMention: false, bellOnError: false);
123     immutable queryLine = sink.data[11..$].idup;
124     version(TwitchSupport) string nickstring = "Nickname";
125     else string nickstring = "nickname";
126     //nickstring ~= "/anyone";
127     nickstring ~= " (n1ckn4m3)";
128     immutable expected = "[chan] [#nickname] " ~ nickstring ~ `: "Blah balah"`;
129     assert((queryLine == expected), queryLine);
130     //sink.clear();
131 }

See Also

Meta