-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
345 lines (301 loc) · 7.76 KB
/
index.js
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
let d, clrs, style
d = console.log
//!baseTheme
import { EditorView } from '@codemirror/view'
function bgDec
(bg, bold) {
let fg, c
fg = bg - 10
c = clrs[fg]
return bold ? c.boldBg : c.bg
}
function isBg
(num) {
return (num >= 40) && (num <= 47)
}
function isClr
(num) {
return isBg(num) || clrs[num]
}
function makeDec
(attr) {
let css
css = ''
if (attr.fg)
css += ' cm-ansi-' + clrs[attr.fg].name
if (attr.bg)
css += ' cm-ansi-' + clrs[attr.bg - 10].name + '-bg'
if (attr.bold)
css += ' cm-ansi-bold'
return Decoration.mark({ attributes: { class: css } })
}
function clr
(name, color) {
let css
css = 'cm-ansi-' + name
if (color) {
style['.' + css] = { color: color }
style['.' + css + '-bg'] = { backgroundColor: color }
}
else {
// special case for regular fg with bold (eg ESC[1m or ESC[1;40m)
style['.' + css] = {}
style['.' + css + '-bg'] = {}
}
return { name: name,
norm: Decoration.mark({ attributes: { class: css } }),
bg: Decoration.mark({ attributes: { class: css + '-bg' } }),
bold: Decoration.mark({ attributes: { class: css + ' cm-ansi-bold' } }),
boldBg: Decoration.mark({ attributes: { class: css + '-bg cm-ansi-bold' } }) }
}
function clr2
(i, name, color, bright) {
clrs[i] = clr(name, color)
clrs[i + 60] = clr('bright' + name, bright)
}
style = { '.cm-ansi-bold': { fontWeight: 'bold' } }
clrs = []
clrs[1] = clr('text', null)
clr2(30, 'black', '#000000', '#555555')
clr2(31, 'red', '#AA0000', '#FF5555')
clr2(32, 'green', '#00AA00', '#55FF55')
clr2(33, 'yellow', '#AA5500', '#FFFF55')
clr2(34, 'blue', '#0000AA', '#5555FF')
clr2(35, 'magenta', '#AA00AA', '#FF55FF')
clr2(36, 'cyan', '#00AAAA', '#55FFFF')
clr2(37, 'white', '#AAAAAA', '#FFFFFF')
const baseTheme = EditorView.baseTheme(style)
//!facet
import { Facet } from '@codemirror/state'
const stepSize = Facet.define({
combine: values => values.length ? Math.min(...values) : 2
})
//!constructor
export function ansi
(options = {}) {
return [ baseTheme,
options.step == null ? [] : stepSize.of(options.step),
showAnsi ]
}
//!ansiDeco
import { Decoration } from '@codemirror/view'
import { RangeSetBuilder } from '@codemirror/state'
let hide, csRe
hide = Decoration.replace({})
// Select Graphic Rendition
// https://en.wikipedia.org/wiki/ANSI_escape_code#SGR
// ESC[m
// ESC[1m
// ESC[1;32m
// ESC[1;32;43m
csRe = /\x1B\[([0-9]*)((?:;[0-9]+)*)m/gd // (?: ) is non capturing group
function decoLine
(builder, cache, line) {
let fg, bg, bold, ranges, hit, matches
function boldOn
() {
bold = 1
}
function boldOff
() {
bold = 0
}
function push
(attr) {
attr.cache = 1
if (attr.from == undefined)
// pushing for the line cache, eg for reset
attr.skipStyle = 1
else if (attr.hide) {
// this 'attribute' hides the control sequences
attr.dec = hide
// cache is for attributes that affect the style
attr.cache = 0
}
else if (attr.bg && attr.fg)
attr.dec = makeDec(attr)
else if (attr.bg)
attr.dec = bgDec(attr.bg, attr.bold)
else if (attr.fg)
attr.dec = attr.bold ? clrs[attr.fg].bold : clrs[attr.fg].norm
else if (attr.bold)
attr.dec = clrs[1].bold
ranges.push(attr)
}
function add
(from, len /* of marker */, to, num, init) {
//d('add ' + from + ' ' + len + ' ' + to + ' ' + num + ' ' + init)
// terminate previous
if (init) {
// skip because initializing line with cached info from previous line
}
else if ((fg || bg || bold) && ranges.length) {
let last
last = ranges.at(-1)
last.to = from
}
// hide control sequence
if (1)
push({ from: from, to: from + len, hide: 1 })
// reset
if (num == 0) {
fg = 0
bg = 0
push({ bold: 0, fg: fg, bg: bg }) // dummy, for cache
boldOff()
return
}
// weight change
if ([ 1, 22 ].includes(num)) {
if (num == 22) {
// normal
boldOff()
if (fg || bg)
push({ from: from + len, to: to, fg: fg, bg: bg, bold: 0 })
else
push({ bold: 0, fg: 0, bg: 0 }) // dummy, for cache
}
if (num == 1) {
// bold
boldOn()
push({ from: from + len, to: to, fg: fg, bg: bg, bold: 1 })
}
return
}
// default fg
if (num == 39) {
fg = 0
if (bg || bold)
push({ from: from + len, to: to, fg: fg, bg: bg, bold: bold })
else
push({ bold: 0, fg: 0, bg: 0 }) // dummy, for cache
return
}
// default bg
if (num == 49) {
bg = 0
if (fg || bold)
push({ from: from + len, to: to, fg: fg, bg: bg, bold: bold })
else
push({ bold: 0, fg: 0, bg: 0 }) // dummy, for cache
return
}
// color
if (isClr(num)) {
if (isBg(num))
bg = num
else
fg = num
push({ from: from + len, to: to, fg: fg, bg: bg, bold: bold })
return
}
}
function addAttr
(line,
start, // of control sequence, offset into line
end, // of control sequence, offset into line
slice,
init) {
let num
num = parseInt(slice)
add(line.from + start, end - start, line.to, num, init)
}
function addGroup
(line, start, end, group) {
let slice, num
if (group[0] == group[1])
// should only happen for first group, via ESC[m;
num = 0
else {
slice = line.text.slice(group[0])
num = parseInt(slice)
}
add(line.from + start, end - start, line.to, num)
}
ranges = []
if (line.number > 0)
hit = cache[line.number - 1]
fg = hit?.fg || 0
bg = hit?.bg || 0
bold = hit?.bold || 0
if (hit) {
if (0) {
d('hit ' + line.number)
d('fg ' + hit.fg)
d('bg ' + hit.bg)
d('bold ' + hit.bold)
}
// Only one because they're in the same position.
// Will pick up other values from fg/bg/bold vars in add.
// Last arg 1 to prevent closing previous attr, because we're starting a new line.
if (hit.fg)
addAttr(line, 0, 0, hit.fg, 1)
else if (hit.bg)
addAttr(line, 0, 0, hit.bg, 1)
else if (hit.bold)
addAttr(line, 0, 0, 1, 1)
}
csRe.lastIndex = 0
matches = line.text.matchAll(csRe)
matches.forEach(match => {
let start, end
start = match.indices[0][0]
end = match.indices[0][1]
// First attribute has own group
addGroup(line, start, end, match.indices[1])
// Remaining attributes need to be split
if (match.indices.length > 2) {
let group2
group2 = match.indices[2]
if (group2[0] == group2[1])
return
line.text.slice(group2[0], group2[1]).split(';').forEach(attr => {
if (attr.length)
addAttr(line, start, end, attr)
})
}
})
ranges.forEach(r => r.skipStyle || builder.add(r.from, r.to, r.dec))
{
let filtered
filtered = ranges.filter(r => r.cache)
if (filtered.length) {
cache[line.number] = filtered.at(-1)
if (0) {
d('cached ' + line.number)
d('fg ' + cache[line.number].fg)
d('bg ' + cache[line.number].bg)
d('bold ' + cache[line.number].bold)
}
}
}
}
function ansiDeco
(view) {
let builder, cache
builder = new RangeSetBuilder()
cache = []
for (let { from, to } of view.visibleRanges)
for (let pos = from; pos <= to;) {
let line
line = view.state.doc.lineAt(pos)
decoLine(builder, cache, line)
pos = line.to + 1
}
return builder.finish()
}
//!showAnsi
import { ViewPlugin } from '@codemirror/view'
const showAnsi = ViewPlugin.fromClass(class {
constructor
(view) {
this.decorations = ansiDeco(view)
}
update
(update) {
if (update.docChanged || update.viewportChanged)
this.decorations = ansiDeco(update.view)
}
}, {
decorations: v => v.decorations
})