brsh 2.8.1 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/.github/workflows/release.yml +3 -1
  2. package/CHANGELOG.md +32 -0
  3. package/POSIX-CHECKLIST.md +3 -3
  4. package/Readme.md +138 -223
  5. package/bin/Readme.md +70 -24
  6. package/bin/brsh-shell.js +16 -131
  7. package/bin/exportFileSystem.js +4 -0
  8. package/bin/zsh-server.js +261 -0
  9. package/dist/brsh.css +1 -1
  10. package/dist/shell.es.js +1020 -648
  11. package/dist/shell.es.js.map +1 -1
  12. package/dist/shell.min.js +34 -19
  13. package/dist/shell.min.js.map +1 -1
  14. package/dist/terminal.cjs.js +77 -100
  15. package/dist/terminal.cjs.js.map +1 -1
  16. package/dist/terminal.es.js +1382 -603
  17. package/dist/terminal.es.js.map +1 -1
  18. package/dist/terminal.min.js +77 -100
  19. package/dist/terminal.min.js.map +1 -1
  20. package/dist/wbt.es.js +410 -238
  21. package/dist/wbt.es.js.map +1 -1
  22. package/dist/wbt.min.js +10 -8
  23. package/dist/wbt.min.js.map +1 -1
  24. package/index.js +472 -0
  25. package/lib/Command.js +30 -0
  26. package/lib/InteractiveCommand.js +28 -0
  27. package/lib/Readme.md +111 -12
  28. package/lib/ReplCommand.js +15 -0
  29. package/lib/Terminal.js +769 -510
  30. package/lib/TtyStream.js +181 -0
  31. package/lib/WasmCommand.js +26 -5
  32. package/lib/builtins/cat.js +2 -2
  33. package/lib/builtins/grep.js +2 -2
  34. package/lib/local/clear.js +3 -0
  35. package/lib/terminal/DomRenderer.js +325 -0
  36. package/lib/terminal/PtyBridge.js +169 -0
  37. package/lib/terminal/ScreenBuffer.js +594 -0
  38. package/lib/terminal/VtParser.js +728 -0
  39. package/lib/wbt/index.js +3 -2
  40. package/lib/wbt/registry.js +15 -1
  41. package/package.json +4 -2
  42. package/styles/terminal.css +33 -4
  43. package/test/terminal.test.js +762 -40
  44. package/test/wbt.test.js +92 -0
