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.
- package/.github/workflows/release.yml +3 -1
- package/CHANGELOG.md +32 -0
- package/POSIX-CHECKLIST.md +3 -3
- package/Readme.md +138 -223
- package/bin/Readme.md +70 -24
- package/bin/brsh-shell.js +16 -131
- package/bin/exportFileSystem.js +4 -0
- package/bin/zsh-server.js +261 -0
- package/dist/brsh.css +1 -1
- package/dist/shell.es.js +1020 -648
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +34 -19
- package/dist/shell.min.js.map +1 -1
- package/dist/terminal.cjs.js +77 -100
- package/dist/terminal.cjs.js.map +1 -1
- package/dist/terminal.es.js +1382 -603
- package/dist/terminal.es.js.map +1 -1
- package/dist/terminal.min.js +77 -100
- package/dist/terminal.min.js.map +1 -1
- package/dist/wbt.es.js +410 -238
- package/dist/wbt.es.js.map +1 -1
- package/dist/wbt.min.js +10 -8
- package/dist/wbt.min.js.map +1 -1
- package/index.js +472 -0
- package/lib/Command.js +30 -0
- package/lib/InteractiveCommand.js +28 -0
- package/lib/Readme.md +111 -12
- package/lib/ReplCommand.js +15 -0
- package/lib/Terminal.js +769 -510
- package/lib/TtyStream.js +181 -0
- package/lib/WasmCommand.js +26 -5
- package/lib/builtins/cat.js +2 -2
- package/lib/builtins/grep.js +2 -2
- package/lib/local/clear.js +3 -0
- package/lib/terminal/DomRenderer.js +325 -0
- package/lib/terminal/PtyBridge.js +169 -0
- package/lib/terminal/ScreenBuffer.js +594 -0
- package/lib/terminal/VtParser.js +728 -0
- package/lib/wbt/index.js +3 -2
- package/lib/wbt/registry.js +15 -1
- package/package.json +4 -2
- package/styles/terminal.css +33 -4
- package/test/terminal.test.js +762 -40
- package/test/wbt.test.js +92 -0
package/lib/TtyStream.js
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const events = require('events');
|
|
4
|
+
const EventEmitter = events.EventEmitter || events.default || events;
|
|
5
|
+
|
|
6
|
+
function parseVtKeys(seq) {
|
|
7
|
+
if (!seq) return [];
|
|
8
|
+
const str = typeof seq === 'string'
|
|
9
|
+
? seq
|
|
10
|
+
: (seq instanceof Uint8Array || (typeof Buffer !== 'undefined' && Buffer.isBuffer(seq)))
|
|
11
|
+
? new TextDecoder().decode(seq)
|
|
12
|
+
: String(seq);
|
|
13
|
+
|
|
14
|
+
const keys = [];
|
|
15
|
+
let i = 0;
|
|
16
|
+
|
|
17
|
+
while (i < str.length) {
|
|
18
|
+
const ch = str[i];
|
|
19
|
+
const code = str.charCodeAt(i);
|
|
20
|
+
|
|
21
|
+
if (code === 0x1b) {
|
|
22
|
+
if (str.charCodeAt(i + 1) === 0x7f || str.charCodeAt(i + 1) === 0x08) {
|
|
23
|
+
keys.push('^W');
|
|
24
|
+
i += 2;
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
if (str[i + 1] === '[' || str[i + 1] === 'O') {
|
|
28
|
+
let j = i + 2;
|
|
29
|
+
while (j < str.length && str.charCodeAt(j) >= 0x20 && str.charCodeAt(j) <= 0x3f) {
|
|
30
|
+
j++;
|
|
31
|
+
}
|
|
32
|
+
if (j < str.length) {
|
|
33
|
+
const body = str.slice(i + 2, j + 1);
|
|
34
|
+
const map = {
|
|
35
|
+
A: 'ArrowUp',
|
|
36
|
+
B: 'ArrowDown',
|
|
37
|
+
C: 'ArrowRight',
|
|
38
|
+
D: 'ArrowLeft',
|
|
39
|
+
H: 'Home',
|
|
40
|
+
F: 'End',
|
|
41
|
+
'1~': 'Home',
|
|
42
|
+
'3~': 'Delete',
|
|
43
|
+
'4~': 'End',
|
|
44
|
+
'5~': 'PageUp',
|
|
45
|
+
'6~': 'PageDown',
|
|
46
|
+
'7~': 'Home',
|
|
47
|
+
'8~': 'End'
|
|
48
|
+
};
|
|
49
|
+
if (map[body]) {
|
|
50
|
+
keys.push(map[body]);
|
|
51
|
+
}
|
|
52
|
+
i = j + 1;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
keys.push('Escape');
|
|
57
|
+
i++;
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (code === 0x0d || code === 0x0a) {
|
|
62
|
+
keys.push('Enter');
|
|
63
|
+
if (code === 0x0d && str[i + 1] === '\n') i++;
|
|
64
|
+
i++;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (code === 0x7f || code === 0x08) {
|
|
68
|
+
keys.push('Backspace');
|
|
69
|
+
i++;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (code === 0x09) {
|
|
73
|
+
keys.push('Tab');
|
|
74
|
+
i++;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (code >= 0x01 && code <= 0x1a) {
|
|
78
|
+
keys.push('^' + String.fromCharCode(code + 0x40));
|
|
79
|
+
i++;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
keys.push(ch);
|
|
83
|
+
i++;
|
|
84
|
+
}
|
|
85
|
+
return keys;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
class TtyStream extends EventEmitter {
|
|
89
|
+
constructor(options = {}) {
|
|
90
|
+
super();
|
|
91
|
+
this.isTTY = options.isTTY !== false;
|
|
92
|
+
this.readable = true;
|
|
93
|
+
this.writable = true;
|
|
94
|
+
this.columns = options.columns || 80;
|
|
95
|
+
this.rows = options.rows || 24;
|
|
96
|
+
this.isRaw = false;
|
|
97
|
+
this._encoding = null;
|
|
98
|
+
this._onWrite = options.onWrite || null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
write(chunk, encoding, callback) {
|
|
102
|
+
if (chunk == null) return true;
|
|
103
|
+
if (typeof this._onWrite === 'function') {
|
|
104
|
+
this._onWrite(chunk);
|
|
105
|
+
}
|
|
106
|
+
this.emit('data', this._formatChunk(chunk));
|
|
107
|
+
if (typeof encoding === 'function') encoding();
|
|
108
|
+
else if (typeof callback === 'function') callback();
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
pushData(chunk) {
|
|
113
|
+
if (chunk == null) return;
|
|
114
|
+
this.emit('data', this._formatChunk(chunk));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
_formatChunk(chunk) {
|
|
118
|
+
if (this._encoding) {
|
|
119
|
+
if (typeof chunk === 'string') return chunk;
|
|
120
|
+
if (chunk instanceof Uint8Array || (typeof Buffer !== 'undefined' && Buffer.isBuffer(chunk))) {
|
|
121
|
+
return new TextDecoder().decode(chunk);
|
|
122
|
+
}
|
|
123
|
+
return String(chunk);
|
|
124
|
+
}
|
|
125
|
+
return chunk;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
setRawMode(flag) {
|
|
129
|
+
this.isRaw = Boolean(flag);
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
setEncoding(enc) {
|
|
134
|
+
this._encoding = enc;
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
getWindowSize() {
|
|
139
|
+
return [this.columns, this.rows];
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
resize(cols, rows) {
|
|
143
|
+
this.columns = cols;
|
|
144
|
+
this.rows = rows;
|
|
145
|
+
this.emit('resize');
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
resume() { return this; }
|
|
149
|
+
pause() { return this; }
|
|
150
|
+
pipe(dest) {
|
|
151
|
+
this.on('data', chunk => dest.write(chunk));
|
|
152
|
+
return dest;
|
|
153
|
+
}
|
|
154
|
+
unpipe() { return this; }
|
|
155
|
+
end(chunk, encoding, cb) {
|
|
156
|
+
if (chunk != null) {
|
|
157
|
+
this.write(chunk, typeof encoding === 'string' ? encoding : undefined);
|
|
158
|
+
}
|
|
159
|
+
this.emit('end');
|
|
160
|
+
if (typeof encoding === 'function') encoding();
|
|
161
|
+
else if (typeof cb === 'function') cb();
|
|
162
|
+
return this;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
destroy() {
|
|
166
|
+
this.removeAllListeners();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
toString() {
|
|
170
|
+
return '';
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
valueOf() {
|
|
174
|
+
return '';
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
module.exports = {
|
|
179
|
+
TtyStream,
|
|
180
|
+
parseVtKeys
|
|
181
|
+
};
|
package/lib/WasmCommand.js
CHANGED
|
@@ -135,7 +135,11 @@ class WasmCommand extends ScriptCommand {
|
|
|
135
135
|
if (outBuf[2]) { this.stdErr = outBuf[2]; outBuf[2] = ''; }
|
|
136
136
|
};
|
|
137
137
|
|
|
138
|
-
const stdinBytes =
|
|
138
|
+
const stdinBytes = typeof this.stdin === 'string'
|
|
139
|
+
? encoder.encode(this.stdin)
|
|
140
|
+
: (this.stdin instanceof Uint8Array
|
|
141
|
+
? this.stdin
|
|
142
|
+
: (typeof Buffer !== 'undefined' && Buffer.isBuffer(this.stdin) ? new Uint8Array(this.stdin) : new Uint8Array(0)));
|
|
139
143
|
let stdinPos = 0;
|
|
140
144
|
|
|
141
145
|
// ── Virtual WASI file descriptor table backed by this.context.fs ──
|
|
@@ -965,10 +969,26 @@ function createGenericWasmCommand(pkgMeta, config = {}, binNameOverride = null)
|
|
|
965
969
|
|
|
966
970
|
try {
|
|
967
971
|
// 1. Direct `.wasm` URL or persisted raw WASI binary -> run via WasmCommand
|
|
968
|
-
if (pkgMeta.type === 'wasm' && (this._persistedWasmBytes || pkgMeta.wasmUrl)
|
|
969
|
-
|
|
970
|
-
if (
|
|
971
|
-
|
|
972
|
+
if ((pkgMeta.type === 'wasm' || pkgMeta.type === 'custom-wasm') && (this._persistedWasmBytes || pkgMeta.wasmUrl)) {
|
|
973
|
+
let bytes = this._persistedWasmBytes;
|
|
974
|
+
if (!bytes && config.moduleLoader) {
|
|
975
|
+
try {
|
|
976
|
+
const loaded = await config.moduleLoader(pkgMeta.name, pkgMeta.cdnUrl);
|
|
977
|
+
if (loaded instanceof Uint8Array || loaded instanceof ArrayBuffer) {
|
|
978
|
+
bytes = loaded instanceof Uint8Array ? loaded : new Uint8Array(loaded);
|
|
979
|
+
} else if (loaded && (loaded.wasmBytes || loaded.wasm || loaded.wasmBinary)) {
|
|
980
|
+
bytes = loaded.wasmBytes || loaded.wasm || loaded.wasmBinary;
|
|
981
|
+
}
|
|
982
|
+
} catch { /* ignore */ }
|
|
983
|
+
}
|
|
984
|
+
if (!bytes && pkgMeta.wasmUrl) {
|
|
985
|
+
bytes = await this._fetchWasmBytes(pkgMeta.wasmUrl);
|
|
986
|
+
}
|
|
987
|
+
if (bytes) {
|
|
988
|
+
if (isHelp) this._inspectedWasm = inspectWasmBytes(bytes);
|
|
989
|
+
await this._runRawWasmBytes(bytes, args);
|
|
990
|
+
return;
|
|
991
|
+
}
|
|
972
992
|
} else {
|
|
973
993
|
const cdnUrl = pkgMeta.cdnUrl || pkgMeta.wasmUrl || `https://esm.sh/${pkgMeta.name}`;
|
|
974
994
|
const origFetch = typeof globalThis.fetch === 'function' ? globalThis.fetch : null;
|
|
@@ -1495,3 +1515,4 @@ WasmCommand.createGenericWasmCommand = createGenericWasmCommand;
|
|
|
1495
1515
|
WasmCommand.createCommand = createGenericWasmCommand;
|
|
1496
1516
|
|
|
1497
1517
|
module.exports = WasmCommand;
|
|
1518
|
+
|
package/lib/builtins/cat.js
CHANGED
|
@@ -30,7 +30,7 @@ class Cat extends Command {
|
|
|
30
30
|
const parts = [];
|
|
31
31
|
|
|
32
32
|
if (paths.length === 0) {
|
|
33
|
-
const stdinContent = this.stdin;
|
|
33
|
+
const stdinContent = typeof this.stdin === 'string' ? this.stdin : null;
|
|
34
34
|
if (stdinContent == null) {
|
|
35
35
|
this.stdErr = 'cat: no file specified';
|
|
36
36
|
this.exitCode = 1;
|
|
@@ -40,7 +40,7 @@ class Cat extends Command {
|
|
|
40
40
|
} else {
|
|
41
41
|
for (const path of paths) {
|
|
42
42
|
if (path === '-') {
|
|
43
|
-
parts.push(this.stdin
|
|
43
|
+
parts.push(typeof this.stdin === 'string' ? this.stdin : '');
|
|
44
44
|
continue;
|
|
45
45
|
}
|
|
46
46
|
const file = this.fs.getFileByPath(path);
|
package/lib/builtins/grep.js
CHANGED
|
@@ -87,7 +87,7 @@ class Grep extends Command {
|
|
|
87
87
|
if (patternFile !== null) {
|
|
88
88
|
let pfContent = '';
|
|
89
89
|
if (patternFile === '-') {
|
|
90
|
-
pfContent = this.stdin
|
|
90
|
+
pfContent = typeof this.stdin === 'string' ? this.stdin : '';
|
|
91
91
|
} else {
|
|
92
92
|
const pf = this.fs.getFileByPath(patternFile);
|
|
93
93
|
if (pf === null || pf === undefined) {
|
|
@@ -136,7 +136,7 @@ class Grep extends Command {
|
|
|
136
136
|
// Collect input sources: {label, content}
|
|
137
137
|
const sources = [];
|
|
138
138
|
let hasError = false;
|
|
139
|
-
const stdinContent = this.stdin
|
|
139
|
+
const stdinContent = typeof this.stdin === 'string' ? this.stdin : '';
|
|
140
140
|
|
|
141
141
|
const collectFiles = (p, topLevel = false) => {
|
|
142
142
|
const entry = this.fs.getFileByPath(p);
|
package/lib/local/clear.js
CHANGED
|
@@ -0,0 +1,325 @@
|
|
|
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
|
+
import { sanitizeUrl, DEFAULT_URL_WHITELIST } from './VtParser.js';
|
|
14
|
+
|
|
15
|
+
const ANSI_COLOR_NAMES = [
|
|
16
|
+
'ansi-black',
|
|
17
|
+
'ansi-red',
|
|
18
|
+
'ansi-green',
|
|
19
|
+
'ansi-yellow',
|
|
20
|
+
'ansi-blue',
|
|
21
|
+
'ansi-magenta',
|
|
22
|
+
'ansi-cyan',
|
|
23
|
+
'ansi-white',
|
|
24
|
+
'ansi-bright-black',
|
|
25
|
+
'ansi-bright-red',
|
|
26
|
+
'ansi-bright-green',
|
|
27
|
+
'ansi-bright-yellow',
|
|
28
|
+
'ansi-bright-blue',
|
|
29
|
+
'ansi-bright-magenta',
|
|
30
|
+
'ansi-bright-cyan',
|
|
31
|
+
'ansi-bright-white'
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
const CUBE_STEPS = [0, 95, 135, 175, 215, 255];
|
|
35
|
+
|
|
36
|
+
function colorIndexToCss(idx) {
|
|
37
|
+
if (typeof idx === 'string') return idx;
|
|
38
|
+
if (typeof idx !== 'number' || idx < 16 || idx > 255) return null;
|
|
39
|
+
if (idx >= 232) {
|
|
40
|
+
const v = 8 + (idx - 232) * 10;
|
|
41
|
+
return `rgb(${v}, ${v}, ${v})`;
|
|
42
|
+
}
|
|
43
|
+
const n = idx - 16;
|
|
44
|
+
const r = CUBE_STEPS[Math.floor(n / 36) % 6];
|
|
45
|
+
const g = CUBE_STEPS[Math.floor(n / 6) % 6];
|
|
46
|
+
const b = CUBE_STEPS[n % 6];
|
|
47
|
+
return `rgb(${r}, ${g}, ${b})`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function escapeHtml(str) {
|
|
51
|
+
return str
|
|
52
|
+
.replace(/&/g, '&')
|
|
53
|
+
.replace(/</g, '<')
|
|
54
|
+
.replace(/>/g, '>')
|
|
55
|
+
.replace(/"/g, '"');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const AUTO_URL_REGEX = /(?:https?:\/\/|mailto:|tel:)[^\s<>"'`]+/g;
|
|
59
|
+
|
|
60
|
+
function trimUrlTrailingPunct(url) {
|
|
61
|
+
let end = url.length;
|
|
62
|
+
while (end > 0) {
|
|
63
|
+
const ch = url[end - 1];
|
|
64
|
+
if ('.!?,;:'.includes(ch)) {
|
|
65
|
+
end--;
|
|
66
|
+
} else if (ch === ')') {
|
|
67
|
+
const slice = url.slice(0, end);
|
|
68
|
+
const opens = (slice.match(/\(/g) || []).length;
|
|
69
|
+
const closes = (slice.match(/\)/g) || []).length;
|
|
70
|
+
if (closes > opens) end--;
|
|
71
|
+
else break;
|
|
72
|
+
} else if (ch === ']') {
|
|
73
|
+
const slice = url.slice(0, end);
|
|
74
|
+
const opens = (slice.match(/\[/g) || []).length;
|
|
75
|
+
const closes = (slice.match(/\]/g) || []).length;
|
|
76
|
+
if (closes > opens) end--;
|
|
77
|
+
else break;
|
|
78
|
+
} else {
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return url.slice(0, end);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function detectRowLinks(rowCells, whitelist = DEFAULT_URL_WHITELIST) {
|
|
86
|
+
let lineStr = '';
|
|
87
|
+
for (let c = 0; c < rowCells.length; c++) {
|
|
88
|
+
lineStr += rowCells[c].ch || ' ';
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const links = new Array(rowCells.length).fill(null);
|
|
92
|
+
for (let c = 0; c < rowCells.length; c++) {
|
|
93
|
+
if (rowCells[c].link) {
|
|
94
|
+
links[c] = rowCells[c].link;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
AUTO_URL_REGEX.lastIndex = 0;
|
|
99
|
+
let match;
|
|
100
|
+
while ((match = AUTO_URL_REGEX.exec(lineStr)) !== null) {
|
|
101
|
+
const cleaned = trimUrlTrailingPunct(match[0]);
|
|
102
|
+
const valid = sanitizeUrl(cleaned, whitelist);
|
|
103
|
+
if (!valid) continue;
|
|
104
|
+
const start = match.index;
|
|
105
|
+
const end = start + cleaned.length;
|
|
106
|
+
for (let c = start; c < end && c < links.length; c++) {
|
|
107
|
+
if (!links[c]) {
|
|
108
|
+
links[c] = valid;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return links;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
class DomRenderer {
|
|
116
|
+
constructor(container, screen, options = {}) {
|
|
117
|
+
this.container = container;
|
|
118
|
+
this.screen = screen;
|
|
119
|
+
this.cursorStyle = options.cursor || 'none';
|
|
120
|
+
this.urlWhitelist = options.urlWhitelist || DEFAULT_URL_WHITELIST;
|
|
121
|
+
this._rowElements = [];
|
|
122
|
+
this._rafId = null;
|
|
123
|
+
this._lastCursorRow = -1;
|
|
124
|
+
this._lastCursorCol = -1;
|
|
125
|
+
this._lastCursorVisible = false;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
scheduleRender() {
|
|
129
|
+
if (this._rafId !== null) return;
|
|
130
|
+
if (typeof requestAnimationFrame === 'function') {
|
|
131
|
+
this._rafId = requestAnimationFrame(() => {
|
|
132
|
+
this._rafId = null;
|
|
133
|
+
this.flushRender();
|
|
134
|
+
});
|
|
135
|
+
} else {
|
|
136
|
+
this.flushRender();
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
flushRender() {
|
|
141
|
+
if (this._rafId !== null && typeof cancelAnimationFrame === 'function') {
|
|
142
|
+
cancelAnimationFrame(this._rafId);
|
|
143
|
+
this._rafId = null;
|
|
144
|
+
}
|
|
145
|
+
if (!this.container || typeof document === 'undefined') return;
|
|
146
|
+
|
|
147
|
+
const rows = this.screen.rows;
|
|
148
|
+
this._ensureRowElements(rows);
|
|
149
|
+
|
|
150
|
+
const showCursor = this.cursorStyle !== 'none'
|
|
151
|
+
&& this.screen.cursorVisible
|
|
152
|
+
&& this.screen.viewportOffset === 0;
|
|
153
|
+
const curRow = showCursor ? this.screen.cursorRow : -1;
|
|
154
|
+
const curCol = showCursor ? this.screen.cursorCol : -1;
|
|
155
|
+
|
|
156
|
+
if (this._lastCursorRow !== curRow || this._lastCursorCol !== curCol || this._lastCursorVisible !== showCursor) {
|
|
157
|
+
if (this._lastCursorRow >= 0 && this._lastCursorRow < rows) {
|
|
158
|
+
this.screen.dirtyRows.add(this._lastCursorRow);
|
|
159
|
+
}
|
|
160
|
+
if (curRow >= 0 && curRow < rows) {
|
|
161
|
+
this.screen.dirtyRows.add(curRow);
|
|
162
|
+
}
|
|
163
|
+
this._lastCursorRow = curRow;
|
|
164
|
+
this._lastCursorCol = curCol;
|
|
165
|
+
this._lastCursorVisible = showCursor;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
for (const r of this.screen.dirtyRows) {
|
|
169
|
+
if (r >= 0 && r < rows) {
|
|
170
|
+
this._renderRow(r, r === curRow ? curCol : -1);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
this.screen.dirtyRows.clear();
|
|
174
|
+
this.screen.fullRedrawNeeded = false;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
_ensureRowElements(count) {
|
|
178
|
+
while (this._rowElements.length < count) {
|
|
179
|
+
const el = document.createElement('li');
|
|
180
|
+
if (el.classList) {
|
|
181
|
+
el.classList.add('terminal__line');
|
|
182
|
+
el.classList.add('terminal__row');
|
|
183
|
+
}
|
|
184
|
+
this.container.appendChild(el);
|
|
185
|
+
this._rowElements.push(el);
|
|
186
|
+
}
|
|
187
|
+
while (this._rowElements.length > count) {
|
|
188
|
+
const removed = this._rowElements.pop();
|
|
189
|
+
if (removed && removed.parentNode === this.container && this.container.removeChild) {
|
|
190
|
+
this.container.removeChild(removed);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
_renderRow(rowIndex, cursorCol) {
|
|
196
|
+
const rowEl = this._rowElements[rowIndex];
|
|
197
|
+
const cells = this.screen.getVisibleRow(rowIndex);
|
|
198
|
+
if (!rowEl || !cells) return;
|
|
199
|
+
|
|
200
|
+
const links = detectRowLinks(cells, this.urlWhitelist);
|
|
201
|
+
|
|
202
|
+
// Determine rightmost meaningful cell so we don't emit trailing unstyled spaces
|
|
203
|
+
// unless in alt screen or up to the cursor column.
|
|
204
|
+
let lastCol = this.screen.isAltScreen ? cells.length - 1 : -1;
|
|
205
|
+
if (!this.screen.isAltScreen) {
|
|
206
|
+
for (let c = cells.length - 1; c >= 0; c--) {
|
|
207
|
+
const cell = cells[c];
|
|
208
|
+
if (cell.ch !== ' ' || cell.fg !== null || cell.bg !== null || cell.flags !== 0 || links[c] !== null) {
|
|
209
|
+
lastCol = c;
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (cursorCol > lastCol) {
|
|
214
|
+
lastCol = cursorCol;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (lastCol < 0) {
|
|
219
|
+
rowEl.innerHTML = '';
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
let html = '';
|
|
224
|
+
let runText = '';
|
|
225
|
+
let runKey = null;
|
|
226
|
+
let runCell = null;
|
|
227
|
+
let runLink = null;
|
|
228
|
+
let runIsCursor = false;
|
|
229
|
+
|
|
230
|
+
const flushRun = () => {
|
|
231
|
+
if (!runText) return;
|
|
232
|
+
html += this._formatRun(runText, runCell, runLink, runIsCursor);
|
|
233
|
+
runText = '';
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
for (let c = 0; c <= lastCol; c++) {
|
|
237
|
+
const cell = cells[c];
|
|
238
|
+
const link = links[c];
|
|
239
|
+
const isCursor = c === cursorCol;
|
|
240
|
+
const key = `${cell.fg ?? ''}|${cell.bg ?? ''}|${cell.flags}|${link ?? ''}|${isCursor ? 1 : 0}`;
|
|
241
|
+
|
|
242
|
+
if (key !== runKey) {
|
|
243
|
+
flushRun();
|
|
244
|
+
runKey = key;
|
|
245
|
+
runCell = cell;
|
|
246
|
+
runLink = link;
|
|
247
|
+
runIsCursor = isCursor;
|
|
248
|
+
}
|
|
249
|
+
runText += cell.ch || ' ';
|
|
250
|
+
}
|
|
251
|
+
flushRun();
|
|
252
|
+
|
|
253
|
+
rowEl.innerHTML = html;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
_formatRun(text, cell, link, isCursor) {
|
|
257
|
+
const classes = [];
|
|
258
|
+
const styles = [];
|
|
259
|
+
const flags = cell.flags || 0;
|
|
260
|
+
const inverse = (flags & ATTR_INVERSE) !== 0;
|
|
261
|
+
|
|
262
|
+
const fg = inverse ? cell.bg : cell.fg;
|
|
263
|
+
const bg = inverse ? cell.fg : cell.bg;
|
|
264
|
+
|
|
265
|
+
if (inverse && cell.fg === null && cell.bg === null) {
|
|
266
|
+
classes.push('terminal__inverse');
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (typeof fg === 'number' && fg >= 0 && fg < 16) {
|
|
270
|
+
classes.push(`${ANSI_COLOR_NAMES[fg]}-fg`);
|
|
271
|
+
} else if (fg !== null) {
|
|
272
|
+
const css = colorIndexToCss(fg);
|
|
273
|
+
if (css) styles.push(`color:${css}`);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (typeof bg === 'number' && bg >= 0 && bg < 16) {
|
|
277
|
+
classes.push(`${ANSI_COLOR_NAMES[bg]}-bg`);
|
|
278
|
+
} else if (bg !== null) {
|
|
279
|
+
const css = colorIndexToCss(bg);
|
|
280
|
+
if (css) styles.push(`background-color:${css}`);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if (flags & ATTR_BOLD) styles.push('font-weight:bold');
|
|
284
|
+
if (flags & ATTR_DIM) styles.push('opacity:0.65');
|
|
285
|
+
if (flags & ATTR_ITALIC) styles.push('font-style:italic');
|
|
286
|
+
if ((flags & ATTR_UNDERLINE) && (flags & ATTR_STRIKE)) {
|
|
287
|
+
styles.push('text-decoration:underline line-through');
|
|
288
|
+
} else if (flags & ATTR_UNDERLINE) {
|
|
289
|
+
styles.push('text-decoration:underline');
|
|
290
|
+
} else if (flags & ATTR_STRIKE) {
|
|
291
|
+
styles.push('text-decoration:line-through');
|
|
292
|
+
}
|
|
293
|
+
if (flags & ATTR_BLINK) classes.push('terminal__blink');
|
|
294
|
+
if (flags & ATTR_HIDDEN) styles.push('visibility:hidden');
|
|
295
|
+
|
|
296
|
+
if (isCursor) {
|
|
297
|
+
classes.push('terminal__cursor');
|
|
298
|
+
classes.push(this.cursorStyle);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const escapedText = escapeHtml(text);
|
|
302
|
+
let inner = escapedText;
|
|
303
|
+
|
|
304
|
+
if (classes.length > 0 || styles.length > 0) {
|
|
305
|
+
const classAttr = classes.length > 0 ? ` class="${classes.join(' ')}"` : '';
|
|
306
|
+
const styleAttr = styles.length > 0 ? ` style="${styles.join(';')}"` : '';
|
|
307
|
+
inner = `<span${classAttr}${styleAttr}>${escapedText}</span>`;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if (link) {
|
|
311
|
+
const safeHref = escapeHtml(link);
|
|
312
|
+
return `<a href="${safeHref}" target="_blank" rel="noopener noreferrer" class="terminal__link">${inner}</a>`;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return inner;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export {
|
|
320
|
+
DomRenderer,
|
|
321
|
+
detectRowLinks,
|
|
322
|
+
trimUrlTrailingPunct,
|
|
323
|
+
colorIndexToCss
|
|
324
|
+
};
|
|
325
|
+
export default DomRenderer;
|