This repository has been archived by the owner on Apr 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EscapeSequenceParser.cpp
561 lines (556 loc) · 13.7 KB
/
EscapeSequenceParser.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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
#include "EscapeSequenceParser.hpp"
#include <QFontDatabase>
/* TODO: more complete Vt102 emulation */
// based on information: http://en.m.wikipedia.org/wiki/ANSI_escape_code
// http://misc.flogisoft.com/bash/tip_colors_and_formatting
// http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
void ParseEscapeSequence(int attribute, QListIterator<QString>& i, QTextCharFormat& textCharFormat,
const QTextCharFormat& defaultTextCharFormat) {
switch (attribute) {
case 0: { // Normal/Default (reset all attributes)
textCharFormat = defaultTextCharFormat;
break;
}
case 1: { // Bold/Bright (bold or increased intensity)
textCharFormat.setFontWeight(QFont::Bold);
break;
}
case 2: { // Dim/Faint (decreased intensity)
textCharFormat.setFontWeight(QFont::Light);
break;
}
case 3: { // Italicized (italic on)
textCharFormat.setFontItalic(true);
break;
}
case 4: { // Underscore (single underlined)
textCharFormat.setUnderlineStyle(QTextCharFormat::SingleUnderline);
textCharFormat.setFontUnderline(true);
break;
}
case 5: { // Blink (slow, appears as Bold)
textCharFormat.setFontWeight(QFont::Bold);
break;
}
case 6: { // Blink (rapid, appears as very Bold)
textCharFormat.setFontWeight(QFont::Black);
break;
}
case 7: { // Reverse/Inverse (swap foreground and background)
QBrush foregroundBrush = textCharFormat.foreground();
textCharFormat.setForeground(textCharFormat.background());
textCharFormat.setBackground(foregroundBrush);
break;
}
case 8: { // Concealed/Hidden/Invisible (usefull for passwords)
textCharFormat.setForeground(textCharFormat.background());
break;
}
case 9: { // Crossed-out characters
textCharFormat.setFontStrikeOut(true);
break;
}
case 10: { // Primary (default) font
textCharFormat.setFont(defaultTextCharFormat.font());
break;
}
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
case 19: {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
QString fontFamily = textCharFormat.fontFamily();
QStringList fontStyles = QFontDatabase::styles(fontFamily);
int fontStyleIndex = attribute - 11;
if (fontStyleIndex < fontStyles.length()) {
textCharFormat.setFont(
QFontDatabase::font(fontFamily, fontStyles.at(fontStyleIndex), textCharFormat.font().pointSize()));
}
#else
QFontDatabase fontDatabase;
QString fontFamily = textCharFormat.fontFamily();
QStringList fontStyles = fontDatabase.styles(fontFamily);
int fontStyleIndex = attribute - 11;
if (fontStyleIndex < fontStyles.length()) {
textCharFormat.setFont(
fontDatabase.font(fontFamily, fontStyles.at(fontStyleIndex), textCharFormat.font().pointSize()));
}
#endif
break;
}
case 20: { // Fraktur (unsupported)
break;
}
case 21: { // Set Bold off
textCharFormat.setFontWeight(QFont::Normal);
break;
}
case 22: { // Set Dim off
textCharFormat.setFontWeight(QFont::Normal);
break;
}
case 23: { // Unset italic and unset fraktur
textCharFormat.setFontItalic(false);
break;
}
case 24: { // Unset underlining
textCharFormat.setUnderlineStyle(QTextCharFormat::NoUnderline);
textCharFormat.setFontUnderline(false);
break;
}
case 25: { // Unset Blink/Bold
textCharFormat.setFontWeight(QFont::Normal);
break;
}
case 26: { // Reserved
break;
}
case 27: { // Positive (non-inverted)
QBrush backgroundBrush = textCharFormat.background();
textCharFormat.setBackground(textCharFormat.foreground());
textCharFormat.setForeground(backgroundBrush);
break;
}
case 28: {
textCharFormat.setForeground(defaultTextCharFormat.foreground());
textCharFormat.setBackground(defaultTextCharFormat.background());
break;
}
case 29: {
textCharFormat.setUnderlineStyle(QTextCharFormat::NoUnderline);
textCharFormat.setFontUnderline(false);
break;
}
case 30:
case 31:
case 32:
case 33:
case 34:
case 35:
case 36:
case 37: {
int colorIndex = attribute - 30;
QColor color;
if (QFont::Normal < textCharFormat.fontWeight()) {
switch (colorIndex) {
case 0: {
color = Qt::darkGray;
break;
}
case 1: {
color = Qt::red;
break;
}
case 2: {
color = Qt::green;
break;
}
case 3: {
color = Qt::yellow;
break;
}
case 4: {
color = Qt::blue;
break;
}
case 5: {
color = Qt::magenta;
break;
}
case 6: {
color = Qt::cyan;
break;
}
case 7: {
color = Qt::white;
break;
}
default: { Q_ASSERT(false); }
}
} else {
/* Normally dark colors, but forced to light colors for visibility */
switch (colorIndex) {
case 0: {
color = Qt::darkGray;
break;
}
case 1: {
color = Qt::red;
break;
}
case 2: {
color = Qt::green;
break;
}
case 3: {
color = Qt::yellow;
break;
}
case 4: {
color = Qt::blue;
break;
}
case 5: {
color = Qt::magenta;
break;
}
case 6: {
color = Qt::cyan;
break;
}
case 7: {
color = Qt::white;
break;
}
default: { Q_ASSERT(false); }
}
}
textCharFormat.setForeground(color);
break;
}
case 38: {
if (i.hasNext()) {
bool ok = false;
int selector = i.next().toInt(&ok);
Q_ASSERT(ok);
QColor color;
switch (selector) {
case 2: {
if (!i.hasNext()) {
break;
}
int red = i.next().toInt(&ok);
Q_ASSERT(ok);
if (!i.hasNext()) {
break;
}
int green = i.next().toInt(&ok);
Q_ASSERT(ok);
if (!i.hasNext()) {
break;
}
int blue = i.next().toInt(&ok);
Q_ASSERT(ok);
color.setRgb(red, green, blue);
break;
}
case 5: {
if (!i.hasNext()) {
break;
}
int index = i.next().toInt(&ok);
Q_ASSERT(ok);
if (index >= 0 && index <= 0x07) { // 0x00-0x07: standard colors (as in ESC [ 30..37 m)
return ParseEscapeSequence(index - 0x00 + 30, i, textCharFormat, defaultTextCharFormat);
} else if (index >= 0x08 && index <= 0x0F) { // 0x08-0x0F: high intensity colors (as in ESC [ 90..97 m)
return ParseEscapeSequence(index - 0x08 + 90, i, textCharFormat, defaultTextCharFormat);
} else if (index >= 0x10 && index <= 0xE7) { // 0x10-0xE7: 6*6*6=216 colors: 16 + 36*r + 6*g + b (0≤r,g,b≤5)
index -= 0x10;
int red = index % 6;
index /= 6;
int green = index % 6;
index /= 6;
int blue = index % 6;
index /= 6;
Q_ASSERT(index == 0);
color.setRgb(red, green, blue);
break;
} else if (index >= 0xE8 && index <= 0xFF) { // 0xE8-0xFF: grayscale from black to white in 24 steps
qreal intensity = qreal(index - 0xE8) / (0xFF - 0xE8);
color.setRgbF(intensity, intensity, intensity);
break;
}
textCharFormat.setForeground(color);
break;
}
default: { break; }
}
}
break;
}
case 39: {
textCharFormat.setForeground(defaultTextCharFormat.foreground());
break;
}
case 40:
case 41:
case 42:
case 43:
case 44:
case 45:
case 46:
case 47: {
int colorIndex = attribute - 40;
QColor color;
switch (colorIndex) {
case 0: {
color = Qt::darkGray;
break;
}
case 1: {
color = Qt::red;
break;
}
case 2: {
color = Qt::green;
break;
}
case 3: {
color = Qt::yellow;
break;
}
case 4: {
color = Qt::blue;
break;
}
case 5: {
color = Qt::magenta;
break;
}
case 6: {
color = Qt::cyan;
break;
}
case 7: {
color = Qt::white;
break;
}
default: { Q_ASSERT(false); }
}
textCharFormat.setBackground(color);
break;
}
case 48: {
if (i.hasNext()) {
bool ok = false;
int selector = i.next().toInt(&ok);
Q_ASSERT(ok);
QColor color;
switch (selector) {
case 2: {
if (!i.hasNext()) {
break;
}
int red = i.next().toInt(&ok);
Q_ASSERT(ok);
if (!i.hasNext()) {
break;
}
int green = i.next().toInt(&ok);
Q_ASSERT(ok);
if (!i.hasNext()) {
break;
}
int blue = i.next().toInt(&ok);
Q_ASSERT(ok);
color.setRgb(red, green, blue);
break;
}
case 5: {
if (!i.hasNext()) {
break;
}
int index = i.next().toInt(&ok);
Q_ASSERT(ok);
if (index >= 0x00 && index <= 0x07) { // 0x00-0x07: standard colors (as in ESC [ 40..47 m)
return ParseEscapeSequence(index - 0x00 + 40, i, textCharFormat, defaultTextCharFormat);
} else if (index >= 0x08 && index <= 0x0F) { // 0x08-0x0F: high intensity colors (as in ESC [ 100..107 m)
return ParseEscapeSequence(index - 0x08 + 100, i, textCharFormat, defaultTextCharFormat);
} else if (index >= 0x10 && index <= 0xE7) { // 0x10-0xE7: 6*6*6=216 colors: 16 + 36*r + 6*g + b (0≤r,g,b≤5)
index -= 0x10;
int red = index % 6;
index /= 6;
int green = index % 6;
index /= 6;
int blue = index % 6;
index /= 6;
Q_ASSERT(index == 0);
color.setRgb(red, green, blue);
break;
} else if (index >= 0xE8 && index <= 0xFF) { // 0xE8-0xFF: grayscale from black to white in 24 steps
qreal intensity = qreal(index - 0xE8) / (0xFF - 0xE8);
color.setRgbF(intensity, intensity, intensity);
}
}
textCharFormat.setBackground(color);
break;
}
}
break;
}
case 49: {
textCharFormat.setBackground(defaultTextCharFormat.background());
break;
}
case 90:
case 91:
case 92:
case 93:
case 94:
case 95:
case 96:
case 97: {
int colorIndex = attribute - 90;
QColor color;
switch (colorIndex) {
case 0: {
color = Qt::darkGray;
break;
}
case 1: {
color = Qt::red;
break;
}
case 2: {
color = Qt::green;
break;
}
case 3: {
color = Qt::yellow;
break;
}
case 4: {
color = Qt::blue;
break;
}
case 5: {
color = Qt::magenta;
break;
}
case 6: {
color = Qt::cyan;
break;
}
case 7: {
color = Qt::white;
break;
}
default: { Q_ASSERT(false); }
}
// color.setRedF(color.redF() * 0.8);
// color.setGreenF(color.greenF() * 0.8);
// color.setBlueF(color.blueF() * 0.8);
textCharFormat.setForeground(color);
break;
}
case 100:
case 101:
case 102:
case 103:
case 104:
case 105:
case 106:
case 107: {
int colorIndex = attribute - 100;
QColor color;
switch (colorIndex) {
case 0: {
color = Qt::darkGray;
break;
}
case 1: {
color = Qt::red;
break;
}
case 2: {
color = Qt::green;
break;
}
case 3: {
color = Qt::yellow;
break;
}
case 4: {
color = Qt::blue;
break;
}
case 5: {
color = Qt::magenta;
break;
}
case 6: {
color = Qt::cyan;
break;
}
case 7: {
color = Qt::white;
break;
}
default: { Q_ASSERT(false); }
}
// color.setRedF(color.redF() * 0.8);
// color.setGreenF(color.greenF() * 0.8);
// color.setBlueF(color.blueF() * 0.8);
textCharFormat.setBackground(color);
break;
}
default: { break; }
}
}
void ReturnInsert(QTextCursor& cur, const QString& text) {
const auto DoLine = [&](const QString& line) {
const auto DoReturn = [&](const QString& ret) {
if (!ret.isEmpty()) {
cur.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, ret.size());
cur.insertText(ret);
}
};
const QStringList list = line.split(QLatin1Char{'\r'});
DoReturn(list.front());
if (list.size() > 1) {
for (auto it = list.begin() + 1; it != list.end(); ++it) {
cur.movePosition(QTextCursor::StartOfBlock);
DoReturn(*it);
}
}
};
#if _WIN32
const QStringList lineSplit = text.split(QStringLiteral("\r\n"));
#else
const QStringList lineSplit = text.split(QLatin1Char{'\n'});
#endif
DoLine(lineSplit.front());
if (lineSplit.size() > 1) {
for (auto it = lineSplit.begin() + 1; it != lineSplit.end(); ++it) {
cur.movePosition(QTextCursor::EndOfLine);
cur.insertBlock();
DoLine(*it);
}
}
}
void ReturnInsert(QTextCursor& cur, const QString& text, const QTextCharFormat& format) {
const auto DoLine = [&](const QString& line) {
const auto DoReturn = [&](const QString& ret) {
if (!ret.isEmpty()) {
cur.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, ret.size());
cur.insertText(ret, format);
}
};
const QStringList list = line.split(QLatin1Char{'\r'});
DoReturn(list.front());
if (list.size() > 1) {
for (auto it = list.begin() + 1; it != list.end(); ++it) {
cur.movePosition(QTextCursor::StartOfBlock);
DoReturn(*it);
}
}
};
#if _WIN32
const QStringList lineSplit = text.split(QStringLiteral("\r\n"));
#else
const QStringList lineSplit = text.split(QLatin1Char{'\n'});
#endif
DoLine(lineSplit.front());
if (lineSplit.size() > 1) {
for (auto it = lineSplit.begin() + 1; it != lineSplit.end(); ++it) {
cur.movePosition(QTextCursor::EndOfLine);
cur.insertBlock();
DoLine(*it);
}
}
}