@innovatorved/code-lab 0.7.0 → 0.7.2
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 +2 -2
- package/CHANGELOG.md +30 -0
- package/README.md +6 -6
- package/dist/code-lab-sql.esm.js +19 -19
- package/dist/code-lab-sql.umd.js +20 -20
- package/dist/code-lab.css +1 -1
- package/dist/code-lab.d.ts +47 -38
- package/dist/code-lab.esm.js +1183 -1909
- package/dist/code-lab.js +1182 -1906
- package/dist/extensions/cpp.esm.js +132 -8760
- package/dist/extensions/cpp.js +124 -8770
- package/dist/extensions/guided-lab.esm.js +33 -6836
- package/dist/extensions/guided-lab.js +33 -6854
- package/dist/extensions/javascript.esm.js +1 -36
- package/dist/extensions/javascript.js +1 -58
- package/dist/extensions/python.esm.js +113 -8122
- package/dist/extensions/python.js +113 -8140
- package/dist/extensions/typescript.esm.js +1 -55
- package/dist/extensions/typescript.js +1 -77
- package/dist/index.d.ts +1 -1
- package/dist/logger.d.ts +4 -20
- package/dist/modular-kernel.d.ts +0 -5
- package/dist/packages/core/src/errors.d.ts +5 -1
- package/dist/packages/fs/src/backends.d.ts +0 -27
- package/dist/packages/fs/src/index.d.ts +1 -1
- package/dist/packages/ui/src/html.d.ts +1 -0
- package/dist/packages/ui/src/index.d.ts +1 -0
- package/dist/{apps/web/src/index.d.ts → packages/ui/src/kernel.d.ts} +3 -11
- package/dist/packages/ui/src/observability.d.ts +1 -1
- package/dist/packages/ui/src/settings-dialog.d.ts +0 -1
- package/dist/preview-sw.js +1 -1
- package/dist/runtime.d.ts +3 -0
- package/dist/runtime.esm.js +78 -78
- package/dist/runtime.js +78 -78
- package/dist/schemas.d.ts +10 -1
- package/dist/schemas.esm.js +33 -6815
- package/dist/schemas.js +33 -6831
- package/dist/shell.esm.js +277 -285
- package/dist/shell.js +277 -285
- package/dist/terminal.esm.js +9 -7
- package/dist/terminal.js +7 -7
- package/dist/workbench/command-palette.d.ts +0 -4
- package/dist/workbench/editor.d.ts +0 -4
- package/dist/workbench/index.d.ts +0 -22
- package/dist/workbench/intellisense.d.ts +1 -8
- package/dist/workbench/xterm-adapter.d.ts +0 -1
- package/package.json +4 -13
- package/dist/code-lab.umd.js +0 -6117
- package/dist/packages/editor/src/index.d.ts +0 -118
- package/dist/packages/preview/src/index.d.ts +0 -34
- package/dist/packages/runtime/src/index.d.ts +0 -47
- package/dist/packages/terminal/src/index.d.ts +0 -54
- package/dist/workbench/drawer.d.ts +0 -18
- package/dist/workbench/file-tree.d.ts +0 -11
- package/dist/workbench/guided-pane.d.ts +0 -11
- package/dist/workbench/styles.d.ts +0 -4
- package/dist/workbench/toolbar.d.ts +0 -17
- package/packages/commands/README.md +0 -7
- package/packages/commands/package.json +0 -16
- package/packages/commands/src/index.ts +0 -467
- package/packages/commands/tsconfig.json +0 -22
- package/packages/core/README.md +0 -12
- package/packages/core/package.json +0 -13
- package/packages/core/src/di.ts +0 -147
- package/packages/core/src/disposable.ts +0 -112
- package/packages/core/src/errors.ts +0 -205
- package/packages/core/src/events.ts +0 -157
- package/packages/core/src/index.ts +0 -49
- package/packages/core/src/path.ts +0 -118
- package/packages/core/src/registry.ts +0 -61
- package/packages/core/tsconfig.json +0 -19
- package/packages/editor/README.md +0 -9
- package/packages/editor/package.json +0 -18
- package/packages/editor/src/index.ts +0 -344
- package/packages/editor/tsconfig.json +0 -24
- package/packages/fs/README.md +0 -7
- package/packages/fs/package.json +0 -16
- package/packages/fs/src/backends.ts +0 -301
- package/packages/fs/src/index.ts +0 -18
- package/packages/fs/src/types.ts +0 -44
- package/packages/fs/src/vfs-service.ts +0 -257
- package/packages/fs/tsconfig.json +0 -22
- package/packages/preview/README.md +0 -3
- package/packages/preview/package.json +0 -16
- package/packages/preview/src/index.ts +0 -125
- package/packages/preview/tsconfig.json +0 -22
- package/packages/runtime/README.md +0 -3
- package/packages/runtime/package.json +0 -17
- package/packages/runtime/src/index.ts +0 -134
- package/packages/runtime/tsconfig.json +0 -23
- package/packages/settings/README.md +0 -7
- package/packages/settings/package.json +0 -16
- package/packages/settings/src/builtin-schemas.ts +0 -262
- package/packages/settings/src/configuration-service.ts +0 -609
- package/packages/settings/src/index.ts +0 -38
- package/packages/settings/src/jsonc.ts +0 -189
- package/packages/settings/src/schema.ts +0 -164
- package/packages/settings/tsconfig.json +0 -22
- package/packages/sql/README.md +0 -12
- package/packages/sql/package.json +0 -13
- package/packages/sql/src/completions.ts +0 -359
- package/packages/sql/src/diagnostics.ts +0 -200
- package/packages/sql/src/formatter.ts +0 -271
- package/packages/sql/src/index.ts +0 -54
- package/packages/sql/src/sql-editor.ts +0 -802
- package/packages/sql/src/styles.ts +0 -524
- package/packages/sql/src/tokenizer.ts +0 -457
- package/packages/sql/src/types.ts +0 -148
- package/packages/sql/tsconfig.json +0 -19
- package/packages/terminal/README.md +0 -3
- package/packages/terminal/package.json +0 -16
- package/packages/terminal/src/index.ts +0 -183
- package/packages/terminal/tsconfig.json +0 -22
- package/packages/ui/README.md +0 -8
- package/packages/ui/package.json +0 -18
- package/packages/ui/src/index.ts +0 -40
- package/packages/ui/src/observability.ts +0 -173
- package/packages/ui/src/plugin-registry.ts +0 -289
- package/packages/ui/src/settings-dialog.ts +0 -939
- package/packages/ui/src/themes.ts +0 -333
- package/packages/ui/tsconfig.json +0 -24
- package/src/code-lab.css +0 -8584
- package/src/csp.ts +0 -590
- package/src/diagnostics.ts +0 -287
- package/src/extensions/cpp.ts +0 -2389
- package/src/extensions/guided-lab.ts +0 -64
- package/src/extensions/javascript.ts +0 -54
- package/src/extensions/python.ts +0 -1737
- package/src/extensions/typescript.ts +0 -85
- package/src/extensions/umbrella.ts +0 -5
- package/src/html.ts +0 -88
- package/src/index.ts +0 -129
- package/src/logger.ts +0 -139
- package/src/modular-kernel.ts +0 -11
- package/src/preview-sw.js +0 -296
- package/src/runtime/test-harness.ts +0 -57
- package/src/runtime.ts +0 -10480
- package/src/sample.html +0 -304
- package/src/schemas.ts +0 -366
- package/src/scm.ts +0 -2453
- package/src/search.ts +0 -433
- package/src/shell.ts +0 -10987
- package/src/storage.ts +0 -773
- package/src/terminal.ts +0 -1400
- package/src/workbench/command-palette.ts +0 -381
- package/src/workbench/diff-editor.ts +0 -470
- package/src/workbench/drawer.ts +0 -157
- package/src/workbench/editor.ts +0 -49
- package/src/workbench/file-tree.ts +0 -51
- package/src/workbench/guided-pane.ts +0 -43
- package/src/workbench/icons.ts +0 -255
- package/src/workbench/index.ts +0 -44
- package/src/workbench/intellisense.ts +0 -1871
- package/src/workbench/share.ts +0 -128
- package/src/workbench/styles.ts +0 -211
- package/src/workbench/toolbar.ts +0 -182
- package/src/workbench/xterm-adapter.ts +0 -1571
- package/src/workbench.ts +0 -22132
- package/src/zip.ts +0 -348
|
@@ -1,1571 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @innovatorved/code-lab — Browser-Native xterm.js PTY Adapter
|
|
3
|
-
*
|
|
4
|
-
* Mounts a real `@xterm/xterm` terminal instance (with `@xterm/addon-fit`,
|
|
5
|
-
* `@xterm/addon-web-links`, and clickable `file:line:col` diagnostic navigation)
|
|
6
|
-
* inside `#cl-terminal-surface`, while maintaining a synchronized hidden mirror
|
|
7
|
-
* (`#cl-terminal-log`, `#cl-pty-active-line`, `#cl-repl-input`) so programmatic
|
|
8
|
-
* consumers and E2E harnesses work seamlessly alongside interactive VT100 I/O.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import type { Terminal, ITheme } from '@xterm/xterm';
|
|
12
|
-
import type { FitAddon } from '@xterm/addon-fit';
|
|
13
|
-
import type { WebLinksAddon } from '@xterm/addon-web-links';
|
|
14
|
-
|
|
15
|
-
interface XtermRuntimeClasses {
|
|
16
|
-
Terminal: typeof Terminal;
|
|
17
|
-
FitAddon: typeof FitAddon;
|
|
18
|
-
WebLinksAddon: typeof WebLinksAddon;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
let _cachedXtermClasses: XtermRuntimeClasses | null = null;
|
|
22
|
-
|
|
23
|
-
function getXtermRuntimeClasses(): XtermRuntimeClasses {
|
|
24
|
-
if (!_cachedXtermClasses) {
|
|
25
|
-
// Lazy-require ESM builds so esbuild wraps xterm in a deferred __esm/__commonJS factory
|
|
26
|
-
// (keeping Node/Bun headless imports free of xterm's MessageChannel queue and ~38 KB smaller gzipped)
|
|
27
|
-
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
28
|
-
const xtermMod = require('@xterm/xterm/lib/xterm.mjs') as { Terminal: typeof Terminal };
|
|
29
|
-
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
30
|
-
const fitMod = require('@xterm/addon-fit/lib/addon-fit.mjs') as { FitAddon: typeof FitAddon };
|
|
31
|
-
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
32
|
-
const linksMod = require('@xterm/addon-web-links/lib/addon-web-links.mjs') as {
|
|
33
|
-
WebLinksAddon: typeof WebLinksAddon;
|
|
34
|
-
};
|
|
35
|
-
_cachedXtermClasses = {
|
|
36
|
-
Terminal: xtermMod.Terminal,
|
|
37
|
-
FitAddon: fitMod.FitAddon,
|
|
38
|
-
WebLinksAddon: linksMod.WebLinksAddon
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
return _cachedXtermClasses;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
interface SharedTerminalHelpers {
|
|
45
|
-
ansiToHtml?: (text: string) => string;
|
|
46
|
-
escapeHtml?: (str: string) => string;
|
|
47
|
-
resolveAnsiControlCodes?: (text: string) => string;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function getTerminalHelpers(): SharedTerminalHelpers {
|
|
51
|
-
const g = globalThis as unknown as { CodeLabTerminal?: SharedTerminalHelpers };
|
|
52
|
-
return g.CodeLabTerminal || {};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function escapeHtml(str: string): string {
|
|
56
|
-
const fn = getTerminalHelpers().escapeHtml;
|
|
57
|
-
if (typeof fn === 'function') return fn(str);
|
|
58
|
-
return String(str == null ? '' : str)
|
|
59
|
-
.replace(/&/g, '&')
|
|
60
|
-
.replace(/</g, '<')
|
|
61
|
-
.replace(/>/g, '>')
|
|
62
|
-
.replace(/"/g, '"');
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function ansiToHtml(text: string): string {
|
|
66
|
-
const fn = getTerminalHelpers().ansiToHtml;
|
|
67
|
-
if (typeof fn === 'function') return fn(text);
|
|
68
|
-
return escapeHtml(String(text == null ? '' : text).replace(/\x1b\[[0-9;?]*[A-Za-z]/g, ''));
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function resolveAnsiControlCodes(text: string): string {
|
|
72
|
-
const fn = getTerminalHelpers().resolveAnsiControlCodes;
|
|
73
|
-
if (typeof fn === 'function') return fn(text);
|
|
74
|
-
return String(text == null ? '' : text);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export interface XtermAdapterOptions {
|
|
78
|
-
cwd?: string;
|
|
79
|
-
logs?: Array<{ type: string; text: string }>;
|
|
80
|
-
history?: string[];
|
|
81
|
-
theme?: 'dark' | 'light' | 'hc' | 'high-contrast';
|
|
82
|
-
fontSize?: number;
|
|
83
|
-
fontFamily?: string;
|
|
84
|
-
onCommand?: (line: string) => Promise<unknown> | unknown;
|
|
85
|
-
getCompletions?: (
|
|
86
|
-
buffer: string,
|
|
87
|
-
cursor: number
|
|
88
|
-
) => { matches: string[]; token: string; tokenStart: number } | null;
|
|
89
|
-
onInterrupt?: () => void;
|
|
90
|
-
onResize?: (cols: number, rows: number) => void;
|
|
91
|
-
onOpenFileLink?: (filePath: string, line: number, column: number) => void;
|
|
92
|
-
onOpenUrl?: (url: string) => boolean | void;
|
|
93
|
-
maxScrollback?: number;
|
|
94
|
-
initialLogs?: Array<{ type: string; text: string }>;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const VALID_LOG_TYPES = new Set([
|
|
98
|
-
'stdout',
|
|
99
|
-
'stderr',
|
|
100
|
-
'system',
|
|
101
|
-
'info',
|
|
102
|
-
'warn',
|
|
103
|
-
'error',
|
|
104
|
-
'success'
|
|
105
|
-
]);
|
|
106
|
-
|
|
107
|
-
function sanitizeLogType(type?: string): string {
|
|
108
|
-
const t = String(type || 'stdout').trim();
|
|
109
|
-
return VALID_LOG_TYPES.has(t) ? t : 'stdout';
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export function getXtermTheme(mode: string = 'dark'): ITheme {
|
|
113
|
-
if (mode === 'light') {
|
|
114
|
-
return {
|
|
115
|
-
background: '#ffffff',
|
|
116
|
-
foreground: '#18181b',
|
|
117
|
-
cursor: '#18181b',
|
|
118
|
-
cursorAccent: '#ffffff',
|
|
119
|
-
selectionBackground: 'rgba(24, 24, 27, 0.14)',
|
|
120
|
-
black: '#18181b',
|
|
121
|
-
red: '#b42318',
|
|
122
|
-
green: '#067647',
|
|
123
|
-
yellow: '#b54708',
|
|
124
|
-
blue: '#1d4ed8',
|
|
125
|
-
magenta: '#6d28d9',
|
|
126
|
-
cyan: '#0f766e',
|
|
127
|
-
white: '#52525b',
|
|
128
|
-
brightBlack: '#6e6a64',
|
|
129
|
-
brightRed: '#dc2626',
|
|
130
|
-
brightGreen: '#059669',
|
|
131
|
-
brightYellow: '#d97706',
|
|
132
|
-
brightBlue: '#2563eb',
|
|
133
|
-
brightMagenta: '#7c3aed',
|
|
134
|
-
brightCyan: '#0d9488',
|
|
135
|
-
brightWhite: '#09090b'
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
if (mode === 'hc' || mode === 'high-contrast') {
|
|
139
|
-
return {
|
|
140
|
-
background: '#000000',
|
|
141
|
-
foreground: '#ffffff',
|
|
142
|
-
cursor: '#ffffff',
|
|
143
|
-
cursorAccent: '#000000',
|
|
144
|
-
selectionBackground: 'rgba(255, 255, 255, 0.35)',
|
|
145
|
-
black: '#000000',
|
|
146
|
-
red: '#f87171',
|
|
147
|
-
green: '#34d399',
|
|
148
|
-
yellow: '#fbbf24',
|
|
149
|
-
blue: '#93c5fd',
|
|
150
|
-
magenta: '#c084fc',
|
|
151
|
-
cyan: '#2dd4bf',
|
|
152
|
-
white: '#ffffff',
|
|
153
|
-
brightBlack: '#a1a1aa',
|
|
154
|
-
brightRed: '#f87171',
|
|
155
|
-
brightGreen: '#34d399',
|
|
156
|
-
brightYellow: '#fbbf24',
|
|
157
|
-
brightBlue: '#93c5fd',
|
|
158
|
-
brightMagenta: '#c084fc',
|
|
159
|
-
brightCyan: '#2dd4bf',
|
|
160
|
-
brightWhite: '#ffffff'
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
return {
|
|
164
|
-
background: '#0f0f10',
|
|
165
|
-
foreground: '#f4f4f5',
|
|
166
|
-
cursor: '#f4f4f5',
|
|
167
|
-
cursorAccent: '#0f0f10',
|
|
168
|
-
selectionBackground: 'rgba(244, 244, 245, 0.22)',
|
|
169
|
-
black: '#18181a',
|
|
170
|
-
red: '#f87171',
|
|
171
|
-
green: '#34d399',
|
|
172
|
-
yellow: '#fbbf24',
|
|
173
|
-
blue: '#93c5fd',
|
|
174
|
-
magenta: '#c084fc',
|
|
175
|
-
cyan: '#2dd4bf',
|
|
176
|
-
white: '#d4d4d8',
|
|
177
|
-
brightBlack: '#8e8e97',
|
|
178
|
-
brightRed: '#fca5a5',
|
|
179
|
-
brightGreen: '#6ee7b7',
|
|
180
|
-
brightYellow: '#fde047',
|
|
181
|
-
brightBlue: '#bfdbfe',
|
|
182
|
-
brightMagenta: '#d8b4fe',
|
|
183
|
-
brightCyan: '#5eead4',
|
|
184
|
-
brightWhite: '#ffffff'
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
export function canUseXtermInEnvironment(container?: unknown): boolean {
|
|
189
|
-
if (typeof window === 'undefined' || typeof document === 'undefined') return false;
|
|
190
|
-
if (typeof HTMLCanvasElement === 'undefined') return false;
|
|
191
|
-
if (container !== undefined && (!container || typeof (container as Element).appendChild !== 'function')) {
|
|
192
|
-
return false;
|
|
193
|
-
}
|
|
194
|
-
// Detect lightweight Node mock-DOMs that do not implement real Element layout APIs
|
|
195
|
-
const probe = document.createElement('div');
|
|
196
|
-
if (typeof probe.getBoundingClientRect !== 'function' || typeof window.getComputedStyle !== 'function') {
|
|
197
|
-
return false;
|
|
198
|
-
}
|
|
199
|
-
if (typeof navigator !== 'undefined' && /jsdom/i.test(navigator.userAgent || '')) {
|
|
200
|
-
return false;
|
|
201
|
-
}
|
|
202
|
-
return true;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
interface ReadLineQueueItem {
|
|
206
|
-
promptText: string;
|
|
207
|
-
prevPrompt: string | null;
|
|
208
|
-
_activated: boolean;
|
|
209
|
-
cleanup: (() => void) | null;
|
|
210
|
-
resolve: (value: string | null) => void;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
const FILE_LINK_RE =
|
|
214
|
-
/(?:\/workspace\/|\.\/)?([A-Za-z0-9_./-]+\.(?:ts|tsx|js|jsx|mjs|cjs|py|cpp|cc|cxx|h|hpp|sql|json|css|html|md))(?::(\d+)(?::(\d+))?|\((\d+),(\d+)\))/g;
|
|
215
|
-
|
|
216
|
-
export class XtermTerminalEngine {
|
|
217
|
-
public container: HTMLElement | null;
|
|
218
|
-
public options: XtermAdapterOptions;
|
|
219
|
-
public logs: Array<{ type: string; text: string }>;
|
|
220
|
-
public cwd: string;
|
|
221
|
-
public replMode: boolean;
|
|
222
|
-
public promptOverride: string | null;
|
|
223
|
-
public history: string[];
|
|
224
|
-
public historyIdx: number;
|
|
225
|
-
public savedBuffer: string;
|
|
226
|
-
public buffer: string;
|
|
227
|
-
public cursor: number;
|
|
228
|
-
public searchMode: boolean;
|
|
229
|
-
public searchQuery: string;
|
|
230
|
-
public searchMatchIdx: number;
|
|
231
|
-
public isBusy: boolean;
|
|
232
|
-
public maxScrollback: number;
|
|
233
|
-
public maxHistory: number;
|
|
234
|
-
public onCommand: ((line: string) => Promise<unknown> | unknown) | null;
|
|
235
|
-
public getCompletions: XtermAdapterOptions['getCompletions'] | null;
|
|
236
|
-
public onInterrupt: (() => void) | null;
|
|
237
|
-
|
|
238
|
-
public xterm: Terminal | null = null;
|
|
239
|
-
public fitAddon: FitAddon | null = null;
|
|
240
|
-
|
|
241
|
-
private _readLineQueue: ReadLineQueueItem[] = [];
|
|
242
|
-
private _themeMode: string;
|
|
243
|
-
private _resizeObserver: ResizeObserver | null = null;
|
|
244
|
-
private _fitRaf = 0;
|
|
245
|
-
private _disposed = false;
|
|
246
|
-
private _promptVisibleInXterm = false;
|
|
247
|
-
private _pendingMirrorLine = '';
|
|
248
|
-
private _activeMirrorEl: HTMLElement | null = null;
|
|
249
|
-
private _domListeners: Array<{ target: EventTarget; type: string; fn: EventListener }> = [];
|
|
250
|
-
|
|
251
|
-
constructor(container: HTMLElement | null, options: XtermAdapterOptions = {}) {
|
|
252
|
-
this.container = container || null;
|
|
253
|
-
this.options = options || {};
|
|
254
|
-
this.maxScrollback =
|
|
255
|
-
typeof options.maxScrollback === 'number' && Number.isFinite(options.maxScrollback) && options.maxScrollback > 0
|
|
256
|
-
? Math.floor(options.maxScrollback)
|
|
257
|
-
: 3000;
|
|
258
|
-
this.maxHistory = 1000;
|
|
259
|
-
const seedLogs = Array.isArray(options.initialLogs)
|
|
260
|
-
? options.initialLogs
|
|
261
|
-
: Array.isArray(options.logs)
|
|
262
|
-
? options.logs
|
|
263
|
-
: [];
|
|
264
|
-
this.logs = [...seedLogs];
|
|
265
|
-
if (this.logs.length > this.maxScrollback) {
|
|
266
|
-
this.logs.splice(0, this.logs.length - this.maxScrollback);
|
|
267
|
-
}
|
|
268
|
-
this.cwd = options.cwd || '/workspace';
|
|
269
|
-
this.replMode = false;
|
|
270
|
-
this.promptOverride = null;
|
|
271
|
-
this.history = Array.isArray(options.history) ? options.history : [];
|
|
272
|
-
this.historyIdx = -1;
|
|
273
|
-
this.savedBuffer = '';
|
|
274
|
-
this.buffer = '';
|
|
275
|
-
this.cursor = 0;
|
|
276
|
-
this.searchMode = false;
|
|
277
|
-
this.searchQuery = '';
|
|
278
|
-
this.searchMatchIdx = -1;
|
|
279
|
-
this.isBusy = false;
|
|
280
|
-
this._themeMode = options.theme || 'dark';
|
|
281
|
-
this.onCommand = typeof options.onCommand === 'function' ? options.onCommand : null;
|
|
282
|
-
this.getCompletions = typeof options.getCompletions === 'function' ? options.getCompletions : null;
|
|
283
|
-
this.onInterrupt = typeof options.onInterrupt === 'function' ? options.onInterrupt : null;
|
|
284
|
-
|
|
285
|
-
if (this.container) {
|
|
286
|
-
this.mount(this.container);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
public getPrompt(): string {
|
|
291
|
-
if (this.promptOverride !== null) return this.promptOverride;
|
|
292
|
-
if (this.replMode) return '> ';
|
|
293
|
-
const cwd = this.cwd || '/workspace';
|
|
294
|
-
return `codelab:${cwd}$ `;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
private _getColoredPrompt(): string {
|
|
298
|
-
if (this.searchMode) {
|
|
299
|
-
return `\x1b[36m(reverse-i-search)\x1b[0m\`${this.searchQuery}': `;
|
|
300
|
-
}
|
|
301
|
-
if (this.promptOverride !== null) {
|
|
302
|
-
return this.promptOverride;
|
|
303
|
-
}
|
|
304
|
-
if (this.replMode) {
|
|
305
|
-
return '\x1b[1;32m>\x1b[0m ';
|
|
306
|
-
}
|
|
307
|
-
const rawCwd = this.cwd || '/workspace';
|
|
308
|
-
const displayCwd = rawCwd === '/home/codelab'
|
|
309
|
-
? '~'
|
|
310
|
-
: rawCwd.startsWith('/home/codelab/')
|
|
311
|
-
? '~' + rawCwd.slice('/home/codelab'.length)
|
|
312
|
-
: rawCwd;
|
|
313
|
-
return `\x1b[1;32mcodelab\x1b[0m:\x1b[1m${displayCwd}\x1b[0m$ `;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
public setPrompt(p: string | null): void {
|
|
317
|
-
this.promptOverride = p;
|
|
318
|
-
this.updateActiveLine();
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
public setCwd(cwd: string): void {
|
|
322
|
-
this.cwd = cwd || '/workspace';
|
|
323
|
-
this.updateActiveLine();
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
public setReplMode(enabled: boolean): void {
|
|
327
|
-
this.replMode = Boolean(enabled);
|
|
328
|
-
this.updateActiveLine();
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
public setTheme(mode: 'dark' | 'light' | 'hc' | 'high-contrast'): void {
|
|
332
|
-
this._themeMode = mode;
|
|
333
|
-
if (this.xterm) {
|
|
334
|
-
this.xterm.options.theme = getXtermTheme(mode);
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
public setFontSize(fontSize: number): void {
|
|
339
|
-
if (Number.isFinite(fontSize) && fontSize >= 10 && fontSize <= 28) {
|
|
340
|
-
this.options.fontSize = fontSize;
|
|
341
|
-
const surface = this.container?.querySelector('#cl-terminal-surface') as HTMLElement | null;
|
|
342
|
-
if (surface) surface.style.fontSize = `${fontSize}px`;
|
|
343
|
-
if (this.xterm) {
|
|
344
|
-
this.xterm.options.fontSize = fontSize;
|
|
345
|
-
this.fit();
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
public setMaxScrollback(max: number): void {
|
|
351
|
-
const clamped = Number.isFinite(max) && max > 0 ? Math.floor(max) : 3000;
|
|
352
|
-
this.maxScrollback = clamped;
|
|
353
|
-
this.options.maxScrollback = clamped;
|
|
354
|
-
if (this.xterm) {
|
|
355
|
-
this.xterm.options.scrollback = clamped;
|
|
356
|
-
}
|
|
357
|
-
if (this.logs.length > clamped) {
|
|
358
|
-
this.logs.splice(0, this.logs.length - clamped);
|
|
359
|
-
}
|
|
360
|
-
const logEl = this.container?.querySelector('#cl-terminal-log');
|
|
361
|
-
if (logEl) {
|
|
362
|
-
while (logEl.children.length > clamped && logEl.firstChild) {
|
|
363
|
-
logEl.removeChild(logEl.firstChild);
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
public syncDomMirrorWithLogs(externalLogs?: Array<{ type: string; text: string }>): void {
|
|
369
|
-
if (Array.isArray(externalLogs) && externalLogs !== this.logs) {
|
|
370
|
-
this.logs.splice(0, this.logs.length, ...externalLogs);
|
|
371
|
-
}
|
|
372
|
-
if (this.logs.length > this.maxScrollback) {
|
|
373
|
-
this.logs.splice(0, this.logs.length - this.maxScrollback);
|
|
374
|
-
}
|
|
375
|
-
const logEl = this.container?.querySelector('#cl-terminal-log');
|
|
376
|
-
if (!logEl) return;
|
|
377
|
-
if (logEl.children.length !== this.logs.length) {
|
|
378
|
-
const prevCount = logEl.children.length;
|
|
379
|
-
logEl.innerHTML = this.logs
|
|
380
|
-
.map((item) => `<div class="codelab-log-line ${sanitizeLogType(item.type)}">${ansiToHtml(item.text)}</div>`)
|
|
381
|
-
.join('');
|
|
382
|
-
if (this.xterm) {
|
|
383
|
-
if (this.logs.length === 0) {
|
|
384
|
-
this.xterm.reset();
|
|
385
|
-
this._promptVisibleInXterm = false;
|
|
386
|
-
this.updateActiveLine();
|
|
387
|
-
} else if (this.logs.length > prevCount) {
|
|
388
|
-
this._eraseXtermPromptIfVisible();
|
|
389
|
-
for (let i = prevCount; i < this.logs.length; i++) {
|
|
390
|
-
const txt = String(this.logs[i]?.text || '');
|
|
391
|
-
if (!txt) continue;
|
|
392
|
-
const withNl = txt.endsWith('\n') ? txt : txt + '\n';
|
|
393
|
-
this.xterm.write(this._toCrLf(withNl));
|
|
394
|
-
}
|
|
395
|
-
this._redrawXtermPromptLine();
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
public isCommandRunning(): boolean {
|
|
402
|
-
return Boolean(this.isBusy && this._readLineQueue.length === 0);
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
public fit(): void {
|
|
406
|
-
if (this._disposed || !this.fitAddon || !this.xterm || !this.container) return;
|
|
407
|
-
const rect = this.container.getBoundingClientRect();
|
|
408
|
-
if (rect.width < 24 || rect.height < 24) return;
|
|
409
|
-
try {
|
|
410
|
-
this.fitAddon.fit();
|
|
411
|
-
if (typeof this.options.onResize === 'function') {
|
|
412
|
-
this.options.onResize(this.xterm.cols, this.xterm.rows);
|
|
413
|
-
}
|
|
414
|
-
} catch (_err) {
|
|
415
|
-
void _err;
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
private _scheduleFit(): void {
|
|
420
|
-
if (this._disposed || typeof requestAnimationFrame !== 'function') return;
|
|
421
|
-
if (this._fitRaf) cancelAnimationFrame(this._fitRaf);
|
|
422
|
-
this._fitRaf = requestAnimationFrame(() => {
|
|
423
|
-
this._fitRaf = 0;
|
|
424
|
-
this.fit();
|
|
425
|
-
});
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
private _toCrLf(raw: string): string {
|
|
429
|
-
return raw.replace(/\r?\n/g, '\r\n');
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
private _eraseXtermPromptIfVisible(): void {
|
|
433
|
-
if (this.xterm && this._promptVisibleInXterm) {
|
|
434
|
-
this.xterm.write('\r\x1b[2K');
|
|
435
|
-
this._promptVisibleInXterm = false;
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
private _redrawXtermPromptLine(): void {
|
|
440
|
-
if (!this.xterm) return;
|
|
441
|
-
if (this.isCommandRunning()) {
|
|
442
|
-
this._promptVisibleInXterm = false;
|
|
443
|
-
return;
|
|
444
|
-
}
|
|
445
|
-
const coloredPrompt = this._getColoredPrompt();
|
|
446
|
-
const afterLen = Math.max(0, this.buffer.length - this.cursor);
|
|
447
|
-
const moveBack = afterLen > 0 ? `\x1b[${afterLen}D` : '';
|
|
448
|
-
this.xterm.write(`\r\x1b[2K${coloredPrompt}${this.buffer}${moveBack}`);
|
|
449
|
-
this._promptVisibleInXterm = true;
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
private _pushMirrorLog(type: string, text: string): { type: string; text: string } {
|
|
453
|
-
const entry = { type: sanitizeLogType(type), text };
|
|
454
|
-
this.logs.push(entry);
|
|
455
|
-
if (this.logs.length > this.maxScrollback) {
|
|
456
|
-
const excess = this.logs.length - this.maxScrollback;
|
|
457
|
-
this.logs.splice(0, excess);
|
|
458
|
-
}
|
|
459
|
-
if (this.container) {
|
|
460
|
-
const logEl = this.container.querySelector('#cl-terminal-log');
|
|
461
|
-
if (logEl) {
|
|
462
|
-
const lineEl = document.createElement('div');
|
|
463
|
-
lineEl.className = 'codelab-log-line ' + entry.type;
|
|
464
|
-
lineEl.innerHTML = ansiToHtml(entry.text);
|
|
465
|
-
logEl.appendChild(lineEl);
|
|
466
|
-
while (logEl.children.length > this.maxScrollback && logEl.firstChild) {
|
|
467
|
-
logEl.removeChild(logEl.firstChild);
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
return entry;
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
private _appendMirrorStream(chunk: string, type: string): void {
|
|
475
|
-
const combined = this._pendingMirrorLine + chunk;
|
|
476
|
-
const clearIdx2 = combined.lastIndexOf('\x1b[2J');
|
|
477
|
-
const clearIdx3 = combined.lastIndexOf('\x1b[3J');
|
|
478
|
-
const clearRis = combined.lastIndexOf('\x1bc');
|
|
479
|
-
const lastClear = Math.max(clearIdx2, clearIdx3, clearRis);
|
|
480
|
-
let working = combined;
|
|
481
|
-
if (lastClear !== -1) {
|
|
482
|
-
const skipLen = lastClear === clearRis ? 2 : 4;
|
|
483
|
-
working = combined.slice(lastClear + skipLen).replace(/^(?:\x1b\[[23]J|\x1b\[(?:\d+;\d+)?H)+/, '');
|
|
484
|
-
this._clearMirrorDomOnly();
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
const normalized = working.replace(/\r\n/g, '\n');
|
|
488
|
-
const parts = normalized.split('\n');
|
|
489
|
-
for (let i = 0; i < parts.length - 1; i++) {
|
|
490
|
-
const resolved = resolveAnsiControlCodes(parts[i]);
|
|
491
|
-
if (this._activeMirrorEl && this.logs.length > 0) {
|
|
492
|
-
this.logs[this.logs.length - 1].text = resolved + '\n';
|
|
493
|
-
this._activeMirrorEl.innerHTML = ansiToHtml(resolved);
|
|
494
|
-
this._activeMirrorEl = null;
|
|
495
|
-
} else {
|
|
496
|
-
this._pushMirrorLog(type, resolved + '\n');
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
const tail = parts[parts.length - 1];
|
|
500
|
-
this._pendingMirrorLine = tail;
|
|
501
|
-
if (tail.length > 0) {
|
|
502
|
-
const resolvedTail = resolveAnsiControlCodes(tail);
|
|
503
|
-
if (this._activeMirrorEl && this.logs.length > 0) {
|
|
504
|
-
this.logs[this.logs.length - 1].text = resolvedTail;
|
|
505
|
-
this._activeMirrorEl.innerHTML = ansiToHtml(resolvedTail);
|
|
506
|
-
} else {
|
|
507
|
-
this._pushMirrorLog(type, resolvedTail);
|
|
508
|
-
const logEl = this.container?.querySelector('#cl-terminal-log');
|
|
509
|
-
this._activeMirrorEl = (logEl?.lastElementChild as HTMLElement) || null;
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
private _clearMirrorDomOnly(): void {
|
|
515
|
-
this.logs.length = 0;
|
|
516
|
-
this._pendingMirrorLine = '';
|
|
517
|
-
this._activeMirrorEl = null;
|
|
518
|
-
const logEl = this.container?.querySelector('#cl-terminal-log');
|
|
519
|
-
if (logEl) logEl.innerHTML = '';
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
public writeStreamChunk(data: string, type: string = 'stdout'): void {
|
|
523
|
-
const str = String(data ?? '');
|
|
524
|
-
if (!str) return;
|
|
525
|
-
|
|
526
|
-
this._eraseXtermPromptIfVisible();
|
|
527
|
-
|
|
528
|
-
// Check if chunk contains screen-clear control codes (\x1b[2J, \x1b[3J, \x1bc)
|
|
529
|
-
const clearIdx2 = str.lastIndexOf('\x1b[2J');
|
|
530
|
-
const clearIdx3 = str.lastIndexOf('\x1b[3J');
|
|
531
|
-
const clearRis = str.lastIndexOf('\x1bc');
|
|
532
|
-
const lastClear = Math.max(clearIdx2, clearIdx3, clearRis);
|
|
533
|
-
|
|
534
|
-
if (lastClear !== -1 && this.xterm) {
|
|
535
|
-
const skipLen = lastClear === clearRis ? 2 : 4;
|
|
536
|
-
const afterClear = str
|
|
537
|
-
.slice(lastClear + skipLen)
|
|
538
|
-
.replace(/^(?:\x1b\[[23]J|\x1b\[(?:\d+;\d+)?H)+/, '');
|
|
539
|
-
this.xterm.reset();
|
|
540
|
-
this._clearMirrorDomOnly();
|
|
541
|
-
if (afterClear) {
|
|
542
|
-
const colored = type === 'stderr' && !afterClear.includes('\x1b[')
|
|
543
|
-
? `\x1b[31m${this._toCrLf(afterClear)}\x1b[0m`
|
|
544
|
-
: this._toCrLf(afterClear);
|
|
545
|
-
this.xterm.write(colored);
|
|
546
|
-
this._appendMirrorStream(afterClear, type);
|
|
547
|
-
}
|
|
548
|
-
} else {
|
|
549
|
-
if (this.xterm) {
|
|
550
|
-
const formatted = type === 'stderr' && !str.includes('\x1b[')
|
|
551
|
-
? `\x1b[31m${this._toCrLf(str)}\x1b[0m`
|
|
552
|
-
: this._toCrLf(str);
|
|
553
|
-
this.xterm.write(formatted);
|
|
554
|
-
}
|
|
555
|
-
this._appendMirrorStream(str, type);
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
if (!this.isCommandRunning()) {
|
|
559
|
-
// Ensure prompt appears on a fresh line if streamed output did not end with \n
|
|
560
|
-
if (this.xterm && this._pendingMirrorLine.length > 0) {
|
|
561
|
-
this.xterm.write('\r\n');
|
|
562
|
-
this._pendingMirrorLine = '';
|
|
563
|
-
this._activeMirrorEl = null;
|
|
564
|
-
}
|
|
565
|
-
this._redrawXtermPromptLine();
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
|
|
569
|
-
public write(data: string, type: string = 'stdout'): void {
|
|
570
|
-
this.writeStreamChunk(data, type);
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
public writeln(data: string, type: string = 'stdout'): void {
|
|
574
|
-
this.writeStreamChunk(String(data ?? '') + '\n', type);
|
|
575
|
-
}
|
|
576
|
-
|
|
577
|
-
public clear(): void {
|
|
578
|
-
this._clearMirrorDomOnly();
|
|
579
|
-
this._promptVisibleInXterm = false;
|
|
580
|
-
if (this.xterm) {
|
|
581
|
-
this.xterm.reset();
|
|
582
|
-
}
|
|
583
|
-
this.updateActiveLine();
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
public getHistory(): string[] {
|
|
587
|
-
return [...this.history];
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
private _activateNextReadLine(): void {
|
|
591
|
-
if (this._readLineQueue.length === 0) return;
|
|
592
|
-
const head = this._readLineQueue[0];
|
|
593
|
-
if (head && !head._activated) {
|
|
594
|
-
head._activated = true;
|
|
595
|
-
head.prevPrompt = this.promptOverride;
|
|
596
|
-
this.promptOverride = head.promptText;
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
private _drainReadLineQueue(result: string | null = null): void {
|
|
601
|
-
if (this._readLineQueue.length === 0) return;
|
|
602
|
-
const queued = this._readLineQueue.splice(0, this._readLineQueue.length);
|
|
603
|
-
let restoredPrompt: string | null | undefined = undefined;
|
|
604
|
-
for (const entry of queued) {
|
|
605
|
-
if (entry._activated && restoredPrompt === undefined) {
|
|
606
|
-
restoredPrompt = entry.prevPrompt;
|
|
607
|
-
}
|
|
608
|
-
if (entry.cleanup) entry.cleanup();
|
|
609
|
-
}
|
|
610
|
-
if (restoredPrompt !== undefined) {
|
|
611
|
-
this.promptOverride = restoredPrompt;
|
|
612
|
-
}
|
|
613
|
-
for (const entry of queued) {
|
|
614
|
-
try {
|
|
615
|
-
entry.resolve(result);
|
|
616
|
-
} catch (_err) {
|
|
617
|
-
void _err;
|
|
618
|
-
}
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
public readLine(promptText: string = '', options: { signal?: AbortSignal } = {}): Promise<string | null> {
|
|
623
|
-
if (options.signal?.aborted) {
|
|
624
|
-
return Promise.resolve(null);
|
|
625
|
-
}
|
|
626
|
-
return new Promise((resolve) => {
|
|
627
|
-
const item: ReadLineQueueItem = {
|
|
628
|
-
promptText: String(promptText ?? ''),
|
|
629
|
-
prevPrompt: null,
|
|
630
|
-
_activated: false,
|
|
631
|
-
cleanup: null,
|
|
632
|
-
resolve
|
|
633
|
-
};
|
|
634
|
-
if (options.signal) {
|
|
635
|
-
const onAbort = () => {
|
|
636
|
-
const idx = this._readLineQueue.indexOf(item);
|
|
637
|
-
if (idx !== -1) {
|
|
638
|
-
this._readLineQueue.splice(idx, 1);
|
|
639
|
-
if (item._activated) {
|
|
640
|
-
this.promptOverride = item.prevPrompt;
|
|
641
|
-
this._activateNextReadLine();
|
|
642
|
-
}
|
|
643
|
-
this.updateActiveLine();
|
|
644
|
-
resolve(null);
|
|
645
|
-
}
|
|
646
|
-
};
|
|
647
|
-
options.signal.addEventListener('abort', onAbort, { once: true });
|
|
648
|
-
item.cleanup = () => {
|
|
649
|
-
options.signal?.removeEventListener('abort', onAbort);
|
|
650
|
-
};
|
|
651
|
-
}
|
|
652
|
-
this._readLineQueue.push(item);
|
|
653
|
-
this._activateNextReadLine();
|
|
654
|
-
this.updateActiveLine();
|
|
655
|
-
});
|
|
656
|
-
}
|
|
657
|
-
|
|
658
|
-
public focus(): void {
|
|
659
|
-
if (this.xterm) {
|
|
660
|
-
try {
|
|
661
|
-
this.xterm.focus();
|
|
662
|
-
return;
|
|
663
|
-
} catch (_err) {
|
|
664
|
-
void _err;
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
const input = this.container?.querySelector('#cl-repl-input') as HTMLInputElement | null;
|
|
668
|
-
input?.focus();
|
|
669
|
-
}
|
|
670
|
-
|
|
671
|
-
private _addDomListener(target: EventTarget, type: string, fn: EventListener): void {
|
|
672
|
-
target.addEventListener(type, fn);
|
|
673
|
-
this._domListeners.push({ target, type, fn });
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
private _cleanupDomListeners(): void {
|
|
677
|
-
for (const l of this._domListeners) {
|
|
678
|
-
try {
|
|
679
|
-
l.target.removeEventListener(l.type, l.fn);
|
|
680
|
-
} catch (_err) {
|
|
681
|
-
void _err;
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
|
-
this._domListeners = [];
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
public mount(container: HTMLElement): void {
|
|
688
|
-
this.destroy();
|
|
689
|
-
this._disposed = false;
|
|
690
|
-
this.container = container;
|
|
691
|
-
|
|
692
|
-
const initialLogsHtml = this.logs
|
|
693
|
-
.map((item) => `<div class="codelab-log-line ${sanitizeLogType(item.type)}">${ansiToHtml(item.text)}</div>`)
|
|
694
|
-
.join('');
|
|
695
|
-
|
|
696
|
-
const fontPx = this.options.fontSize || 13;
|
|
697
|
-
this.container.innerHTML = `
|
|
698
|
-
<div class="codelab-terminal codelab-terminal--xterm" id="cl-terminal-surface" tabindex="0" style="font-size:${fontPx}px;">
|
|
699
|
-
<div class="codelab-xterm-mount" id="cl-xterm-mount"></div>
|
|
700
|
-
<div class="codelab-terminal__mirror" aria-hidden="true">
|
|
701
|
-
<div class="codelab-terminal__log" id="cl-terminal-log">${initialLogsHtml}</div>
|
|
702
|
-
<div class="codelab-pty__active-line" id="cl-pty-active-line">
|
|
703
|
-
<span class="codelab-pty__prompt">${escapeHtml(this.getPrompt())}</span><span class="codelab-pty__before"></span><span class="codelab-pty__cursor blink"> </span><span class="codelab-pty__after"></span>
|
|
704
|
-
</div>
|
|
705
|
-
</div>
|
|
706
|
-
<form class="codelab-repl" id="cl-repl-form" style="position:absolute;opacity:0;width:1px;height:1px;overflow:hidden;">
|
|
707
|
-
<input type="text" class="codelab-repl__input" id="cl-repl-input" autocomplete="off" spellcheck="false" aria-label="Terminal command input" value="${escapeHtml(this.buffer)}">
|
|
708
|
-
</form>
|
|
709
|
-
</div>
|
|
710
|
-
`;
|
|
711
|
-
|
|
712
|
-
const xtermMount = this.container.querySelector('#cl-xterm-mount') as HTMLElement | null;
|
|
713
|
-
const replInput = this.container.querySelector('#cl-repl-input') as HTMLInputElement | null;
|
|
714
|
-
const replForm = this.container.querySelector('#cl-repl-form') as HTMLFormElement | null;
|
|
715
|
-
const surface = this.container.querySelector('#cl-terminal-surface') as HTMLElement | null;
|
|
716
|
-
|
|
717
|
-
if (xtermMount && canUseXtermInEnvironment(xtermMount)) {
|
|
718
|
-
const { Terminal: XtermTerminal, FitAddon: XtermFitAddon, WebLinksAddon: XtermWebLinksAddon } =
|
|
719
|
-
getXtermRuntimeClasses();
|
|
720
|
-
const computedMono =
|
|
721
|
-
typeof window !== 'undefined' && typeof window.getComputedStyle === 'function'
|
|
722
|
-
? window.getComputedStyle(xtermMount).getPropertyValue('--cl-font-mono').trim()
|
|
723
|
-
: '';
|
|
724
|
-
const term = new XtermTerminal({
|
|
725
|
-
fontFamily:
|
|
726
|
-
this.options.fontFamily ||
|
|
727
|
-
computedMono ||
|
|
728
|
-
'"Geist Mono", ui-monospace, SFMono-Regular, "SF Mono", Menlo, Monaco, Consolas, monospace',
|
|
729
|
-
fontSize: this.options.fontSize || 13,
|
|
730
|
-
lineHeight: 1.35,
|
|
731
|
-
cursorBlink: true,
|
|
732
|
-
cursorStyle: 'block',
|
|
733
|
-
convertEol: true,
|
|
734
|
-
scrollback: this.maxScrollback,
|
|
735
|
-
allowProposedApi: true,
|
|
736
|
-
theme: getXtermTheme(this._themeMode)
|
|
737
|
-
});
|
|
738
|
-
|
|
739
|
-
const fitAddon = new XtermFitAddon();
|
|
740
|
-
term.loadAddon(fitAddon);
|
|
741
|
-
|
|
742
|
-
const webLinksAddon = new XtermWebLinksAddon((_event, uri) => {
|
|
743
|
-
if (this.options.onOpenUrl) {
|
|
744
|
-
const handled = this.options.onOpenUrl(uri);
|
|
745
|
-
if (handled === true) return;
|
|
746
|
-
}
|
|
747
|
-
if (typeof window !== 'undefined') {
|
|
748
|
-
window.open(uri, '_blank', 'noopener,noreferrer');
|
|
749
|
-
}
|
|
750
|
-
});
|
|
751
|
-
term.loadAddon(webLinksAddon);
|
|
752
|
-
|
|
753
|
-
// Register file:line:col clickable diagnostic links
|
|
754
|
-
term.registerLinkProvider({
|
|
755
|
-
provideLinks: (bufferLineNumber, callback) => {
|
|
756
|
-
const line = term.buffer.active.getLine(bufferLineNumber - 1);
|
|
757
|
-
if (!line) {
|
|
758
|
-
callback(undefined);
|
|
759
|
-
return;
|
|
760
|
-
}
|
|
761
|
-
const lineText = line.translateToString(true);
|
|
762
|
-
const links: Array<{
|
|
763
|
-
range: { start: { x: number; y: number }; end: { x: number; y: number } };
|
|
764
|
-
text: string;
|
|
765
|
-
activate: () => void;
|
|
766
|
-
}> = [];
|
|
767
|
-
FILE_LINK_RE.lastIndex = 0;
|
|
768
|
-
let m: RegExpExecArray | null;
|
|
769
|
-
while ((m = FILE_LINK_RE.exec(lineText)) !== null) {
|
|
770
|
-
const fullMatch = m[0];
|
|
771
|
-
const relFile = m[1];
|
|
772
|
-
const lineNum = Number(m[2] || m[4] || 1);
|
|
773
|
-
const colNum = Number(m[3] || m[5] || 1);
|
|
774
|
-
const startX = m.index + 1;
|
|
775
|
-
const endX = m.index + fullMatch.length;
|
|
776
|
-
links.push({
|
|
777
|
-
range: {
|
|
778
|
-
start: { x: startX, y: bufferLineNumber },
|
|
779
|
-
end: { x: endX, y: bufferLineNumber }
|
|
780
|
-
},
|
|
781
|
-
text: fullMatch,
|
|
782
|
-
activate: () => {
|
|
783
|
-
if (this.options.onOpenFileLink) {
|
|
784
|
-
this.options.onOpenFileLink(relFile, lineNum, colNum);
|
|
785
|
-
}
|
|
786
|
-
}
|
|
787
|
-
});
|
|
788
|
-
}
|
|
789
|
-
callback(links.length > 0 ? links : undefined);
|
|
790
|
-
}
|
|
791
|
-
});
|
|
792
|
-
|
|
793
|
-
term.attachCustomKeyEventHandler((ev: KeyboardEvent) => {
|
|
794
|
-
if (ev.type !== 'keydown') return true;
|
|
795
|
-
const isMetaOnly = ev.metaKey && !ev.ctrlKey;
|
|
796
|
-
const isCtrlOnly = ev.ctrlKey && !ev.metaKey;
|
|
797
|
-
// Cmd+C (macOS) or Ctrl+Shift+C copies selection when present
|
|
798
|
-
if ((isMetaOnly && (ev.key === 'c' || ev.key === 'C')) || (isCtrlOnly && ev.shiftKey && (ev.key === 'c' || ev.key === 'C'))) {
|
|
799
|
-
if (term.hasSelection() && typeof navigator !== 'undefined' && navigator.clipboard?.writeText) {
|
|
800
|
-
navigator.clipboard.writeText(term.getSelection()).catch(() => {});
|
|
801
|
-
}
|
|
802
|
-
return false;
|
|
803
|
-
}
|
|
804
|
-
// Cmd+K clears terminal on macOS
|
|
805
|
-
if (isMetaOnly && (ev.key === 'k' || ev.key === 'K')) {
|
|
806
|
-
ev.preventDefault();
|
|
807
|
-
this.clear();
|
|
808
|
-
return false;
|
|
809
|
-
}
|
|
810
|
-
// Allow workbench toggle shortcuts (Ctrl+`, Cmd+J, Cmd+P, Cmd+Shift+P) to bubble to workbench
|
|
811
|
-
if (ev.ctrlKey && ev.key === '`') {
|
|
812
|
-
return false;
|
|
813
|
-
}
|
|
814
|
-
if (isMetaOnly && (ev.key === 'p' || ev.key === 'P' || ev.key === 'j' || ev.key === 'J')) {
|
|
815
|
-
return false;
|
|
816
|
-
}
|
|
817
|
-
return true;
|
|
818
|
-
});
|
|
819
|
-
|
|
820
|
-
term.open(xtermMount);
|
|
821
|
-
this.xterm = term;
|
|
822
|
-
this.fitAddon = fitAddon;
|
|
823
|
-
|
|
824
|
-
// Replay initial logs if session already had history before remount
|
|
825
|
-
for (const entry of this.logs) {
|
|
826
|
-
const txt = String(entry.text || '');
|
|
827
|
-
if (!txt) continue;
|
|
828
|
-
const withNewline = txt.endsWith('\n') ? txt : txt + '\n';
|
|
829
|
-
term.write(this._toCrLf(withNewline));
|
|
830
|
-
}
|
|
831
|
-
|
|
832
|
-
term.onData((data) => {
|
|
833
|
-
this._handleXtermData(data);
|
|
834
|
-
});
|
|
835
|
-
|
|
836
|
-
if (typeof ResizeObserver !== 'undefined') {
|
|
837
|
-
this._resizeObserver = new ResizeObserver(() => {
|
|
838
|
-
this._scheduleFit();
|
|
839
|
-
});
|
|
840
|
-
this._resizeObserver.observe(xtermMount);
|
|
841
|
-
}
|
|
842
|
-
|
|
843
|
-
this._scheduleFit();
|
|
844
|
-
this.updateActiveLine();
|
|
845
|
-
}
|
|
846
|
-
|
|
847
|
-
if (surface) {
|
|
848
|
-
this._addDomListener(surface, 'click', (e) => {
|
|
849
|
-
if (e.target === replInput) return;
|
|
850
|
-
if (this.xterm && this.xterm.hasSelection()) return;
|
|
851
|
-
this.focus();
|
|
852
|
-
});
|
|
853
|
-
}
|
|
854
|
-
|
|
855
|
-
if (replInput) {
|
|
856
|
-
const syncFromReplInput = () => {
|
|
857
|
-
if (this.isCommandRunning()) {
|
|
858
|
-
replInput.value = '';
|
|
859
|
-
return;
|
|
860
|
-
}
|
|
861
|
-
const nextVal = replInput.value || '';
|
|
862
|
-
const nextPos = typeof replInput.selectionStart === 'number' ? replInput.selectionStart : nextVal.length;
|
|
863
|
-
if (nextVal !== this.buffer || nextPos !== this.cursor) {
|
|
864
|
-
this.buffer = nextVal;
|
|
865
|
-
this.cursor = nextPos;
|
|
866
|
-
this.updateActiveLine();
|
|
867
|
-
}
|
|
868
|
-
};
|
|
869
|
-
this._addDomListener(replInput, 'keydown', (e) => {
|
|
870
|
-
this.handleKeyDown(e as KeyboardEvent);
|
|
871
|
-
});
|
|
872
|
-
this._addDomListener(replInput, 'paste', (e) => {
|
|
873
|
-
const ce = e as ClipboardEvent;
|
|
874
|
-
const text = ce.clipboardData?.getData('text/plain') || '';
|
|
875
|
-
if (text && text.includes('\n')) {
|
|
876
|
-
ce.preventDefault();
|
|
877
|
-
void this.pasteText(text);
|
|
878
|
-
}
|
|
879
|
-
});
|
|
880
|
-
this._addDomListener(replInput, 'input', syncFromReplInput);
|
|
881
|
-
this._addDomListener(replInput, 'change', syncFromReplInput);
|
|
882
|
-
this._addDomListener(replInput, 'click', syncFromReplInput);
|
|
883
|
-
this._addDomListener(replInput, 'keyup', (e) => {
|
|
884
|
-
const ke = e as KeyboardEvent;
|
|
885
|
-
if (ke.key === 'ArrowLeft' || ke.key === 'ArrowRight' || ke.key === 'Home' || ke.key === 'End') {
|
|
886
|
-
syncFromReplInput();
|
|
887
|
-
}
|
|
888
|
-
});
|
|
889
|
-
}
|
|
890
|
-
|
|
891
|
-
if (replForm) {
|
|
892
|
-
this._addDomListener(replForm, 'submit', (e) => {
|
|
893
|
-
e.preventDefault();
|
|
894
|
-
this.submitLine();
|
|
895
|
-
});
|
|
896
|
-
}
|
|
897
|
-
}
|
|
898
|
-
|
|
899
|
-
private _findReverseSearchMatch(query: string, startIdx?: number): number {
|
|
900
|
-
if (!query || !this.history.length) return -1;
|
|
901
|
-
const from = typeof startIdx === 'number' ? Math.min(startIdx, this.history.length - 1) : this.history.length - 1;
|
|
902
|
-
for (let i = from; i >= 0; i--) {
|
|
903
|
-
if (this.history[i] && this.history[i].includes(query)) return i;
|
|
904
|
-
}
|
|
905
|
-
return -1;
|
|
906
|
-
}
|
|
907
|
-
|
|
908
|
-
private _handleInterrupt(): void {
|
|
909
|
-
const wasBusy = this.isBusy || this._readLineQueue.length > 0;
|
|
910
|
-
this.isBusy = false;
|
|
911
|
-
if (this.xterm) {
|
|
912
|
-
this.xterm.write('^C\r\n');
|
|
913
|
-
this._promptVisibleInXterm = false;
|
|
914
|
-
}
|
|
915
|
-
this._pushMirrorLog('system', wasBusy ? '^C' : `${this.getPrompt()}${this.buffer}^C`);
|
|
916
|
-
this.buffer = '';
|
|
917
|
-
this.cursor = 0;
|
|
918
|
-
this.historyIdx = -1;
|
|
919
|
-
this.searchMode = false;
|
|
920
|
-
this.searchQuery = '';
|
|
921
|
-
this.searchMatchIdx = -1;
|
|
922
|
-
this._drainReadLineQueue(null);
|
|
923
|
-
if (this.replMode) this.replMode = false;
|
|
924
|
-
if (this.onInterrupt) {
|
|
925
|
-
try {
|
|
926
|
-
this.onInterrupt();
|
|
927
|
-
} catch (_err) {
|
|
928
|
-
void _err;
|
|
929
|
-
}
|
|
930
|
-
}
|
|
931
|
-
this.updateActiveLine();
|
|
932
|
-
}
|
|
933
|
-
|
|
934
|
-
private _handleXtermData(data: string): void {
|
|
935
|
-
if (!data) return;
|
|
936
|
-
|
|
937
|
-
// Multi-char paste without ESC prefix
|
|
938
|
-
if (data.length > 1 && !data.startsWith('\x1b')) {
|
|
939
|
-
if (data.includes('\x03')) {
|
|
940
|
-
this._handleInterrupt();
|
|
941
|
-
return;
|
|
942
|
-
}
|
|
943
|
-
void this.pasteText(data);
|
|
944
|
-
return;
|
|
945
|
-
}
|
|
946
|
-
|
|
947
|
-
// Ctrl+C
|
|
948
|
-
if (data === '\x03') {
|
|
949
|
-
this._handleInterrupt();
|
|
950
|
-
return;
|
|
951
|
-
}
|
|
952
|
-
|
|
953
|
-
// Ctrl+L
|
|
954
|
-
if (data === '\x0c') {
|
|
955
|
-
this.clear();
|
|
956
|
-
return;
|
|
957
|
-
}
|
|
958
|
-
|
|
959
|
-
if (this.isCommandRunning()) {
|
|
960
|
-
return;
|
|
961
|
-
}
|
|
962
|
-
|
|
963
|
-
// Ctrl+R (reverse history search)
|
|
964
|
-
if (data === '\x12') {
|
|
965
|
-
if (!this.searchMode) {
|
|
966
|
-
this.searchMode = true;
|
|
967
|
-
this.searchQuery = '';
|
|
968
|
-
this.searchMatchIdx = -1;
|
|
969
|
-
} else if (this.searchQuery) {
|
|
970
|
-
const nextStart = this.searchMatchIdx > 0 ? this.searchMatchIdx - 1 : this.history.length - 1;
|
|
971
|
-
const mIdx = this._findReverseSearchMatch(this.searchQuery, nextStart);
|
|
972
|
-
if (mIdx !== -1) {
|
|
973
|
-
this.searchMatchIdx = mIdx;
|
|
974
|
-
this.buffer = this.history[mIdx];
|
|
975
|
-
this.cursor = this.buffer.length;
|
|
976
|
-
}
|
|
977
|
-
}
|
|
978
|
-
this.updateActiveLine();
|
|
979
|
-
return;
|
|
980
|
-
}
|
|
981
|
-
|
|
982
|
-
if (this.searchMode) {
|
|
983
|
-
if (data === '\x1b' || data === '\x07') {
|
|
984
|
-
this.searchMode = false;
|
|
985
|
-
this.searchQuery = '';
|
|
986
|
-
this.searchMatchIdx = -1;
|
|
987
|
-
this.updateActiveLine();
|
|
988
|
-
return;
|
|
989
|
-
}
|
|
990
|
-
if (data === '\x7f' || data === '\b') {
|
|
991
|
-
this.searchQuery = this.searchQuery.slice(0, -1);
|
|
992
|
-
const mIdx = this._findReverseSearchMatch(this.searchQuery, this.history.length - 1);
|
|
993
|
-
this.searchMatchIdx = mIdx;
|
|
994
|
-
if (mIdx !== -1) {
|
|
995
|
-
this.buffer = this.history[mIdx];
|
|
996
|
-
this.cursor = this.buffer.length;
|
|
997
|
-
}
|
|
998
|
-
this.updateActiveLine();
|
|
999
|
-
return;
|
|
1000
|
-
}
|
|
1001
|
-
if (data === '\r' || data === '\n') {
|
|
1002
|
-
this.searchMode = false;
|
|
1003
|
-
this.searchQuery = '';
|
|
1004
|
-
this.searchMatchIdx = -1;
|
|
1005
|
-
this.submitLine();
|
|
1006
|
-
return;
|
|
1007
|
-
}
|
|
1008
|
-
if (data >= ' ' && !data.startsWith('\x1b')) {
|
|
1009
|
-
this.searchQuery += data;
|
|
1010
|
-
const mIdx = this._findReverseSearchMatch(this.searchQuery, this.history.length - 1);
|
|
1011
|
-
this.searchMatchIdx = mIdx;
|
|
1012
|
-
if (mIdx !== -1) {
|
|
1013
|
-
this.buffer = this.history[mIdx];
|
|
1014
|
-
this.cursor = this.buffer.length;
|
|
1015
|
-
}
|
|
1016
|
-
this.updateActiveLine();
|
|
1017
|
-
return;
|
|
1018
|
-
}
|
|
1019
|
-
this.searchMode = false;
|
|
1020
|
-
this.searchQuery = '';
|
|
1021
|
-
this.searchMatchIdx = -1;
|
|
1022
|
-
}
|
|
1023
|
-
|
|
1024
|
-
// Enter
|
|
1025
|
-
if (data === '\r' || data === '\n') {
|
|
1026
|
-
this.submitLine();
|
|
1027
|
-
return;
|
|
1028
|
-
}
|
|
1029
|
-
|
|
1030
|
-
// Ctrl+D (EOF on empty buffer)
|
|
1031
|
-
if (data === '\x04') {
|
|
1032
|
-
if (this.buffer === '') {
|
|
1033
|
-
if (this._readLineQueue.length > 0) {
|
|
1034
|
-
if (this.xterm) {
|
|
1035
|
-
this.xterm.write('\r\n');
|
|
1036
|
-
this._promptVisibleInXterm = false;
|
|
1037
|
-
}
|
|
1038
|
-
this._drainReadLineQueue(null);
|
|
1039
|
-
this.updateActiveLine();
|
|
1040
|
-
return;
|
|
1041
|
-
}
|
|
1042
|
-
if (this.replMode) {
|
|
1043
|
-
this.replMode = false;
|
|
1044
|
-
if (this.xterm) {
|
|
1045
|
-
this.xterm.write('\r\n');
|
|
1046
|
-
this._promptVisibleInXterm = false;
|
|
1047
|
-
}
|
|
1048
|
-
this.updateActiveLine();
|
|
1049
|
-
return;
|
|
1050
|
-
}
|
|
1051
|
-
}
|
|
1052
|
-
return;
|
|
1053
|
-
}
|
|
1054
|
-
|
|
1055
|
-
// Backspace
|
|
1056
|
-
if (data === '\x7f' || data === '\b') {
|
|
1057
|
-
if (this.cursor > 0) {
|
|
1058
|
-
this.buffer = this.buffer.slice(0, this.cursor - 1) + this.buffer.slice(this.cursor);
|
|
1059
|
-
this.cursor--;
|
|
1060
|
-
this.updateActiveLine();
|
|
1061
|
-
}
|
|
1062
|
-
return;
|
|
1063
|
-
}
|
|
1064
|
-
|
|
1065
|
-
// Delete
|
|
1066
|
-
if (data === '\x1b[3~') {
|
|
1067
|
-
if (this.cursor < this.buffer.length) {
|
|
1068
|
-
this.buffer = this.buffer.slice(0, this.cursor) + this.buffer.slice(this.cursor + 1);
|
|
1069
|
-
this.updateActiveLine();
|
|
1070
|
-
}
|
|
1071
|
-
return;
|
|
1072
|
-
}
|
|
1073
|
-
|
|
1074
|
-
// Tab
|
|
1075
|
-
if (data === '\t') {
|
|
1076
|
-
this.autoComplete();
|
|
1077
|
-
return;
|
|
1078
|
-
}
|
|
1079
|
-
|
|
1080
|
-
// ArrowUp / ArrowDown
|
|
1081
|
-
if (data === '\x1b[A') {
|
|
1082
|
-
if (!this.history.length) return;
|
|
1083
|
-
if (this.historyIdx === -1) {
|
|
1084
|
-
this.savedBuffer = this.buffer;
|
|
1085
|
-
this.historyIdx = this.history.length - 1;
|
|
1086
|
-
} else if (this.historyIdx > 0) {
|
|
1087
|
-
this.historyIdx--;
|
|
1088
|
-
}
|
|
1089
|
-
this.buffer = this.history[this.historyIdx] || '';
|
|
1090
|
-
this.cursor = this.buffer.length;
|
|
1091
|
-
this.updateActiveLine();
|
|
1092
|
-
return;
|
|
1093
|
-
}
|
|
1094
|
-
if (data === '\x1b[B') {
|
|
1095
|
-
if (this.historyIdx !== -1) {
|
|
1096
|
-
if (this.historyIdx < this.history.length - 1) {
|
|
1097
|
-
this.historyIdx++;
|
|
1098
|
-
this.buffer = this.history[this.historyIdx] || '';
|
|
1099
|
-
} else {
|
|
1100
|
-
this.historyIdx = -1;
|
|
1101
|
-
this.buffer = this.savedBuffer || '';
|
|
1102
|
-
}
|
|
1103
|
-
this.cursor = this.buffer.length;
|
|
1104
|
-
this.updateActiveLine();
|
|
1105
|
-
}
|
|
1106
|
-
return;
|
|
1107
|
-
}
|
|
1108
|
-
|
|
1109
|
-
// ArrowLeft / ArrowRight
|
|
1110
|
-
if (data === '\x1b[D') {
|
|
1111
|
-
if (this.cursor > 0) {
|
|
1112
|
-
this.cursor--;
|
|
1113
|
-
this.updateActiveLine();
|
|
1114
|
-
}
|
|
1115
|
-
return;
|
|
1116
|
-
}
|
|
1117
|
-
if (data === '\x1b[C') {
|
|
1118
|
-
if (this.cursor < this.buffer.length) {
|
|
1119
|
-
this.cursor++;
|
|
1120
|
-
this.updateActiveLine();
|
|
1121
|
-
}
|
|
1122
|
-
return;
|
|
1123
|
-
}
|
|
1124
|
-
|
|
1125
|
-
// Word backward / forward (Alt+B / Alt+Left / Ctrl+Left, Alt+F / Alt+Right / Ctrl+Right)
|
|
1126
|
-
if (data === '\x1bb' || data === '\x1b[1;3D' || data === '\x1b[1;5D') {
|
|
1127
|
-
let pos = this.cursor;
|
|
1128
|
-
while (pos > 0 && /\s/.test(this.buffer[pos - 1])) pos--;
|
|
1129
|
-
while (pos > 0 && !/\s/.test(this.buffer[pos - 1])) pos--;
|
|
1130
|
-
this.cursor = pos;
|
|
1131
|
-
this.updateActiveLine();
|
|
1132
|
-
return;
|
|
1133
|
-
}
|
|
1134
|
-
if (data === '\x1bf' || data === '\x1b[1;3C' || data === '\x1b[1;5C') {
|
|
1135
|
-
let pos = this.cursor;
|
|
1136
|
-
while (pos < this.buffer.length && /\s/.test(this.buffer[pos])) pos++;
|
|
1137
|
-
while (pos < this.buffer.length && !/\s/.test(this.buffer[pos])) pos++;
|
|
1138
|
-
this.cursor = pos;
|
|
1139
|
-
this.updateActiveLine();
|
|
1140
|
-
return;
|
|
1141
|
-
}
|
|
1142
|
-
|
|
1143
|
-
// Home / Ctrl+A
|
|
1144
|
-
if (data === '\x01' || data === '\x1b[H' || data === '\x1bOH' || data === '\x1b[1~') {
|
|
1145
|
-
this.cursor = 0;
|
|
1146
|
-
this.updateActiveLine();
|
|
1147
|
-
return;
|
|
1148
|
-
}
|
|
1149
|
-
|
|
1150
|
-
// End / Ctrl+E
|
|
1151
|
-
if (data === '\x05' || data === '\x1b[F' || data === '\x1bOF' || data === '\x1b[4~') {
|
|
1152
|
-
this.cursor = this.buffer.length;
|
|
1153
|
-
this.updateActiveLine();
|
|
1154
|
-
return;
|
|
1155
|
-
}
|
|
1156
|
-
|
|
1157
|
-
// Ctrl+U (kill before cursor)
|
|
1158
|
-
if (data === '\x15') {
|
|
1159
|
-
this.buffer = this.buffer.slice(this.cursor);
|
|
1160
|
-
this.cursor = 0;
|
|
1161
|
-
this.updateActiveLine();
|
|
1162
|
-
return;
|
|
1163
|
-
}
|
|
1164
|
-
|
|
1165
|
-
// Ctrl+K (kill after cursor)
|
|
1166
|
-
if (data === '\x0b') {
|
|
1167
|
-
this.buffer = this.buffer.slice(0, this.cursor);
|
|
1168
|
-
this.updateActiveLine();
|
|
1169
|
-
return;
|
|
1170
|
-
}
|
|
1171
|
-
|
|
1172
|
-
// Ctrl+W / Alt+Backspace (delete word backward)
|
|
1173
|
-
if (data === '\x17' || data === '\x1b\x7f') {
|
|
1174
|
-
let pos = this.cursor;
|
|
1175
|
-
while (pos > 0 && /\s/.test(this.buffer[pos - 1])) pos--;
|
|
1176
|
-
while (pos > 0 && !/\s/.test(this.buffer[pos - 1])) pos--;
|
|
1177
|
-
this.buffer = this.buffer.slice(0, pos) + this.buffer.slice(this.cursor);
|
|
1178
|
-
this.cursor = pos;
|
|
1179
|
-
this.updateActiveLine();
|
|
1180
|
-
return;
|
|
1181
|
-
}
|
|
1182
|
-
|
|
1183
|
-
// Printable character
|
|
1184
|
-
if (data >= ' ' && !data.startsWith('\x1b')) {
|
|
1185
|
-
this.buffer = this.buffer.slice(0, this.cursor) + data + this.buffer.slice(this.cursor);
|
|
1186
|
-
this.cursor += data.length;
|
|
1187
|
-
this.updateActiveLine();
|
|
1188
|
-
}
|
|
1189
|
-
}
|
|
1190
|
-
|
|
1191
|
-
public handleKeyDown(e: KeyboardEvent): void {
|
|
1192
|
-
if (!e) return;
|
|
1193
|
-
const key = e.key;
|
|
1194
|
-
const isCtrlOnly = Boolean(e.ctrlKey && !e.metaKey);
|
|
1195
|
-
const isMetaOnly = Boolean(e.metaKey && !e.ctrlKey);
|
|
1196
|
-
const isCtrl = Boolean(e.ctrlKey || e.metaKey);
|
|
1197
|
-
const isFromReplInput = Boolean(e.target && (e.target as HTMLElement).id === 'cl-repl-input');
|
|
1198
|
-
|
|
1199
|
-
if ((isCtrl && (key === 'l' || key === 'L')) || (isMetaOnly && (key === 'k' || key === 'K'))) {
|
|
1200
|
-
e.preventDefault?.();
|
|
1201
|
-
this.clear();
|
|
1202
|
-
return;
|
|
1203
|
-
}
|
|
1204
|
-
if (isMetaOnly && (key === 'c' || key === 'C' || key === 'v' || key === 'V' || key === 'a' || key === 'A' || key === 'x' || key === 'X')) {
|
|
1205
|
-
return;
|
|
1206
|
-
}
|
|
1207
|
-
if (isCtrlOnly && (key === 'c' || key === 'C')) {
|
|
1208
|
-
if (e.shiftKey) return;
|
|
1209
|
-
e.preventDefault?.();
|
|
1210
|
-
this._handleInterrupt();
|
|
1211
|
-
return;
|
|
1212
|
-
}
|
|
1213
|
-
if (this.isCommandRunning()) {
|
|
1214
|
-
e.preventDefault?.();
|
|
1215
|
-
return;
|
|
1216
|
-
}
|
|
1217
|
-
if (isCtrl && (key === 'r' || key === 'R')) {
|
|
1218
|
-
e.preventDefault?.();
|
|
1219
|
-
this._handleXtermData('\x12');
|
|
1220
|
-
return;
|
|
1221
|
-
}
|
|
1222
|
-
if (key === 'Enter') {
|
|
1223
|
-
e.preventDefault?.();
|
|
1224
|
-
this._handleXtermData('\r');
|
|
1225
|
-
return;
|
|
1226
|
-
}
|
|
1227
|
-
if (key === 'Tab') {
|
|
1228
|
-
e.preventDefault?.();
|
|
1229
|
-
this._handleXtermData('\t');
|
|
1230
|
-
return;
|
|
1231
|
-
}
|
|
1232
|
-
if (key === 'ArrowUp') {
|
|
1233
|
-
e.preventDefault?.();
|
|
1234
|
-
this._handleXtermData('\x1b[A');
|
|
1235
|
-
return;
|
|
1236
|
-
}
|
|
1237
|
-
if (key === 'ArrowDown') {
|
|
1238
|
-
e.preventDefault?.();
|
|
1239
|
-
this._handleXtermData('\x1b[B');
|
|
1240
|
-
return;
|
|
1241
|
-
}
|
|
1242
|
-
if (isCtrlOnly && (key === 'u' || key === 'U')) {
|
|
1243
|
-
e.preventDefault?.();
|
|
1244
|
-
this._handleXtermData('\x15');
|
|
1245
|
-
return;
|
|
1246
|
-
}
|
|
1247
|
-
if (isCtrlOnly && (key === 'k' || key === 'K')) {
|
|
1248
|
-
e.preventDefault?.();
|
|
1249
|
-
this._handleXtermData('\x0b');
|
|
1250
|
-
return;
|
|
1251
|
-
}
|
|
1252
|
-
if (isCtrlOnly && (key === 'w' || key === 'W')) {
|
|
1253
|
-
e.preventDefault?.();
|
|
1254
|
-
this._handleXtermData('\x17');
|
|
1255
|
-
return;
|
|
1256
|
-
}
|
|
1257
|
-
if (isCtrlOnly && (key === 'd' || key === 'D')) {
|
|
1258
|
-
e.preventDefault?.();
|
|
1259
|
-
this._handleXtermData('\x04');
|
|
1260
|
-
return;
|
|
1261
|
-
}
|
|
1262
|
-
if (key === 'Escape') {
|
|
1263
|
-
e.preventDefault?.();
|
|
1264
|
-
this._handleXtermData('\x1b');
|
|
1265
|
-
return;
|
|
1266
|
-
}
|
|
1267
|
-
if (isFromReplInput) {
|
|
1268
|
-
// Allow native HTMLInputElement editing (typing, Backspace, Delete, SelectAll + fill(''), caret arrows)
|
|
1269
|
-
return;
|
|
1270
|
-
}
|
|
1271
|
-
if (key === 'Backspace') {
|
|
1272
|
-
e.preventDefault?.();
|
|
1273
|
-
this._handleXtermData(e.altKey ? '\x1b\x7f' : '\x7f');
|
|
1274
|
-
return;
|
|
1275
|
-
}
|
|
1276
|
-
if (key === 'Delete') {
|
|
1277
|
-
e.preventDefault?.();
|
|
1278
|
-
this._handleXtermData('\x1b[3~');
|
|
1279
|
-
return;
|
|
1280
|
-
}
|
|
1281
|
-
if (key === 'ArrowLeft') {
|
|
1282
|
-
e.preventDefault?.();
|
|
1283
|
-
this._handleXtermData(e.altKey || isCtrlOnly ? '\x1b[1;5D' : '\x1b[D');
|
|
1284
|
-
return;
|
|
1285
|
-
}
|
|
1286
|
-
if (key === 'ArrowRight') {
|
|
1287
|
-
e.preventDefault?.();
|
|
1288
|
-
this._handleXtermData(e.altKey || isCtrlOnly ? '\x1b[1;5C' : '\x1b[C');
|
|
1289
|
-
return;
|
|
1290
|
-
}
|
|
1291
|
-
if (key === 'Home' || (isCtrlOnly && (key === 'a' || key === 'A'))) {
|
|
1292
|
-
e.preventDefault?.();
|
|
1293
|
-
this._handleXtermData('\x01');
|
|
1294
|
-
return;
|
|
1295
|
-
}
|
|
1296
|
-
if (key === 'End' || (isCtrlOnly && (key === 'e' || key === 'E'))) {
|
|
1297
|
-
e.preventDefault?.();
|
|
1298
|
-
this._handleXtermData('\x05');
|
|
1299
|
-
return;
|
|
1300
|
-
}
|
|
1301
|
-
if (e.altKey && (key === 'b' || key === 'B')) {
|
|
1302
|
-
e.preventDefault?.();
|
|
1303
|
-
this._handleXtermData('\x1bb');
|
|
1304
|
-
return;
|
|
1305
|
-
}
|
|
1306
|
-
if (e.altKey && (key === 'f' || key === 'F')) {
|
|
1307
|
-
e.preventDefault?.();
|
|
1308
|
-
this._handleXtermData('\x1bf');
|
|
1309
|
-
return;
|
|
1310
|
-
}
|
|
1311
|
-
if (key && key.length === 1 && !isCtrl && !e.altKey) {
|
|
1312
|
-
e.preventDefault?.();
|
|
1313
|
-
this._handleXtermData(key);
|
|
1314
|
-
}
|
|
1315
|
-
}
|
|
1316
|
-
|
|
1317
|
-
public autoComplete(): void {
|
|
1318
|
-
if (!this.getCompletions) return;
|
|
1319
|
-
const res = this.getCompletions(this.buffer, this.cursor);
|
|
1320
|
-
if (!res || !Array.isArray(res.matches) || res.matches.length === 0) return;
|
|
1321
|
-
|
|
1322
|
-
const matches = res.matches;
|
|
1323
|
-
if (matches.length === 1) {
|
|
1324
|
-
const match = matches[0];
|
|
1325
|
-
const appendChar = match.endsWith('/') ? '' : ' ';
|
|
1326
|
-
const before = this.buffer.slice(0, res.tokenStart);
|
|
1327
|
-
const after = this.buffer.slice(this.cursor);
|
|
1328
|
-
this.buffer = before + match + appendChar + after;
|
|
1329
|
-
this.cursor = before.length + match.length + appendChar.length;
|
|
1330
|
-
this.updateActiveLine();
|
|
1331
|
-
return;
|
|
1332
|
-
}
|
|
1333
|
-
|
|
1334
|
-
let lcp = matches[0];
|
|
1335
|
-
for (let i = 1; i < matches.length; i++) {
|
|
1336
|
-
let j = 0;
|
|
1337
|
-
while (j < lcp.length && j < matches[i].length && lcp[j] === matches[i][j]) j++;
|
|
1338
|
-
lcp = lcp.slice(0, j);
|
|
1339
|
-
}
|
|
1340
|
-
if (lcp.length > res.token.length) {
|
|
1341
|
-
const before = this.buffer.slice(0, res.tokenStart);
|
|
1342
|
-
const after = this.buffer.slice(this.cursor);
|
|
1343
|
-
this.buffer = before + lcp + after;
|
|
1344
|
-
this.cursor = before.length + lcp.length;
|
|
1345
|
-
}
|
|
1346
|
-
|
|
1347
|
-
const candidatesLine = matches.join(' ');
|
|
1348
|
-
if (this.xterm) {
|
|
1349
|
-
this.xterm.write(`\r\n${candidatesLine}\r\n`);
|
|
1350
|
-
this._promptVisibleInXterm = false;
|
|
1351
|
-
}
|
|
1352
|
-
this._pushMirrorLog('system', `${this.getPrompt()}${this.buffer}`);
|
|
1353
|
-
this._pushMirrorLog('stdout', candidatesLine);
|
|
1354
|
-
this.updateActiveLine();
|
|
1355
|
-
}
|
|
1356
|
-
|
|
1357
|
-
public async pasteText(rawText: string): Promise<void> {
|
|
1358
|
-
if (!rawText || this.isCommandRunning()) return;
|
|
1359
|
-
const normalized = String(rawText).replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
|
1360
|
-
if (!normalized.includes('\n')) {
|
|
1361
|
-
this.buffer = this.buffer.slice(0, this.cursor) + normalized + this.buffer.slice(this.cursor);
|
|
1362
|
-
this.cursor += normalized.length;
|
|
1363
|
-
this.updateActiveLine();
|
|
1364
|
-
return;
|
|
1365
|
-
}
|
|
1366
|
-
const parts = normalized.split('\n');
|
|
1367
|
-
for (let i = 0; i < parts.length; i++) {
|
|
1368
|
-
const seg = parts[i];
|
|
1369
|
-
if (i < parts.length - 1) {
|
|
1370
|
-
this.buffer = this.buffer.slice(0, this.cursor) + seg + this.buffer.slice(this.cursor);
|
|
1371
|
-
this.cursor = this.buffer.length;
|
|
1372
|
-
this.updateActiveLine();
|
|
1373
|
-
this.submitLine();
|
|
1374
|
-
while (this.isCommandRunning()) {
|
|
1375
|
-
await new Promise((r) => setTimeout(r, 10));
|
|
1376
|
-
}
|
|
1377
|
-
} else if (seg.length > 0) {
|
|
1378
|
-
this.buffer = seg;
|
|
1379
|
-
this.cursor = seg.length;
|
|
1380
|
-
this.updateActiveLine();
|
|
1381
|
-
}
|
|
1382
|
-
}
|
|
1383
|
-
}
|
|
1384
|
-
|
|
1385
|
-
public submitLine(): void {
|
|
1386
|
-
// 1. Resolve interactive stdin readLine request if one is waiting
|
|
1387
|
-
if (this._readLineQueue.length > 0) {
|
|
1388
|
-
const line = this.buffer;
|
|
1389
|
-
const promptSnap = this.getPrompt();
|
|
1390
|
-
if (this.xterm) {
|
|
1391
|
-
this._redrawXtermPromptLine();
|
|
1392
|
-
this.xterm.write('\r\n');
|
|
1393
|
-
this._promptVisibleInXterm = false;
|
|
1394
|
-
}
|
|
1395
|
-
this._pushMirrorLog('system', `${promptSnap}${line}`);
|
|
1396
|
-
this.buffer = '';
|
|
1397
|
-
this.cursor = 0;
|
|
1398
|
-
this.historyIdx = -1;
|
|
1399
|
-
this.searchMode = false;
|
|
1400
|
-
this.searchQuery = '';
|
|
1401
|
-
this.searchMatchIdx = -1;
|
|
1402
|
-
|
|
1403
|
-
const entry = this._readLineQueue.shift();
|
|
1404
|
-
if (entry) {
|
|
1405
|
-
entry.cleanup?.();
|
|
1406
|
-
this.promptOverride = entry.prevPrompt;
|
|
1407
|
-
this._activateNextReadLine();
|
|
1408
|
-
this.updateActiveLine();
|
|
1409
|
-
entry.resolve(line);
|
|
1410
|
-
}
|
|
1411
|
-
return;
|
|
1412
|
-
}
|
|
1413
|
-
|
|
1414
|
-
if (this.isBusy) return;
|
|
1415
|
-
|
|
1416
|
-
const line = this.buffer;
|
|
1417
|
-
const promptSnap = this.getPrompt();
|
|
1418
|
-
if (this.xterm) {
|
|
1419
|
-
this._redrawXtermPromptLine();
|
|
1420
|
-
this.xterm.write('\r\n');
|
|
1421
|
-
this._promptVisibleInXterm = false;
|
|
1422
|
-
}
|
|
1423
|
-
|
|
1424
|
-
this.buffer = '';
|
|
1425
|
-
this.cursor = 0;
|
|
1426
|
-
this.historyIdx = -1;
|
|
1427
|
-
this.searchMode = false;
|
|
1428
|
-
this.searchQuery = '';
|
|
1429
|
-
this.searchMatchIdx = -1;
|
|
1430
|
-
this._pendingMirrorLine = '';
|
|
1431
|
-
this._activeMirrorEl = null;
|
|
1432
|
-
|
|
1433
|
-
this._pushMirrorLog('system', `${promptSnap}${line}`);
|
|
1434
|
-
|
|
1435
|
-
const trimmed = line.trim();
|
|
1436
|
-
if (trimmed && this.history[this.history.length - 1] !== trimmed) {
|
|
1437
|
-
this.history.push(trimmed);
|
|
1438
|
-
if (this.history.length > this.maxHistory) {
|
|
1439
|
-
this.history.splice(0, this.history.length - this.maxHistory);
|
|
1440
|
-
}
|
|
1441
|
-
}
|
|
1442
|
-
|
|
1443
|
-
if (!this.onCommand) {
|
|
1444
|
-
this.updateActiveLine();
|
|
1445
|
-
return;
|
|
1446
|
-
}
|
|
1447
|
-
|
|
1448
|
-
if (!trimmed) {
|
|
1449
|
-
this.updateActiveLine();
|
|
1450
|
-
return;
|
|
1451
|
-
}
|
|
1452
|
-
|
|
1453
|
-
this.isBusy = true;
|
|
1454
|
-
this.updateActiveLine();
|
|
1455
|
-
|
|
1456
|
-
let cmdRes: unknown;
|
|
1457
|
-
try {
|
|
1458
|
-
cmdRes = this.onCommand(line);
|
|
1459
|
-
} catch (err) {
|
|
1460
|
-
this.isBusy = false;
|
|
1461
|
-
this.updateActiveLine();
|
|
1462
|
-
throw err;
|
|
1463
|
-
}
|
|
1464
|
-
|
|
1465
|
-
if (cmdRes && typeof (cmdRes as Promise<unknown>).then === 'function') {
|
|
1466
|
-
Promise.resolve(cmdRes)
|
|
1467
|
-
.catch(() => {})
|
|
1468
|
-
.finally(() => {
|
|
1469
|
-
if (this._disposed) return;
|
|
1470
|
-
if (this.xterm && this._pendingMirrorLine.length > 0) {
|
|
1471
|
-
this.xterm.write('\r\n');
|
|
1472
|
-
this._pendingMirrorLine = '';
|
|
1473
|
-
this._activeMirrorEl = null;
|
|
1474
|
-
}
|
|
1475
|
-
this.isBusy = false;
|
|
1476
|
-
this.updateActiveLine();
|
|
1477
|
-
});
|
|
1478
|
-
} else {
|
|
1479
|
-
if (this.xterm && this._pendingMirrorLine.length > 0) {
|
|
1480
|
-
this.xterm.write('\r\n');
|
|
1481
|
-
this._pendingMirrorLine = '';
|
|
1482
|
-
this._activeMirrorEl = null;
|
|
1483
|
-
}
|
|
1484
|
-
this.isBusy = false;
|
|
1485
|
-
this.updateActiveLine();
|
|
1486
|
-
}
|
|
1487
|
-
}
|
|
1488
|
-
|
|
1489
|
-
public updateActiveLine(): void {
|
|
1490
|
-
this._redrawXtermPromptLine();
|
|
1491
|
-
|
|
1492
|
-
if (!this.container) return;
|
|
1493
|
-
const input = this.container.querySelector('#cl-repl-input') as HTMLInputElement | null;
|
|
1494
|
-
if (input) {
|
|
1495
|
-
if (input.value !== this.buffer) {
|
|
1496
|
-
input.value = this.buffer;
|
|
1497
|
-
}
|
|
1498
|
-
if (typeof document !== 'undefined' && document.activeElement === input && typeof input.setSelectionRange === 'function') {
|
|
1499
|
-
try {
|
|
1500
|
-
input.setSelectionRange(this.cursor, this.cursor);
|
|
1501
|
-
} catch (_err) {
|
|
1502
|
-
void _err;
|
|
1503
|
-
}
|
|
1504
|
-
}
|
|
1505
|
-
}
|
|
1506
|
-
const lineEl = this.container.querySelector('#cl-pty-active-line') as HTMLElement | null;
|
|
1507
|
-
if (!lineEl) return;
|
|
1508
|
-
|
|
1509
|
-
if (this.isCommandRunning()) {
|
|
1510
|
-
lineEl.setAttribute('data-busy', 'true');
|
|
1511
|
-
lineEl.style.display = 'none';
|
|
1512
|
-
lineEl.innerHTML =
|
|
1513
|
-
'<span class="codelab-pty__prompt"></span><span class="codelab-pty__before"></span><span class="codelab-pty__cursor"></span><span class="codelab-pty__after"></span>';
|
|
1514
|
-
return;
|
|
1515
|
-
}
|
|
1516
|
-
|
|
1517
|
-
lineEl.removeAttribute('data-busy');
|
|
1518
|
-
lineEl.style.display = '';
|
|
1519
|
-
const prompt = this.searchMode
|
|
1520
|
-
? `(reverse-i-search)\`${this.searchQuery}\`: `
|
|
1521
|
-
: this.getPrompt();
|
|
1522
|
-
const before = this.buffer.slice(0, this.cursor);
|
|
1523
|
-
const cursorChar = this.cursor < this.buffer.length ? this.buffer[this.cursor] : ' ';
|
|
1524
|
-
const after = this.cursor < this.buffer.length ? this.buffer.slice(this.cursor + 1) : '';
|
|
1525
|
-
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>`;
|
|
1526
|
-
}
|
|
1527
|
-
|
|
1528
|
-
public render(): void {
|
|
1529
|
-
if (this.container && !this.xterm) {
|
|
1530
|
-
this.mount(this.container);
|
|
1531
|
-
return;
|
|
1532
|
-
}
|
|
1533
|
-
this.updateActiveLine();
|
|
1534
|
-
this._scheduleFit();
|
|
1535
|
-
}
|
|
1536
|
-
|
|
1537
|
-
public destroy(): void {
|
|
1538
|
-
this._disposed = true;
|
|
1539
|
-
this._drainReadLineQueue(null);
|
|
1540
|
-
this._cleanupDomListeners();
|
|
1541
|
-
if (this._fitRaf && typeof cancelAnimationFrame === 'function') {
|
|
1542
|
-
cancelAnimationFrame(this._fitRaf);
|
|
1543
|
-
this._fitRaf = 0;
|
|
1544
|
-
}
|
|
1545
|
-
if (this._resizeObserver) {
|
|
1546
|
-
try {
|
|
1547
|
-
this._resizeObserver.disconnect();
|
|
1548
|
-
} catch (_err) {
|
|
1549
|
-
void _err;
|
|
1550
|
-
}
|
|
1551
|
-
this._resizeObserver = null;
|
|
1552
|
-
}
|
|
1553
|
-
if (this.xterm) {
|
|
1554
|
-
try {
|
|
1555
|
-
this.xterm.dispose();
|
|
1556
|
-
} catch (_err) {
|
|
1557
|
-
void _err;
|
|
1558
|
-
}
|
|
1559
|
-
this.xterm = null;
|
|
1560
|
-
}
|
|
1561
|
-
this.fitAddon = null;
|
|
1562
|
-
this._promptVisibleInXterm = false;
|
|
1563
|
-
}
|
|
1564
|
-
}
|
|
1565
|
-
|
|
1566
|
-
export function createXtermTerminalEngine(
|
|
1567
|
-
container: HTMLElement | null,
|
|
1568
|
-
options: XtermAdapterOptions = {}
|
|
1569
|
-
): XtermTerminalEngine {
|
|
1570
|
-
return new XtermTerminalEngine(container, options);
|
|
1571
|
-
}
|