-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtextconsole.cpp
113 lines (104 loc) · 4.07 KB
/
qtextconsole.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "qtextconsole.h"
void QTextConsole::insertAnsiText(const QString &text)
{
QTextCursor cursor = textCursor();
QRegularExpression re("\x1b\\[([0-9;]+)m");
QRegularExpressionMatch match;
int lastPos = 0;
auto it = re.globalMatch(text);
while (it.hasNext())
{
match = it.next();
QString before = text.mid(lastPos, match.capturedStart() - lastPos);
QString ansiCodes = match.captured(1);
cursor.insertText(before);
applyAnsiCodes(cursor, ansiCodes.split(';'));
lastPos = match.capturedEnd();
}
cursor.insertText(text.mid(lastPos));
}
void QTextConsole::applyAnsiCodes(QTextCursor &cursor, const QStringList &codes)
{
QTextCharFormat format = cursor.charFormat();
for (const QString &code : codes)
{
bool ok;
int n = code.toInt(&ok);
if (!ok) continue;
switch (n)
{
case 0:
format = QTextCharFormat(); // Reset
format.clearBackground();
format.clearForeground();
format.setFontWeight(QFont::Normal);
format.setFontUnderline(false);
bright = false;
cursor.mergeBlockCharFormat(format);
break;
case 1:
// format.setFontWeight(QFont::Bold);
// format.setForeground(QColor(255, 255, 255)); //hellweiß (richtiges weiß)
bright = true;
break;
case 22:
bright = false; // Normal intensity
break;
case 4:
format.setFontUnderline(true);
break;
case 24:
format.setFontUnderline(false);
break;
case 30:
format.setForeground(bright ? QColor(85,85,85) : QColor(0,0,0)); //grau (eher dunkelgrau) / schwarz
break;
case 31:
format.setForeground(bright ? QColor(255,85,85) : QColor(187,0,0)); //hellrot / rot
break;
case 32:
format.setForeground(bright ? QColor(85,255,85) : QColor(0,187,0)); //hellgrün / grün
break;
case 33:
format.setForeground(bright ? QColor(255,255,85) : QColor(187,187,0)); //hellgelb(ingame gaelb) / gelb (ingame braun)
break;
case 34:
format.setForeground(bright ? QColor(85,85,255) : QColor(0,0,187)); //hellblau / blau
break;
case 35:
format.setForeground(bright ? QColor(255,85,255) : QColor(187,0,187)); //hellmagenta(ingame helllila) / magenta(ingame lila)
break;
case 36:
format.setForeground(bright ? QColor(85,255,255) : QColor(0,187,187)); //hellcyan / cyan
break;
case 37:
format.setForeground(bright ? QColor(255, 255, 255) : QColor(187,187,187)); //hellweiß (richtiges weiß) / weiß (eher hellgrau)
break;
case 40:
format.setBackground(bright ? QColor(85,85,85) : QColor(0,0,0)); //grau (eher dunkelgrau) / schwarz
break;
case 41:
format.setBackground(bright ? QColor(255,85,85) : QColor(187,0,0)); //hellrot / rot
break;
case 42:
format.setBackground(bright ? QColor(85,255,85) : QColor(0,187,0)); //hellgrün / grün
break;
case 43:
format.setBackground(bright ? QColor(255,255,85) : QColor(187,187,0)); //hellgelb(ingame gaelb) / gelb (ingame braun)
break;
case 44:
format.setBackground(bright ? QColor(85,85,255) : QColor(0,0,187)); //hellblau / blau
break;
case 45:
format.setBackground(bright ? QColor(255,85,255) : QColor(187,0,187)); //hellmagenta(ingame helllila) / magenta(ingame lila)
break;
case 46:
format.setBackground(bright ? QColor(85,255,255) : QColor(0,187,187)); //hellcyan / cyan
break;
case 47:
format.setBackground(bright ? QColor(255, 255, 255) : QColor(187,187,187)); //hellweiß (richtiges weiß) / weiß (eher hellgrau)
break;
}
}
cursor.setCharFormat(format);
}