@innovatorved/code-lab 0.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/ARCHITECTURE.md +114 -0
- package/LICENSE +21 -0
- package/README.md +156 -0
- package/dist/code-lab.css +4100 -0
- package/dist/code-lab.d.ts +348 -0
- package/dist/code-lab.esm.js +21954 -0
- package/dist/code-lab.js +21933 -0
- package/dist/extensions/guided-lab.d.ts +13 -0
- package/dist/extensions/guided-lab.esm.js +4218 -0
- package/dist/extensions/guided-lab.js +4236 -0
- package/dist/index.d.ts +11 -0
- package/dist/preview-sw.js +149 -0
- package/dist/runtime.d.ts +48 -0
- package/dist/runtime.esm.js +7510 -0
- package/dist/runtime.js +7491 -0
- package/dist/sample.html +304 -0
- package/dist/schemas.d.ts +514 -0
- package/dist/shell.d.ts +66 -0
- package/dist/shell.esm.js +6405 -0
- package/dist/shell.js +6384 -0
- package/dist/terminal.d.ts +102 -0
- package/dist/terminal.esm.js +1083 -0
- package/dist/terminal.js +1072 -0
- package/dist/workbench.d.ts +1 -0
- package/package.json +98 -0
- package/src/code-lab.css +4100 -0
- package/src/extensions/guided-lab.ts +54 -0
- package/src/index.ts +28 -0
- package/src/preview-sw.js +149 -0
- package/src/runtime.js +7465 -0
- package/src/runtime.ts +7512 -0
- package/src/sample.html +304 -0
- package/src/schemas.ts +177 -0
- package/src/shell.js +6358 -0
- package/src/shell.ts +6456 -0
- package/src/terminal.js +1046 -0
- package/src/terminal.ts +1097 -0
- package/src/workbench.js +6987 -0
- package/src/workbench.ts +6988 -0
package/src/terminal.js
ADDED
|
@@ -0,0 +1,1046 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @innovatorved/code-lab - Pure-JS PTY Terminal Emulator Component
|
|
3
|
+
*
|
|
4
|
+
* High-fidelity, self-contained terminal emulator engine providing:
|
|
5
|
+
* - Inline prompt with cwd awareness (codelab:${cwd}$ )
|
|
6
|
+
* - Interactive REPL sub-mode (> ) with .exit and Ctrl+D/Ctrl+C
|
|
7
|
+
* - Character-by-character editing with cursor navigation:
|
|
8
|
+
* ArrowLeft, ArrowRight, Home, End, Ctrl+A, Ctrl+E, Ctrl+U, Ctrl+K, Backspace, Delete
|
|
9
|
+
* - Tab auto-completion for built-in commands and VFS filesystem paths
|
|
10
|
+
* - History navigation (ArrowUp, ArrowDown)
|
|
11
|
+
* - ANSI 16/256 SGR escape sequence translation to styled Geist HTML spans
|
|
12
|
+
* - Headless DOM safety (defensive guards for Node.js test environments)
|
|
13
|
+
*
|
|
14
|
+
* (C) 2026 InnovatorVed / CodeLab. MIT License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
(function (root, factory) {
|
|
18
|
+
const exports = factory();
|
|
19
|
+
if (root) {
|
|
20
|
+
root.CodeLabTerminal = exports;
|
|
21
|
+
root.PTYTerminalEngine = exports.PTYTerminalEngine;
|
|
22
|
+
root.createPTYTerminalEngine = exports.createPTYTerminalEngine;
|
|
23
|
+
root.createTerminal = exports.createTerminal;
|
|
24
|
+
root.ansiToHtml = exports.ansiToHtml;
|
|
25
|
+
root.parseAnsiToHtml = exports.parseAnsiToHtml;
|
|
26
|
+
}
|
|
27
|
+
if (typeof module !== 'undefined' && module.exports) {
|
|
28
|
+
module.exports = exports;
|
|
29
|
+
} else if (typeof define === 'function' && define.amd) {
|
|
30
|
+
define([], function () { return exports; });
|
|
31
|
+
}
|
|
32
|
+
})(typeof globalThis !== 'undefined' ? globalThis : (typeof window !== 'undefined' ? window : this), function () {
|
|
33
|
+
'use strict';
|
|
34
|
+
|
|
35
|
+
const ANSI_CODE_MAP = {
|
|
36
|
+
1: 'cl-ansi-bold',
|
|
37
|
+
2: 'cl-ansi-dim',
|
|
38
|
+
3: 'cl-ansi-italic',
|
|
39
|
+
4: 'cl-ansi-underline',
|
|
40
|
+
30: 'cl-ansi-black',
|
|
41
|
+
31: 'cl-ansi-red',
|
|
42
|
+
32: 'cl-ansi-green',
|
|
43
|
+
33: 'cl-ansi-yellow',
|
|
44
|
+
34: 'cl-ansi-blue',
|
|
45
|
+
35: 'cl-ansi-magenta',
|
|
46
|
+
36: 'cl-ansi-cyan',
|
|
47
|
+
37: 'cl-ansi-white',
|
|
48
|
+
90: 'cl-ansi-gray',
|
|
49
|
+
91: 'cl-ansi-bright-red',
|
|
50
|
+
92: 'cl-ansi-bright-green',
|
|
51
|
+
93: 'cl-ansi-bright-yellow',
|
|
52
|
+
94: 'cl-ansi-bright-blue',
|
|
53
|
+
95: 'cl-ansi-bright-magenta',
|
|
54
|
+
96: 'cl-ansi-bright-cyan',
|
|
55
|
+
97: 'cl-ansi-bright-white',
|
|
56
|
+
40: 'cl-ansi-bg-black',
|
|
57
|
+
41: 'cl-ansi-bg-red',
|
|
58
|
+
42: 'cl-ansi-bg-green',
|
|
59
|
+
43: 'cl-ansi-bg-yellow',
|
|
60
|
+
44: 'cl-ansi-bg-blue',
|
|
61
|
+
45: 'cl-ansi-bg-magenta',
|
|
62
|
+
46: 'cl-ansi-bg-cyan',
|
|
63
|
+
47: 'cl-ansi-bg-white',
|
|
64
|
+
100: 'cl-ansi-bg-bright-black',
|
|
65
|
+
101: 'cl-ansi-bg-bright-red',
|
|
66
|
+
102: 'cl-ansi-bg-bright-green',
|
|
67
|
+
103: 'cl-ansi-bg-bright-yellow',
|
|
68
|
+
104: 'cl-ansi-bg-bright-blue',
|
|
69
|
+
105: 'cl-ansi-bg-bright-magenta',
|
|
70
|
+
106: 'cl-ansi-bg-bright-cyan',
|
|
71
|
+
107: 'cl-ansi-bg-bright-white'
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const ANSI_256_PALETTE = [
|
|
75
|
+
'#000000', '#cd3131', '#0dbc79', '#e5e510', '#2472c8', '#bc3fbc', '#11a8cd', '#e5e5e5',
|
|
76
|
+
'#666666', '#f14c4c', '#23d18b', '#f5f543', '#3b8eea', '#d670d6', '#29b8db', '#ffffff'
|
|
77
|
+
];
|
|
78
|
+
|
|
79
|
+
function ansi256ToColor(n) {
|
|
80
|
+
const idx = Number(n);
|
|
81
|
+
if (!Number.isFinite(idx) || idx < 0 || idx > 255) return null;
|
|
82
|
+
if (idx < 16) return ANSI_256_PALETTE[idx];
|
|
83
|
+
if (idx < 232) {
|
|
84
|
+
const c = idx - 16;
|
|
85
|
+
const r = Math.floor(c / 36);
|
|
86
|
+
const g = Math.floor((c % 36) / 6);
|
|
87
|
+
const b = c % 6;
|
|
88
|
+
const toChannel = v => (v === 0 ? 0 : 55 + v * 40);
|
|
89
|
+
return `rgb(${toChannel(r)}, ${toChannel(g)}, ${toChannel(b)})`;
|
|
90
|
+
}
|
|
91
|
+
const gray = 8 + (idx - 232) * 10;
|
|
92
|
+
return `rgb(${gray}, ${gray}, ${gray})`;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function escapeHtml(str) {
|
|
96
|
+
if (str == null) return '';
|
|
97
|
+
return String(str)
|
|
98
|
+
.replace(/&/g, '&')
|
|
99
|
+
.replace(/</g, '<')
|
|
100
|
+
.replace(/>/g, '>')
|
|
101
|
+
.replace(/"/g, '"')
|
|
102
|
+
.replace(/'/g, ''');
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function resolveAnsiControlCodes(raw) {
|
|
106
|
+
if (!raw) return '';
|
|
107
|
+
const s = String(raw);
|
|
108
|
+
if (!s.includes('\x1b') && !s.includes('\r')) return s;
|
|
109
|
+
|
|
110
|
+
let text = s;
|
|
111
|
+
const lastClearScreen = text.lastIndexOf('\x1b[2J');
|
|
112
|
+
if (lastClearScreen !== -1) {
|
|
113
|
+
text = text.slice(lastClearScreen + 4);
|
|
114
|
+
text = text.replace(/^\x1b\[(?:1;1)?H/, '');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
text = text.replace(/\r\n/g, '\n');
|
|
118
|
+
const rawLines = text.split('\n');
|
|
119
|
+
const resolvedLines = [];
|
|
120
|
+
|
|
121
|
+
for (let l = 0; l < rawLines.length; l++) {
|
|
122
|
+
let line = rawLines[l];
|
|
123
|
+
|
|
124
|
+
while (/\x1b\[(?:1)?A/.test(line)) {
|
|
125
|
+
const upMatch = /\x1b\[(?:1)?A/.exec(line);
|
|
126
|
+
const beforeUp = line.slice(0, upMatch.index);
|
|
127
|
+
let afterUp = line.slice(upMatch.index + upMatch[0].length);
|
|
128
|
+
if (resolvedLines.length > 0) {
|
|
129
|
+
if (afterUp.includes('\x1b[2K')) {
|
|
130
|
+
afterUp = afterUp.slice(afterUp.lastIndexOf('\x1b[2K') + 4);
|
|
131
|
+
resolvedLines[resolvedLines.length - 1] = afterUp;
|
|
132
|
+
} else if (afterUp.includes('\r')) {
|
|
133
|
+
const parts = afterUp.split('\r');
|
|
134
|
+
resolvedLines[resolvedLines.length - 1] = parts[parts.length - 1];
|
|
135
|
+
} else {
|
|
136
|
+
resolvedLines[resolvedLines.length - 1] = afterUp;
|
|
137
|
+
}
|
|
138
|
+
line = beforeUp;
|
|
139
|
+
} else {
|
|
140
|
+
line = beforeUp + afterUp;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (line.includes('\x1b[2K')) {
|
|
145
|
+
const idx = line.lastIndexOf('\x1b[2K');
|
|
146
|
+
line = line.slice(idx + 4);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (line.includes('\r')) {
|
|
150
|
+
const parts = line.split('\r');
|
|
151
|
+
line = parts[parts.length - 1];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
line = line.replace(/\x1b\[[0]?K/g, '');
|
|
155
|
+
line = line.replace(/\x1b\[(?:1)?G/g, '');
|
|
156
|
+
resolvedLines.push(line);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return resolvedLines.join('\n');
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function ansiToHtml(str) {
|
|
163
|
+
if (!str) return '';
|
|
164
|
+
const resolved = resolveAnsiControlCodes(String(str));
|
|
165
|
+
if (!resolved.includes('\x1b[')) {
|
|
166
|
+
return escapeHtml(resolved);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Strip non-color cursor control sequences without stripping lowercase 'm' SGR codes
|
|
170
|
+
const cleaned = resolved.replace(/\x1b\[[0-9;?]*(?!m)[A-Za-z]/g, '');
|
|
171
|
+
|
|
172
|
+
const tokenRegex = /\x1b\[([0-9;]*)m/g;
|
|
173
|
+
let result = '';
|
|
174
|
+
let lastIdx = 0;
|
|
175
|
+
let match;
|
|
176
|
+
|
|
177
|
+
const state = {
|
|
178
|
+
bold: false,
|
|
179
|
+
dim: false,
|
|
180
|
+
italic: false,
|
|
181
|
+
underline: false,
|
|
182
|
+
fgClass: null,
|
|
183
|
+
bgClass: null,
|
|
184
|
+
fgColor: null,
|
|
185
|
+
bgColor: null
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
const resetState = () => {
|
|
189
|
+
state.bold = false;
|
|
190
|
+
state.dim = false;
|
|
191
|
+
state.italic = false;
|
|
192
|
+
state.underline = false;
|
|
193
|
+
state.fgClass = null;
|
|
194
|
+
state.bgClass = null;
|
|
195
|
+
state.fgColor = null;
|
|
196
|
+
state.bgColor = null;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const renderChunk = (rawText) => {
|
|
200
|
+
if (!rawText) return '';
|
|
201
|
+
const escaped = escapeHtml(rawText);
|
|
202
|
+
const classes = [];
|
|
203
|
+
if (state.bold) classes.push('cl-ansi-bold');
|
|
204
|
+
if (state.dim) classes.push('cl-ansi-dim');
|
|
205
|
+
if (state.italic) classes.push('cl-ansi-italic');
|
|
206
|
+
if (state.underline) classes.push('cl-ansi-underline');
|
|
207
|
+
if (state.fgClass) classes.push(state.fgClass);
|
|
208
|
+
if (state.bgClass) classes.push(state.bgClass);
|
|
209
|
+
|
|
210
|
+
const styles = [];
|
|
211
|
+
if (state.fgColor) styles.push(`color:${state.fgColor}`);
|
|
212
|
+
if (state.bgColor) styles.push(`background-color:${state.bgColor}`);
|
|
213
|
+
|
|
214
|
+
if (classes.length === 0 && styles.length === 0) {
|
|
215
|
+
return escaped;
|
|
216
|
+
}
|
|
217
|
+
const classAttr = classes.length > 0 ? ` class="${classes.join(' ')}"` : '';
|
|
218
|
+
const styleAttr = styles.length > 0 ? ` style="${styles.join(';')}"` : '';
|
|
219
|
+
return `<span${classAttr}${styleAttr}>${escaped}</span>`;
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
while ((match = tokenRegex.exec(cleaned)) !== null) {
|
|
223
|
+
const rawText = cleaned.slice(lastIdx, match.index);
|
|
224
|
+
if (rawText) result += renderChunk(rawText);
|
|
225
|
+
lastIdx = tokenRegex.lastIndex;
|
|
226
|
+
|
|
227
|
+
const codes = match[1] ? match[1].split(';').map(Number) : [0];
|
|
228
|
+
for (let i = 0; i < codes.length; i++) {
|
|
229
|
+
const code = codes[i];
|
|
230
|
+
if (code === 0 || Number.isNaN(code)) {
|
|
231
|
+
resetState();
|
|
232
|
+
} else if (code === 1) {
|
|
233
|
+
state.bold = true;
|
|
234
|
+
} else if (code === 2) {
|
|
235
|
+
state.dim = true;
|
|
236
|
+
} else if (code === 3) {
|
|
237
|
+
state.italic = true;
|
|
238
|
+
} else if (code === 4) {
|
|
239
|
+
state.underline = true;
|
|
240
|
+
} else if (code === 22) {
|
|
241
|
+
state.bold = false;
|
|
242
|
+
state.dim = false;
|
|
243
|
+
} else if (code === 23) {
|
|
244
|
+
state.italic = false;
|
|
245
|
+
} else if (code === 24) {
|
|
246
|
+
state.underline = false;
|
|
247
|
+
} else if (code === 39) {
|
|
248
|
+
state.fgClass = null;
|
|
249
|
+
state.fgColor = null;
|
|
250
|
+
} else if (code === 49) {
|
|
251
|
+
state.bgClass = null;
|
|
252
|
+
state.bgColor = null;
|
|
253
|
+
} else if ((code === 38 || code === 48) && codes[i + 1] === 5 && i + 2 < codes.length) {
|
|
254
|
+
const color = ansi256ToColor(codes[i + 2]);
|
|
255
|
+
if (color) {
|
|
256
|
+
if (code === 38) {
|
|
257
|
+
state.fgClass = null;
|
|
258
|
+
state.fgColor = color;
|
|
259
|
+
} else {
|
|
260
|
+
state.bgClass = null;
|
|
261
|
+
state.bgColor = color;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
i += 2;
|
|
265
|
+
} else if ((code === 38 || code === 48) && codes[i + 1] === 2 && i + 4 < codes.length) {
|
|
266
|
+
const r = Math.max(0, Math.min(255, Number(codes[i + 2]) || 0));
|
|
267
|
+
const g = Math.max(0, Math.min(255, Number(codes[i + 3]) || 0));
|
|
268
|
+
const b = Math.max(0, Math.min(255, Number(codes[i + 4]) || 0));
|
|
269
|
+
const color = `rgb(${r}, ${g}, ${b})`;
|
|
270
|
+
if (code === 38) {
|
|
271
|
+
state.fgClass = null;
|
|
272
|
+
state.fgColor = color;
|
|
273
|
+
} else {
|
|
274
|
+
state.bgClass = null;
|
|
275
|
+
state.bgColor = color;
|
|
276
|
+
}
|
|
277
|
+
i += 4;
|
|
278
|
+
} else if ((code >= 30 && code <= 37) || (code >= 90 && code <= 97)) {
|
|
279
|
+
state.fgClass = ANSI_CODE_MAP[code] || null;
|
|
280
|
+
state.fgColor = null;
|
|
281
|
+
} else if ((code >= 40 && code <= 47) || (code >= 100 && code <= 107)) {
|
|
282
|
+
state.bgClass = ANSI_CODE_MAP[code] || null;
|
|
283
|
+
state.bgColor = null;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const remaining = cleaned.slice(lastIdx);
|
|
289
|
+
if (remaining) result += renderChunk(remaining);
|
|
290
|
+
return result;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const parseAnsiToHtml = ansiToHtml;
|
|
294
|
+
|
|
295
|
+
class PTYTerminalEngine {
|
|
296
|
+
constructor(container, options = {}) {
|
|
297
|
+
if (
|
|
298
|
+
container &&
|
|
299
|
+
typeof container === 'object' &&
|
|
300
|
+
typeof container.querySelector !== 'function' &&
|
|
301
|
+
typeof container.appendChild !== 'function' &&
|
|
302
|
+
arguments.length === 1
|
|
303
|
+
) {
|
|
304
|
+
options = container;
|
|
305
|
+
container = options.container || null;
|
|
306
|
+
}
|
|
307
|
+
this.container = container || null;
|
|
308
|
+
this.options = options || {};
|
|
309
|
+
this.logs = Array.isArray(this.options.logs) ? this.options.logs : [];
|
|
310
|
+
this.cwd = this.options.cwd || '/workspace';
|
|
311
|
+
this.replMode = Boolean(this.options.replMode);
|
|
312
|
+
this.promptOverride = this.options.prompt || null;
|
|
313
|
+
this.history = Array.isArray(this.options.history) ? this.options.history : [];
|
|
314
|
+
this.historyIdx = -1;
|
|
315
|
+
this.savedBuffer = '';
|
|
316
|
+
this.buffer = '';
|
|
317
|
+
this.cursor = 0;
|
|
318
|
+
this.searchMode = false;
|
|
319
|
+
this.searchQuery = '';
|
|
320
|
+
this.searchMatchIdx = -1;
|
|
321
|
+
this._readLineQueue = [];
|
|
322
|
+
this.onCommand = typeof this.options.onCommand === 'function' ? this.options.onCommand : null;
|
|
323
|
+
this.getCompletions = typeof this.options.getCompletions === 'function' ? this.options.getCompletions : null;
|
|
324
|
+
this.onKey = typeof this.options.onKey === 'function' ? this.options.onKey : null;
|
|
325
|
+
this.onInterrupt = typeof this.options.onInterrupt === 'function' ? this.options.onInterrupt : null;
|
|
326
|
+
|
|
327
|
+
// Stream & active line buffering state
|
|
328
|
+
this._activeLineCommitted = true;
|
|
329
|
+
this._cursorCol = 0;
|
|
330
|
+
this._pendingEscape = '';
|
|
331
|
+
this._targetLineIdx = -1;
|
|
332
|
+
this._needsRewrite = false;
|
|
333
|
+
this._activeLineEl = null;
|
|
334
|
+
|
|
335
|
+
this._mounted = false;
|
|
336
|
+
this._listeners = [];
|
|
337
|
+
|
|
338
|
+
if (this.container) {
|
|
339
|
+
this.mount(this.container);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
getPrompt() {
|
|
344
|
+
if (this.promptOverride) return this.promptOverride;
|
|
345
|
+
if (this.replMode) return '> ';
|
|
346
|
+
const cwd = this.cwd || '/workspace';
|
|
347
|
+
return `codelab:${cwd}$ `;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
setPrompt(p) {
|
|
351
|
+
this.promptOverride = p;
|
|
352
|
+
this.updateActiveLine();
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
setCwd(cwd) {
|
|
356
|
+
this.cwd = cwd;
|
|
357
|
+
this.updateActiveLine();
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
setReplMode(enabled) {
|
|
361
|
+
this.replMode = Boolean(enabled);
|
|
362
|
+
this.updateActiveLine();
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
writeStreamChunk(data, type = 'stdout') {
|
|
366
|
+
let str = String(data != null ? data : '');
|
|
367
|
+
if (!str) return;
|
|
368
|
+
|
|
369
|
+
if (this._pendingEscape) {
|
|
370
|
+
str = this._pendingEscape + str;
|
|
371
|
+
this._pendingEscape = '';
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
let modified = false;
|
|
375
|
+
let i = 0;
|
|
376
|
+
|
|
377
|
+
while (i < str.length) {
|
|
378
|
+
if (str[i] === '\x1b') {
|
|
379
|
+
const rest = str.slice(i);
|
|
380
|
+
if (/^\x1b(?:\[[0-9;?]*)?$/.test(rest)) {
|
|
381
|
+
this._pendingEscape = rest;
|
|
382
|
+
break;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const csiMatch = /^\x1b\[([0-9;?]*)([A-Za-z])/.exec(rest);
|
|
386
|
+
if (csiMatch) {
|
|
387
|
+
const params = csiMatch[1];
|
|
388
|
+
const cmd = csiMatch[2];
|
|
389
|
+
const seq = csiMatch[0];
|
|
390
|
+
i += seq.length;
|
|
391
|
+
|
|
392
|
+
if (cmd === 'K') {
|
|
393
|
+
const cur = this._getActiveLine(type);
|
|
394
|
+
if (params === '2') {
|
|
395
|
+
cur.text = '';
|
|
396
|
+
this._cursorCol = 0;
|
|
397
|
+
this._needsRewrite = false;
|
|
398
|
+
} else {
|
|
399
|
+
cur.text = cur.text.slice(0, this._cursorCol);
|
|
400
|
+
this._needsRewrite = false;
|
|
401
|
+
}
|
|
402
|
+
modified = true;
|
|
403
|
+
} else if (cmd === 'A') {
|
|
404
|
+
const count = parseInt(params, 10) || 1;
|
|
405
|
+
if (this.logs.length > 0) {
|
|
406
|
+
const baseIdx = this._activeLineCommitted ? this.logs.length : this.logs.length - 1;
|
|
407
|
+
this._targetLineIdx = Math.max(0, baseIdx - count);
|
|
408
|
+
this._activeLineCommitted = false;
|
|
409
|
+
this._cursorCol = 0;
|
|
410
|
+
this._needsRewrite = false;
|
|
411
|
+
const targetLine = this.logs[this._targetLineIdx];
|
|
412
|
+
if (targetLine) targetLine.text = '';
|
|
413
|
+
this._activeLineEl = null;
|
|
414
|
+
}
|
|
415
|
+
} else if (cmd === 'G') {
|
|
416
|
+
this._cursorCol = 0;
|
|
417
|
+
this._needsRewrite = true;
|
|
418
|
+
} else if (cmd === 'J' && params === '2') {
|
|
419
|
+
this.clear();
|
|
420
|
+
} else if (cmd === 'm') {
|
|
421
|
+
const cur = this._getActiveLine(type);
|
|
422
|
+
cur.text += seq;
|
|
423
|
+
modified = true;
|
|
424
|
+
}
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const ch = str[i];
|
|
430
|
+
if (ch === '\r') {
|
|
431
|
+
this._cursorCol = 0;
|
|
432
|
+
this._needsRewrite = true;
|
|
433
|
+
i++;
|
|
434
|
+
} else if (ch === '\n') {
|
|
435
|
+
const cur = this._getActiveLine(type);
|
|
436
|
+
cur.text += '\n';
|
|
437
|
+
this._activeLineCommitted = true;
|
|
438
|
+
this._cursorCol = 0;
|
|
439
|
+
this._targetLineIdx = -1;
|
|
440
|
+
this._needsRewrite = false;
|
|
441
|
+
this._activeLineEl = null;
|
|
442
|
+
modified = true;
|
|
443
|
+
i++;
|
|
444
|
+
} else {
|
|
445
|
+
const cur = this._getActiveLine(type);
|
|
446
|
+
if (this._needsRewrite && this._cursorCol === 0 && cur.text.length > 0 && !cur.text.endsWith('\n')) {
|
|
447
|
+
cur.text = ch;
|
|
448
|
+
this._needsRewrite = false;
|
|
449
|
+
} else {
|
|
450
|
+
cur.text += ch;
|
|
451
|
+
}
|
|
452
|
+
this._cursorCol++;
|
|
453
|
+
modified = true;
|
|
454
|
+
i++;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
if (modified) {
|
|
459
|
+
this._updateActiveDomLine();
|
|
460
|
+
}
|
|
461
|
+
this._scrollToBottom();
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
write(data, type = 'stdout') {
|
|
465
|
+
this.writeStreamChunk(data, type);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
writeln(data, type = 'stdout') {
|
|
469
|
+
this.writeStreamChunk(String(data) + '\n', type);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
_getActiveLine(type) {
|
|
473
|
+
if (this._targetLineIdx >= 0 && this._targetLineIdx < this.logs.length) {
|
|
474
|
+
return this.logs[this._targetLineIdx];
|
|
475
|
+
}
|
|
476
|
+
if (this._activeLineCommitted || this.logs.length === 0) {
|
|
477
|
+
const newLine = { text: '', type };
|
|
478
|
+
this.logs.push(newLine);
|
|
479
|
+
this._activeLineCommitted = false;
|
|
480
|
+
this._cursorCol = 0;
|
|
481
|
+
this._targetLineIdx = this.logs.length - 1;
|
|
482
|
+
this._needsRewrite = false;
|
|
483
|
+
this._appendDomLine(newLine);
|
|
484
|
+
return newLine;
|
|
485
|
+
}
|
|
486
|
+
this._targetLineIdx = this.logs.length - 1;
|
|
487
|
+
return this.logs[this.logs.length - 1];
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
_appendDomLine(line) {
|
|
491
|
+
if (!this.container || typeof document === 'undefined') return;
|
|
492
|
+
const logEl = this.container.querySelector('#cl-terminal-log');
|
|
493
|
+
if (!logEl) return;
|
|
494
|
+
const lineEl = document.createElement('div');
|
|
495
|
+
lineEl.className = 'codelab-log-line ' + (line.type || 'stdout');
|
|
496
|
+
lineEl.innerHTML = ansiToHtml(line.text);
|
|
497
|
+
logEl.appendChild(lineEl);
|
|
498
|
+
this._activeLineEl = lineEl;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
_updateActiveDomLine() {
|
|
502
|
+
if (!this.container || typeof document === 'undefined') return;
|
|
503
|
+
const line = this._getActiveLineSync();
|
|
504
|
+
if (!line) return;
|
|
505
|
+
let lineEl = this._activeLineEl;
|
|
506
|
+
if (!lineEl || !lineEl.parentNode) {
|
|
507
|
+
const logEl = this.container.querySelector('#cl-terminal-log');
|
|
508
|
+
if (logEl) {
|
|
509
|
+
if (this._targetLineIdx >= 0 && this._targetLineIdx < logEl.children.length) {
|
|
510
|
+
lineEl = logEl.children[this._targetLineIdx];
|
|
511
|
+
} else {
|
|
512
|
+
lineEl = logEl.children[logEl.children.length - 1];
|
|
513
|
+
}
|
|
514
|
+
this._activeLineEl = lineEl;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
if (lineEl) {
|
|
518
|
+
lineEl.className = 'codelab-log-line ' + (line.type || 'stdout');
|
|
519
|
+
lineEl.innerHTML = ansiToHtml(line.text);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
_getActiveLineSync() {
|
|
524
|
+
if (this._targetLineIdx >= 0 && this._targetLineIdx < this.logs.length) {
|
|
525
|
+
return this.logs[this._targetLineIdx];
|
|
526
|
+
}
|
|
527
|
+
return this.logs[this.logs.length - 1] || null;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
_scrollToBottom() {
|
|
531
|
+
if (!this.container) return;
|
|
532
|
+
const surface = this.container.querySelector('#cl-terminal-surface') || this.container;
|
|
533
|
+
if (surface && typeof surface.scrollTop === 'number' && typeof surface.scrollHeight === 'number') {
|
|
534
|
+
surface.scrollTop = surface.scrollHeight;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
clear() {
|
|
539
|
+
this.logs.length = 0;
|
|
540
|
+
this._activeLineCommitted = true;
|
|
541
|
+
this._cursorCol = 0;
|
|
542
|
+
this._targetLineIdx = -1;
|
|
543
|
+
this._needsRewrite = false;
|
|
544
|
+
this._activeLineEl = null;
|
|
545
|
+
if (this.container && typeof this.container.querySelector === 'function') {
|
|
546
|
+
const logEl = this.container.querySelector('#cl-terminal-log');
|
|
547
|
+
if (logEl) {
|
|
548
|
+
logEl.innerHTML = '';
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
getHistory() {
|
|
554
|
+
return [...this.history];
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
readLine(promptText = '') {
|
|
558
|
+
const prevPrompt = this.promptOverride;
|
|
559
|
+
if (promptText) {
|
|
560
|
+
this.setPrompt(promptText);
|
|
561
|
+
}
|
|
562
|
+
return new Promise((resolve) => {
|
|
563
|
+
this._readLineQueue.push((line) => {
|
|
564
|
+
this.promptOverride = prevPrompt;
|
|
565
|
+
this.updateActiveLine();
|
|
566
|
+
resolve(line);
|
|
567
|
+
});
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
focus() {
|
|
572
|
+
if (!this.container || typeof this.container.querySelector !== 'function') return;
|
|
573
|
+
const input = this.container.querySelector('#cl-repl-input');
|
|
574
|
+
if (input && typeof input.focus === 'function') {
|
|
575
|
+
try { input.focus(); } catch (_) {}
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
mount(container) {
|
|
580
|
+
if (typeof container === 'string' && typeof document !== 'undefined') {
|
|
581
|
+
container = document.querySelector(container);
|
|
582
|
+
}
|
|
583
|
+
this.container = container || null;
|
|
584
|
+
if (!this.container || typeof this.container.querySelector !== 'function') {
|
|
585
|
+
this._mounted = false;
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
this._mounted = true;
|
|
589
|
+
this.render();
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
_cleanupListeners() {
|
|
593
|
+
if (!Array.isArray(this._listeners)) {
|
|
594
|
+
this._listeners = [];
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
for (const entry of this._listeners) {
|
|
598
|
+
if (typeof entry === 'function') {
|
|
599
|
+
try { entry(); } catch (_) {}
|
|
600
|
+
} else if (entry && entry.target && typeof entry.target.removeEventListener === 'function') {
|
|
601
|
+
try { entry.target.removeEventListener(entry.type, entry.handler); } catch (_) {}
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
this._listeners = [];
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
destroy() {
|
|
608
|
+
this._cleanupListeners();
|
|
609
|
+
this._mounted = false;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
_addListener(target, type, handler) {
|
|
613
|
+
if (target && typeof target.addEventListener === 'function') {
|
|
614
|
+
target.addEventListener(type, handler);
|
|
615
|
+
this._listeners.push({ target, type, handler });
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
_bindEvents() {
|
|
620
|
+
this._cleanupListeners();
|
|
621
|
+
if (!this.container || typeof this.container.querySelector !== 'function') return;
|
|
622
|
+
const termSurface = this.container.querySelector('#cl-terminal-surface') || this.container;
|
|
623
|
+
const input = this.container.querySelector('#cl-repl-input');
|
|
624
|
+
const form = this.container.querySelector('#cl-repl-form');
|
|
625
|
+
|
|
626
|
+
if (termSurface) {
|
|
627
|
+
this._addListener(termSurface, 'click', (e) => {
|
|
628
|
+
if (e && e.target === input) return;
|
|
629
|
+
const hasSelection = typeof window !== 'undefined' && window.getSelection && String(window.getSelection());
|
|
630
|
+
if (!hasSelection) this.focus();
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
if (input) {
|
|
635
|
+
this._addListener(input, 'keydown', (e) => this.handleKeyDown(e));
|
|
636
|
+
this._addListener(input, 'input', () => {
|
|
637
|
+
if (input.value !== this.buffer) {
|
|
638
|
+
this.buffer = input.value;
|
|
639
|
+
this.cursor = typeof input.selectionStart === 'number' ? input.selectionStart : this.buffer.length;
|
|
640
|
+
this.updateActiveLine();
|
|
641
|
+
}
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
if (form) {
|
|
646
|
+
this._addListener(form, 'submit', (e) => {
|
|
647
|
+
if (e && typeof e.preventDefault === 'function') e.preventDefault();
|
|
648
|
+
this.submitLine();
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
_findReverseSearchMatch(query, startIdx) {
|
|
654
|
+
if (!query || !this.history.length) return -1;
|
|
655
|
+
const from = typeof startIdx === 'number' ? Math.min(startIdx, this.history.length - 1) : this.history.length - 1;
|
|
656
|
+
for (let i = from; i >= 0; i--) {
|
|
657
|
+
if (this.history[i] && this.history[i].includes(query)) {
|
|
658
|
+
return i;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
return -1;
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
handleKeyDown(e) {
|
|
665
|
+
if (!e) return;
|
|
666
|
+
if (this.onKey) {
|
|
667
|
+
try {
|
|
668
|
+
const res = this.onKey(e);
|
|
669
|
+
if (res === false) return;
|
|
670
|
+
} catch (_) {}
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
const key = e.key;
|
|
674
|
+
const isCtrl = Boolean(e.ctrlKey || e.metaKey);
|
|
675
|
+
|
|
676
|
+
if (isCtrl && (key === 'r' || key === 'R')) {
|
|
677
|
+
if (e.preventDefault) e.preventDefault();
|
|
678
|
+
if (!this.searchMode) {
|
|
679
|
+
this.searchMode = true;
|
|
680
|
+
this.searchQuery = '';
|
|
681
|
+
this.searchMatchIdx = -1;
|
|
682
|
+
} else if (this.searchQuery) {
|
|
683
|
+
const nextStart = this.searchMatchIdx > 0 ? this.searchMatchIdx - 1 : this.history.length - 1;
|
|
684
|
+
const matchIdx = this._findReverseSearchMatch(this.searchQuery, nextStart);
|
|
685
|
+
if (matchIdx !== -1) {
|
|
686
|
+
this.searchMatchIdx = matchIdx;
|
|
687
|
+
this.buffer = this.history[matchIdx];
|
|
688
|
+
this.cursor = this.buffer.length;
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
this.updateActiveLine();
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
if (this.searchMode) {
|
|
696
|
+
if (key === 'Escape' || (isCtrl && (key === 'g' || key === 'G'))) {
|
|
697
|
+
if (e.preventDefault) e.preventDefault();
|
|
698
|
+
this.searchMode = false;
|
|
699
|
+
this.searchQuery = '';
|
|
700
|
+
this.searchMatchIdx = -1;
|
|
701
|
+
this.updateActiveLine();
|
|
702
|
+
return;
|
|
703
|
+
}
|
|
704
|
+
if (key === 'Backspace') {
|
|
705
|
+
if (e.preventDefault) e.preventDefault();
|
|
706
|
+
this.searchQuery = this.searchQuery.slice(0, -1);
|
|
707
|
+
const matchIdx = this._findReverseSearchMatch(this.searchQuery, this.history.length - 1);
|
|
708
|
+
this.searchMatchIdx = matchIdx;
|
|
709
|
+
if (matchIdx !== -1) {
|
|
710
|
+
this.buffer = this.history[matchIdx];
|
|
711
|
+
this.cursor = this.buffer.length;
|
|
712
|
+
}
|
|
713
|
+
this.updateActiveLine();
|
|
714
|
+
return;
|
|
715
|
+
}
|
|
716
|
+
if (key === 'Enter') {
|
|
717
|
+
if (e.preventDefault) e.preventDefault();
|
|
718
|
+
this.searchMode = false;
|
|
719
|
+
this.searchQuery = '';
|
|
720
|
+
this.searchMatchIdx = -1;
|
|
721
|
+
this.submitLine();
|
|
722
|
+
return;
|
|
723
|
+
}
|
|
724
|
+
if (key && key.length === 1 && !isCtrl && !e.altKey) {
|
|
725
|
+
if (e.preventDefault) e.preventDefault();
|
|
726
|
+
this.searchQuery += key;
|
|
727
|
+
const matchIdx = this._findReverseSearchMatch(this.searchQuery, this.history.length - 1);
|
|
728
|
+
this.searchMatchIdx = matchIdx;
|
|
729
|
+
if (matchIdx !== -1) {
|
|
730
|
+
this.buffer = this.history[matchIdx];
|
|
731
|
+
this.cursor = this.buffer.length;
|
|
732
|
+
}
|
|
733
|
+
this.updateActiveLine();
|
|
734
|
+
return;
|
|
735
|
+
}
|
|
736
|
+
this.searchMode = false;
|
|
737
|
+
this.searchQuery = '';
|
|
738
|
+
this.searchMatchIdx = -1;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
if (key === 'ArrowLeft') {
|
|
742
|
+
if (e.preventDefault) e.preventDefault();
|
|
743
|
+
if (this.cursor > 0) {
|
|
744
|
+
this.cursor--;
|
|
745
|
+
this.updateActiveLine();
|
|
746
|
+
}
|
|
747
|
+
return;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
if (key === 'ArrowRight') {
|
|
751
|
+
if (e.preventDefault) e.preventDefault();
|
|
752
|
+
if (this.cursor < this.buffer.length) {
|
|
753
|
+
this.cursor++;
|
|
754
|
+
this.updateActiveLine();
|
|
755
|
+
}
|
|
756
|
+
return;
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
if (key === 'Home' || (isCtrl && (key === 'a' || key === 'A'))) {
|
|
760
|
+
if (e.preventDefault) e.preventDefault();
|
|
761
|
+
this.cursor = 0;
|
|
762
|
+
this.updateActiveLine();
|
|
763
|
+
return;
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
if (key === 'End' || (isCtrl && (key === 'e' || key === 'E'))) {
|
|
767
|
+
if (e.preventDefault) e.preventDefault();
|
|
768
|
+
this.cursor = this.buffer.length;
|
|
769
|
+
this.updateActiveLine();
|
|
770
|
+
return;
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
if (isCtrl && (key === 'u' || key === 'U')) {
|
|
774
|
+
if (e.preventDefault) e.preventDefault();
|
|
775
|
+
this.buffer = this.buffer.slice(this.cursor);
|
|
776
|
+
this.cursor = 0;
|
|
777
|
+
this.updateActiveLine();
|
|
778
|
+
return;
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
if (isCtrl && (key === 'k' || key === 'K')) {
|
|
782
|
+
if (e.preventDefault) e.preventDefault();
|
|
783
|
+
this.buffer = this.buffer.slice(0, this.cursor);
|
|
784
|
+
this.updateActiveLine();
|
|
785
|
+
return;
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
if (key === 'Backspace') {
|
|
789
|
+
if (e.preventDefault) e.preventDefault();
|
|
790
|
+
if (this.cursor > 0) {
|
|
791
|
+
this.buffer = this.buffer.slice(0, this.cursor - 1) + this.buffer.slice(this.cursor);
|
|
792
|
+
this.cursor--;
|
|
793
|
+
this.updateActiveLine();
|
|
794
|
+
}
|
|
795
|
+
return;
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
if (key === 'Delete') {
|
|
799
|
+
if (e.preventDefault) e.preventDefault();
|
|
800
|
+
if (this.cursor < this.buffer.length) {
|
|
801
|
+
this.buffer = this.buffer.slice(0, this.cursor) + this.buffer.slice(this.cursor + 1);
|
|
802
|
+
this.updateActiveLine();
|
|
803
|
+
}
|
|
804
|
+
return;
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
if (isCtrl && (key === 'l' || key === 'L')) {
|
|
808
|
+
if (e.preventDefault) e.preventDefault();
|
|
809
|
+
this.clear();
|
|
810
|
+
return;
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
if (isCtrl && (key === 'c' || key === 'C')) {
|
|
814
|
+
if (e.preventDefault) e.preventDefault();
|
|
815
|
+
this.logs.push({ type: 'system', text: `${this.getPrompt()}${this.buffer}^C` });
|
|
816
|
+
this.buffer = '';
|
|
817
|
+
this.cursor = 0;
|
|
818
|
+
this.historyIdx = -1;
|
|
819
|
+
if (this.replMode) {
|
|
820
|
+
this.replMode = false;
|
|
821
|
+
}
|
|
822
|
+
if (this.onInterrupt) {
|
|
823
|
+
try { this.onInterrupt(); } catch (_) {}
|
|
824
|
+
}
|
|
825
|
+
this.render();
|
|
826
|
+
return;
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
if (isCtrl && (key === 'd' || key === 'D')) {
|
|
830
|
+
if (this.buffer === '') {
|
|
831
|
+
if (e.preventDefault) e.preventDefault();
|
|
832
|
+
if (this.replMode) {
|
|
833
|
+
this.logs.push({ type: 'system', text: `${this.getPrompt()}.exit` });
|
|
834
|
+
this.replMode = false;
|
|
835
|
+
this.render();
|
|
836
|
+
return;
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
if (key === 'ArrowUp') {
|
|
842
|
+
if (e.preventDefault) e.preventDefault();
|
|
843
|
+
if (!this.history.length) return;
|
|
844
|
+
if (this.historyIdx === -1) {
|
|
845
|
+
this.savedBuffer = this.buffer;
|
|
846
|
+
this.historyIdx = this.history.length - 1;
|
|
847
|
+
} else if (this.historyIdx > 0) {
|
|
848
|
+
this.historyIdx--;
|
|
849
|
+
}
|
|
850
|
+
this.buffer = this.history[this.historyIdx] || '';
|
|
851
|
+
this.cursor = this.buffer.length;
|
|
852
|
+
this.updateActiveLine();
|
|
853
|
+
return;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
if (key === 'ArrowDown') {
|
|
857
|
+
if (e.preventDefault) e.preventDefault();
|
|
858
|
+
if (this.historyIdx !== -1) {
|
|
859
|
+
if (this.historyIdx < this.history.length - 1) {
|
|
860
|
+
this.historyIdx++;
|
|
861
|
+
this.buffer = this.history[this.historyIdx] || '';
|
|
862
|
+
} else {
|
|
863
|
+
this.historyIdx = -1;
|
|
864
|
+
this.buffer = this.savedBuffer || '';
|
|
865
|
+
}
|
|
866
|
+
this.cursor = this.buffer.length;
|
|
867
|
+
this.updateActiveLine();
|
|
868
|
+
}
|
|
869
|
+
return;
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
if (key === 'Tab') {
|
|
873
|
+
if (e.preventDefault) e.preventDefault();
|
|
874
|
+
this.autoComplete();
|
|
875
|
+
return;
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
if (key === 'Enter') {
|
|
879
|
+
if (e.preventDefault) e.preventDefault();
|
|
880
|
+
this.submitLine();
|
|
881
|
+
return;
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
// Printable character
|
|
885
|
+
if (key && key.length === 1 && !isCtrl && !e.altKey) {
|
|
886
|
+
if (e.preventDefault) e.preventDefault();
|
|
887
|
+
this.buffer = this.buffer.slice(0, this.cursor) + key + this.buffer.slice(this.cursor);
|
|
888
|
+
this.cursor++;
|
|
889
|
+
this.updateActiveLine();
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
autoComplete() {
|
|
894
|
+
if (!this.getCompletions) return;
|
|
895
|
+
const res = this.getCompletions(this.buffer, this.cursor);
|
|
896
|
+
if (!res || !Array.isArray(res.matches) || res.matches.length === 0) return;
|
|
897
|
+
|
|
898
|
+
const matches = res.matches;
|
|
899
|
+
if (matches.length === 1) {
|
|
900
|
+
const match = matches[0];
|
|
901
|
+
const isDir = match.endsWith('/');
|
|
902
|
+
const appendChar = isDir ? '' : ' ';
|
|
903
|
+
const before = this.buffer.slice(0, res.tokenStart);
|
|
904
|
+
const after = this.buffer.slice(this.cursor);
|
|
905
|
+
this.buffer = before + match + appendChar + after;
|
|
906
|
+
this.cursor = before.length + match.length + appendChar.length;
|
|
907
|
+
this.updateActiveLine();
|
|
908
|
+
} else {
|
|
909
|
+
// Find longest common prefix
|
|
910
|
+
let lcp = matches[0];
|
|
911
|
+
for (let i = 1; i < matches.length; i++) {
|
|
912
|
+
let j = 0;
|
|
913
|
+
while (j < lcp.length && j < matches[i].length && lcp[j] === matches[i][j]) {
|
|
914
|
+
j++;
|
|
915
|
+
}
|
|
916
|
+
lcp = lcp.slice(0, j);
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
if (lcp.length > res.token.length) {
|
|
920
|
+
const before = this.buffer.slice(0, res.tokenStart);
|
|
921
|
+
const after = this.buffer.slice(this.cursor);
|
|
922
|
+
this.buffer = before + lcp + after;
|
|
923
|
+
this.cursor = before.length + lcp.length;
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
// Print candidate matches
|
|
927
|
+
this.logs.push({ type: 'system', text: `${this.getPrompt()}${this.buffer}` });
|
|
928
|
+
this.logs.push({ type: 'stdout', text: matches.join(' ') });
|
|
929
|
+
this.render();
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
submitLine() {
|
|
934
|
+
const line = this.buffer;
|
|
935
|
+
this.buffer = '';
|
|
936
|
+
this.cursor = 0;
|
|
937
|
+
this.historyIdx = -1;
|
|
938
|
+
this.searchMode = false;
|
|
939
|
+
this.searchQuery = '';
|
|
940
|
+
this.searchMatchIdx = -1;
|
|
941
|
+
|
|
942
|
+
if (line.trim()) {
|
|
943
|
+
if (this.history[this.history.length - 1] !== line.trim()) {
|
|
944
|
+
this.history.push(line.trim());
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
if (this._readLineQueue && this._readLineQueue.length > 0) {
|
|
949
|
+
const resolver = this._readLineQueue.shift();
|
|
950
|
+
this.updateActiveLine();
|
|
951
|
+
resolver(line);
|
|
952
|
+
return;
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
this.updateActiveLine();
|
|
956
|
+
if (this.onCommand) {
|
|
957
|
+
this.onCommand(line);
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
updateActiveLine() {
|
|
962
|
+
if (!this.container || typeof this.container.querySelector !== 'function') return;
|
|
963
|
+
const input = this.container.querySelector('#cl-repl-input');
|
|
964
|
+
if (input && input.value !== this.buffer) {
|
|
965
|
+
input.value = this.buffer;
|
|
966
|
+
}
|
|
967
|
+
const lineEl = this.container.querySelector('#cl-pty-active-line');
|
|
968
|
+
if (!lineEl) return;
|
|
969
|
+
|
|
970
|
+
const prompt = this.searchMode
|
|
971
|
+
? `(reverse-i-search)\`${this.searchQuery}\`: `
|
|
972
|
+
: this.getPrompt();
|
|
973
|
+
const before = this.buffer.slice(0, this.cursor);
|
|
974
|
+
const cursorChar = this.cursor < this.buffer.length ? this.buffer[this.cursor] : ' ';
|
|
975
|
+
const after = this.cursor < this.buffer.length ? this.buffer.slice(this.cursor + 1) : '';
|
|
976
|
+
|
|
977
|
+
lineEl.innerHTML = `<span class="codelab-pty__prompt">${escapeHtml(prompt)}</span><span class="codelab-pty__before">${escapeHtml(before)}</span><span class="codelab-pty__cursor blink">${escapeHtml(cursorChar)}</span><span class="codelab-pty__after">${escapeHtml(after)}</span>`;
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
render() {
|
|
981
|
+
if (!this.container || typeof this.container.querySelector !== 'function') return;
|
|
982
|
+
const prevInput = this.container.querySelector('#cl-repl-input');
|
|
983
|
+
const hadFocus = Boolean(
|
|
984
|
+
prevInput &&
|
|
985
|
+
typeof document !== 'undefined' &&
|
|
986
|
+
document.activeElement === prevInput
|
|
987
|
+
);
|
|
988
|
+
|
|
989
|
+
const logsHtml = this.logs
|
|
990
|
+
.map(item => `<div class="codelab-log-line ${escapeHtml(item.type || '')}">${ansiToHtml(item.text)}</div>`)
|
|
991
|
+
.join('');
|
|
992
|
+
|
|
993
|
+
const prompt = this.searchMode
|
|
994
|
+
? `(reverse-i-search)\`${this.searchQuery}\`: `
|
|
995
|
+
: this.getPrompt();
|
|
996
|
+
const before = this.buffer.slice(0, this.cursor);
|
|
997
|
+
const cursorChar = this.cursor < this.buffer.length ? this.buffer[this.cursor] : ' ';
|
|
998
|
+
const after = this.cursor < this.buffer.length ? this.buffer.slice(this.cursor + 1) : '';
|
|
999
|
+
|
|
1000
|
+
this.container.innerHTML = `
|
|
1001
|
+
<div class="codelab-terminal" id="cl-terminal-surface" tabindex="0">
|
|
1002
|
+
<div class="codelab-terminal__log" id="cl-terminal-log">${logsHtml}</div>
|
|
1003
|
+
<div class="codelab-pty__active-line" id="cl-pty-active-line">
|
|
1004
|
+
<span class="codelab-pty__prompt">${escapeHtml(prompt)}</span><span class="codelab-pty__before">${escapeHtml(before)}</span><span class="codelab-pty__cursor blink">${escapeHtml(cursorChar)}</span><span class="codelab-pty__after">${escapeHtml(after)}</span>
|
|
1005
|
+
</div>
|
|
1006
|
+
<form class="codelab-repl" id="cl-repl-form" style="position:absolute;opacity:0;pointer-events:none;height:0;overflow:hidden;">
|
|
1007
|
+
<input type="text" class="codelab-repl__input" id="cl-repl-input" autocomplete="off" spellcheck="false" aria-label="Terminal command input" value="${escapeHtml(this.buffer)}">
|
|
1008
|
+
</form>
|
|
1009
|
+
</div>
|
|
1010
|
+
`;
|
|
1011
|
+
|
|
1012
|
+
this._scrollToBottom();
|
|
1013
|
+
|
|
1014
|
+
this._bindEvents();
|
|
1015
|
+
|
|
1016
|
+
if (hadFocus) {
|
|
1017
|
+
const nextInput = this.container.querySelector('#cl-repl-input');
|
|
1018
|
+
if (nextInput && typeof nextInput.focus === 'function') {
|
|
1019
|
+
try {
|
|
1020
|
+
nextInput.focus();
|
|
1021
|
+
if (typeof nextInput.setSelectionRange === 'function') {
|
|
1022
|
+
nextInput.setSelectionRange(this.cursor, this.cursor);
|
|
1023
|
+
}
|
|
1024
|
+
} catch (_) {}
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
function createTerminal(container, options = {}) {
|
|
1031
|
+
return new PTYTerminalEngine(container, options);
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
const createPTYTerminalEngine = createTerminal;
|
|
1035
|
+
|
|
1036
|
+
return {
|
|
1037
|
+
PTYTerminalEngine,
|
|
1038
|
+
createPTYTerminalEngine,
|
|
1039
|
+
createTerminal,
|
|
1040
|
+
ansiToHtml,
|
|
1041
|
+
parseAnsiToHtml,
|
|
1042
|
+
escapeHtml,
|
|
1043
|
+
ANSI_CODE_MAP,
|
|
1044
|
+
resolveAnsiControlCodes
|
|
1045
|
+
};
|
|
1046
|
+
});
|