@bypane/cli 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +77 -22
- package/dist/app-host.js +408 -0
- package/dist/app-protocol.js +1 -0
- package/dist/app-worker.js +556 -0
- package/dist/cli.js +134 -0
- package/dist/demo.js +264 -0
- package/dist/expr.js +460 -0
- package/dist/mcp.js +114 -5
- package/dist/multiplexer.js +4 -2
- package/dist/primitives.js +669 -0
- package/dist/selection.js +170 -0
- package/dist/setup.js +22 -21
- package/dist/skill.js +52 -10
- package/dist/state.js +446 -19
- package/dist/ui-surface.js +744 -0
- package/dist/ui.js +482 -36
- package/examples/spring-studio.ts +420 -0
- package/package.json +2 -1
- package/skills/bypane/SKILL.md +52 -10
|
@@ -0,0 +1,556 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
import module from "node:module";
|
|
10
|
+
import { pathToFileURL } from "node:url";
|
|
11
|
+
import { createTestApp, defaultTheme, } from "tui";
|
|
12
|
+
const BOLD = 1;
|
|
13
|
+
const DIM = 2;
|
|
14
|
+
const ITALIC = 4;
|
|
15
|
+
const UNDERLINE = 8;
|
|
16
|
+
const INVERSE = 16;
|
|
17
|
+
const STRIKETHROUGH = 32;
|
|
18
|
+
const RGB = 0x1000000;
|
|
19
|
+
const workerExt = import.meta.url.endsWith(".ts") ? ".ts" : ".js";
|
|
20
|
+
const primitivesUrl = new URL(`./primitives${workerExt}`, import.meta.url).href;
|
|
21
|
+
const protocolUrl = new URL(`./app-protocol${workerExt}`, import.meta.url).href;
|
|
22
|
+
// Resolve "tui", "tui/*", and "bypane/primitives" from anywhere on disk (including /tmp inline apps).
|
|
23
|
+
if (typeof module.registerHooks === "function") {
|
|
24
|
+
module.registerHooks({
|
|
25
|
+
resolve(specifier, context, nextResolve) {
|
|
26
|
+
if (specifier === "tui" || specifier.startsWith("tui/")) {
|
|
27
|
+
return nextResolve(specifier, { ...context, parentURL: import.meta.url });
|
|
28
|
+
}
|
|
29
|
+
if (specifier === "bypane/primitives" || specifier === "bypane") {
|
|
30
|
+
return { url: primitivesUrl, shortCircuit: true };
|
|
31
|
+
}
|
|
32
|
+
if (specifier === "bypane/protocol") {
|
|
33
|
+
return { url: protocolUrl, shortCircuit: true };
|
|
34
|
+
}
|
|
35
|
+
return nextResolve(specifier, context);
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
const workerTheme = {
|
|
40
|
+
...defaultTheme,
|
|
41
|
+
primary: { fg: "#7aa2ff", bold: true },
|
|
42
|
+
success: { fg: "#5fd787", bold: true },
|
|
43
|
+
destructive: { fg: "#ff7a90", bold: true },
|
|
44
|
+
muted: { fg: "#8b93a6" },
|
|
45
|
+
heading: { fg: "#e6edf7", bold: true },
|
|
46
|
+
border: { fg: "#465066" },
|
|
47
|
+
code: { fg: "#7dcfff" },
|
|
48
|
+
link: { fg: "#7aa2ff", underline: true },
|
|
49
|
+
match: { fg: "#10141c", bg: "#f5a623", bold: true },
|
|
50
|
+
selected: { fg: "#5fd787", bold: true },
|
|
51
|
+
highlight: { fg: "#0e131d", bg: "#7aa2ff", bold: true },
|
|
52
|
+
};
|
|
53
|
+
function send(msg) {
|
|
54
|
+
if (typeof process.send === "function" && process.connected) {
|
|
55
|
+
process.send(msg);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function sendError(err) {
|
|
59
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
60
|
+
send({
|
|
61
|
+
type: "error",
|
|
62
|
+
message: e.message || String(err),
|
|
63
|
+
stack: e.stack,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
process.on("uncaughtException", (err) => {
|
|
67
|
+
sendError(err);
|
|
68
|
+
});
|
|
69
|
+
process.on("unhandledRejection", (reason) => {
|
|
70
|
+
sendError(reason);
|
|
71
|
+
});
|
|
72
|
+
function colorFromRaw(value) {
|
|
73
|
+
if (value < 0)
|
|
74
|
+
return "default";
|
|
75
|
+
if (value >= RGB)
|
|
76
|
+
return `#${(value & 0xffffff).toString(16).padStart(6, "0")}`;
|
|
77
|
+
return value;
|
|
78
|
+
}
|
|
79
|
+
function encodeSgrMouse(mouse, ctrl, alt, shift) {
|
|
80
|
+
const modBits = (shift ? 4 : 0) | (alt ? 8 : 0) | (ctrl ? 16 : 0);
|
|
81
|
+
const btnIndex = mouse.button === "left"
|
|
82
|
+
? 0
|
|
83
|
+
: mouse.button === "middle"
|
|
84
|
+
? 1
|
|
85
|
+
: mouse.button === "right"
|
|
86
|
+
? 2
|
|
87
|
+
: 3;
|
|
88
|
+
let code = modBits;
|
|
89
|
+
let end = "M";
|
|
90
|
+
if (mouse.action === "scroll-up")
|
|
91
|
+
code |= 64 | 0;
|
|
92
|
+
else if (mouse.action === "scroll-down")
|
|
93
|
+
code |= 64 | 1;
|
|
94
|
+
else if (mouse.action === "scroll-left")
|
|
95
|
+
code |= 64 | 2;
|
|
96
|
+
else if (mouse.action === "scroll-right")
|
|
97
|
+
code |= 64 | 3;
|
|
98
|
+
else if (mouse.action === "drag")
|
|
99
|
+
code |= 32 | btnIndex;
|
|
100
|
+
else if (mouse.action === "move")
|
|
101
|
+
code |= 32 | 3;
|
|
102
|
+
else if (mouse.action === "release") {
|
|
103
|
+
code |= btnIndex;
|
|
104
|
+
end = "m";
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
code |= btnIndex;
|
|
108
|
+
}
|
|
109
|
+
return `\x1b[<${code};${Math.max(0, mouse.x | 0) + 1};${Math.max(0, mouse.y | 0) + 1}${end}`;
|
|
110
|
+
}
|
|
111
|
+
function dispatchKey(app, key) {
|
|
112
|
+
if (key.name === "paste") {
|
|
113
|
+
app.input(`\x1b[200~${key.text}\x1b[201~`);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (key.name === "char") {
|
|
117
|
+
if (!key.ctrl && !key.alt) {
|
|
118
|
+
if (key.text === " ") {
|
|
119
|
+
app.press(key.shift ? "shift+space" : "space");
|
|
120
|
+
}
|
|
121
|
+
else if (key.text) {
|
|
122
|
+
app.type(key.text);
|
|
123
|
+
}
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const target = key.text === " " ? "space" : key.base || key.text;
|
|
127
|
+
if (target === "+") {
|
|
128
|
+
const mod = 1 + (key.shift ? 1 : 0) + (key.alt ? 2 : 0) + (key.ctrl ? 4 : 0);
|
|
129
|
+
app.input(`\x1b[43;${mod}u`);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
const parts = [];
|
|
133
|
+
if (key.ctrl)
|
|
134
|
+
parts.push("ctrl");
|
|
135
|
+
if (key.alt)
|
|
136
|
+
parts.push("alt");
|
|
137
|
+
if (key.shift)
|
|
138
|
+
parts.push("shift");
|
|
139
|
+
parts.push(target);
|
|
140
|
+
app.press(parts.join("+"));
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (key.name === "mouse" ||
|
|
144
|
+
key.name === "reply" ||
|
|
145
|
+
key.name === "shift" ||
|
|
146
|
+
key.name === "ctrl" ||
|
|
147
|
+
key.name === "alt" ||
|
|
148
|
+
key.name === "focus" ||
|
|
149
|
+
key.name === "blur") {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
const parts = [];
|
|
153
|
+
if (key.ctrl)
|
|
154
|
+
parts.push("ctrl");
|
|
155
|
+
if (key.alt)
|
|
156
|
+
parts.push("alt");
|
|
157
|
+
if (key.shift)
|
|
158
|
+
parts.push("shift");
|
|
159
|
+
parts.push(key.name);
|
|
160
|
+
app.press(parts.join("+"));
|
|
161
|
+
}
|
|
162
|
+
function sameTypedArray(a, b) {
|
|
163
|
+
if (!a || a.length !== b.length)
|
|
164
|
+
return false;
|
|
165
|
+
for (let i = 0; i < a.length; i++) {
|
|
166
|
+
if (a[i] !== b[i])
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
function sameStringArray(a, b) {
|
|
172
|
+
if (!a || a.length !== b.length)
|
|
173
|
+
return false;
|
|
174
|
+
for (let i = 0; i < a.length; i++) {
|
|
175
|
+
if (a[i] !== b[i])
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
let testApp = null;
|
|
181
|
+
let definition = null;
|
|
182
|
+
let appState = {};
|
|
183
|
+
let stateListeners = new Set();
|
|
184
|
+
let isActive = true;
|
|
185
|
+
let disposed = false;
|
|
186
|
+
let fixedFps = 0;
|
|
187
|
+
let tickTimer = null;
|
|
188
|
+
let lastTickAt = performance.now();
|
|
189
|
+
let sentStyleCount = 1;
|
|
190
|
+
let sentCopies = 0;
|
|
191
|
+
let prevCols = 0;
|
|
192
|
+
let prevRows = 0;
|
|
193
|
+
let prevChars = null;
|
|
194
|
+
let prevStyles = null;
|
|
195
|
+
let prevWidths = null;
|
|
196
|
+
let prevClusters = null;
|
|
197
|
+
let prevCursorKey = "";
|
|
198
|
+
let prevFocusables = null;
|
|
199
|
+
let prevFocused = "";
|
|
200
|
+
let prevHasTrap = false;
|
|
201
|
+
let prevSummary = undefined;
|
|
202
|
+
function safeSummary() {
|
|
203
|
+
if (!definition?.summary)
|
|
204
|
+
return undefined;
|
|
205
|
+
try {
|
|
206
|
+
const out = definition.summary();
|
|
207
|
+
return typeof out === "string" ? out.trim() : undefined;
|
|
208
|
+
}
|
|
209
|
+
catch {
|
|
210
|
+
return undefined;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
function emitFrame(force = false) {
|
|
214
|
+
if (!testApp || disposed)
|
|
215
|
+
return;
|
|
216
|
+
const f = testApp.frame;
|
|
217
|
+
const cells = f.cells;
|
|
218
|
+
const styles = f.styles;
|
|
219
|
+
const copied = testApp.copied();
|
|
220
|
+
while (sentCopies < copied.length) {
|
|
221
|
+
send({ type: "copy", text: copied[sentCopies] });
|
|
222
|
+
sentCopies++;
|
|
223
|
+
}
|
|
224
|
+
const focusables = f.focusables.map((item) => item.id);
|
|
225
|
+
const cursorKey = f.cursor ? `${f.cursor.x},${f.cursor.y}` : "";
|
|
226
|
+
const hasTrap = f.traps.length > 0;
|
|
227
|
+
const summary = safeSummary();
|
|
228
|
+
const unchanged = !force &&
|
|
229
|
+
cells.columns === prevCols &&
|
|
230
|
+
cells.rows === prevRows &&
|
|
231
|
+
cursorKey === prevCursorKey &&
|
|
232
|
+
f.focused === prevFocused &&
|
|
233
|
+
hasTrap === prevHasTrap &&
|
|
234
|
+
summary === prevSummary &&
|
|
235
|
+
sameStringArray(prevFocusables, focusables) &&
|
|
236
|
+
sameStringArray(prevClusters, cells.clusters) &&
|
|
237
|
+
sameTypedArray(prevChars, cells.chars) &&
|
|
238
|
+
sameTypedArray(prevStyles, cells.styles) &&
|
|
239
|
+
sameTypedArray(prevWidths, cells.widths);
|
|
240
|
+
if (unchanged)
|
|
241
|
+
return;
|
|
242
|
+
const styleOffset = sentStyleCount;
|
|
243
|
+
const newStyles = [];
|
|
244
|
+
const totalStyles = styles.foregrounds.length;
|
|
245
|
+
for (let i = sentStyleCount; i < totalStyles; i++) {
|
|
246
|
+
const attrs = styles.attributes[i] ?? 0;
|
|
247
|
+
const link = styles.links[i] ?? "";
|
|
248
|
+
newStyles.push({
|
|
249
|
+
fg: colorFromRaw(styles.foregrounds[i] ?? -1),
|
|
250
|
+
bg: colorFromRaw(styles.backgrounds[i] ?? -1),
|
|
251
|
+
bold: (attrs & BOLD) !== 0,
|
|
252
|
+
dim: (attrs & DIM) !== 0,
|
|
253
|
+
italic: (attrs & ITALIC) !== 0,
|
|
254
|
+
underline: (attrs & UNDERLINE) !== 0,
|
|
255
|
+
inverse: (attrs & INVERSE) !== 0,
|
|
256
|
+
strikethrough: (attrs & STRIKETHROUGH) !== 0,
|
|
257
|
+
...(link ? { link } : {}),
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
sentStyleCount = totalStyles;
|
|
261
|
+
const charsCopy = new Uint32Array(cells.chars);
|
|
262
|
+
const stylesCopy = new Uint32Array(cells.styles);
|
|
263
|
+
const widthsCopy = new Uint8Array(cells.widths);
|
|
264
|
+
const clustersCopy = cells.clusters.slice();
|
|
265
|
+
prevCols = cells.columns;
|
|
266
|
+
prevRows = cells.rows;
|
|
267
|
+
prevChars = charsCopy;
|
|
268
|
+
prevStyles = stylesCopy;
|
|
269
|
+
prevWidths = widthsCopy;
|
|
270
|
+
prevClusters = clustersCopy;
|
|
271
|
+
prevCursorKey = cursorKey;
|
|
272
|
+
prevFocusables = focusables;
|
|
273
|
+
prevFocused = f.focused;
|
|
274
|
+
prevHasTrap = hasTrap;
|
|
275
|
+
prevSummary = summary;
|
|
276
|
+
const serialized = {
|
|
277
|
+
columns: cells.columns,
|
|
278
|
+
rows: cells.rows,
|
|
279
|
+
chars: charsCopy,
|
|
280
|
+
styles: stylesCopy,
|
|
281
|
+
widths: widthsCopy,
|
|
282
|
+
clusters: clustersCopy,
|
|
283
|
+
styleOffset,
|
|
284
|
+
newStyles,
|
|
285
|
+
cursor: f.cursor ? { x: f.cursor.x, y: f.cursor.y } : null,
|
|
286
|
+
focusables,
|
|
287
|
+
focused: f.focused,
|
|
288
|
+
hasTrap,
|
|
289
|
+
animating: f.animating,
|
|
290
|
+
};
|
|
291
|
+
send({ type: "frame", frame: serialized, summary });
|
|
292
|
+
}
|
|
293
|
+
function clearTick() {
|
|
294
|
+
if (tickTimer) {
|
|
295
|
+
clearTimeout(tickTimer);
|
|
296
|
+
tickTimer = null;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
function scheduleTick() {
|
|
300
|
+
clearTick();
|
|
301
|
+
if (!testApp || disposed || !isActive)
|
|
302
|
+
return;
|
|
303
|
+
const intervalMs = fixedFps > 0 ? Math.max(16, Math.round(1000 / fixedFps)) : testApp.frame.animating ? 33 : 0;
|
|
304
|
+
if (intervalMs <= 0)
|
|
305
|
+
return;
|
|
306
|
+
tickTimer = setTimeout(() => {
|
|
307
|
+
tickTimer = null;
|
|
308
|
+
if (!testApp || disposed || !isActive)
|
|
309
|
+
return;
|
|
310
|
+
const now = performance.now();
|
|
311
|
+
const dt = Math.max(1, Math.min(250, now - lastTickAt));
|
|
312
|
+
lastTickAt = now;
|
|
313
|
+
try {
|
|
314
|
+
testApp.advance(dt);
|
|
315
|
+
emitFrame();
|
|
316
|
+
}
|
|
317
|
+
catch (err) {
|
|
318
|
+
sendError(err);
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
scheduleTick();
|
|
322
|
+
}, intervalMs);
|
|
323
|
+
}
|
|
324
|
+
async function handleInit(msg) {
|
|
325
|
+
isActive = msg.active;
|
|
326
|
+
appState = { ...msg.state };
|
|
327
|
+
stateListeners = new Set();
|
|
328
|
+
sentStyleCount = 1;
|
|
329
|
+
sentCopies = 0;
|
|
330
|
+
const ctx = {
|
|
331
|
+
id: msg.id,
|
|
332
|
+
title: msg.title,
|
|
333
|
+
get state() {
|
|
334
|
+
return appState;
|
|
335
|
+
},
|
|
336
|
+
setState(patch) {
|
|
337
|
+
appState = { ...appState, ...patch };
|
|
338
|
+
send({ type: "state", state: { ...appState }, summary: safeSummary() });
|
|
339
|
+
if (testApp && !disposed) {
|
|
340
|
+
testApp.frame.redraw();
|
|
341
|
+
testApp.settle();
|
|
342
|
+
emitFrame();
|
|
343
|
+
scheduleTick();
|
|
344
|
+
}
|
|
345
|
+
},
|
|
346
|
+
submit(action, patch) {
|
|
347
|
+
if (patch)
|
|
348
|
+
appState = { ...appState, ...patch };
|
|
349
|
+
send({ type: "submit", action, state: { ...appState }, summary: safeSummary() });
|
|
350
|
+
},
|
|
351
|
+
send(message, patch) {
|
|
352
|
+
if (patch)
|
|
353
|
+
appState = { ...appState, ...patch };
|
|
354
|
+
send({ type: "send", message, state: { ...appState }, summary: safeSummary() });
|
|
355
|
+
},
|
|
356
|
+
toast(text, tone = "info") {
|
|
357
|
+
send({ type: "toast", text, tone });
|
|
358
|
+
},
|
|
359
|
+
redraw() {
|
|
360
|
+
if (testApp && !disposed) {
|
|
361
|
+
testApp.frame.redraw();
|
|
362
|
+
testApp.settle();
|
|
363
|
+
emitFrame();
|
|
364
|
+
scheduleTick();
|
|
365
|
+
}
|
|
366
|
+
},
|
|
367
|
+
onState(listener) {
|
|
368
|
+
stateListeners.add(listener);
|
|
369
|
+
return () => stateListeners.delete(listener);
|
|
370
|
+
},
|
|
371
|
+
};
|
|
372
|
+
let mod;
|
|
373
|
+
try {
|
|
374
|
+
mod = (await import(__rewriteRelativeImportExtension(pathToFileURL(msg.modulePath).href)));
|
|
375
|
+
}
|
|
376
|
+
catch (err) {
|
|
377
|
+
sendError(err);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
let def = null;
|
|
381
|
+
try {
|
|
382
|
+
if (typeof mod.default === "function") {
|
|
383
|
+
const created = await mod.default(ctx);
|
|
384
|
+
if (!created || typeof created !== "object" || typeof created.view !== "function") {
|
|
385
|
+
throw new Error("Default export function must return { view(frame), keys?(key, frame), fps?, hints?, summary?, dispose? }");
|
|
386
|
+
}
|
|
387
|
+
def = created;
|
|
388
|
+
}
|
|
389
|
+
else if (mod.default &&
|
|
390
|
+
typeof mod.default === "object" &&
|
|
391
|
+
typeof mod.default.view === "function") {
|
|
392
|
+
def = mod.default;
|
|
393
|
+
}
|
|
394
|
+
else if (typeof mod.view === "function") {
|
|
395
|
+
const viewFn = mod.view;
|
|
396
|
+
const keysFn = typeof mod.keys === "function" ? mod.keys : undefined;
|
|
397
|
+
const summaryFn = typeof mod.summary === "function" ? mod.summary : undefined;
|
|
398
|
+
const disposeFn = typeof mod.dispose === "function" ? mod.dispose : undefined;
|
|
399
|
+
def = {
|
|
400
|
+
view: (frame) => viewFn(frame, ctx),
|
|
401
|
+
keys: keysFn ? (key, frame) => keysFn(key, frame, ctx) : undefined,
|
|
402
|
+
fps: typeof mod.fps === "number" ? mod.fps : undefined,
|
|
403
|
+
hints: Array.isArray(mod.hints) ? mod.hints.map(String) : undefined,
|
|
404
|
+
captureKeys: typeof mod.captureKeys === "boolean" ? mod.captureKeys : undefined,
|
|
405
|
+
summary: summaryFn ? () => summaryFn(ctx) : undefined,
|
|
406
|
+
dispose: disposeFn,
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
else {
|
|
410
|
+
throw new Error("App module must export `default function (ctx) { return { view(frame) { ... } } }` or named `export function view(frame, ctx)`.");
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
catch (err) {
|
|
414
|
+
sendError(err);
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
definition = def;
|
|
418
|
+
fixedFps = typeof def.fps === "number" && def.fps > 0 ? Math.max(1, Math.min(60, Math.round(def.fps))) : 0;
|
|
419
|
+
try {
|
|
420
|
+
testApp = createTestApp({
|
|
421
|
+
columns: Math.max(10, msg.columns),
|
|
422
|
+
rows: Math.max(4, msg.rows),
|
|
423
|
+
mouse: true,
|
|
424
|
+
graphics: false,
|
|
425
|
+
fps: fixedFps || undefined,
|
|
426
|
+
theme: workerTheme,
|
|
427
|
+
view: (frame) => {
|
|
428
|
+
try {
|
|
429
|
+
def.view(frame);
|
|
430
|
+
}
|
|
431
|
+
catch (err) {
|
|
432
|
+
sendError(err);
|
|
433
|
+
}
|
|
434
|
+
},
|
|
435
|
+
keys: (key, frame) => {
|
|
436
|
+
if (!def?.keys)
|
|
437
|
+
return false;
|
|
438
|
+
try {
|
|
439
|
+
return Boolean(def.keys(key, frame));
|
|
440
|
+
}
|
|
441
|
+
catch (err) {
|
|
442
|
+
sendError(err);
|
|
443
|
+
return false;
|
|
444
|
+
}
|
|
445
|
+
},
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
catch (err) {
|
|
449
|
+
sendError(err);
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
send({
|
|
453
|
+
type: "ready",
|
|
454
|
+
hints: def.hints,
|
|
455
|
+
captureKeys: def.captureKeys,
|
|
456
|
+
fps: fixedFps || undefined,
|
|
457
|
+
state: { ...appState },
|
|
458
|
+
summary: safeSummary(),
|
|
459
|
+
});
|
|
460
|
+
lastTickAt = performance.now();
|
|
461
|
+
emitFrame(true);
|
|
462
|
+
scheduleTick();
|
|
463
|
+
}
|
|
464
|
+
async function handleDispose() {
|
|
465
|
+
if (disposed)
|
|
466
|
+
return;
|
|
467
|
+
disposed = true;
|
|
468
|
+
clearTick();
|
|
469
|
+
try {
|
|
470
|
+
await definition?.dispose?.();
|
|
471
|
+
}
|
|
472
|
+
catch {
|
|
473
|
+
// Ignore dispose errors on shutdown.
|
|
474
|
+
}
|
|
475
|
+
process.exit(0);
|
|
476
|
+
}
|
|
477
|
+
process.on("message", (msg) => {
|
|
478
|
+
if (!msg || typeof msg !== "object")
|
|
479
|
+
return;
|
|
480
|
+
switch (msg.type) {
|
|
481
|
+
case "init":
|
|
482
|
+
void handleInit(msg);
|
|
483
|
+
return;
|
|
484
|
+
case "resize":
|
|
485
|
+
if (testApp && !disposed) {
|
|
486
|
+
testApp.resize(Math.max(10, msg.columns), Math.max(4, msg.rows));
|
|
487
|
+
emitFrame(true);
|
|
488
|
+
scheduleTick();
|
|
489
|
+
}
|
|
490
|
+
return;
|
|
491
|
+
case "active":
|
|
492
|
+
isActive = msg.active;
|
|
493
|
+
if (isActive && testApp && !disposed) {
|
|
494
|
+
lastTickAt = performance.now();
|
|
495
|
+
emitFrame(true);
|
|
496
|
+
scheduleTick();
|
|
497
|
+
}
|
|
498
|
+
else {
|
|
499
|
+
clearTick();
|
|
500
|
+
}
|
|
501
|
+
return;
|
|
502
|
+
case "focus":
|
|
503
|
+
if (testApp && !disposed && testApp.frame.focused !== msg.id) {
|
|
504
|
+
testApp.frame.focused = msg.id;
|
|
505
|
+
testApp.frame.redraw();
|
|
506
|
+
testApp.settle();
|
|
507
|
+
emitFrame();
|
|
508
|
+
scheduleTick();
|
|
509
|
+
}
|
|
510
|
+
return;
|
|
511
|
+
case "key":
|
|
512
|
+
if (testApp && !disposed) {
|
|
513
|
+
try {
|
|
514
|
+
dispatchKey(testApp, msg.key);
|
|
515
|
+
emitFrame();
|
|
516
|
+
scheduleTick();
|
|
517
|
+
}
|
|
518
|
+
catch (err) {
|
|
519
|
+
sendError(err);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
return;
|
|
523
|
+
case "mouse":
|
|
524
|
+
if (testApp && !disposed) {
|
|
525
|
+
try {
|
|
526
|
+
testApp.input(encodeSgrMouse(msg.mouse, msg.ctrl, msg.alt, msg.shift));
|
|
527
|
+
emitFrame();
|
|
528
|
+
scheduleTick();
|
|
529
|
+
}
|
|
530
|
+
catch (err) {
|
|
531
|
+
sendError(err);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
return;
|
|
535
|
+
case "state":
|
|
536
|
+
appState = { ...appState, ...msg.state };
|
|
537
|
+
for (const listener of stateListeners) {
|
|
538
|
+
try {
|
|
539
|
+
listener({ ...appState });
|
|
540
|
+
}
|
|
541
|
+
catch (err) {
|
|
542
|
+
sendError(err);
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
if (testApp && !disposed) {
|
|
546
|
+
testApp.frame.redraw();
|
|
547
|
+
testApp.settle();
|
|
548
|
+
emitFrame();
|
|
549
|
+
scheduleTick();
|
|
550
|
+
}
|
|
551
|
+
return;
|
|
552
|
+
case "dispose":
|
|
553
|
+
void handleDispose();
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
});
|