invert

Terminal-inverts the colours of a piece of text in a string.

version(Colours)
pure @safe
string
invert
(
const string line
,
const string toInvert
,
const bool caseSensitive = true
)

Parameters

line string

Line to examine and invert a substring of.

toInvert string

Substring to invert.

caseSensitive bool

Whether or not to look for matches case-insensitively, yet invert with the casing passed.

Return Value

Type: string

Line with the substring in it inverted, if inversion was successful, else (a duplicate of) the line unchanged.

Examples

immutable line = "This is an example!";
writeln(line.invert("example"));  // "example" substring visually inverted
writeln(line.invert("EXAMPLE", caseInsensitive: true)); // "example" inverted as "EXAMPLE"
1 import kameloso.terminal.colours.defs : TerminalFormat, TerminalReset;
2 import std.format : format;
3 
4 immutable pre = "%c[%dm".format(TerminalToken.format, TerminalFormat.reverse);
5 immutable post = "%c[%dm".format(TerminalToken.format, TerminalReset.invert);
6 
7 {
8     immutable line = "abc".invert("abc");
9     immutable expected = pre ~ "abc" ~ post;
10     assert((line == expected), line);
11 }
12 {
13     immutable line = "abc abc".invert("abc");
14     immutable inverted = pre ~ "abc" ~ post;
15     immutable expected = inverted ~ ' ' ~ inverted;
16     assert((line == expected), line);
17 }
18 {
19     immutable line = "abca abc".invert("abc");
20     immutable inverted = pre ~ "abc" ~ post;
21     immutable expected = "abca " ~ inverted;
22     assert((line == expected), line);
23 }
24 {
25     immutable line = "abcabc".invert("abc");
26     immutable expected = "abcabc";
27     assert((line == expected), line);
28 }
29 {
30     immutable line = "kameloso^^".invert("kameloso");
31     immutable expected = "kameloso^^";
32     assert((line == expected), line);
33 }
34 {
35     immutable line = "foo kameloso bar".invert("kameloso");
36     immutable expected = "foo " ~ pre ~ "kameloso" ~ post ~ " bar";
37     assert((line == expected), line);
38 }
39 {
40     immutable line = "fookameloso bar".invert("kameloso");
41     immutable expected = "fookameloso bar";
42     assert((line == expected), line);
43 }
44 {
45     immutable line = "foo kamelosobar".invert("kameloso");
46     immutable expected = "foo kamelosobar";
47     assert((line == expected), line);
48 }
49 {
50     immutable line = "foo(kameloso)bar".invert("kameloso");
51     immutable expected = "foo(" ~ pre ~ "kameloso" ~ post ~ ")bar";
52     assert((line == expected), line);
53 }
54 {
55     immutable line = "kameloso: 8ball".invert("kameloso");
56     immutable expected = pre ~ "kameloso" ~ post ~ ": 8ball";
57     assert((line == expected), line);
58 }
59 {
60     immutable line = "Welcome to the freenode Internet Relay Chat Network kameloso^"
61         .invert("kameloso^");
62     immutable expected = "Welcome to the freenode Internet Relay Chat Network " ~
63         pre ~ "kameloso^" ~ post;
64     assert((line == expected), line);
65 }
66 {
67     immutable line = "kameloso^: wfwef".invert("kameloso^");
68     immutable expected = pre ~ "kameloso^" ~ post ~ ": wfwef";
69     assert((line == expected), line);
70 }
71 {
72     immutable line = "[kameloso^]".invert("kameloso^");
73     immutable expected = "[kameloso^]";
74     assert((line == expected), line);
75 }
76 {
77     immutable line = `"kameloso^"`.invert("kameloso^");
78     immutable expected = "\"" ~ pre ~ "kameloso^" ~ post ~ "\"";
79     assert((line == expected), line);
80 }
81 {
82     immutable line = "kameloso^".invert("kameloso");
83     immutable expected = "kameloso^";
84     assert((line == expected), line);
85 }
86 {
87     immutable line = "That guy kameloso? is a bot".invert("kameloso");
88     immutable expected = "That guy " ~ pre ~ "kameloso" ~ post  ~ "? is a bot";
89     assert((line == expected), line);
90 }
91 {
92     immutable line = "kameloso`".invert("kameloso");
93     immutable expected = "kameloso`";
94     assert((line == expected), line);
95 }
96 {
97     immutable line = "kameloso9".invert("kameloso");
98     immutable expected = "kameloso9";
99     assert((line == expected), line);
100 }
101 {
102     immutable line = "kameloso-".invert("kameloso");
103     immutable expected = "kameloso-";
104     assert((line == expected), line);
105 }
106 {
107     immutable line = "kameloso_".invert("kameloso");
108     immutable expected = "kameloso_";
109     assert((line == expected), line);
110 }
111 {
112     immutable line = "kameloso_".invert("kameloso_");
113     immutable expected = pre ~ "kameloso_" ~ post;
114     assert((line == expected), line);
115 }
116 {
117     immutable line = "kameloso kameloso kameloso kameloso kameloso".invert("kameloso");
118     immutable expected = "%1$skameloso%2$s %1$skameloso%2$s %1$skameloso%2$s %1$skameloso%2$s %1$skameloso%2$s"
119         .format(pre, post);
120     assert((line == expected), line);
121 }
122 
123 // Case-insensitive tests
124 
125 {
126     immutable line = "KAMELOSO".invert("kameloso", caseSensitive: false);
127     immutable expected = pre ~ "kameloso" ~ post;
128     assert((line == expected), line);
129 }
130 {
131     immutable line = "KamelosoTV".invert("kameloso", caseSensitive: false);
132     immutable expected = "KamelosoTV";
133     assert((line == expected), line);
134 }
135 {
136     immutable line = "Blah blah kAmElOsO Blah blah".invert("kameloso", caseSensitive: false);
137     immutable expected = "Blah blah " ~ pre ~ "kameloso" ~ post ~ " Blah blah";
138     assert((line == expected), line);
139 }
140 {
141     immutable line = "Blah blah".invert("kameloso");
142     immutable expected = "Blah blah";
143     assert((line == expected), line);
144 }
145 {
146     immutable line = "".invert("kameloso");
147     immutable expected = "";
148     assert((line == expected), line);
149 }
150 {
151     immutable line = "KAMELOSO".invert("kameloso");
152     immutable expected = "KAMELOSO";
153     assert((line == expected), line);
154 }