terminal-pilot 0.0.5 → 0.0.7
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/README.md +40 -0
- package/dist/ansi.js +68 -63
- package/dist/ansi.js.map +7 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +6254 -0
- package/dist/cli.js.map +7 -0
- package/dist/commands/close-session.d.ts +12 -0
- package/dist/commands/close-session.js +1509 -0
- package/dist/commands/close-session.js.map +7 -0
- package/dist/commands/create-session.d.ts +26 -0
- package/dist/commands/create-session.js +1516 -0
- package/dist/commands/create-session.js.map +7 -0
- package/dist/commands/fill.d.ts +10 -0
- package/dist/commands/fill.js +1512 -0
- package/dist/commands/fill.js.map +7 -0
- package/dist/commands/get-session.d.ts +18 -0
- package/dist/commands/get-session.js +1514 -0
- package/dist/commands/get-session.js.map +7 -0
- package/dist/commands/index.d.ts +242 -0
- package/dist/commands/index.js +3973 -0
- package/dist/commands/index.js.map +7 -0
- package/dist/commands/install.d.ts +20 -0
- package/dist/commands/install.js +1480 -0
- package/dist/commands/install.js.map +7 -0
- package/dist/commands/installer.d.ts +43 -0
- package/dist/commands/installer.js +289 -0
- package/dist/commands/installer.js.map +7 -0
- package/dist/commands/list-sessions.d.ts +16 -0
- package/dist/commands/list-sessions.js +1513 -0
- package/dist/commands/list-sessions.js.map +7 -0
- package/dist/commands/press-key.d.ts +10 -0
- package/dist/commands/press-key.js +1512 -0
- package/dist/commands/press-key.js.map +7 -0
- package/dist/commands/read-history.d.ts +16 -0
- package/dist/commands/read-history.js +1511 -0
- package/dist/commands/read-history.js.map +7 -0
- package/dist/commands/read-screen.d.ts +21 -0
- package/dist/commands/read-screen.js +1515 -0
- package/dist/commands/read-screen.js.map +7 -0
- package/dist/commands/resize.d.ts +12 -0
- package/dist/commands/resize.js +1512 -0
- package/dist/commands/resize.js.map +7 -0
- package/dist/commands/runtime.d.ts +36 -0
- package/dist/commands/runtime.js +1262 -0
- package/dist/commands/runtime.js.map +7 -0
- package/dist/commands/screenshot.d.ts +14 -0
- package/dist/commands/screenshot.js +2280 -0
- package/dist/commands/screenshot.js.map +7 -0
- package/dist/commands/send-signal.d.ts +10 -0
- package/dist/commands/send-signal.js +1512 -0
- package/dist/commands/send-signal.js.map +7 -0
- package/dist/commands/type.d.ts +10 -0
- package/dist/commands/type.js +1512 -0
- package/dist/commands/type.js.map +7 -0
- package/dist/commands/uninstall.d.ts +14 -0
- package/dist/commands/uninstall.js +529 -0
- package/dist/commands/uninstall.js.map +7 -0
- package/dist/commands/wait-for-exit.d.ts +14 -0
- package/dist/commands/wait-for-exit.js +1513 -0
- package/dist/commands/wait-for-exit.js.map +7 -0
- package/dist/commands/wait-for.d.ts +20 -0
- package/dist/commands/wait-for.js +1520 -0
- package/dist/commands/wait-for.js.map +7 -0
- package/dist/exports.compile-check.js +1 -1
- package/dist/exports.compile-check.js.map +7 -0
- package/dist/index.js +1118 -5
- package/dist/index.js.map +7 -0
- package/dist/keys.js +58 -49
- package/dist/keys.js.map +7 -0
- package/dist/templates/terminal-pilot.md +45 -0
- package/dist/terminal-buffer.d.ts +9 -1
- package/dist/terminal-buffer.js +525 -425
- package/dist/terminal-buffer.js.map +7 -0
- package/dist/terminal-pilot.js +1105 -35
- package/dist/terminal-pilot.js.map +7 -0
- package/dist/terminal-screen.js +108 -26
- package/dist/terminal-screen.js.map +7 -0
- package/dist/terminal-session.js +1020 -292
- package/dist/terminal-session.js.map +7 -0
- package/dist/testing/cli-repl.d.ts +15 -0
- package/dist/testing/cli-repl.js +6359 -0
- package/dist/testing/cli-repl.js.map +7 -0
- package/dist/testing/qa-cli.d.ts +1 -0
- package/dist/testing/qa-cli.js +7276 -0
- package/dist/testing/qa-cli.js.map +7 -0
- package/package.json +37 -7
|
@@ -0,0 +1,1512 @@
|
|
|
1
|
+
// ../cmdkit/src/index.ts
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
|
|
4
|
+
// ../cmdkit-schema/src/index.ts
|
|
5
|
+
function assertValidEnumValues(values) {
|
|
6
|
+
if (values.length === 0) {
|
|
7
|
+
throw new Error("Enum schema requires at least one value");
|
|
8
|
+
}
|
|
9
|
+
const uniqueValues = new Set(values);
|
|
10
|
+
if (uniqueValues.size !== values.length) {
|
|
11
|
+
throw new Error("Enum schema values must be unique");
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
var S = {
|
|
15
|
+
String(options = {}) {
|
|
16
|
+
return {
|
|
17
|
+
kind: "string",
|
|
18
|
+
...options
|
|
19
|
+
};
|
|
20
|
+
},
|
|
21
|
+
Number(options = {}) {
|
|
22
|
+
return {
|
|
23
|
+
kind: "number",
|
|
24
|
+
...options
|
|
25
|
+
};
|
|
26
|
+
},
|
|
27
|
+
Boolean(options = {}) {
|
|
28
|
+
return {
|
|
29
|
+
kind: "boolean",
|
|
30
|
+
...options
|
|
31
|
+
};
|
|
32
|
+
},
|
|
33
|
+
Enum(values, options = {}) {
|
|
34
|
+
assertValidEnumValues(values);
|
|
35
|
+
return {
|
|
36
|
+
kind: "enum",
|
|
37
|
+
values,
|
|
38
|
+
...options
|
|
39
|
+
};
|
|
40
|
+
},
|
|
41
|
+
Array(item, options = {}) {
|
|
42
|
+
return {
|
|
43
|
+
kind: "array",
|
|
44
|
+
item,
|
|
45
|
+
...options
|
|
46
|
+
};
|
|
47
|
+
},
|
|
48
|
+
Object(shape) {
|
|
49
|
+
return {
|
|
50
|
+
kind: "object",
|
|
51
|
+
shape
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
Optional(inner) {
|
|
55
|
+
return {
|
|
56
|
+
kind: "optional",
|
|
57
|
+
inner
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// ../cmdkit/src/index.ts
|
|
63
|
+
var commandConfigSymbol = /* @__PURE__ */ Symbol("cmdkit.command.config");
|
|
64
|
+
var commandSourcePathSymbol = /* @__PURE__ */ Symbol("cmdkit.command.sourcePath");
|
|
65
|
+
var UserError = class extends Error {
|
|
66
|
+
constructor(message) {
|
|
67
|
+
super(message);
|
|
68
|
+
this.name = "UserError";
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
function cloneScope(scope) {
|
|
72
|
+
return scope === void 0 ? void 0 : [...scope];
|
|
73
|
+
}
|
|
74
|
+
function cloneSecretDefinition(secret) {
|
|
75
|
+
return {
|
|
76
|
+
env: secret.env,
|
|
77
|
+
description: secret.description,
|
|
78
|
+
optional: secret.optional
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function cloneSecrets(secrets) {
|
|
82
|
+
if (secrets === void 0) {
|
|
83
|
+
return {};
|
|
84
|
+
}
|
|
85
|
+
return Object.fromEntries(
|
|
86
|
+
Object.entries(secrets).map(([key, secret]) => [key, cloneSecretDefinition(secret)])
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
function cloneRequires(requires) {
|
|
90
|
+
if (requires === void 0) {
|
|
91
|
+
return void 0;
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
auth: requires.auth,
|
|
95
|
+
apiVersion: requires.apiVersion,
|
|
96
|
+
check: requires.check
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function parseStackPath(candidate) {
|
|
100
|
+
if (candidate.startsWith("file://")) {
|
|
101
|
+
try {
|
|
102
|
+
return fileURLToPath(candidate);
|
|
103
|
+
} catch {
|
|
104
|
+
return void 0;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (candidate.startsWith("/")) {
|
|
108
|
+
return candidate;
|
|
109
|
+
}
|
|
110
|
+
return void 0;
|
|
111
|
+
}
|
|
112
|
+
function extractStackPath(line) {
|
|
113
|
+
const trimmed = line.trim();
|
|
114
|
+
const fileIndex = trimmed.indexOf("file://");
|
|
115
|
+
if (fileIndex >= 0) {
|
|
116
|
+
const location2 = trimmed.slice(fileIndex);
|
|
117
|
+
const separatorIndex2 = location2.lastIndexOf(":");
|
|
118
|
+
const previousSeparatorIndex2 = separatorIndex2 >= 0 ? location2.lastIndexOf(":", separatorIndex2 - 1) : -1;
|
|
119
|
+
const candidate2 = separatorIndex2 >= 0 && previousSeparatorIndex2 >= 0 ? location2.slice(0, previousSeparatorIndex2) : location2;
|
|
120
|
+
return parseStackPath(candidate2);
|
|
121
|
+
}
|
|
122
|
+
const slashIndex = trimmed.indexOf("/");
|
|
123
|
+
if (slashIndex < 0) {
|
|
124
|
+
return void 0;
|
|
125
|
+
}
|
|
126
|
+
const location = trimmed.slice(slashIndex);
|
|
127
|
+
const separatorIndex = location.lastIndexOf(":");
|
|
128
|
+
const previousSeparatorIndex = separatorIndex >= 0 ? location.lastIndexOf(":", separatorIndex - 1) : -1;
|
|
129
|
+
const candidate = separatorIndex >= 0 && previousSeparatorIndex >= 0 ? location.slice(0, previousSeparatorIndex) : location;
|
|
130
|
+
return parseStackPath(candidate);
|
|
131
|
+
}
|
|
132
|
+
function inferCommandSourcePath() {
|
|
133
|
+
const stack = new Error().stack;
|
|
134
|
+
if (typeof stack !== "string") {
|
|
135
|
+
return void 0;
|
|
136
|
+
}
|
|
137
|
+
for (const line of stack.split("\n").slice(1)) {
|
|
138
|
+
const candidate = extractStackPath(line);
|
|
139
|
+
if (candidate === void 0) {
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (candidate.includes("/packages/cmdkit/src/index.ts") || candidate.includes("/packages/cmdkit/dist/index.js")) {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
return candidate;
|
|
146
|
+
}
|
|
147
|
+
return void 0;
|
|
148
|
+
}
|
|
149
|
+
function composeChecks(parentCheck, childCheck) {
|
|
150
|
+
if (parentCheck === void 0) {
|
|
151
|
+
return childCheck;
|
|
152
|
+
}
|
|
153
|
+
if (childCheck === void 0) {
|
|
154
|
+
return parentCheck;
|
|
155
|
+
}
|
|
156
|
+
return async (ctx) => {
|
|
157
|
+
const parentResult = await parentCheck(ctx);
|
|
158
|
+
if (!parentResult.ok) {
|
|
159
|
+
return parentResult;
|
|
160
|
+
}
|
|
161
|
+
return childCheck(ctx);
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
function mergeRequires(parent, child) {
|
|
165
|
+
if (parent === void 0 && child === void 0) {
|
|
166
|
+
return void 0;
|
|
167
|
+
}
|
|
168
|
+
const merged = {
|
|
169
|
+
auth: child?.auth ?? parent?.auth,
|
|
170
|
+
apiVersion: child?.apiVersion ?? parent?.apiVersion,
|
|
171
|
+
check: composeChecks(parent?.check, child?.check)
|
|
172
|
+
};
|
|
173
|
+
if (merged.auth === void 0 && merged.apiVersion === void 0 && merged.check === void 0) {
|
|
174
|
+
return void 0;
|
|
175
|
+
}
|
|
176
|
+
return merged;
|
|
177
|
+
}
|
|
178
|
+
function mergeSecrets(parent, child) {
|
|
179
|
+
return cloneSecrets({
|
|
180
|
+
...parent,
|
|
181
|
+
...child
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
function resolveCommandScope(ownScope, inheritedScope) {
|
|
185
|
+
return cloneScope(ownScope ?? inheritedScope) ?? ["cli", "sdk"];
|
|
186
|
+
}
|
|
187
|
+
function createBaseCommand(config) {
|
|
188
|
+
const command = {
|
|
189
|
+
kind: "command",
|
|
190
|
+
name: config.name,
|
|
191
|
+
description: config.description,
|
|
192
|
+
aliases: [...config.aliases ?? []],
|
|
193
|
+
positional: [...config.positional ?? []],
|
|
194
|
+
params: config.params,
|
|
195
|
+
secrets: cloneSecrets(config.secrets),
|
|
196
|
+
scope: resolveCommandScope(config.scope, void 0),
|
|
197
|
+
confirm: config.confirm ?? false,
|
|
198
|
+
requires: cloneRequires(config.requires),
|
|
199
|
+
handler: config.handler,
|
|
200
|
+
render: config.render
|
|
201
|
+
};
|
|
202
|
+
Object.defineProperty(command, commandConfigSymbol, {
|
|
203
|
+
value: {
|
|
204
|
+
scope: cloneScope(config.scope),
|
|
205
|
+
secrets: cloneSecrets(config.secrets),
|
|
206
|
+
requires: cloneRequires(config.requires),
|
|
207
|
+
sourcePath: inferCommandSourcePath()
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
return command;
|
|
211
|
+
}
|
|
212
|
+
function getInternalCommandConfig(command) {
|
|
213
|
+
return command[commandConfigSymbol];
|
|
214
|
+
}
|
|
215
|
+
function materializeCommand(command, inherited) {
|
|
216
|
+
const internal = getInternalCommandConfig(command);
|
|
217
|
+
const materialized = {
|
|
218
|
+
kind: "command",
|
|
219
|
+
name: command.name,
|
|
220
|
+
description: command.description,
|
|
221
|
+
aliases: [...command.aliases],
|
|
222
|
+
positional: [...command.positional],
|
|
223
|
+
params: command.params,
|
|
224
|
+
secrets: mergeSecrets(inherited.secrets, internal.secrets),
|
|
225
|
+
scope: resolveCommandScope(internal.scope, inherited.scope),
|
|
226
|
+
confirm: command.confirm,
|
|
227
|
+
requires: mergeRequires(inherited.requires, internal.requires),
|
|
228
|
+
handler: command.handler,
|
|
229
|
+
render: command.render
|
|
230
|
+
};
|
|
231
|
+
Object.defineProperty(materialized, commandConfigSymbol, {
|
|
232
|
+
value: {
|
|
233
|
+
scope: cloneScope(internal.scope),
|
|
234
|
+
secrets: cloneSecrets(internal.secrets),
|
|
235
|
+
requires: cloneRequires(internal.requires),
|
|
236
|
+
sourcePath: internal.sourcePath
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
Object.defineProperty(materialized, commandSourcePathSymbol, {
|
|
240
|
+
value: internal.sourcePath
|
|
241
|
+
});
|
|
242
|
+
return materialized;
|
|
243
|
+
}
|
|
244
|
+
function defineCommand(config) {
|
|
245
|
+
return materializeCommand(createBaseCommand(config), {
|
|
246
|
+
scope: void 0,
|
|
247
|
+
secrets: {},
|
|
248
|
+
requires: void 0
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// src/terminal-pilot.ts
|
|
253
|
+
import { randomUUID } from "node:crypto";
|
|
254
|
+
|
|
255
|
+
// src/terminal-session.ts
|
|
256
|
+
import { spawn as spawnChildProcess } from "node:child_process";
|
|
257
|
+
import { EventEmitter } from "node:events";
|
|
258
|
+
import * as nodePty from "node-pty";
|
|
259
|
+
|
|
260
|
+
// src/ansi.ts
|
|
261
|
+
var ESC = 27;
|
|
262
|
+
var BEL = 7;
|
|
263
|
+
var ST = 156;
|
|
264
|
+
var CSI = 155;
|
|
265
|
+
var OSC = 157;
|
|
266
|
+
var DCS = 144;
|
|
267
|
+
var SOS = 152;
|
|
268
|
+
var PM = 158;
|
|
269
|
+
var APC = 159;
|
|
270
|
+
function stripAnsi(input) {
|
|
271
|
+
let output = "";
|
|
272
|
+
for (let index = 0; index < input.length; index += 1) {
|
|
273
|
+
const code = input.charCodeAt(index);
|
|
274
|
+
if (code === ESC) {
|
|
275
|
+
const nextCode = input.charCodeAt(index + 1);
|
|
276
|
+
if (nextCode === 91) {
|
|
277
|
+
index = consumeCsi(input, index + 2);
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
if (nextCode === 93) {
|
|
281
|
+
index = consumeTerminatedString(input, index + 2, true);
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
if (nextCode === 80 || nextCode === 88 || nextCode === 94 || nextCode === 95) {
|
|
285
|
+
index = consumeTerminatedString(input, index + 2, false);
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
if (!Number.isNaN(nextCode)) {
|
|
289
|
+
index += 1;
|
|
290
|
+
}
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
if (code === CSI) {
|
|
294
|
+
index = consumeCsi(input, index + 1);
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
if (code === OSC) {
|
|
298
|
+
index = consumeTerminatedString(input, index + 1, true);
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
if (code === DCS || code === SOS || code === PM || code === APC) {
|
|
302
|
+
index = consumeTerminatedString(input, index + 1, false);
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
output += input[index];
|
|
306
|
+
}
|
|
307
|
+
return output;
|
|
308
|
+
}
|
|
309
|
+
function consumeCsi(input, index) {
|
|
310
|
+
while (index < input.length) {
|
|
311
|
+
const code = input.charCodeAt(index);
|
|
312
|
+
if (code >= 64 && code <= 126) {
|
|
313
|
+
return index;
|
|
314
|
+
}
|
|
315
|
+
index += 1;
|
|
316
|
+
}
|
|
317
|
+
return input.length;
|
|
318
|
+
}
|
|
319
|
+
function consumeTerminatedString(input, index, allowBellTerminator) {
|
|
320
|
+
while (index < input.length) {
|
|
321
|
+
const code = input.charCodeAt(index);
|
|
322
|
+
if (code === ST || allowBellTerminator && code === BEL) {
|
|
323
|
+
return index;
|
|
324
|
+
}
|
|
325
|
+
if (code === ESC && input.charCodeAt(index + 1) === 92) {
|
|
326
|
+
return index + 1;
|
|
327
|
+
}
|
|
328
|
+
index += 1;
|
|
329
|
+
}
|
|
330
|
+
return input.length;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// src/terminal-buffer.ts
|
|
334
|
+
var RESET_SGR = "\x1B[0m";
|
|
335
|
+
var TerminalBuffer = class {
|
|
336
|
+
_cols;
|
|
337
|
+
_rows;
|
|
338
|
+
_screen;
|
|
339
|
+
_cursorX = 0;
|
|
340
|
+
_cursorY = 0;
|
|
341
|
+
_savedCursor = { x: 0, y: 0 };
|
|
342
|
+
_scrollTop = 0;
|
|
343
|
+
_scrollBottom;
|
|
344
|
+
_state = 0 /* Normal */;
|
|
345
|
+
_csiParams = "";
|
|
346
|
+
_csiPrivate = "";
|
|
347
|
+
_style = createDefaultStyleState();
|
|
348
|
+
_styleSequence = "";
|
|
349
|
+
displayBuffer;
|
|
350
|
+
constructor(cols, rows) {
|
|
351
|
+
this._cols = cols;
|
|
352
|
+
this._rows = rows;
|
|
353
|
+
this._scrollBottom = rows - 1;
|
|
354
|
+
this._screen = this._makeScreen(cols, rows);
|
|
355
|
+
this.displayBuffer = Object.defineProperties(
|
|
356
|
+
{},
|
|
357
|
+
{
|
|
358
|
+
cursorX: { get: () => this._cursorX, enumerable: true },
|
|
359
|
+
cursorY: { get: () => this._cursorY, enumerable: true },
|
|
360
|
+
data: { get: () => this._screen, enumerable: true }
|
|
361
|
+
}
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
write(data) {
|
|
365
|
+
for (const ch of data) {
|
|
366
|
+
this._feed(ch);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
renderLine(row) {
|
|
370
|
+
const cells = this._screen[row] ?? [];
|
|
371
|
+
let lastVisibleCell = -1;
|
|
372
|
+
for (let index = cells.length - 1; index >= 0; index -= 1) {
|
|
373
|
+
if (cells[index] !== null) {
|
|
374
|
+
lastVisibleCell = index;
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
if (lastVisibleCell === -1) {
|
|
379
|
+
return "";
|
|
380
|
+
}
|
|
381
|
+
let line = "";
|
|
382
|
+
let activeStyle = "";
|
|
383
|
+
for (let index = 0; index <= lastVisibleCell; index += 1) {
|
|
384
|
+
const cell = cells[index];
|
|
385
|
+
const cellStyle = cell?.style ?? "";
|
|
386
|
+
if (cellStyle !== activeStyle) {
|
|
387
|
+
line += cellStyle.length > 0 ? cellStyle : RESET_SGR;
|
|
388
|
+
activeStyle = cellStyle;
|
|
389
|
+
}
|
|
390
|
+
line += cell?.[1] ?? " ";
|
|
391
|
+
}
|
|
392
|
+
if (activeStyle.length > 0) {
|
|
393
|
+
line += RESET_SGR;
|
|
394
|
+
}
|
|
395
|
+
return line;
|
|
396
|
+
}
|
|
397
|
+
resize(cols, rows) {
|
|
398
|
+
while (this._screen.length < rows) {
|
|
399
|
+
this._screen.push(this._makeRow(cols));
|
|
400
|
+
}
|
|
401
|
+
this._screen.length = rows;
|
|
402
|
+
for (let y = 0; y < rows; y++) {
|
|
403
|
+
const row = this._screen[y] ?? this._makeRow(cols);
|
|
404
|
+
while (row.length < cols) row.push(null);
|
|
405
|
+
row.length = cols;
|
|
406
|
+
this._screen[y] = row;
|
|
407
|
+
}
|
|
408
|
+
this._cols = cols;
|
|
409
|
+
this._rows = rows;
|
|
410
|
+
this._scrollTop = 0;
|
|
411
|
+
this._scrollBottom = rows - 1;
|
|
412
|
+
this._cursorX = this._clamp(this._cursorX, 0, cols - 1);
|
|
413
|
+
this._cursorY = this._clamp(this._cursorY, 0, rows - 1);
|
|
414
|
+
}
|
|
415
|
+
_makeScreen(cols, rows) {
|
|
416
|
+
return Array.from({ length: rows }, () => this._makeRow(cols));
|
|
417
|
+
}
|
|
418
|
+
_makeRow(cols) {
|
|
419
|
+
return Array(cols).fill(null);
|
|
420
|
+
}
|
|
421
|
+
_clamp(value, min, max) {
|
|
422
|
+
return Math.max(min, Math.min(max, value));
|
|
423
|
+
}
|
|
424
|
+
_setChar(y, x, ch) {
|
|
425
|
+
const row = this._screen[y];
|
|
426
|
+
if (row && x >= 0 && x < this._cols) {
|
|
427
|
+
const cell = [ch.charCodeAt(0), ch];
|
|
428
|
+
if (this._styleSequence.length > 0) {
|
|
429
|
+
Object.defineProperty(cell, "style", {
|
|
430
|
+
value: this._styleSequence,
|
|
431
|
+
writable: true,
|
|
432
|
+
configurable: true
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
row[x] = cell;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
_eraseLine(y, fromX, toX) {
|
|
439
|
+
const row = this._screen[y];
|
|
440
|
+
if (!row) return;
|
|
441
|
+
for (let x = fromX; x <= toX && x < this._cols; x++) {
|
|
442
|
+
row[x] = null;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
_scrollUp(count) {
|
|
446
|
+
for (let i = 0; i < count; i++) {
|
|
447
|
+
this._screen.splice(this._scrollTop, 1);
|
|
448
|
+
this._screen.splice(this._scrollBottom, 0, this._makeRow(this._cols));
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
_scrollDown(count) {
|
|
452
|
+
for (let i = 0; i < count; i++) {
|
|
453
|
+
this._screen.splice(this._scrollBottom, 1);
|
|
454
|
+
this._screen.splice(this._scrollTop, 0, this._makeRow(this._cols));
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
_newline() {
|
|
458
|
+
if (this._cursorY === this._scrollBottom) {
|
|
459
|
+
this._scrollUp(1);
|
|
460
|
+
} else {
|
|
461
|
+
this._cursorY = Math.min(this._cursorY + 1, this._rows - 1);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
_parseCsiParams() {
|
|
465
|
+
if (!this._csiParams) return [];
|
|
466
|
+
return this._csiParams.split(";").map((s) => s === "" ? 0 : parseInt(s, 10));
|
|
467
|
+
}
|
|
468
|
+
_execCsi(final) {
|
|
469
|
+
const params2 = this._parseCsiParams();
|
|
470
|
+
const p0 = params2[0] ?? 0;
|
|
471
|
+
const p1 = params2[1] ?? 0;
|
|
472
|
+
if (this._csiPrivate === "?") {
|
|
473
|
+
if (final === "h" || final === "l") {
|
|
474
|
+
if (params2.includes(1049)) {
|
|
475
|
+
if (final === "h") {
|
|
476
|
+
this._screen = this._makeScreen(this._cols, this._rows);
|
|
477
|
+
this._cursorX = 0;
|
|
478
|
+
this._cursorY = 0;
|
|
479
|
+
} else {
|
|
480
|
+
this._screen = this._makeScreen(this._cols, this._rows);
|
|
481
|
+
this._cursorX = 0;
|
|
482
|
+
this._cursorY = 0;
|
|
483
|
+
}
|
|
484
|
+
this._resetStyle();
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
switch (final) {
|
|
490
|
+
case "A":
|
|
491
|
+
this._cursorY = this._clamp(this._cursorY - Math.max(1, p0), 0, this._rows - 1);
|
|
492
|
+
break;
|
|
493
|
+
case "B":
|
|
494
|
+
this._cursorY = this._clamp(this._cursorY + Math.max(1, p0), 0, this._rows - 1);
|
|
495
|
+
break;
|
|
496
|
+
case "C":
|
|
497
|
+
// cursor forward
|
|
498
|
+
case "a":
|
|
499
|
+
this._cursorX = this._clamp(this._cursorX + Math.max(1, p0), 0, this._cols - 1);
|
|
500
|
+
break;
|
|
501
|
+
case "D":
|
|
502
|
+
this._cursorX = this._clamp(this._cursorX - Math.max(1, p0), 0, this._cols - 1);
|
|
503
|
+
break;
|
|
504
|
+
case "E":
|
|
505
|
+
this._cursorY = this._clamp(this._cursorY + Math.max(1, p0), 0, this._rows - 1);
|
|
506
|
+
this._cursorX = 0;
|
|
507
|
+
break;
|
|
508
|
+
case "F":
|
|
509
|
+
this._cursorY = this._clamp(this._cursorY - Math.max(1, p0), 0, this._rows - 1);
|
|
510
|
+
this._cursorX = 0;
|
|
511
|
+
break;
|
|
512
|
+
case "G":
|
|
513
|
+
// cursor horizontal absolute
|
|
514
|
+
case "`":
|
|
515
|
+
this._cursorX = this._clamp(Math.max(1, p0) - 1, 0, this._cols - 1);
|
|
516
|
+
break;
|
|
517
|
+
case "H":
|
|
518
|
+
// cursor position
|
|
519
|
+
case "f":
|
|
520
|
+
this._cursorY = this._clamp(Math.max(1, p0) - 1, 0, this._rows - 1);
|
|
521
|
+
this._cursorX = this._clamp(Math.max(1, p1) - 1, 0, this._cols - 1);
|
|
522
|
+
break;
|
|
523
|
+
case "I":
|
|
524
|
+
for (let i = 0; i < Math.max(1, p0); i++) {
|
|
525
|
+
this._cursorX = Math.min(this._cols - 1, (Math.floor(this._cursorX / 8) + 1) * 8);
|
|
526
|
+
}
|
|
527
|
+
break;
|
|
528
|
+
case "J":
|
|
529
|
+
if (p0 === 0) {
|
|
530
|
+
this._eraseLine(this._cursorY, this._cursorX, this._cols - 1);
|
|
531
|
+
for (let y = this._cursorY + 1; y < this._rows; y++) this._eraseLine(y, 0, this._cols - 1);
|
|
532
|
+
} else if (p0 === 1) {
|
|
533
|
+
for (let y = 0; y < this._cursorY; y++) this._eraseLine(y, 0, this._cols - 1);
|
|
534
|
+
this._eraseLine(this._cursorY, 0, this._cursorX);
|
|
535
|
+
} else if (p0 === 2 || p0 === 3) {
|
|
536
|
+
for (let y = 0; y < this._rows; y++) this._eraseLine(y, 0, this._cols - 1);
|
|
537
|
+
}
|
|
538
|
+
break;
|
|
539
|
+
case "K":
|
|
540
|
+
if (p0 === 0) this._eraseLine(this._cursorY, this._cursorX, this._cols - 1);
|
|
541
|
+
else if (p0 === 1) this._eraseLine(this._cursorY, 0, this._cursorX);
|
|
542
|
+
else if (p0 === 2) this._eraseLine(this._cursorY, 0, this._cols - 1);
|
|
543
|
+
break;
|
|
544
|
+
case "X":
|
|
545
|
+
this._eraseLine(this._cursorY, this._cursorX, this._cursorX + Math.max(1, p0) - 1);
|
|
546
|
+
break;
|
|
547
|
+
case "L": {
|
|
548
|
+
const n = Math.max(1, p0);
|
|
549
|
+
for (let i = 0; i < n; i++) {
|
|
550
|
+
this._screen.splice(this._scrollBottom, 1);
|
|
551
|
+
this._screen.splice(this._cursorY, 0, this._makeRow(this._cols));
|
|
552
|
+
}
|
|
553
|
+
break;
|
|
554
|
+
}
|
|
555
|
+
case "M": {
|
|
556
|
+
const n = Math.max(1, p0);
|
|
557
|
+
for (let i = 0; i < n; i++) {
|
|
558
|
+
this._screen.splice(this._cursorY, 1);
|
|
559
|
+
this._screen.splice(this._scrollBottom, 0, this._makeRow(this._cols));
|
|
560
|
+
}
|
|
561
|
+
break;
|
|
562
|
+
}
|
|
563
|
+
case "P": {
|
|
564
|
+
const row = this._screen[this._cursorY];
|
|
565
|
+
if (row) {
|
|
566
|
+
const n = Math.max(1, p0);
|
|
567
|
+
row.splice(this._cursorX, n);
|
|
568
|
+
while (row.length < this._cols) row.push(null);
|
|
569
|
+
}
|
|
570
|
+
break;
|
|
571
|
+
}
|
|
572
|
+
case "@": {
|
|
573
|
+
const row = this._screen[this._cursorY];
|
|
574
|
+
if (row) {
|
|
575
|
+
const n = Math.max(1, p0);
|
|
576
|
+
for (let i = 0; i < n; i++) row.splice(this._cursorX, 0, null);
|
|
577
|
+
row.splice(this._cols);
|
|
578
|
+
}
|
|
579
|
+
break;
|
|
580
|
+
}
|
|
581
|
+
case "S":
|
|
582
|
+
this._scrollUp(Math.max(1, p0));
|
|
583
|
+
break;
|
|
584
|
+
case "T":
|
|
585
|
+
if (params2.length <= 1) this._scrollDown(Math.max(1, p0));
|
|
586
|
+
break;
|
|
587
|
+
case "Z": {
|
|
588
|
+
const n = Math.max(1, p0);
|
|
589
|
+
for (let i = 0; i < n; i++) {
|
|
590
|
+
this._cursorX = Math.max(0, (Math.ceil(this._cursorX / 8) - 1) * 8);
|
|
591
|
+
}
|
|
592
|
+
break;
|
|
593
|
+
}
|
|
594
|
+
case "d":
|
|
595
|
+
this._cursorY = this._clamp(Math.max(1, p0) - 1, 0, this._rows - 1);
|
|
596
|
+
break;
|
|
597
|
+
case "e":
|
|
598
|
+
this._cursorY = this._clamp(this._cursorY + Math.max(1, p0), 0, this._rows - 1);
|
|
599
|
+
break;
|
|
600
|
+
case "r": {
|
|
601
|
+
const top = this._clamp(Math.max(1, p0) - 1, 0, this._rows - 1);
|
|
602
|
+
const bottom = this._clamp((p1 === 0 ? this._rows : p1) - 1, 0, this._rows - 1);
|
|
603
|
+
if (top < bottom) {
|
|
604
|
+
this._scrollTop = top;
|
|
605
|
+
this._scrollBottom = bottom;
|
|
606
|
+
}
|
|
607
|
+
this._cursorX = 0;
|
|
608
|
+
this._cursorY = 0;
|
|
609
|
+
break;
|
|
610
|
+
}
|
|
611
|
+
case "s":
|
|
612
|
+
this._savedCursor = { x: this._cursorX, y: this._cursorY };
|
|
613
|
+
break;
|
|
614
|
+
case "u":
|
|
615
|
+
this._cursorX = this._clamp(this._savedCursor.x, 0, this._cols - 1);
|
|
616
|
+
this._cursorY = this._clamp(this._savedCursor.y, 0, this._rows - 1);
|
|
617
|
+
break;
|
|
618
|
+
case "m":
|
|
619
|
+
this._applySgr(params2);
|
|
620
|
+
break;
|
|
621
|
+
default:
|
|
622
|
+
break;
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
_feed(ch) {
|
|
626
|
+
const code = ch.charCodeAt(0);
|
|
627
|
+
switch (this._state) {
|
|
628
|
+
case 0 /* Normal */:
|
|
629
|
+
this._feedNormal(ch, code);
|
|
630
|
+
break;
|
|
631
|
+
case 1 /* Escape */:
|
|
632
|
+
this._feedEscape(ch, code);
|
|
633
|
+
break;
|
|
634
|
+
case 2 /* Csi */:
|
|
635
|
+
this._feedCsi(ch, code);
|
|
636
|
+
break;
|
|
637
|
+
case 3 /* Osc */:
|
|
638
|
+
if (code === 7 || code === 156) {
|
|
639
|
+
this._state = 0 /* Normal */;
|
|
640
|
+
} else if (code === 27) {
|
|
641
|
+
this._state = 0 /* Normal */;
|
|
642
|
+
}
|
|
643
|
+
break;
|
|
644
|
+
case 4 /* Str */:
|
|
645
|
+
if (code === 156 || code === 7) {
|
|
646
|
+
this._state = 0 /* Normal */;
|
|
647
|
+
} else if (code === 27) {
|
|
648
|
+
this._state = 0 /* Normal */;
|
|
649
|
+
}
|
|
650
|
+
break;
|
|
651
|
+
case 5 /* EscCharset */:
|
|
652
|
+
this._state = 0 /* Normal */;
|
|
653
|
+
break;
|
|
654
|
+
case 6 /* EscHash */:
|
|
655
|
+
this._state = 0 /* Normal */;
|
|
656
|
+
break;
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
_feedNormal(ch, code) {
|
|
660
|
+
if (code === 27) {
|
|
661
|
+
this._state = 1 /* Escape */;
|
|
662
|
+
} else if (code === 155) {
|
|
663
|
+
this._csiParams = "";
|
|
664
|
+
this._csiPrivate = "";
|
|
665
|
+
this._state = 2 /* Csi */;
|
|
666
|
+
} else if (code === 157) {
|
|
667
|
+
this._state = 3 /* Osc */;
|
|
668
|
+
} else if (code === 144 || code === 152 || code === 158 || code === 159) {
|
|
669
|
+
this._state = 4 /* Str */;
|
|
670
|
+
} else if (code === 7 || code === 5 || code === 6) {
|
|
671
|
+
} else if (code === 8) {
|
|
672
|
+
if (this._cursorX > 0) this._cursorX--;
|
|
673
|
+
} else if (code === 127) {
|
|
674
|
+
if (this._cursorX > 0) {
|
|
675
|
+
this._cursorX--;
|
|
676
|
+
this._setChar(this._cursorY, this._cursorX, " ");
|
|
677
|
+
}
|
|
678
|
+
} else if (code === 9) {
|
|
679
|
+
this._cursorX = Math.min(this._cols - 1, (Math.floor(this._cursorX / 8) + 1) * 8);
|
|
680
|
+
} else if (code === 10 || code === 11 || code === 12) {
|
|
681
|
+
this._newline();
|
|
682
|
+
} else if (code === 13) {
|
|
683
|
+
this._cursorX = 0;
|
|
684
|
+
} else if (code === 14 || code === 15) {
|
|
685
|
+
} else if (code >= 32 && code !== 127) {
|
|
686
|
+
this._setChar(this._cursorY, this._cursorX, ch);
|
|
687
|
+
this._cursorX++;
|
|
688
|
+
if (this._cursorX >= this._cols) {
|
|
689
|
+
this._cursorX = 0;
|
|
690
|
+
this._newline();
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
_feedEscape(ch, code) {
|
|
695
|
+
this._state = 0 /* Normal */;
|
|
696
|
+
if (code === 91) {
|
|
697
|
+
this._csiParams = "";
|
|
698
|
+
this._csiPrivate = "";
|
|
699
|
+
this._state = 2 /* Csi */;
|
|
700
|
+
} else if (code === 93) {
|
|
701
|
+
this._state = 3 /* Osc */;
|
|
702
|
+
} else if (code === 80 || code === 88 || code === 94 || code === 95) {
|
|
703
|
+
this._state = 4 /* Str */;
|
|
704
|
+
} else if (code === 40 || code === 41 || code === 42 || code === 43 || code === 45 || code === 46) {
|
|
705
|
+
this._state = 5 /* EscCharset */;
|
|
706
|
+
} else if (code === 35) {
|
|
707
|
+
this._state = 6 /* EscHash */;
|
|
708
|
+
} else if (code === 55) {
|
|
709
|
+
this._savedCursor = { x: this._cursorX, y: this._cursorY };
|
|
710
|
+
} else if (code === 56) {
|
|
711
|
+
this._cursorX = this._clamp(this._savedCursor.x, 0, this._cols - 1);
|
|
712
|
+
this._cursorY = this._clamp(this._savedCursor.y, 0, this._rows - 1);
|
|
713
|
+
} else if (code === 68) {
|
|
714
|
+
this._newline();
|
|
715
|
+
} else if (code === 69) {
|
|
716
|
+
this._cursorX = 0;
|
|
717
|
+
this._newline();
|
|
718
|
+
} else if (code === 77) {
|
|
719
|
+
if (this._cursorY === this._scrollTop) {
|
|
720
|
+
this._scrollDown(1);
|
|
721
|
+
} else {
|
|
722
|
+
this._cursorY = Math.max(0, this._cursorY - 1);
|
|
723
|
+
}
|
|
724
|
+
} else if (code === 72) {
|
|
725
|
+
} else if (code === 99) {
|
|
726
|
+
this._screen = this._makeScreen(this._cols, this._rows);
|
|
727
|
+
this._cursorX = 0;
|
|
728
|
+
this._cursorY = 0;
|
|
729
|
+
this._savedCursor = { x: 0, y: 0 };
|
|
730
|
+
this._scrollTop = 0;
|
|
731
|
+
this._scrollBottom = this._rows - 1;
|
|
732
|
+
this._resetStyle();
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
_feedCsi(ch, code) {
|
|
736
|
+
if (code >= 64 && code <= 126) {
|
|
737
|
+
this._execCsi(ch);
|
|
738
|
+
this._state = 0 /* Normal */;
|
|
739
|
+
} else if (code === 63 || code === 33 || code === 62 || code === 32) {
|
|
740
|
+
this._csiPrivate = ch;
|
|
741
|
+
} else if (code >= 48 && code <= 57 || code === 59) {
|
|
742
|
+
this._csiParams += ch;
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
_resetStyle() {
|
|
746
|
+
this._style = createDefaultStyleState();
|
|
747
|
+
this._styleSequence = "";
|
|
748
|
+
}
|
|
749
|
+
_applySgr(params2) {
|
|
750
|
+
const normalizedParams = params2.length === 0 ? [0] : params2;
|
|
751
|
+
for (let index = 0; index < normalizedParams.length; index += 1) {
|
|
752
|
+
const value = normalizedParams[index] ?? 0;
|
|
753
|
+
switch (value) {
|
|
754
|
+
case 0:
|
|
755
|
+
this._resetStyle();
|
|
756
|
+
break;
|
|
757
|
+
case 1:
|
|
758
|
+
this._style.bold = true;
|
|
759
|
+
break;
|
|
760
|
+
case 2:
|
|
761
|
+
this._style.dim = true;
|
|
762
|
+
break;
|
|
763
|
+
case 3:
|
|
764
|
+
this._style.italic = true;
|
|
765
|
+
break;
|
|
766
|
+
case 4:
|
|
767
|
+
this._style.underline = true;
|
|
768
|
+
break;
|
|
769
|
+
case 7:
|
|
770
|
+
this._style.inverse = true;
|
|
771
|
+
break;
|
|
772
|
+
case 9:
|
|
773
|
+
this._style.strikethrough = true;
|
|
774
|
+
break;
|
|
775
|
+
case 21:
|
|
776
|
+
case 22:
|
|
777
|
+
this._style.bold = false;
|
|
778
|
+
this._style.dim = false;
|
|
779
|
+
break;
|
|
780
|
+
case 23:
|
|
781
|
+
this._style.italic = false;
|
|
782
|
+
break;
|
|
783
|
+
case 24:
|
|
784
|
+
this._style.underline = false;
|
|
785
|
+
break;
|
|
786
|
+
case 27:
|
|
787
|
+
this._style.inverse = false;
|
|
788
|
+
break;
|
|
789
|
+
case 29:
|
|
790
|
+
this._style.strikethrough = false;
|
|
791
|
+
break;
|
|
792
|
+
case 39:
|
|
793
|
+
this._style.fg = void 0;
|
|
794
|
+
break;
|
|
795
|
+
case 49:
|
|
796
|
+
this._style.bg = void 0;
|
|
797
|
+
break;
|
|
798
|
+
case 38:
|
|
799
|
+
case 48:
|
|
800
|
+
index = this._applyExtendedColor(value, normalizedParams, index);
|
|
801
|
+
break;
|
|
802
|
+
default:
|
|
803
|
+
if (value >= 30 && value <= 37 || value >= 90 && value <= 97) {
|
|
804
|
+
this._style.fg = [value];
|
|
805
|
+
} else if (value >= 40 && value <= 47 || value >= 100 && value <= 107) {
|
|
806
|
+
this._style.bg = [value];
|
|
807
|
+
}
|
|
808
|
+
break;
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
this._styleSequence = serializeStyleState(this._style);
|
|
812
|
+
}
|
|
813
|
+
_applyExtendedColor(control, params2, index) {
|
|
814
|
+
const mode = params2[index + 1];
|
|
815
|
+
const target = control === 38 ? "fg" : "bg";
|
|
816
|
+
if (mode === 5) {
|
|
817
|
+
const paletteIndex = params2[index + 2];
|
|
818
|
+
if (paletteIndex !== void 0) {
|
|
819
|
+
this._style[target] = [control, 5, paletteIndex];
|
|
820
|
+
return index + 2;
|
|
821
|
+
}
|
|
822
|
+
return index;
|
|
823
|
+
}
|
|
824
|
+
if (mode === 2) {
|
|
825
|
+
const red = params2[index + 2];
|
|
826
|
+
const green = params2[index + 3];
|
|
827
|
+
const blue = params2[index + 4];
|
|
828
|
+
if (red !== void 0 && green !== void 0 && blue !== void 0) {
|
|
829
|
+
this._style[target] = [control, 2, red, green, blue];
|
|
830
|
+
return index + 4;
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
return index;
|
|
834
|
+
}
|
|
835
|
+
};
|
|
836
|
+
function createDefaultStyleState() {
|
|
837
|
+
return {
|
|
838
|
+
bold: false,
|
|
839
|
+
dim: false,
|
|
840
|
+
italic: false,
|
|
841
|
+
underline: false,
|
|
842
|
+
inverse: false,
|
|
843
|
+
strikethrough: false
|
|
844
|
+
};
|
|
845
|
+
}
|
|
846
|
+
function serializeStyleState(state) {
|
|
847
|
+
const codes = [];
|
|
848
|
+
if (state.bold) {
|
|
849
|
+
codes.push(1);
|
|
850
|
+
}
|
|
851
|
+
if (state.dim) {
|
|
852
|
+
codes.push(2);
|
|
853
|
+
}
|
|
854
|
+
if (state.italic) {
|
|
855
|
+
codes.push(3);
|
|
856
|
+
}
|
|
857
|
+
if (state.underline) {
|
|
858
|
+
codes.push(4);
|
|
859
|
+
}
|
|
860
|
+
if (state.inverse) {
|
|
861
|
+
codes.push(7);
|
|
862
|
+
}
|
|
863
|
+
if (state.strikethrough) {
|
|
864
|
+
codes.push(9);
|
|
865
|
+
}
|
|
866
|
+
if (state.fg !== void 0) {
|
|
867
|
+
codes.push(...state.fg);
|
|
868
|
+
}
|
|
869
|
+
if (state.bg !== void 0) {
|
|
870
|
+
codes.push(...state.bg);
|
|
871
|
+
}
|
|
872
|
+
return codes.length === 0 ? "" : `\x1B[${codes.join(";")}m`;
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
// src/keys.ts
|
|
876
|
+
var NAMED_KEY_SEQUENCES = {
|
|
877
|
+
Enter: "\r",
|
|
878
|
+
Tab: " ",
|
|
879
|
+
Escape: "\x1B",
|
|
880
|
+
Backspace: "\x7F",
|
|
881
|
+
Delete: "\x1B[3~",
|
|
882
|
+
ArrowUp: "\x1B[A",
|
|
883
|
+
ArrowDown: "\x1B[B",
|
|
884
|
+
ArrowRight: "\x1B[C",
|
|
885
|
+
ArrowLeft: "\x1B[D",
|
|
886
|
+
Home: "\x1B[H",
|
|
887
|
+
End: "\x1B[F",
|
|
888
|
+
PageUp: "\x1B[5~",
|
|
889
|
+
PageDown: "\x1B[6~",
|
|
890
|
+
Space: " "
|
|
891
|
+
};
|
|
892
|
+
var NAMED_KEY_LOWER = new Map(
|
|
893
|
+
Object.entries(NAMED_KEY_SEQUENCES).map(([k, v]) => [k.toLowerCase(), v])
|
|
894
|
+
);
|
|
895
|
+
var VALID_KEYS_HINT = `Valid keys: ${Object.keys(NAMED_KEY_SEQUENCES).join(", ")}, Control+<letter>, Alt+<key>`;
|
|
896
|
+
function unknownKeyError(key) {
|
|
897
|
+
return new Error(`Unknown terminal key: ${key}. ${VALID_KEYS_HINT}`);
|
|
898
|
+
}
|
|
899
|
+
function keyToSequence(key) {
|
|
900
|
+
const lowerKey = key.toLowerCase();
|
|
901
|
+
const namedSequence = NAMED_KEY_LOWER.get(lowerKey);
|
|
902
|
+
if (namedSequence !== void 0) {
|
|
903
|
+
return namedSequence;
|
|
904
|
+
}
|
|
905
|
+
if (lowerKey.startsWith("control+")) {
|
|
906
|
+
return controlKeyToSequence(key.slice("control+".length));
|
|
907
|
+
}
|
|
908
|
+
if (lowerKey.startsWith("alt+")) {
|
|
909
|
+
const nestedKey = key.slice("alt+".length);
|
|
910
|
+
if (nestedKey.length === 0) {
|
|
911
|
+
throw unknownKeyError(key);
|
|
912
|
+
}
|
|
913
|
+
if (nestedKey.length === 1) {
|
|
914
|
+
return "\x1B" + nestedKey;
|
|
915
|
+
}
|
|
916
|
+
try {
|
|
917
|
+
return "\x1B" + keyToSequence(nestedKey);
|
|
918
|
+
} catch {
|
|
919
|
+
throw unknownKeyError(key);
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
if (key.length === 1) {
|
|
923
|
+
return key;
|
|
924
|
+
}
|
|
925
|
+
throw unknownKeyError(key);
|
|
926
|
+
}
|
|
927
|
+
function controlKeyToSequence(controlKey) {
|
|
928
|
+
if (controlKey.length !== 1) {
|
|
929
|
+
throw unknownKeyError(`Control+${controlKey}`);
|
|
930
|
+
}
|
|
931
|
+
const uppercaseLetter = controlKey.toUpperCase();
|
|
932
|
+
const charCode = uppercaseLetter.charCodeAt(0);
|
|
933
|
+
if (charCode < 65 || charCode > 90) {
|
|
934
|
+
throw unknownKeyError(`Control+${controlKey}`);
|
|
935
|
+
}
|
|
936
|
+
return String.fromCharCode(charCode - 64);
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
// src/terminal-screen.ts
|
|
940
|
+
var TerminalScreen = class {
|
|
941
|
+
lines;
|
|
942
|
+
rawLines;
|
|
943
|
+
cursor;
|
|
944
|
+
size;
|
|
945
|
+
constructor({
|
|
946
|
+
lines,
|
|
947
|
+
rawLines,
|
|
948
|
+
cursor,
|
|
949
|
+
size
|
|
950
|
+
}) {
|
|
951
|
+
this.lines = Object.freeze(lines.map((line) => stripAnsi(line)));
|
|
952
|
+
this.rawLines = Object.freeze([...rawLines]);
|
|
953
|
+
this.cursor = Object.freeze({ ...cursor });
|
|
954
|
+
this.size = Object.freeze({ ...size });
|
|
955
|
+
Object.freeze(this);
|
|
956
|
+
}
|
|
957
|
+
get text() {
|
|
958
|
+
return this.lines.join("\n");
|
|
959
|
+
}
|
|
960
|
+
contains(substring) {
|
|
961
|
+
return this.text.includes(substring);
|
|
962
|
+
}
|
|
963
|
+
line(index) {
|
|
964
|
+
const normalizedIndex = index < 0 ? this.lines.length + index : index;
|
|
965
|
+
const line = this.lines[normalizedIndex];
|
|
966
|
+
if (line === void 0) {
|
|
967
|
+
throw new RangeError(`Line index out of bounds: ${index}`);
|
|
968
|
+
}
|
|
969
|
+
return line;
|
|
970
|
+
}
|
|
971
|
+
};
|
|
972
|
+
|
|
973
|
+
// src/terminal-session.ts
|
|
974
|
+
var DEFAULT_COLS = 120;
|
|
975
|
+
var DEFAULT_ROWS = 40;
|
|
976
|
+
var DEFAULT_TIMEOUT_MS = 1e4;
|
|
977
|
+
var WAIT_FOR_POLL_MS = 10;
|
|
978
|
+
var TYPE_DELAY_MS = 15;
|
|
979
|
+
var CLOSE_AFTER_SIGNAL_GRACE_MS = 250;
|
|
980
|
+
var CLOSE_AFTER_SIGTERM_MS = 1e3;
|
|
981
|
+
var TerminalSession = class {
|
|
982
|
+
id;
|
|
983
|
+
command;
|
|
984
|
+
pid;
|
|
985
|
+
exitCode = null;
|
|
986
|
+
pty;
|
|
987
|
+
terminal;
|
|
988
|
+
emitter = new EventEmitter();
|
|
989
|
+
exitPromise;
|
|
990
|
+
rawBuffer = "";
|
|
991
|
+
lastDataAt = Date.now();
|
|
992
|
+
currentCols;
|
|
993
|
+
currentRows;
|
|
994
|
+
closeRequested = false;
|
|
995
|
+
signalRequested = false;
|
|
996
|
+
constructor({
|
|
997
|
+
id,
|
|
998
|
+
command,
|
|
999
|
+
args = [],
|
|
1000
|
+
cwd = process.cwd(),
|
|
1001
|
+
env = process.env,
|
|
1002
|
+
cols = DEFAULT_COLS,
|
|
1003
|
+
rows = DEFAULT_ROWS,
|
|
1004
|
+
observe = false
|
|
1005
|
+
}) {
|
|
1006
|
+
this.id = id;
|
|
1007
|
+
this.command = command;
|
|
1008
|
+
this.currentCols = cols;
|
|
1009
|
+
this.currentRows = rows;
|
|
1010
|
+
this.terminal = new TerminalBuffer(cols, rows);
|
|
1011
|
+
this.pty = createPtyProcess({ command, args, cwd, env, cols, rows });
|
|
1012
|
+
this.pid = this.pty.pid;
|
|
1013
|
+
const dataSubscription = this.pty.onData((chunk) => {
|
|
1014
|
+
this.rawBuffer += chunk;
|
|
1015
|
+
this.lastDataAt = Date.now();
|
|
1016
|
+
this.terminal.write(chunk);
|
|
1017
|
+
if (observe) {
|
|
1018
|
+
process.stderr.write(chunk);
|
|
1019
|
+
}
|
|
1020
|
+
});
|
|
1021
|
+
let exitSubscription;
|
|
1022
|
+
this.exitPromise = new Promise((resolve) => {
|
|
1023
|
+
exitSubscription = this.pty.onExit(({ exitCode }) => {
|
|
1024
|
+
if (this.exitCode !== null) {
|
|
1025
|
+
resolve(this.exitCode);
|
|
1026
|
+
return;
|
|
1027
|
+
}
|
|
1028
|
+
this.exitCode = exitCode;
|
|
1029
|
+
dataSubscription.dispose();
|
|
1030
|
+
exitSubscription?.dispose();
|
|
1031
|
+
this.emitter.emit("exit", exitCode);
|
|
1032
|
+
resolve(exitCode);
|
|
1033
|
+
});
|
|
1034
|
+
});
|
|
1035
|
+
}
|
|
1036
|
+
async type(text) {
|
|
1037
|
+
for (const character of text) {
|
|
1038
|
+
await this.send(character);
|
|
1039
|
+
await sleep(TYPE_DELAY_MS);
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
async fill(text) {
|
|
1043
|
+
await this.send(text.replace(/\r?\n/g, "\r"));
|
|
1044
|
+
}
|
|
1045
|
+
async press(key) {
|
|
1046
|
+
await this.send(keyToSequence(key));
|
|
1047
|
+
}
|
|
1048
|
+
async send(raw) {
|
|
1049
|
+
if (this.exitCode !== null) {
|
|
1050
|
+
return;
|
|
1051
|
+
}
|
|
1052
|
+
this.pty.write(raw);
|
|
1053
|
+
}
|
|
1054
|
+
async signal(sig) {
|
|
1055
|
+
if (this.exitCode !== null) {
|
|
1056
|
+
return;
|
|
1057
|
+
}
|
|
1058
|
+
this.signalRequested = true;
|
|
1059
|
+
this.pty.kill(sig);
|
|
1060
|
+
}
|
|
1061
|
+
async waitFor(pattern, opts) {
|
|
1062
|
+
const timeout = opts?.timeout ?? DEFAULT_TIMEOUT_MS;
|
|
1063
|
+
const startedAt = Date.now();
|
|
1064
|
+
while (Date.now() - startedAt <= timeout) {
|
|
1065
|
+
const matched = matchPattern(this.rawBuffer, pattern);
|
|
1066
|
+
if (matched !== null) {
|
|
1067
|
+
return matched;
|
|
1068
|
+
}
|
|
1069
|
+
await sleep(WAIT_FOR_POLL_MS);
|
|
1070
|
+
}
|
|
1071
|
+
throw new Error(`Timed out waiting for pattern after ${timeout}ms: ${String(pattern)}`);
|
|
1072
|
+
}
|
|
1073
|
+
async waitForQuiet(ms) {
|
|
1074
|
+
while (true) {
|
|
1075
|
+
const remaining = ms - (Date.now() - this.lastDataAt);
|
|
1076
|
+
if (remaining <= 0) {
|
|
1077
|
+
return;
|
|
1078
|
+
}
|
|
1079
|
+
await sleep(remaining);
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
async screen() {
|
|
1083
|
+
const rawLines = [];
|
|
1084
|
+
for (let row = 0; row < this.currentRows; row += 1) {
|
|
1085
|
+
rawLines.push(this.terminal.renderLine(row));
|
|
1086
|
+
}
|
|
1087
|
+
return new TerminalScreen({
|
|
1088
|
+
lines: rawLines,
|
|
1089
|
+
rawLines,
|
|
1090
|
+
cursor: {
|
|
1091
|
+
row: this.terminal.displayBuffer.cursorY,
|
|
1092
|
+
col: this.terminal.displayBuffer.cursorX
|
|
1093
|
+
},
|
|
1094
|
+
size: {
|
|
1095
|
+
rows: this.currentRows,
|
|
1096
|
+
cols: this.currentCols
|
|
1097
|
+
}
|
|
1098
|
+
});
|
|
1099
|
+
}
|
|
1100
|
+
async history(opts) {
|
|
1101
|
+
const normalized = normalizeHistoryBuffer(stripAnsi(this.rawBuffer));
|
|
1102
|
+
const lines = splitHistoryLines(normalized);
|
|
1103
|
+
if (opts?.last === void 0) {
|
|
1104
|
+
return lines;
|
|
1105
|
+
}
|
|
1106
|
+
const start = Math.max(0, lines.length - opts.last);
|
|
1107
|
+
return lines.slice(start);
|
|
1108
|
+
}
|
|
1109
|
+
async resize(cols, rows) {
|
|
1110
|
+
this.currentCols = cols;
|
|
1111
|
+
this.currentRows = rows;
|
|
1112
|
+
if (this.exitCode === null) {
|
|
1113
|
+
this.pty.resize(cols, rows);
|
|
1114
|
+
}
|
|
1115
|
+
this.terminal.resize(cols, rows);
|
|
1116
|
+
}
|
|
1117
|
+
async waitForExit(opts) {
|
|
1118
|
+
if (this.exitCode !== null) {
|
|
1119
|
+
return this.exitCode;
|
|
1120
|
+
}
|
|
1121
|
+
if (opts?.timeout !== void 0) {
|
|
1122
|
+
const result = await waitForExit(this.exitPromise, opts.timeout);
|
|
1123
|
+
if (result === null) {
|
|
1124
|
+
throw new Error(`Timed out waiting for process to exit after ${opts.timeout}ms`);
|
|
1125
|
+
}
|
|
1126
|
+
return result;
|
|
1127
|
+
}
|
|
1128
|
+
return this.exitPromise;
|
|
1129
|
+
}
|
|
1130
|
+
async close() {
|
|
1131
|
+
if (this.exitCode !== null) {
|
|
1132
|
+
return this.exitCode;
|
|
1133
|
+
}
|
|
1134
|
+
if (!this.closeRequested) {
|
|
1135
|
+
this.closeRequested = true;
|
|
1136
|
+
const gracefulExitCode = await waitForExit(this.exitPromise, CLOSE_AFTER_SIGNAL_GRACE_MS);
|
|
1137
|
+
if (gracefulExitCode !== null) {
|
|
1138
|
+
return gracefulExitCode;
|
|
1139
|
+
}
|
|
1140
|
+
if (this.signalRequested) {
|
|
1141
|
+
return this.exitPromise;
|
|
1142
|
+
}
|
|
1143
|
+
if (this.exitCode === null) {
|
|
1144
|
+
this.pty.kill("SIGTERM");
|
|
1145
|
+
const afterSigterm = await waitForExit(this.exitPromise, CLOSE_AFTER_SIGTERM_MS);
|
|
1146
|
+
if (afterSigterm !== null) {
|
|
1147
|
+
return afterSigterm;
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
if (this.exitCode === null) {
|
|
1151
|
+
this.pty.kill("SIGKILL");
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
return this.exitPromise;
|
|
1155
|
+
}
|
|
1156
|
+
on(event, cb) {
|
|
1157
|
+
this.emitter.on(event, cb);
|
|
1158
|
+
}
|
|
1159
|
+
};
|
|
1160
|
+
function createPtyProcess({
|
|
1161
|
+
command,
|
|
1162
|
+
args,
|
|
1163
|
+
cwd,
|
|
1164
|
+
env,
|
|
1165
|
+
cols,
|
|
1166
|
+
rows
|
|
1167
|
+
}) {
|
|
1168
|
+
try {
|
|
1169
|
+
return nodePty.spawn(command, args, {
|
|
1170
|
+
cwd,
|
|
1171
|
+
env,
|
|
1172
|
+
cols,
|
|
1173
|
+
rows,
|
|
1174
|
+
encoding: "utf8"
|
|
1175
|
+
});
|
|
1176
|
+
} catch {
|
|
1177
|
+
return createChildProcessFallback({ command, args, cwd, env });
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
function createChildProcessFallback({
|
|
1181
|
+
command,
|
|
1182
|
+
args,
|
|
1183
|
+
cwd,
|
|
1184
|
+
env
|
|
1185
|
+
}) {
|
|
1186
|
+
const child = spawnChildProcess(command, args, {
|
|
1187
|
+
cwd,
|
|
1188
|
+
env,
|
|
1189
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
1190
|
+
});
|
|
1191
|
+
return new ChildProcessFallback(child);
|
|
1192
|
+
}
|
|
1193
|
+
var ChildProcessFallback = class {
|
|
1194
|
+
pid;
|
|
1195
|
+
child;
|
|
1196
|
+
dataEmitter = new EventEmitter();
|
|
1197
|
+
exitEmitter = new EventEmitter();
|
|
1198
|
+
constructor(child) {
|
|
1199
|
+
this.child = child;
|
|
1200
|
+
this.pid = child.pid ?? -1;
|
|
1201
|
+
child.stdout.setEncoding("utf8");
|
|
1202
|
+
child.stderr.setEncoding("utf8");
|
|
1203
|
+
child.stdout.on("data", this.handleData);
|
|
1204
|
+
child.stderr.on("data", this.handleData);
|
|
1205
|
+
child.on("exit", (exitCode, signal) => {
|
|
1206
|
+
this.exitEmitter.emit("exit", {
|
|
1207
|
+
exitCode: exitCode ?? signalToExitCode(signal),
|
|
1208
|
+
signal: void 0
|
|
1209
|
+
});
|
|
1210
|
+
});
|
|
1211
|
+
}
|
|
1212
|
+
write(data) {
|
|
1213
|
+
this.child.stdin.write(data);
|
|
1214
|
+
}
|
|
1215
|
+
resize() {
|
|
1216
|
+
}
|
|
1217
|
+
kill(signal) {
|
|
1218
|
+
this.child.kill(signal);
|
|
1219
|
+
}
|
|
1220
|
+
onData(listener) {
|
|
1221
|
+
this.dataEmitter.on("data", listener);
|
|
1222
|
+
return {
|
|
1223
|
+
dispose: () => {
|
|
1224
|
+
this.dataEmitter.off("data", listener);
|
|
1225
|
+
}
|
|
1226
|
+
};
|
|
1227
|
+
}
|
|
1228
|
+
onExit(listener) {
|
|
1229
|
+
this.exitEmitter.on("exit", listener);
|
|
1230
|
+
return {
|
|
1231
|
+
dispose: () => {
|
|
1232
|
+
this.exitEmitter.off("exit", listener);
|
|
1233
|
+
}
|
|
1234
|
+
};
|
|
1235
|
+
}
|
|
1236
|
+
handleData = (chunk) => {
|
|
1237
|
+
this.dataEmitter.emit("data", String(chunk));
|
|
1238
|
+
};
|
|
1239
|
+
};
|
|
1240
|
+
function signalToExitCode(signal) {
|
|
1241
|
+
if (signal === null) {
|
|
1242
|
+
return 0;
|
|
1243
|
+
}
|
|
1244
|
+
const signalNumbers = {
|
|
1245
|
+
SIGTERM: 15,
|
|
1246
|
+
SIGINT: 2,
|
|
1247
|
+
SIGHUP: 1,
|
|
1248
|
+
SIGKILL: 9
|
|
1249
|
+
};
|
|
1250
|
+
const signalNumber = signalNumbers[signal];
|
|
1251
|
+
if (signalNumber === void 0) {
|
|
1252
|
+
return 1;
|
|
1253
|
+
}
|
|
1254
|
+
return 128 + signalNumber;
|
|
1255
|
+
}
|
|
1256
|
+
function matchPattern(buffer, pattern) {
|
|
1257
|
+
const clean = normalizeHistoryBuffer(stripAnsi(buffer));
|
|
1258
|
+
for (const line of clean.split("\n")) {
|
|
1259
|
+
if (typeof pattern === "string") {
|
|
1260
|
+
if (line.includes(pattern)) return line;
|
|
1261
|
+
} else {
|
|
1262
|
+
const flags = removeCharacter(pattern.flags, "g");
|
|
1263
|
+
if (new RegExp(pattern.source, flags).test(line)) return line;
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
return null;
|
|
1267
|
+
}
|
|
1268
|
+
function removeCharacter(input, charToRemove) {
|
|
1269
|
+
let output = "";
|
|
1270
|
+
for (const character of input) {
|
|
1271
|
+
if (character !== charToRemove) {
|
|
1272
|
+
output += character;
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
return output;
|
|
1276
|
+
}
|
|
1277
|
+
function splitHistoryLines(input) {
|
|
1278
|
+
const lines = input.split("\n");
|
|
1279
|
+
if (lines.length > 0 && lines[lines.length - 1] === "") {
|
|
1280
|
+
lines.pop();
|
|
1281
|
+
}
|
|
1282
|
+
return lines;
|
|
1283
|
+
}
|
|
1284
|
+
function normalizeHistoryBuffer(input) {
|
|
1285
|
+
let output = "";
|
|
1286
|
+
for (const character of input) {
|
|
1287
|
+
if (character === "\r" || character === "\b") {
|
|
1288
|
+
continue;
|
|
1289
|
+
}
|
|
1290
|
+
output += character;
|
|
1291
|
+
}
|
|
1292
|
+
return output;
|
|
1293
|
+
}
|
|
1294
|
+
function sleep(ms) {
|
|
1295
|
+
return new Promise((resolve) => {
|
|
1296
|
+
setTimeout(resolve, ms);
|
|
1297
|
+
});
|
|
1298
|
+
}
|
|
1299
|
+
async function waitForExit(exitPromise, timeout) {
|
|
1300
|
+
return new Promise((resolve) => {
|
|
1301
|
+
let settled = false;
|
|
1302
|
+
const timer = setTimeout(() => {
|
|
1303
|
+
if (settled) {
|
|
1304
|
+
return;
|
|
1305
|
+
}
|
|
1306
|
+
settled = true;
|
|
1307
|
+
resolve(null);
|
|
1308
|
+
}, timeout);
|
|
1309
|
+
void exitPromise.then((code) => {
|
|
1310
|
+
if (settled) {
|
|
1311
|
+
return;
|
|
1312
|
+
}
|
|
1313
|
+
settled = true;
|
|
1314
|
+
clearTimeout(timer);
|
|
1315
|
+
resolve(code);
|
|
1316
|
+
});
|
|
1317
|
+
});
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1320
|
+
// src/terminal-pilot.ts
|
|
1321
|
+
var DEFAULT_COLS2 = 120;
|
|
1322
|
+
var DEFAULT_ROWS2 = 40;
|
|
1323
|
+
var TerminalPilot = class _TerminalPilot {
|
|
1324
|
+
sessionMap = /* @__PURE__ */ new Map();
|
|
1325
|
+
static async launch() {
|
|
1326
|
+
return new _TerminalPilot();
|
|
1327
|
+
}
|
|
1328
|
+
async newSession(opts) {
|
|
1329
|
+
const session = new TerminalSession({
|
|
1330
|
+
id: randomUUID(),
|
|
1331
|
+
command: opts.command,
|
|
1332
|
+
args: opts.args,
|
|
1333
|
+
cwd: opts.cwd,
|
|
1334
|
+
env: opts.env,
|
|
1335
|
+
cols: opts.cols ?? DEFAULT_COLS2,
|
|
1336
|
+
rows: opts.rows ?? DEFAULT_ROWS2,
|
|
1337
|
+
observe: opts.observe ?? false
|
|
1338
|
+
});
|
|
1339
|
+
this.sessionMap.set(session.id, session);
|
|
1340
|
+
return session;
|
|
1341
|
+
}
|
|
1342
|
+
getSession(id) {
|
|
1343
|
+
const session = this.sessionMap.get(id);
|
|
1344
|
+
if (session === void 0) {
|
|
1345
|
+
throw new Error(`Session not found: ${id}`);
|
|
1346
|
+
}
|
|
1347
|
+
return session;
|
|
1348
|
+
}
|
|
1349
|
+
deleteSession(id) {
|
|
1350
|
+
this.sessionMap.delete(id);
|
|
1351
|
+
}
|
|
1352
|
+
sessions() {
|
|
1353
|
+
return [...this.sessionMap.values()].filter((s) => s.exitCode === null);
|
|
1354
|
+
}
|
|
1355
|
+
async close() {
|
|
1356
|
+
const sessions = [...this.sessionMap.values()];
|
|
1357
|
+
try {
|
|
1358
|
+
await Promise.all(sessions.map((session) => session.close()));
|
|
1359
|
+
} finally {
|
|
1360
|
+
this.sessionMap.clear();
|
|
1361
|
+
}
|
|
1362
|
+
}
|
|
1363
|
+
};
|
|
1364
|
+
|
|
1365
|
+
// src/commands/runtime.ts
|
|
1366
|
+
var SESSION_ENV_VAR = "TERMINAL_PILOT_SESSION";
|
|
1367
|
+
var sharedRuntime;
|
|
1368
|
+
function getTerminalPilotRuntime(runtime) {
|
|
1369
|
+
if (runtime !== void 0) {
|
|
1370
|
+
return runtime;
|
|
1371
|
+
}
|
|
1372
|
+
sharedRuntime ??= createTerminalPilotRuntime();
|
|
1373
|
+
return sharedRuntime;
|
|
1374
|
+
}
|
|
1375
|
+
function createTerminalPilotRuntime(options = {}) {
|
|
1376
|
+
const launchPilot = options.launchPilot ?? TerminalPilot.launch;
|
|
1377
|
+
const nameToId = /* @__PURE__ */ new Map();
|
|
1378
|
+
const idToName = /* @__PURE__ */ new Map();
|
|
1379
|
+
let pilotPromise;
|
|
1380
|
+
function getRequestedName(name, env) {
|
|
1381
|
+
return name ?? env?.get(SESSION_ENV_VAR);
|
|
1382
|
+
}
|
|
1383
|
+
async function getPilot() {
|
|
1384
|
+
pilotPromise ??= launchPilot();
|
|
1385
|
+
return pilotPromise;
|
|
1386
|
+
}
|
|
1387
|
+
function nextSessionName() {
|
|
1388
|
+
let index = 1;
|
|
1389
|
+
while (nameToId.has(`s${index}`)) {
|
|
1390
|
+
index += 1;
|
|
1391
|
+
}
|
|
1392
|
+
return `s${index}`;
|
|
1393
|
+
}
|
|
1394
|
+
function rememberSession(name, session) {
|
|
1395
|
+
nameToId.set(name, session.id);
|
|
1396
|
+
idToName.set(session.id, name);
|
|
1397
|
+
return { name, session };
|
|
1398
|
+
}
|
|
1399
|
+
function forgetSession(name, sessionId) {
|
|
1400
|
+
nameToId.delete(name);
|
|
1401
|
+
idToName.delete(sessionId);
|
|
1402
|
+
}
|
|
1403
|
+
function formatAvailableSessions(names) {
|
|
1404
|
+
if (names.length === 0) {
|
|
1405
|
+
return "No active sessions are available.";
|
|
1406
|
+
}
|
|
1407
|
+
return `Available sessions: ${names.join(", ")}.`;
|
|
1408
|
+
}
|
|
1409
|
+
async function lookupNamedSession(name) {
|
|
1410
|
+
const sessionId = nameToId.get(name);
|
|
1411
|
+
if (sessionId === void 0) {
|
|
1412
|
+
const active = await listSessions();
|
|
1413
|
+
throw new UserError(`Session "${name}" was not found. ${formatAvailableSessions(active.map((entry) => entry.name))}`);
|
|
1414
|
+
}
|
|
1415
|
+
const pilot = await getPilot();
|
|
1416
|
+
try {
|
|
1417
|
+
return { name, session: pilot.getSession(sessionId) };
|
|
1418
|
+
} catch {
|
|
1419
|
+
forgetSession(name, sessionId);
|
|
1420
|
+
const active = await listSessions();
|
|
1421
|
+
throw new UserError(`Session "${name}" was not found. ${formatAvailableSessions(active.map((entry) => entry.name))}`);
|
|
1422
|
+
}
|
|
1423
|
+
}
|
|
1424
|
+
async function listSessions() {
|
|
1425
|
+
const pilot = await getPilot();
|
|
1426
|
+
return pilot.sessions().flatMap((session) => {
|
|
1427
|
+
const name = idToName.get(session.id);
|
|
1428
|
+
if (name === void 0) {
|
|
1429
|
+
return [];
|
|
1430
|
+
}
|
|
1431
|
+
return [{ name, session }];
|
|
1432
|
+
});
|
|
1433
|
+
}
|
|
1434
|
+
return {
|
|
1435
|
+
async createSession(params2, env) {
|
|
1436
|
+
const requestedName = getRequestedName(params2.session, env) ?? nextSessionName();
|
|
1437
|
+
if (nameToId.has(requestedName)) {
|
|
1438
|
+
throw new UserError(`Session "${requestedName}" already exists.`);
|
|
1439
|
+
}
|
|
1440
|
+
const pilot = await getPilot();
|
|
1441
|
+
const session = await pilot.newSession({
|
|
1442
|
+
command: params2.command,
|
|
1443
|
+
args: params2.args,
|
|
1444
|
+
cwd: params2.cwd,
|
|
1445
|
+
cols: params2.cols,
|
|
1446
|
+
rows: params2.rows,
|
|
1447
|
+
observe: params2.observe
|
|
1448
|
+
});
|
|
1449
|
+
return rememberSession(requestedName, session);
|
|
1450
|
+
},
|
|
1451
|
+
async resolveSession(name, env) {
|
|
1452
|
+
const requestedName = getRequestedName(name, env);
|
|
1453
|
+
if (requestedName !== void 0) {
|
|
1454
|
+
return lookupNamedSession(requestedName);
|
|
1455
|
+
}
|
|
1456
|
+
const active = await listSessions();
|
|
1457
|
+
if (active.length === 1) {
|
|
1458
|
+
return active[0];
|
|
1459
|
+
}
|
|
1460
|
+
if (active.length === 0) {
|
|
1461
|
+
throw new UserError("No active sessions. Create one with create-session.");
|
|
1462
|
+
}
|
|
1463
|
+
throw new UserError(
|
|
1464
|
+
`Multiple active sessions require an explicit session name. Pass --session or set ${SESSION_ENV_VAR}. ${formatAvailableSessions(active.map((entry) => entry.name))}`
|
|
1465
|
+
);
|
|
1466
|
+
},
|
|
1467
|
+
async closeSession(name, env) {
|
|
1468
|
+
const namedSession = await this.resolveSession(name, env);
|
|
1469
|
+
const exitCode = await namedSession.session.close();
|
|
1470
|
+
const pilot = await getPilot();
|
|
1471
|
+
pilot.deleteSession(namedSession.session.id);
|
|
1472
|
+
forgetSession(namedSession.name, namedSession.session.id);
|
|
1473
|
+
return {
|
|
1474
|
+
exitCode,
|
|
1475
|
+
name: namedSession.name
|
|
1476
|
+
};
|
|
1477
|
+
},
|
|
1478
|
+
listSessions,
|
|
1479
|
+
async close() {
|
|
1480
|
+
if (pilotPromise === void 0) {
|
|
1481
|
+
return;
|
|
1482
|
+
}
|
|
1483
|
+
const pilot = await pilotPromise;
|
|
1484
|
+
await pilot.close();
|
|
1485
|
+
pilotPromise = void 0;
|
|
1486
|
+
nameToId.clear();
|
|
1487
|
+
idToName.clear();
|
|
1488
|
+
}
|
|
1489
|
+
};
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
// src/commands/fill.ts
|
|
1493
|
+
var params = S.Object({
|
|
1494
|
+
text: S.String({ description: "Text to write to the session" }),
|
|
1495
|
+
session: S.Optional(S.String({ short: "s", description: "Session name" }))
|
|
1496
|
+
});
|
|
1497
|
+
var fill = defineCommand({
|
|
1498
|
+
name: "fill",
|
|
1499
|
+
description: "Write text to an active terminal session all at once (replaces \\n with \\r)",
|
|
1500
|
+
scope: ["cli", "mcp", "sdk"],
|
|
1501
|
+
positional: ["text"],
|
|
1502
|
+
params,
|
|
1503
|
+
handler: async ({ params: params2, env, terminalPilotRuntime }) => {
|
|
1504
|
+
const namedSession = await getTerminalPilotRuntime(terminalPilotRuntime).resolveSession(params2.session, env);
|
|
1505
|
+
await namedSession.session.fill(params2.text);
|
|
1506
|
+
return void 0;
|
|
1507
|
+
}
|
|
1508
|
+
});
|
|
1509
|
+
export {
|
|
1510
|
+
fill
|
|
1511
|
+
};
|
|
1512
|
+
//# sourceMappingURL=fill.js.map
|