-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcoloredstring.cpp
More file actions
71 lines (64 loc) · 1.59 KB
/
Copy pathcoloredstring.cpp
File metadata and controls
71 lines (64 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "coloredstring.h"
QMap<QString, QString> ColoredString::map = QMap<QString, QString>({
{"green", "32"}
});
ColoredString::ColoredString(const QString text)
{
m_text = text;
}
QString ColoredString::toString() const
{
enum Mode { // todo come up with better names
String,
Tag,
TagString,
TagTag
};
QString out;
auto mode = Mode::String;
QString currentTag;
for (int i = 0; i < m_text.length(); ++i) {
auto letter = m_text.at(i);
switch (mode) {
case String:
if (letter == "<") {
mode = Tag;
continue;
}
out += letter;
break;
case Tag:
if (letter == ">") {
if (!map.contains(currentTag)) {
mode = String;
continue;
}
out += "\033[0;" + map.value(currentTag) + "m";
mode = TagString;
} else {
currentTag += letter;
}
break;
case TagString:
if (letter == "<") {
mode = TagTag;
continue;
}
out += letter;
break;
case TagTag:
if (letter != ">") {
continue;
}
out += "\033[0m";
currentTag = "";
mode = String;
break;
}
}
return out;
}
ColoredString::operator QString() const
{
return toString();
}