-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.cjs
145 lines (117 loc) · 4.04 KB
/
index.cjs
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
const colorText = new Map([
["0", "/"],
["1", "bold"],
["2", "dim"],
["3", "italic"],
["4", "underline"],
["7", "inverse"],
["8", "hidden"],
["9", "strikethrough"],
["22", "/intensity"],
["23", "/italic"],
["24", "/underline"],
["27", "/inverse"],
["28", "/hidden"],
["29", "/strikethrough"],
["30", "black"],
["31", "red"],
["32", "green"],
["33", "yellow"],
["34", "blue"],
["35", "magenta"],
["36", "cyan"],
["37", "white"],
["39", "/color"],
["40", "backgroundBlack"],
["41", "backgroundRed"],
["42", "backgroundGreen"],
["43", "backgroundYellow"],
["44", "backgroundBlue"],
["45", "backgroundMagenta"],
["46", "backgroundCyan"],
["47", "backgroundWhite"],
["49", "/background"],
["53", "overline"],
["55", "/overline"],
["90", "gray"],
["91", "brightRed"],
["92", "brightGreen"],
["93", "brightYellow"],
["94", "brightBlue"],
["95", "brightMagenta"],
["96", "brightCyan"],
["97", "brightWhite"],
["100", "backgroundGray"],
["101", "backgroundBrightRed"],
["102", "backgroundBrightGreen"],
["103", "backgroundBrightYellow"],
["104", "backgroundBrightBlue"],
["105", "backgroundBrightMagenta"],
["106", "backgroundBrightCyan"],
["107", "backgroundBrightWhite"],
]);
const commandText = new Map([
["A", ["moveCursorUpBy", "Row"]],
["B", ["moveCursorDownBy", "Row"]],
["C", ["moveCursorRightBy", "Column"]],
["D", ["moveCursorLeftBy", "Column"]],
["E", ["moveCursorDownBy", "Row", "ToColumn1"]],
["F", ["moveCursorUpBy", "Row", "ToColumn1"]],
["G", ["moveCursorTo", ["Column"]]],
["H", ["moveCursorTo", ["Row", "Column"]]],
["S", ["scrollUpBy", "Row"]],
["T", ["scrollDownBy", "Row"]],
["f", ["moveCursorTo", ["Row", "Column"]]],
]);
function colorOrStyleSequenceReplacer(sequenceText) {
const replacement = [];
const colorParameters = sequenceText.match(/\d{1,3}/g) || ["0"];
for (const colorParameter of colorParameters) {
replacement.push(colorText.get(colorParameter) || "?");
}
return `<${replacement.join(", ")}>`;
}
function moveCursorByReplacer(sequenceText) {
const replacement = [];
sequenceText = sequenceText.trimEnd();
const parameterValue = sequenceText.match(/\[(\d*)/)[1];
const [command, parameterName, tailText] = commandText.get(sequenceText.slice(-1));
replacement.push(command, parameterValue || 1, parameterName);
replacement.push(parameterValue > 1 ? "s" : "");
replacement.push(tailText);
return `<${replacement.join("")}>\n`;
}
function moveCursorToReplacer(sequenceText) {
const replacement = [];
sequenceText = sequenceText.trimEnd();
const parameterValues = sequenceText.match(/\[(\d*;?\d*)/)[1].split(";");
const [command, parameterNames] = commandText.get(sequenceText.slice(-1));
replacement.push(command);
for (const parameterName of parameterNames) {
replacement.push(parameterName, parameterValues.shift() || 1);
}
return `<${replacement.join("")}>\n`;
}
function prettyAnsi(text) {
return text
.replace(/\u001b\[(\d*;?)*m/g, colorOrStyleSequenceReplacer)
.replace(/.(?=\u001b)/g, (match) => `${match}\n`)
.replace(/\u001b\[2J\n?\u001b\[(3J\n?\u001b\[H|0f)\n?/g, "<clearTerminal>\n")
.replace(/\u001b\[\d*[A-FST]\n?/g, moveCursorByReplacer)
.replace(/\u001b\[\d*G\n?/g, moveCursorToReplacer)
.replace(/\u001b\[\d*;?\d*[Hf]\n?/g, moveCursorToReplacer)
.replace(/\u001b\[0?J\n?/g, "<eraseScreenDown>\n")
.replace(/\u001b\[1J\n?/g, "<eraseScreenUp>\n")
.replace(/\u001b\[2J\n?/g, "<eraseScreen>\n")
.replace(/\u001b\[3J\n?/g, "<eraseScreenAndDeleteBuffer>\n")
.replace(/\u001b\[0?K\n?/g, "<eraseLineEnd>\n")
.replace(/\u001b\[1K\n?/g, "<eraseLineStart>\n")
.replace(/\u001b\[2K\n?/g, "<eraseLine>\n")
.replace(/\u001b\[6n\n?/g, "<getCursorPosition>\n")
.replace(/\u001b(\[s|7)\n?/g, "<saveCursorPosition>\n")
.replace(/\u001b(\[u|8)\n?/g, "<restoreCursorPosition>\n")
.replace(/\u001b\[\?25h\n?/g, "<showCursor>\n")
.replace(/\u001b\[\?25l\n?/g, "<hideCursor>\n")
.replace(/\u001b/g, "<ESC>");
}
module.exports = prettyAnsi;