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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
/**
* @TODO I'm hardcoding this to ANSI for now, as it's the most common.
* Ideally we should have configurable control characters for various
* types of terminals that could be emulated via a standard termcap file.
* @NOTE ANSI escape sequence reference:
* https://en.wikipedia.org/wiki/ANSI_escape_code#Escape_sequences
* Note that escape sequences for other terminal types are a thing, and
* currently aren't implemented yet as mentioned in the TODO above, eg:
* https://en.wikipedia.org/wiki/VT52#Escape_sequences
*/
#include "renderhelpers.hpp"
#include <QByteArray>
#include <QString>
#include <QTextCursor>
#include <string>
#include <iostream>
static constexpr const char escapeString = '\033';
bool inverted{false};
void setColor(QTextCharFormat& format, unsigned char n, bool bg=false)
{
QColor color;
if (n < 16)
{
// The normal pre-defined typical 16 colors.
/// @TODO these should probably be configurable.
static const Qt::GlobalColor colorcodes[] = {
// The normal pre-defined typical 8 colors.
Qt::black,
Qt::darkRed,
Qt::darkGreen,
Qt::darkYellow,
Qt::darkBlue,
Qt::darkMagenta,
Qt::darkCyan,
Qt::lightGray,
// bold/intense? versions of the normal 8 colors.
Qt::gray,
Qt::red,
Qt::green,
Qt::yellow,
Qt::blue,
Qt::magenta,
Qt::cyan,
Qt::white
};
color = QColor(colorcodes[n]);
}
else if (n < 232)
{
// indexed 8-bit rgb color pallete.
unsigned int index_R = ((n - 16) / 36);
unsigned char r = index_R > 0 ? 55 + index_R * 40 : 0;
unsigned int index_G = (((n - 16) % 36) / 6);
unsigned char g = index_G > 0 ? 55 + index_G * 40 : 0;
unsigned int index_B = ((n - 16) % 6);
unsigned char b = index_B > 0 ? 55 + index_B * 40 : 0;
color = QColor(r, g, b);
}
else
{
// grayscale pallete.
unsigned char g = (n - 232) * 10 + 8;
color = QColor(g, g, g);
}
if (bg)
format.setBackground(color);
else
format.setForeground(color);
}
QString parseNumber(const QString& input, QString::const_iterator& it)
{
QString result;
while (it != input.cend())
{
const auto currentCharacter = *it;
if (!currentCharacter.isNumber()) break;
result += currentCharacter;
++it;
}
return result;
}
void parseSGR(
std::vector<unsigned char>& args,
const QString& input,
QString::const_iterator& it,
QTextCharFormat& format,
const QTextCharFormat& defaultFormat,
QTextCursor& cursor)
{
if (args.empty()) return;
for (auto it = args.cbegin(); it != args.cend(); ++it)
{
const auto arg = *it;
switch(arg)
{
/// @TODO A whole bunch of unimplemented SGR codes are unimplemented
/// yet (eg: blink or font switching)
case 0: // Reset.
format = defaultFormat;
break;
case 1: // Bold.
format.setFontWeight(QFont::Bold);
break;
case 2: // Light.
format.setFontWeight(QFont::Light);
break;
case 3: // Italic.
format.setFontItalic(true);
break;
case 4: // Underline.
/// @TODO Underline style should be configurable?
format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
break;
case 7: // Reverse video (invert).
if (!inverted)
{
const auto fg = format.foreground();
const auto bg = format.background();
format.setForeground(bg);
format.setBackground(fg);
inverted = true;
}
break;
case 21: // Double underline (or bold off?)
format.setUnderlineStyle(QTextCharFormat::WaveUnderline);
break;
case 22: // Normal weight.
format.setFontWeight(QFont::Normal);
break;
case 23: // Not italic.
format.setFontItalic(false);
break;
case 24: // Not underlined.
format.setFontUnderline(QTextCharFormat::NoUnderline);
break;
case 27: // Not inverted.
if (inverted)
{
const auto fg = format.foreground();
const auto bg = format.background();
format.setForeground(bg);
format.setBackground(fg);
inverted = false;
}
break;
case 29: // Not crossed out.
format.setFontStrikeOut(false);
break;
case 38: // Set foreground (RGB)
if (args.size() > 2)
{
++it;
const auto colMode = *it;
if (colMode == 5)
{
++it;
const auto colNum = *it;
if (colNum == 124)
{
setColor(format, colNum);
}
setColor(format, colNum);
}
else if (colMode == 2)
{
++it;
if (args.size() >= 4)
{
const auto red = *it;
++it;
const auto green = *it;
++it;
const auto blue = *it;
format.setForeground(QColor(red, green, blue));
}
}
}
break;
case 39: // Default foreground color.
format.setForeground(defaultFormat.foreground());
break;
case 48: // Set background (RGB)
if (args.size() > 2)
{
++it;
const auto colMode = *it;
if (colMode == 5)
{
++it;
const auto colNum = *it;
setColor(format, colNum, true);
}
else if (colMode == 2)
{
++it;
if (args.size() >= 4)
{
const auto red = *it;
++it;
const auto green = *it;
++it;
const auto blue = *it;
format.setBackground(QColor(red, green, blue));
}
}
}
break;
case 49: // Default background color.
format.setBackground(defaultFormat.background());
break;
default:
// foreground, background and their bright equivalents
if (arg >= 30 && arg < 38)
setColor(format, arg - 30);
else if (arg >= 40 && arg < 48)
setColor(format, arg - 40, true);
else if (arg >= 90 && arg < 98)
setColor(format, arg - 90 + 8);
else if (arg >= 100 && arg < 108)
setColor(format, arg - 100 + 8, true);
break;
}
}
}
std::vector<unsigned char> parseNumericArguments(const QString& input, QString::const_iterator& it)
{
std::vector<unsigned char> result;
while (it != input.end())
{
const auto currentCharacter = *it;
const auto numStr = parseNumber(input, it);
if (numStr.isEmpty())
{
if (!(currentCharacter == ' ' || currentCharacter == ';'))
{
break;
}
}
else
{
result.emplace_back(numStr.toShort());
continue;
}
++it;
}
return result;
}
void parseCSI(
const QString& input,
QString::const_iterator& it,
QTextCharFormat& format,
const QTextCharFormat& defaultFormat,
QTextCursor& cursor)
{
std::vector<unsigned char> numericArguments = parseNumericArguments(input, it);
char numericArgument = numericArguments.empty() ? 1 : numericArguments[0];
if (it != input.cend())
{
const auto code = (*it).unicode();
switch(code)
{
case 'A': // cursor up
cursor.movePosition(QTextCursor::Up, QTextCursor::MoveAnchor, numericArgument);
break;
case 'B': // cursor down
cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, numericArgument);
break;
case 'C': // cursor forward
cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, numericArgument);
break;
case 'D': // cursor back
cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, numericArgument);
break;
case 'E': // cursor next line
cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, numericArgument);
cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor);
break;
case 'F': // cursor previous line
cursor.movePosition(QTextCursor::Up, QTextCursor::MoveAnchor, numericArgument);
cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor);
break;
case 'G': // cursor horizontal absolute position set
cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor);
cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, numericArgument);
break;
case 'H': // Set cursor position ( row;col )
if (numericArguments.size() > 1)
{
cursor.setPosition(0, QTextCursor::MoveAnchor);
cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, numericArguments[0]);
cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, numericArguments[1]);
}
break;
case 'J': // Erase in display
/// @TODO ANSI escape: implement erase in display.
break;
case 'K': // Erase in line
/// @TODO ANSI escape: implement erase in line.
break;
case 'S': // Scroll up
/// @TODO ANSI escape: implement scroll up.
break;
case 'T': // Scroll down
/// @TODO ANSI escape: implement scroll down.
break;
case 'f': // Horizontal vertical position
if (numericArguments.size() > 1)
{
cursor.setPosition(0, QTextCursor::MoveAnchor);
cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, numericArguments[0]);
cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, numericArguments[1]);
}
break;
case 'm': // SGR
// CSI m is treated as CSI 0 m. (and thus, SGR 0 m)
if (numericArguments.size() == 1) numericArguments.insert(numericArguments.begin(), 0);
parseSGR(numericArguments, input, it, format, defaultFormat, cursor);
break;
default:
// Stuff we ignore: CSI 5i, CSI 4i, CSI 6n.
// We do care about SGR (CSI n <m>)
break;
}
}
}
void RenderEscapeCodes(const QByteArray &input, const QTextCharFormat& format, QTextCursor& cursor)
{
auto textFormat = format;
const auto tokens = input.split(escapeString);
const auto inputString = QString::fromUtf8(input);
for (QString::const_iterator it = inputString.cbegin(); it != inputString.cend(); ++it)
{
const auto currentCharacter = *it;;
if (currentCharacter == escapeString)
{
it++;
const auto escSequence = *it;
if (escSequence == "[")
{
it++;
parseCSI(input, it, textFormat, format, cursor);
}
}
else
{
cursor.insertText(currentCharacter, textFormat);
}
}
}
|