@@ -0,0 +1,728 @@
1
+ 'use strict';
2
+
3
+ import {
4
+ ATTR_BOLD,
5
+ ATTR_DIM,
6
+ ATTR_ITALIC,
7
+ ATTR_UNDERLINE,
8
+ ATTR_BLINK,
9
+ ATTR_INVERSE,
10
+ ATTR_HIDDEN,
11
+ ATTR_STRIKE
12
+ } from './ScreenBuffer.js';
13
+
14
+ const STATE_GROUND = 0;
15
+ const STATE_ESC = 1;
16
+ const STATE_ESC_INTERMEDIATE = 2;
17
+ const STATE_CSI = 3;
18
+ const STATE_OSC = 4;
19
+ const STATE_DCS = 5;
20
+
21
+ // DEC Special Graphics Character Set (VT100 ACS line drawing)
22
+ const DEC_SPECIAL_GRAPHICS = {
23
+ '`': '◆',
24
+ a: '▒',
25
+ f: '°',
26
+ g: '±',
27
+ j: '┘',
28
+ k: '┐',
29
+ l: '┌',
30
+ m: '└',
31
+ n: '┼',
32
+ o: '⎺',
33
+ p: '⎻',
34
+ q: '─',
35
+ r: '⎼',
36
+ s: '⎽',
37
+ t: '├',
38
+ u: '┤',
39
+ v: '┴',
40
+ w: '┬',
41
+ x: '│',
42
+ y: '≤',
43
+ z: '≥',
44
+ '{': 'π',
45
+ '|': '≠',
46
+ '}': '£',
47
+ '~': '·'
48
+ };
49
+
50
+ const DEFAULT_URL_WHITELIST = {
51
+ http: true,
52
+ https: true,
53
+ tel: true,
54
+ mailto: true
55
+ };
56
+
57
+ function sanitizeUrl(url, whitelist = DEFAULT_URL_WHITELIST) {
58
+ if (!url || typeof url !== 'string') return null;
59
+ const trimmed = url.trim();
60
+ const match = trimmed.match(/^([a-zA-Z][a-zA-Z0-9+.-]*):/);
61
+ if (!match) return null;
62
+ const scheme = match[1].toLowerCase();
63
+ if (!whitelist[scheme]) return null;
64
+ return trimmed;
65
+ }
66
+
67
+ class VtParser {
68
+ constructor(screen, options = {}) {
69
+ this.screen = screen;
70
+ this.onResponse = options.onResponse || null;
71
+ this.onTitle = options.onTitle || null;
72
+ this.onBell = options.onBell || null;
73
+ this.urlWhitelist = options.urlWhitelist || DEFAULT_URL_WHITELIST;
74
+
75
+ this.state = STATE_GROUND;
76
+ this._csiPrefix = '';
77
+ this._csiParams = '';
78
+ this._csiIntermediate = '';
79
+ this._escIntermediate = '';
80
+ this._oscBuffer = '';
81
+ this._oscEscSeen = false;
82
+ this._dcsEscSeen = false;
83
+ this._lastPrintedChar = ' ';
84
+
85
+ // Character set state: 'B' = ASCII, '0' = DEC Special Graphics
86
+ this.charsetG0 = 'B';
87
+ this.charsetG1 = 'B';
88
+ this.activeCharset = 0; // 0 = G0, 1 = G1
89
+
90
+ // Terminal mode flags
91
+ this.applicationCursorKeys = false; // ?1
92
+ this.applicationKeypad = false; // ESC = / ESC >
93
+ this.bracketedPasteMode = false; // ?2004
94
+ this.mouseTrackingMode = 0; // 0=off, 1000=normal, 1002=button, 1003=any
95
+ this.mouseProtocol = 'x10'; // 'x10' | 'utf8' (1005) | 'sgr' (1006)
96
+ this.alternateScrollMode = false; // ?1007
97
+ }
98
+
99
+ reset() {
100
+ this.state = STATE_GROUND;
101
+ this._csiPrefix = '';
102
+ this._csiParams = '';
103
+ this._csiIntermediate = '';
104
+ this._escIntermediate = '';
105
+ this._oscBuffer = '';
106
+ this._oscEscSeen = false;
107
+ this._dcsEscSeen = false;
108
+ this._lastPrintedChar = ' ';
109
+ this.charsetG0 = 'B';
110
+ this.charsetG1 = 'B';
111
+ this.activeCharset = 0;
112
+ this.applicationCursorKeys = false;
113
+ this.applicationKeypad = false;
114
+ this.bracketedPasteMode = false;
115
+ this.mouseTrackingMode = 0;
116
+ this.mouseProtocol = 'x10';
117
+ this.alternateScrollMode = false;
118
+ this.screen.reset();
119
+ }
120
+
121
+ write(data) {
122
+ if (data == null) return;
123
+ const str = typeof data === 'string'
124
+ ? data
125
+ : (data instanceof Uint8Array || Buffer.isBuffer(data))
126
+ ? new TextDecoder().decode(data)
127
+ : String(data);
128
+
129
+ for (let i = 0; i < str.length; i++) {
130
+ const ch = str[i];
131
+ const code = str.charCodeAt(i);
132
+
133
+ switch (this.state) {
134
+ case STATE_GROUND:
135
+ this._handleGround(ch, code);
136
+ break;
137
+ case STATE_ESC:
138
+ this._handleEsc(ch, code);
139
+ break;
140
+ case STATE_ESC_INTERMEDIATE:
141
+ this._handleEscIntermediate(ch, code);
142
+ break;
143
+ case STATE_CSI:
144
+ this._handleCsi(ch, code);
145
+ break;
146
+ case STATE_OSC:
147
+ this._handleOsc(ch, code);
148
+ break;
149
+ case STATE_DCS:
150
+ this._handleDcs(ch, code);
151
+ break;
152
+ }
153
+ }
154
+ }
155
+
156
+ _handleGround(ch, code) {
157
+ if (code === 0x1b) { // ESC
158
+ this.state = STATE_ESC;
159
+ this._escIntermediate = '';
160
+ return;
161
+ }
162
+ if (code === 0x0d) { // CR (\r)
163
+ this.screen.carriageReturn();
164
+ return;
165
+ }
166
+ if (code === 0x0a || code === 0x0b || code === 0x0c) { // LF, VT, FF
167
+ this.screen.lineFeed();
168
+ return;
169
+ }
170
+ if (code === 0x08) { // BS (\b)
171
+ this.screen.backspace();
172
+ return;
173
+ }
174
+ if (code === 0x09) { // HT (\t)
175
+ this.screen.tab();
176
+ return;
177
+ }
178
+ if (code === 0x07) { // BEL
179
+ if (this.onBell) this.onBell();
180
+ return;
181
+ }
182
+ if (code === 0x0e) { // SO (Shift Out -> G1)
183
+ this.activeCharset = 1;
184
+ return;
185
+ }
186
+ if (code === 0x0f) { // SI (Shift In -> G0)
187
+ this.activeCharset = 0;
188
+ return;
189
+ }
190
+ if (code < 0x20 || code === 0x7f) {
191
+ return;
192
+ }
193
+
194
+ const charset = this.activeCharset === 0 ? this.charsetG0 : this.charsetG1;
195
+ const mapped = (charset === '0' && DEC_SPECIAL_GRAPHICS[ch]) ? DEC_SPECIAL_GRAPHICS[ch] : ch;
196
+ this._lastPrintedChar = mapped;
197
+ this.screen.putChar(mapped);
198
+ }
199
+
200
+ _handleEsc(ch, code) {
201
+ if (ch === '[') {
202
+ this.state = STATE_CSI;
203
+ this._csiPrefix = '';
204
+ this._csiParams = '';
205
+ this._csiIntermediate = '';
206
+ return;
207
+ }
208
+ if (ch === ']') {
209
+ this.state = STATE_OSC;
210
+ this._oscBuffer = '';
211
+ this._oscEscSeen = false;
212
+ return;
213
+ }
214
+ if (ch === 'P' || ch === 'X' || ch === '^' || ch === '_') {
215
+ // DCS, SOS, PM, APC strings — consume until ST (ESC \)
216
+ this.state = STATE_DCS;
217
+ this._dcsEscSeen = false;
218
+ return;
219
+ }
220
+ if (ch === '(' || ch === ')' || ch === '*' || ch === '+' || ch === '#') {
221
+ this.state = STATE_ESC_INTERMEDIATE;
222
+ this._escIntermediate = ch;
223
+ return;
224
+ }
225
+ if (ch === '7') {
226
+ this.screen.saveCursor();
227
+ this.state = STATE_GROUND;
228
+ return;
229
+ }
230
+ if (ch === '8') {
231
+ this.screen.restoreCursor();
232
+ this.state = STATE_GROUND;
233
+ return;
234
+ }
235
+ if (ch === 'D') { // IND (Index)
236
+ this.screen.index();
237
+ this.state = STATE_GROUND;
238
+ return;
239
+ }
240
+ if (ch === 'M') { // RI (Reverse Index)
241
+ this.screen.reverseIndex();
242
+ this.state = STATE_GROUND;
243
+ return;
244
+ }
245
+ if (ch === 'E') { // NEL (Next Line)
246
+ this.screen.nextLine();
247
+ this.state = STATE_GROUND;
248
+ return;
249
+ }
250
+ if (ch === 'c') { // RIS (Full Reset)
251
+ this.reset();
252
+ return;
253
+ }
254
+ if (ch === '=') { // DECKPAM
255
+ this.applicationKeypad = true;
256
+ this.state = STATE_GROUND;
257
+ return;
258
+ }
259
+ if (ch === '>') { // DECKPNM
260
+ this.applicationKeypad = false;
261
+ this.state = STATE_GROUND;
262
+ return;
263
+ }
264
+ if (code === 0x1b) {
265
+ // Remain in ESC state
266
+ return;
267
+ }
268
+ this.state = STATE_GROUND;
269
+ }
270
+
271
+ _handleEscIntermediate(ch) {
272
+ if (this._escIntermediate === '(') {
273
+ this.charsetG0 = ch === '0' ? '0' : 'B';
274
+ } else if (this._escIntermediate === ')') {
275
+ this.charsetG1 = ch === '0' ? '0' : 'B';
276
+ } else if (this._escIntermediate === '#' && ch === '8') {
277
+ // DECALN screen alignment test (fills screen with 'E')
278
+ for (let r = 0; r < this.screen.rows; r++) {
279
+ for (let c = 0; c < this.screen.cols; c++) {
280
+ this.screen.grid[r][c].ch = 'E';
281
+ }
282
+ }
283
+ this.screen._markAllDirty();
284
+ }
285
+ this.state = STATE_GROUND;
286
+ }
287
+
288
+ _handleCsi(ch, code) {
289
+ // C0 controls inside CSI are executed immediately in VT100
290
+ if (code === 0x0d) {
291
+ this.screen.carriageReturn();
292
+ return;
293
+ }
294
+ if (code === 0x0a || code === 0x0b || code === 0x0c) {
295
+ this.screen.lineFeed();
296
+ return;
297
+ }
298
+ if (code === 0x08) {
299
+ this.screen.backspace();
300
+ return;
301
+ }
302
+ if (code === 0x1b) {
303
+ this.state = STATE_ESC;
304
+ return;
305
+ }
306
+
307
+ // Prefix bytes: ? > < =
308
+ if (this._csiParams.length === 0 && this._csiPrefix.length === 0 && (ch === '?' || ch === '>' || ch === '<' || ch === '=')) {
309
+ this._csiPrefix = ch;
310
+ return;
311
+ }
312
+
313
+ // Parameter bytes: digits, ;, :
314
+ if ((code >= 0x30 && code <= 0x39) || ch === ';' || ch === ':') {
315
+ this._csiParams += ch;
316
+ return;
317
+ }
318
+
319
+ // Intermediate bytes: 0x20..0x2f
320
+ if (code >= 0x20 && code <= 0x2f) {
321
+ this._csiIntermediate += ch;
322
+ return;
323
+ }
324
+
325
+ // Final byte: 0x40..0x7e
326
+ if (code >= 0x40 && code <= 0x7e) {
327
+ this._dispatchCsi(this._csiPrefix, this._csiParams, this._csiIntermediate, ch);
328
+ this.state = STATE_GROUND;
329
+ }
330
+ }
331
+
332
+ _handleOsc(ch, code) {
333
+ if (code === 0x07) { // BEL terminates OSC
334
+ this._dispatchOsc(this._oscBuffer);
335
+ this.state = STATE_GROUND;
336
+ return;
337
+ }
338
+ if (this._oscEscSeen) {
339
+ this._oscEscSeen = false;
340
+ if (ch === '\\') { // ST (ESC \) terminates OSC
341
+ this._dispatchOsc(this._oscBuffer);
342
+ this.state = STATE_GROUND;
343
+ return;
344
+ }
345
+ this._oscBuffer += '\x1b' + ch;
346
+ return;
347
+ }
348
+ if (code === 0x1b) {
349
+ this._oscEscSeen = true;
350
+ return;
351
+ }
352
+ this._oscBuffer += ch;
353
+ }
354
+
355
+ _handleDcs(ch, code) {
356
+ if (code === 0x07) {
357
+ this.state = STATE_GROUND;
358
+ return;
359
+ }
360
+ if (this._dcsEscSeen) {
361
+ this._dcsEscSeen = false;
362
+ if (ch === '\\') {
363
+ this.state = STATE_GROUND;
364
+ return;
365
+ }
366
+ }
367
+ if (code === 0x1b) {
368
+ this._dcsEscSeen = true;
369
+ }
370
+ }
371
+
372
+ _dispatchOsc(raw) {
373
+ const semi = raw.indexOf(';');
374
+ if (semi === -1) return;
375
+ const cmd = raw.slice(0, semi);
376
+ const payload = raw.slice(semi + 1);
377
+
378
+ if (cmd === '0' || cmd === '2') {
379
+ if (this.onTitle) this.onTitle(payload);
380
+ } else if (cmd === '8') {
381
+ // OSC 8 ; params ; URI
382
+ const nextSemi = payload.indexOf(';');
383
+ if (nextSemi !== -1) {
384
+ const uri = payload.slice(nextSemi + 1);
385
+ this.screen.curLink = uri ? sanitizeUrl(uri, this.urlWhitelist) : null;
386
+ }
387
+ }
388
+ }
389
+
390
+ _parseParams(paramStr) {
391
+ if (!paramStr) return [];
392
+ return paramStr.split(';').map(p => {
393
+ if (p.includes(':')) {
394
+ return p.split(':').map(sp => (sp === '' ? 0 : parseInt(sp, 10) || 0));
395
+ }
396
+ return p === '' ? 0 : parseInt(p, 10) || 0;
397
+ });
398
+ }
399
+
400
+ _dispatchCsi(prefix, paramStr, intermediate, final) {
401
+ const params = this._parseParams(paramStr);
402
+ const p0 = typeof params[0] === 'number' ? params[0] : 0;
403
+ const p1 = typeof params[1] === 'number' ? params[1] : 0;
404
+
405
+ if (prefix === '?') {
406
+ this._dispatchDecPrivate(params, final);
407
+ return;
408
+ }
409
+
410
+ if (prefix === '>') {
411
+ if (final === 'c' && this.onResponse) {
412
+ this.onResponse('\x1b[>0;276;0c');
413
+ }
414
+ return;
415
+ }
416
+
417
+ switch (final) {
418
+ case 'A': // CUU - Cursor Up
419
+ this.screen.moveCursorRelative(-Math.max(1, p0), 0);
420
+ break;
421
+ case 'B': // CUD - Cursor Down
422
+ case 'e':
423
+ this.screen.moveCursorRelative(Math.max(1, p0), 0);
424
+ break;
425
+ case 'C': // CUF - Cursor Forward
426
+ case 'a':
427
+ this.screen.moveCursorRelative(0, Math.max(1, p0));
428
+ break;
429
+ case 'D': // CUB - Cursor Back
430
+ this.screen.moveCursorRelative(0, -Math.max(1, p0));
431
+ break;
432
+ case 'E': // CNL - Cursor Next Line
433
+ this.screen.moveCursorRelative(Math.max(1, p0), 0);
434
+ this.screen.carriageReturn();
435
+ break;
436
+ case 'F': // CPL - Cursor Previous Line
437
+ this.screen.moveCursorRelative(-Math.max(1, p0), 0);
438
+ this.screen.carriageReturn();
439
+ break;
440
+ case 'G': // CHA - Cursor Horizontal Absolute
441
+ case '`':
442
+ this.screen.setCursor(this.screen.cursorRow, Math.max(1, p0) - 1);
443
+ break;
444
+ case 'H': // CUP - Cursor Position
445
+ case 'f':
446
+ this.screen.setCursor(Math.max(1, p0) - 1, Math.max(1, p1) - 1);
447
+ break;
448
+ case 'd': // VPA - Vertical Position Absolute
449
+ this.screen.setCursor(Math.max(1, p0) - 1, this.screen.cursorCol);
450
+ break;
451
+ case 'J': // ED - Erase in Display
452
+ this.screen.eraseInDisplay(p0);
453
+ break;
454
+ case 'K': // EL - Erase in Line
455
+ this.screen.eraseInLine(p0);
456
+ break;
457
+ case 'X': // ECH - Erase Characters
458
+ this.screen.eraseChars(Math.max(1, p0));
459
+ break;
460
+ case '@': // ICH - Insert Blank Characters
461
+ this.screen.insertChars(Math.max(1, p0));
462
+ break;
463
+ case 'P': // DCH - Delete Characters
464
+ this.screen.deleteChars(Math.max(1, p0));
465
+ break;
466
+ case 'L': // IL - Insert Lines
467
+ this.screen.insertLines(Math.max(1, p0));
468
+ break;
469
+ case 'M': // DL - Delete Lines
470
+ this.screen.deleteLines(Math.max(1, p0));
471
+ break;
472
+ case 'S': // SU - Scroll Up
473
+ this.screen.scrollUp(Math.max(1, p0));
474
+ break;
475
+ case 'T': // SD - Scroll Down
476
+ this.screen.scrollDown(Math.max(1, p0));
477
+ break;
478
+ case 'r': // DECSTBM - Set Top and Bottom Margins
479
+ this.screen.setScrollRegion(p0 || 1, p1 || this.screen.rows);
480
+ break;
481
+ case 's': // SCOSC - Save Cursor
482
+ this.screen.saveCursor();
483
+ break;
484
+ case 'u': // SCORC - Restore Cursor
485
+ this.screen.restoreCursor();
486
+ break;
487
+ case 'b': { // REP - Repeat preceding graphic character
488
+ const count = Math.min(this.screen.cols * this.screen.rows, Math.max(1, p0));
489
+ for (let i = 0; i < count; i++) {
490
+ this.screen.putChar(this._lastPrintedChar);
491
+ }
492
+ break;
493
+ }
494
+ case 'm': // SGR - Select Graphic Rendition
495
+ this._handleSgr(params);
496
+ break;
497
+ case 'h': // SM - Set Mode
498
+ case 'l': // RM - Reset Mode
499
+ if (p0 === 20) {
500
+ this.screen.convertEol = (final === 'h');
501
+ }
502
+ break;
503
+ case 'n': // DSR - Device Status Report
504
+ if (this.onResponse) {
505
+ if (p0 === 5) {
506
+ this.onResponse('\x1b[0n');
507
+ } else if (p0 === 6) {
508
+ this.onResponse(`\x1b[${this.screen.cursorRow + 1};${this.screen.cursorCol + 1}R`);
509
+ }
510
+ }
511
+ break;
512
+ case 'c': // DA1 - Primary Device Attributes
513
+ if (this.onResponse) {
514
+ this.onResponse('\x1b[?1;2c');
515
+ }
516
+ break;
517
+ case 't': // XTWINOPS
518
+ if (p0 === 18 && this.onResponse) {
519
+ this.onResponse(`\x1b[8;${this.screen.rows};${this.screen.cols}t`);
520
+ }
521
+ break;
522
+ }
523
+ }
524
+
525
+ _dispatchDecPrivate(params, final) {
526
+ if (final === 'c' && this.onResponse) {
527
+ this.onResponse('\x1b[?1;2c');
528
+ return;
529
+ }
530
+ if (final !== 'h' && final !== 'l') return;
531
+ const enable = final === 'h';
532
+
533
+ for (const p of params) {
534
+ const mode = typeof p === 'number' ? p : 0;
535
+ switch (mode) {
536
+ case 1: // DECCKM - Application Cursor Keys
537
+ this.applicationCursorKeys = enable;
538
+ break;
539
+ case 6: // DECOM - Origin Mode
540
+ this.screen.originMode = enable;
541
+ this.screen.setCursor(0, 0);
542
+ break;
543
+ case 7: // DECAWM - Auto-Wrap Mode
544
+ this.screen.autoWrap = enable;
545
+ break;
546
+ case 25: // DECTCEM - Text Cursor Enable
547
+ this.screen.cursorVisible = enable;
548
+ this.screen._markRowDirty(this.screen.cursorRow);
549
+ break;
550
+ case 47:
551
+ case 1047:
552
+ if (enable) {
553
+ this.screen.enterAltScreen(mode === 1047);
554
+ } else {
555
+ this.screen.exitAltScreen();
556
+ }
557
+ break;
558
+ case 1048:
559
+ if (enable) {
560
+ this.screen.saveCursor();
561
+ } else {
562
+ this.screen.restoreCursor();
563
+ }
564
+ break;
565
+ case 1049:
566
+ if (enable) {
567
+ this.screen.saveCursor();
568
+ this.screen.enterAltScreen(true);
569
+ } else {
570
+ this.screen.exitAltScreen();
571
+ this.screen.restoreCursor();
572
+ }
573
+ break;
574
+ case 1000:
575
+ case 1002:
576
+ case 1003:
577
+ if (enable) {
578
+ this.mouseTrackingMode = mode;
579
+ } else if (this.mouseTrackingMode === mode) {
580
+ this.mouseTrackingMode = 0;
581
+ }
582
+ break;
583
+ case 1005:
584
+ this.mouseProtocol = enable ? 'utf8' : 'x10';
585
+ break;
586
+ case 1006:
587
+ this.mouseProtocol = enable ? 'sgr' : 'x10';
588
+ break;
589
+ case 1007:
590
+ this.alternateScrollMode = enable;
591
+ break;
592
+ case 2004:
593
+ this.bracketedPasteMode = enable;
594
+ break;
595
+ }
596
+ }
597
+ }
598
+
599
+ _handleSgr(params) {
600
+ if (params.length === 0) {
601
+ this.screen.resetAttributes();
602
+ return;
603
+ }
604
+
605
+ let i = 0;
606
+ while (i < params.length) {
607
+ const p = params[i];
608
+
609
+ // Handle colon-separated subparams (e.g. [38, 5, n] or [38, 2, cs, r, g, b])
610
+ if (Array.isArray(p)) {
611
+ const head = p[0];
612
+ if (head === 38 || head === 48) {
613
+ const isFg = head === 38;
614
+ if (p[1] === 5 && p.length >= 3) {
615
+ if (isFg) this.screen.curFg = p[2];
616
+ else this.screen.curBg = p[2];
617
+ } else if (p[1] === 2 && p.length >= 5) {
618
+ const offset = p.length >= 6 ? 3 : 2;
619
+ const color = `rgb(${p[offset] || 0}, ${p[offset + 1] || 0}, ${p[offset + 2] || 0})`;
620
+ if (isFg) this.screen.curFg = color;
621
+ else this.screen.curBg = color;
622
+ }
623
+ }
624
+ i++;
625
+ continue;
626
+ }
627
+
628
+ switch (p) {
629
+ case 0:
630
+ this.screen.resetAttributes();
631
+ break;
632
+ case 1:
633
+ this.screen.curFlags |= ATTR_BOLD;
634
+ break;
635
+ case 2:
636
+ this.screen.curFlags |= ATTR_DIM;
637
+ break;
638
+ case 3:
639
+ this.screen.curFlags |= ATTR_ITALIC;
640
+ break;
641
+ case 4:
642
+ this.screen.curFlags |= ATTR_UNDERLINE;
643
+ break;
644
+ case 5:
645
+ case 6:
646
+ this.screen.curFlags |= ATTR_BLINK;
647
+ break;
648
+ case 7:
649
+ this.screen.curFlags |= ATTR_INVERSE;
650
+ break;
651
+ case 8:
652
+ this.screen.curFlags |= ATTR_HIDDEN;
653
+ break;
654
+ case 9:
655
+ this.screen.curFlags |= ATTR_STRIKE;
656
+ break;
657
+ case 21:
658
+ case 22:
659
+ this.screen.curFlags &= ~(ATTR_BOLD | ATTR_DIM);
660
+ break;
661
+ case 23:
662
+ this.screen.curFlags &= ~ATTR_ITALIC;
663
+ break;
664
+ case 24:
665
+ this.screen.curFlags &= ~ATTR_UNDERLINE;
666
+ break;
667
+ case 25:
668
+ this.screen.curFlags &= ~ATTR_BLINK;
669
+ break;
670
+ case 27:
671
+ this.screen.curFlags &= ~ATTR_INVERSE;
672
+ break;
673
+ case 28:
674
+ this.screen.curFlags &= ~ATTR_HIDDEN;
675
+ break;
676
+ case 29:
677
+ this.screen.curFlags &= ~ATTR_STRIKE;
678
+ break;
679
+ case 38:
680
+ case 48: {
681
+ const isFg = p === 38;
682
+ const mode = params[i + 1];
683
+ if (mode === 5 && i + 2 < params.length) {
684
+ const colorIdx = Number(params[i + 2]) || 0;
685
+ if (isFg) this.screen.curFg = colorIdx;
686
+ else this.screen.curBg = colorIdx;
687
+ i += 2;
688
+ } else if (mode === 2 && i + 4 < params.length) {
689
+ const r = Number(params[i + 2]) || 0;
690
+ const g = Number(params[i + 3]) || 0;
691
+ const b = Number(params[i + 4]) || 0;
692
+ const rgb = `rgb(${r}, ${g}, ${b})`;
693
+ if (isFg) this.screen.curFg = rgb;
694
+ else this.screen.curBg = rgb;
695
+ i += 4;
696
+ }
697
+ break;
698
+ }
699
+ case 39:
700
+ this.screen.curFg = null;
701
+ break;
702
+ case 49:
703
+ this.screen.curBg = null;
704
+ break;
705
+ default:
706
+ if (p >= 30 && p <= 37) {
707
+ this.screen.curFg = p - 30;
708
+ } else if (p >= 40 && p <= 47) {
709
+ this.screen.curBg = p - 40;
710
+ } else if (p >= 90 && p <= 97) {
711
+ this.screen.curFg = p - 90 + 8;
712
+ } else if (p >= 100 && p <= 107) {
713
+ this.screen.curBg = p - 100 + 8;
714
+ }
715
+ break;
716
+ }
717
+ i++;
718
+ }
719
+ }
720
+ }
721
+
722
+ export {
723
+ VtParser,
724
+ DEC_SPECIAL_GRAPHICS,
725
+ sanitizeUrl,
726
+ DEFAULT_URL_WHITELIST
727
+ };
728
+ export default VtParser;