@eserstack/mux 0.0.0-bootstrap.0 → 5.0.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/LICENSE +13 -0
- package/README.md +151 -0
- package/dist/engine/effects.d.ts +52 -0
- package/dist/engine/effects.js +2 -0
- package/dist/engine/geometry.d.ts +57 -0
- package/dist/engine/geometry.js +170 -0
- package/dist/engine/mod.d.ts +13 -0
- package/dist/engine/mod.js +5 -0
- package/dist/engine/reducer.d.ts +15 -0
- package/dist/engine/reducer.js +325 -0
- package/dist/engine/sanitize.d.ts +60 -0
- package/dist/engine/sanitize.js +64 -0
- package/dist/engine/split-tree.d.ts +41 -0
- package/dist/engine/split-tree.js +117 -0
- package/dist/engine/types.d.ts +192 -0
- package/dist/engine/types.js +2 -0
- package/dist/frontends/frontend.d.ts +17 -0
- package/dist/frontends/frontend.js +2 -0
- package/dist/frontends/tui/compositor.d.ts +23 -0
- package/dist/frontends/tui/compositor.js +72 -0
- package/dist/frontends/tui/frame.d.ts +9 -0
- package/dist/frontends/tui/frame.js +2 -0
- package/dist/frontends/tui/input-source.d.ts +21 -0
- package/dist/frontends/tui/input-source.js +40 -0
- package/dist/frontends/tui/tui-frontend.d.ts +36 -0
- package/dist/frontends/tui/tui-frontend.js +188 -0
- package/dist/ipc/protocol.d.ts +41 -0
- package/dist/ipc/protocol.js +2 -0
- package/dist/ipc/transport-inprocess.d.ts +9 -0
- package/dist/ipc/transport-inprocess.js +35 -0
- package/dist/ipc/transport-ws.d.ts +19 -0
- package/dist/ipc/transport-ws.js +37 -0
- package/dist/keybindings/defaults.d.ts +3 -0
- package/dist/keybindings/defaults.js +80 -0
- package/dist/keybindings/mod.d.ts +10 -0
- package/dist/keybindings/mod.js +4 -0
- package/dist/keybindings/modes.d.ts +19 -0
- package/dist/keybindings/modes.js +18 -0
- package/dist/keybindings/registry.d.ts +20 -0
- package/dist/keybindings/registry.js +37 -0
- package/dist/mod.d.ts +28 -0
- package/dist/mod.js +21 -0
- package/dist/render/scheduler.d.ts +18 -0
- package/dist/render/scheduler.js +33 -0
- package/dist/runner/inprocess.d.ts +10 -0
- package/dist/runner/inprocess.js +60 -0
- package/dist/scene/diff.d.ts +26 -0
- package/dist/scene/diff.js +83 -0
- package/dist/scene/mod.d.ts +9 -0
- package/dist/scene/mod.js +3 -0
- package/dist/scene/scene.d.ts +30 -0
- package/dist/scene/scene.js +54 -0
- package/dist/server/pty-host.d.ts +25 -0
- package/dist/server/pty-host.js +81 -0
- package/dist/server/server.d.ts +58 -0
- package/dist/server/server.js +204 -0
- package/package.json +51 -1
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/** Create a connected [a, b] transport pair; a.send is delivered to b's handler. */
|
|
3
|
+
export const createInProcessPair = () => {
|
|
4
|
+
let aHandler = null;
|
|
5
|
+
let bHandler = null;
|
|
6
|
+
// Per-endpoint close: closing one side stops delivery TO that side only, so a
|
|
7
|
+
// frontend that closes doesn't also silence the other direction.
|
|
8
|
+
let aClosed = false;
|
|
9
|
+
let bClosed = false;
|
|
10
|
+
const a = {
|
|
11
|
+
send(msg) {
|
|
12
|
+
if (!bClosed)
|
|
13
|
+
bHandler?.(msg);
|
|
14
|
+
},
|
|
15
|
+
onMessage(handler) {
|
|
16
|
+
aHandler = handler;
|
|
17
|
+
},
|
|
18
|
+
close() {
|
|
19
|
+
aClosed = true;
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
const b = {
|
|
23
|
+
send(msg) {
|
|
24
|
+
if (!aClosed)
|
|
25
|
+
aHandler?.(msg);
|
|
26
|
+
},
|
|
27
|
+
onMessage(handler) {
|
|
28
|
+
bHandler = handler;
|
|
29
|
+
},
|
|
30
|
+
close() {
|
|
31
|
+
bClosed = true;
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
return [a, b];
|
|
35
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebSocket transport for the neutral mux protocol.
|
|
3
|
+
*
|
|
4
|
+
* Adapts a `WebSocket` (the socket `runtime.http.upgradeWebSocket` returns on
|
|
5
|
+
* the server, or the browser's `WebSocket` on the client) to a
|
|
6
|
+
* {@link Transport}. Messages are
|
|
7
|
+
* JSON; pane output rides as a string field, so no binary framing is needed.
|
|
8
|
+
*
|
|
9
|
+
* Because the server keeps panes alive across disconnects, a dropped socket just
|
|
10
|
+
* stops delivery; a fresh socket + `server.connect` resumes with a full scene.
|
|
11
|
+
*
|
|
12
|
+
* @module
|
|
13
|
+
*/
|
|
14
|
+
import type { Transport } from "./protocol.ts";
|
|
15
|
+
export type WsTransportOptions = {
|
|
16
|
+
/** Called when the socket closes (e.g. so the server can drop the sink). */
|
|
17
|
+
readonly onClose?: () => void;
|
|
18
|
+
};
|
|
19
|
+
export declare const wsTransport: <Out, In>(socket: WebSocket, opts?: WsTransportOptions) => Transport<Out, In>;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
export const wsTransport = (socket, opts = {}) => {
|
|
3
|
+
let handler = null;
|
|
4
|
+
socket.addEventListener("message", (ev) => {
|
|
5
|
+
if (typeof ev.data !== "string")
|
|
6
|
+
return;
|
|
7
|
+
let msg;
|
|
8
|
+
try {
|
|
9
|
+
msg = JSON.parse(ev.data);
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return; // ignore malformed frames
|
|
13
|
+
}
|
|
14
|
+
handler?.(msg);
|
|
15
|
+
});
|
|
16
|
+
if (opts.onClose !== undefined) {
|
|
17
|
+
socket.addEventListener("close", () => opts.onClose?.());
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
send(msg) {
|
|
21
|
+
if (socket.readyState === WebSocket.OPEN) {
|
|
22
|
+
socket.send(JSON.stringify(msg));
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
onMessage(h) {
|
|
26
|
+
handler = h;
|
|
27
|
+
},
|
|
28
|
+
close() {
|
|
29
|
+
try {
|
|
30
|
+
socket.close();
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
// already closing/closed
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
import * as registry from "./registry.js";
|
|
3
|
+
const toNormal = { type: "switchMode", mode: "normal" };
|
|
4
|
+
const DEFAULT_TABLE = {
|
|
5
|
+
normal: {
|
|
6
|
+
"ctrl+p": { type: "switchMode", mode: "pane" },
|
|
7
|
+
"ctrl+t": { type: "switchMode", mode: "tab" },
|
|
8
|
+
"ctrl+n": { type: "switchMode", mode: "resize" },
|
|
9
|
+
"ctrl+s": { type: "switchMode", mode: "scroll" },
|
|
10
|
+
"ctrl+g": { type: "switchMode", mode: "locked" },
|
|
11
|
+
"ctrl+o": { type: "detach" },
|
|
12
|
+
"ctrl+q": { type: "quit" },
|
|
13
|
+
},
|
|
14
|
+
pane: {
|
|
15
|
+
n: [{ type: "newPane" }, toNormal],
|
|
16
|
+
v: [{ type: "splitPane", direction: "vertical" }, toNormal],
|
|
17
|
+
s: [{ type: "splitPane", direction: "horizontal" }, toNormal],
|
|
18
|
+
x: [{ type: "closeFocusedPane" }, toNormal],
|
|
19
|
+
f: [{ type: "toggleFullscreen" }, toNormal],
|
|
20
|
+
z: [{ type: "toggleFullscreen" }, toNormal],
|
|
21
|
+
left: { type: "focusDirection", direction: "left" },
|
|
22
|
+
right: { type: "focusDirection", direction: "right" },
|
|
23
|
+
up: { type: "focusDirection", direction: "up" },
|
|
24
|
+
down: { type: "focusDirection", direction: "down" },
|
|
25
|
+
h: { type: "focusDirection", direction: "left" },
|
|
26
|
+
l: { type: "focusDirection", direction: "right" },
|
|
27
|
+
k: { type: "focusDirection", direction: "up" },
|
|
28
|
+
j: { type: "focusDirection", direction: "down" },
|
|
29
|
+
enter: toNormal,
|
|
30
|
+
escape: toNormal,
|
|
31
|
+
},
|
|
32
|
+
tab: {
|
|
33
|
+
n: [{ type: "newTab" }, toNormal],
|
|
34
|
+
x: [{ type: "closeTab" }, toNormal],
|
|
35
|
+
left: { type: "prevTab" },
|
|
36
|
+
h: { type: "prevTab" },
|
|
37
|
+
right: { type: "nextTab" },
|
|
38
|
+
l: { type: "nextTab" },
|
|
39
|
+
"1": { type: "gotoTab", index: 0 },
|
|
40
|
+
"2": { type: "gotoTab", index: 1 },
|
|
41
|
+
"3": { type: "gotoTab", index: 2 },
|
|
42
|
+
"4": { type: "gotoTab", index: 3 },
|
|
43
|
+
"5": { type: "gotoTab", index: 4 },
|
|
44
|
+
"6": { type: "gotoTab", index: 5 },
|
|
45
|
+
"7": { type: "gotoTab", index: 6 },
|
|
46
|
+
"8": { type: "gotoTab", index: 7 },
|
|
47
|
+
"9": { type: "gotoTab", index: 8 },
|
|
48
|
+
enter: toNormal,
|
|
49
|
+
escape: toNormal,
|
|
50
|
+
},
|
|
51
|
+
resize: {
|
|
52
|
+
left: { type: "resizePane", direction: "left" },
|
|
53
|
+
right: { type: "resizePane", direction: "right" },
|
|
54
|
+
up: { type: "resizePane", direction: "up" },
|
|
55
|
+
down: { type: "resizePane", direction: "down" },
|
|
56
|
+
h: { type: "resizePane", direction: "left" },
|
|
57
|
+
l: { type: "resizePane", direction: "right" },
|
|
58
|
+
k: { type: "resizePane", direction: "up" },
|
|
59
|
+
j: { type: "resizePane", direction: "down" },
|
|
60
|
+
enter: toNormal,
|
|
61
|
+
escape: toNormal,
|
|
62
|
+
},
|
|
63
|
+
scroll: {
|
|
64
|
+
up: { type: "scroll", action: "up" },
|
|
65
|
+
down: { type: "scroll", action: "down" },
|
|
66
|
+
k: { type: "scroll", action: "up" },
|
|
67
|
+
j: { type: "scroll", action: "down" },
|
|
68
|
+
pageup: { type: "scroll", action: "pageUp" },
|
|
69
|
+
pagedown: { type: "scroll", action: "pageDown" },
|
|
70
|
+
g: { type: "scroll", action: "home" },
|
|
71
|
+
"shift+g": { type: "scroll", action: "end" },
|
|
72
|
+
enter: toNormal,
|
|
73
|
+
escape: toNormal,
|
|
74
|
+
},
|
|
75
|
+
locked: {
|
|
76
|
+
"ctrl+g": toNormal,
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
/** The compiled default keymap. */
|
|
80
|
+
export const defaultKeymap = registry.buildKeymap(DEFAULT_TABLE);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keybinding/mode registry for the multiplexer.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
export type { InputMode, KeyChord } from "./modes.ts";
|
|
7
|
+
export { parseChord } from "./modes.ts";
|
|
8
|
+
export type { Keymap, KeymapTable } from "./registry.ts";
|
|
9
|
+
export { buildKeymap, lookup, mergeOverrides } from "./registry.ts";
|
|
10
|
+
export { defaultKeymap } from "./defaults.ts";
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Input modes and key-chord parsing.
|
|
3
|
+
*
|
|
4
|
+
* A {@link KeyChord} is a canonical string like `"ctrl+p"`, `"alt+left"`,
|
|
5
|
+
* `"shift+tab"`, `"n"`, or `"enter"` — produced from a {@link KeypressEvent} so
|
|
6
|
+
* the registry can look bindings up by a stable key.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import type { KeypressEvent } from "@eserstack/shell/tui";
|
|
11
|
+
export type { InputMode } from "../engine/types.ts";
|
|
12
|
+
/** Canonical, registry-friendly representation of a key + modifiers. */
|
|
13
|
+
export type KeyChord = string;
|
|
14
|
+
/**
|
|
15
|
+
* Build the canonical chord for a key event. Letter case is normalised to lower
|
|
16
|
+
* (shift is carried by the case for printable keys, and added as an explicit
|
|
17
|
+
* `shift+` modifier only for named keys like arrows/tab).
|
|
18
|
+
*/
|
|
19
|
+
export declare const parseChord: (ev: KeypressEvent) => KeyChord;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* Build the canonical chord for a key event. Letter case is normalised to lower
|
|
4
|
+
* (shift is carried by the case for printable keys, and added as an explicit
|
|
5
|
+
* `shift+` modifier only for named keys like arrows/tab).
|
|
6
|
+
*/
|
|
7
|
+
export const parseChord = (ev) => {
|
|
8
|
+
const parts = [];
|
|
9
|
+
if (ev.ctrl)
|
|
10
|
+
parts.push("ctrl");
|
|
11
|
+
if (ev.meta)
|
|
12
|
+
parts.push("alt");
|
|
13
|
+
const isPrintable = ev.name.length === 1;
|
|
14
|
+
if (ev.shift && !isPrintable)
|
|
15
|
+
parts.push("shift");
|
|
16
|
+
parts.push(isPrintable ? ev.name.toLowerCase() : ev.name);
|
|
17
|
+
return parts.join("+");
|
|
18
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keybinding registry: a `Mode -> (Chord -> Action[])` lookup table.
|
|
3
|
+
*
|
|
4
|
+
* Tables are authored as plain objects ({@link KeymapTable}) and compiled into
|
|
5
|
+
* nested maps for O(1) lookup. User overrides merge on top of the defaults.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import type { Action, InputMode } from "../engine/types.ts";
|
|
10
|
+
import type { KeyChord } from "./modes.ts";
|
|
11
|
+
/** Compiled, immutable keymap. */
|
|
12
|
+
export type Keymap = ReadonlyMap<InputMode, ReadonlyMap<KeyChord, readonly Action[]>>;
|
|
13
|
+
/** Authoring shape: a single action or a list per chord, per mode. */
|
|
14
|
+
export type KeymapTable = Partial<Record<InputMode, Record<KeyChord, Action | readonly Action[]>>>;
|
|
15
|
+
/** Compile an authoring table into a {@link Keymap}. */
|
|
16
|
+
export declare const buildKeymap: (table: KeymapTable) => Keymap;
|
|
17
|
+
/** Look up the actions bound to a chord in a mode, or `undefined` if unbound. */
|
|
18
|
+
export declare const lookup: (km: Keymap, mode: InputMode, chord: KeyChord) => readonly Action[] | undefined;
|
|
19
|
+
/** Merge user overrides over a base keymap (per mode, per chord). */
|
|
20
|
+
export declare const mergeOverrides: (base: Keymap, overrides: KeymapTable) => Keymap;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
const toList = (v) => Array.isArray(v) ? v : [v];
|
|
3
|
+
/** Compile an authoring table into a {@link Keymap}. */
|
|
4
|
+
export const buildKeymap = (table) => {
|
|
5
|
+
const map = new Map();
|
|
6
|
+
for (const mode of Object.keys(table)) {
|
|
7
|
+
const chords = table[mode];
|
|
8
|
+
if (chords === undefined)
|
|
9
|
+
continue;
|
|
10
|
+
const inner = new Map();
|
|
11
|
+
for (const chord of Object.keys(chords)) {
|
|
12
|
+
inner.set(chord, toList(chords[chord]));
|
|
13
|
+
}
|
|
14
|
+
map.set(mode, inner);
|
|
15
|
+
}
|
|
16
|
+
return map;
|
|
17
|
+
};
|
|
18
|
+
/** Look up the actions bound to a chord in a mode, or `undefined` if unbound. */
|
|
19
|
+
export const lookup = (km, mode, chord) => km.get(mode)?.get(chord);
|
|
20
|
+
/** Merge user overrides over a base keymap (per mode, per chord). */
|
|
21
|
+
export const mergeOverrides = (base, overrides) => {
|
|
22
|
+
const merged = new Map();
|
|
23
|
+
for (const [mode, chords] of base) {
|
|
24
|
+
merged.set(mode, new Map(chords));
|
|
25
|
+
}
|
|
26
|
+
for (const mode of Object.keys(overrides)) {
|
|
27
|
+
const chords = overrides[mode];
|
|
28
|
+
if (chords === undefined)
|
|
29
|
+
continue;
|
|
30
|
+
const inner = merged.get(mode) ?? new Map();
|
|
31
|
+
for (const chord of Object.keys(chords)) {
|
|
32
|
+
inner.set(chord, toList(chords[chord]));
|
|
33
|
+
}
|
|
34
|
+
merged.set(mode, inner);
|
|
35
|
+
}
|
|
36
|
+
return merged;
|
|
37
|
+
};
|
package/dist/mod.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@eserstack/mux` — a frontend-neutral terminal-multiplexer core.
|
|
3
|
+
*
|
|
4
|
+
* The pure engine (split tree, geometry, reducer), the keybinding/mode registry,
|
|
5
|
+
* the neutral scene model, the server that owns PTYs, and an ANSI TUI frontend.
|
|
6
|
+
* Frontends and transports are interchangeable: the server speaks one neutral
|
|
7
|
+
* protocol whether it drives a terminal or a browser.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
export * as engine from "./engine/mod.ts";
|
|
12
|
+
export * as keybindings from "./keybindings/mod.ts";
|
|
13
|
+
export * as scene from "./scene/mod.ts";
|
|
14
|
+
export type { FrontendToServer, ServerToFrontend, Transport, } from "./ipc/protocol.ts";
|
|
15
|
+
export { createInProcessPair } from "./ipc/transport-inprocess.ts";
|
|
16
|
+
export { wsTransport } from "./ipc/transport-ws.ts";
|
|
17
|
+
export type { WsTransportOptions } from "./ipc/transport-ws.ts";
|
|
18
|
+
export { createScheduler } from "./render/scheduler.ts";
|
|
19
|
+
export type { RenderScheduler } from "./render/scheduler.ts";
|
|
20
|
+
export type { Frontend } from "./frontends/frontend.ts";
|
|
21
|
+
export { createTuiFrontend } from "./frontends/tui/tui-frontend.ts";
|
|
22
|
+
export type { DecorateFn, TuiFrontendOptions, } from "./frontends/tui/tui-frontend.ts";
|
|
23
|
+
export { createServer } from "./server/server.ts";
|
|
24
|
+
export type { MuxServer, MuxServerOptions, SpawnResolver, } from "./server/server.ts";
|
|
25
|
+
export { createPtyHost } from "./server/pty-host.ts";
|
|
26
|
+
export type { PtyHost, PtyHostCallbacks, SpawnRequest, } from "./server/pty-host.ts";
|
|
27
|
+
export { runInProcess } from "./runner/inprocess.ts";
|
|
28
|
+
export type { RunInProcessOptions } from "./runner/inprocess.ts";
|
package/dist/mod.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* `@eserstack/mux` — a frontend-neutral terminal-multiplexer core.
|
|
4
|
+
*
|
|
5
|
+
* The pure engine (split tree, geometry, reducer), the keybinding/mode registry,
|
|
6
|
+
* the neutral scene model, the server that owns PTYs, and an ANSI TUI frontend.
|
|
7
|
+
* Frontends and transports are interchangeable: the server speaks one neutral
|
|
8
|
+
* protocol whether it drives a terminal or a browser.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
export * as engine from "./engine/mod.js";
|
|
13
|
+
export * as keybindings from "./keybindings/mod.js";
|
|
14
|
+
export * as scene from "./scene/mod.js";
|
|
15
|
+
export { createInProcessPair } from "./ipc/transport-inprocess.js";
|
|
16
|
+
export { wsTransport } from "./ipc/transport-ws.js";
|
|
17
|
+
export { createScheduler } from "./render/scheduler.js";
|
|
18
|
+
export { createTuiFrontend } from "./frontends/tui/tui-frontend.js";
|
|
19
|
+
export { createServer } from "./server/server.js";
|
|
20
|
+
export { createPtyHost } from "./server/pty-host.js";
|
|
21
|
+
export { runInProcess } from "./runner/inprocess.js";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A single coalescing render scheduler.
|
|
3
|
+
*
|
|
4
|
+
* Replaces the per-PTY `setTimeout` storms of the old multiplexer: every source
|
|
5
|
+
* of change calls `schedule()`, and at most one render fires per frame (~60fps).
|
|
6
|
+
* `requestFull()` marks the next frame as a full repaint.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
export type RenderScheduler = {
|
|
11
|
+
/** Request a render on the next frame (coalesced). */
|
|
12
|
+
schedule(): void;
|
|
13
|
+
/** Request a full repaint on the next frame. */
|
|
14
|
+
requestFull(): void;
|
|
15
|
+
/** Cancel any pending frame. */
|
|
16
|
+
stop(): void;
|
|
17
|
+
};
|
|
18
|
+
export declare const createScheduler: (render: (fullRedraw: boolean) => void, frameMs?: number) => RenderScheduler;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
export const createScheduler = (render, frameMs = 16) => {
|
|
3
|
+
let pending = false;
|
|
4
|
+
let full = false;
|
|
5
|
+
let timer = null;
|
|
6
|
+
const fire = () => {
|
|
7
|
+
pending = false;
|
|
8
|
+
timer = null;
|
|
9
|
+
const wasFull = full;
|
|
10
|
+
full = false;
|
|
11
|
+
render(wasFull);
|
|
12
|
+
};
|
|
13
|
+
const schedule = () => {
|
|
14
|
+
if (pending)
|
|
15
|
+
return;
|
|
16
|
+
pending = true;
|
|
17
|
+
timer = setTimeout(fire, frameMs);
|
|
18
|
+
};
|
|
19
|
+
return {
|
|
20
|
+
schedule,
|
|
21
|
+
requestFull() {
|
|
22
|
+
full = true;
|
|
23
|
+
schedule();
|
|
24
|
+
},
|
|
25
|
+
stop() {
|
|
26
|
+
if (timer !== null) {
|
|
27
|
+
clearTimeout(timer);
|
|
28
|
+
timer = null;
|
|
29
|
+
}
|
|
30
|
+
pending = false;
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import * as muxServer from "../server/server.ts";
|
|
2
|
+
export type RunInProcessOptions = {
|
|
3
|
+
/** Override the default shell command for terminal panes. */
|
|
4
|
+
readonly shell?: string;
|
|
5
|
+
readonly shellArgs?: readonly string[];
|
|
6
|
+
readonly cwd?: string;
|
|
7
|
+
/** Fully custom spawn resolver (e.g. an agent adapter). */
|
|
8
|
+
readonly resolveSpawn?: muxServer.SpawnResolver;
|
|
9
|
+
};
|
|
10
|
+
export declare const runInProcess: (opts?: RunInProcessOptions) => Promise<number>;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* Run the mux server and the ANSI TUI frontend together in one process.
|
|
4
|
+
*
|
|
5
|
+
* This is the drop-in replacement for the old hand-rolled multiplexer loop, and
|
|
6
|
+
* the simplest way to exercise the whole stack end-to-end. The socket/daemon
|
|
7
|
+
* runners reuse the same server unchanged.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
import * as tui from "@eserstack/shell/tui";
|
|
12
|
+
import * as crossRuntime from "@eserstack/standards/cross-runtime";
|
|
13
|
+
import * as reducer from "../engine/reducer.js";
|
|
14
|
+
import * as keybindings from "../keybindings/mod.js";
|
|
15
|
+
import * as transportInprocess from "../ipc/transport-inprocess.js";
|
|
16
|
+
import * as tuiFrontend from "../frontends/tui/tui-frontend.js";
|
|
17
|
+
import * as muxServer from "../server/server.js";
|
|
18
|
+
const defaultShell = () => {
|
|
19
|
+
if (crossRuntime.getPlatform() === "windows") {
|
|
20
|
+
return { command: "powershell.exe", args: [] };
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
command: crossRuntime.runtime.env.get("SHELL") ?? "/bin/sh",
|
|
24
|
+
args: [],
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
export const runInProcess = async (opts = {}) => {
|
|
28
|
+
const size = tui.terminal.getTerminalSize();
|
|
29
|
+
const initialState = reducer.createInitialState({
|
|
30
|
+
cols: size.cols,
|
|
31
|
+
rows: size.rows,
|
|
32
|
+
});
|
|
33
|
+
const resolveSpawn = opts.resolveSpawn ??
|
|
34
|
+
((pane) => {
|
|
35
|
+
const fallback = defaultShell();
|
|
36
|
+
return {
|
|
37
|
+
command: pane.command ?? opts.shell ?? fallback.command,
|
|
38
|
+
args: pane.args ?? opts.shellArgs ?? fallback.args,
|
|
39
|
+
cwd: pane.cwd ?? opts.cwd,
|
|
40
|
+
env: crossRuntime.runtime.env.toObject(),
|
|
41
|
+
};
|
|
42
|
+
});
|
|
43
|
+
const server = muxServer.createServer({ initialState, resolveSpawn });
|
|
44
|
+
const [serverSide, frontendSide] = transportInprocess.createInProcessPair();
|
|
45
|
+
server.connect(serverSide);
|
|
46
|
+
const frontend = tuiFrontend.createTuiFrontend({
|
|
47
|
+
transport: frontendSide,
|
|
48
|
+
keymap: keybindings.defaultKeymap,
|
|
49
|
+
});
|
|
50
|
+
// Open the first tab; the frontend's attach will pull a full scene.
|
|
51
|
+
server.dispatch({ type: "newTab" });
|
|
52
|
+
try {
|
|
53
|
+
await frontend.run();
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
frontend.stop();
|
|
57
|
+
await server.dispose();
|
|
58
|
+
}
|
|
59
|
+
return 0;
|
|
60
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal scene deltas so the server broadcasts only what changed.
|
|
3
|
+
*
|
|
4
|
+
* Top-level scalars (mode, activeTab, focusedPane, viewport) are compared
|
|
5
|
+
* field-by-field; the tab list and pane list are compared structurally and sent
|
|
6
|
+
* whole when they differ (they are small and change rarely relative to output).
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import type { Chrome } from "../engine/geometry.ts";
|
|
11
|
+
import type { PaneId, Viewport } from "../engine/types.ts";
|
|
12
|
+
import type { Scene, ScenePane, SceneTab } from "./scene.ts";
|
|
13
|
+
export type SceneDelta = {
|
|
14
|
+
mode?: Scene["mode"];
|
|
15
|
+
activeTab?: number;
|
|
16
|
+
focusedPane?: PaneId | null;
|
|
17
|
+
viewport?: Viewport;
|
|
18
|
+
tabs?: readonly SceneTab[];
|
|
19
|
+
panes?: readonly ScenePane[];
|
|
20
|
+
chrome?: Chrome;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Compute the delta from `prev` to `next`, or `null` when nothing changed.
|
|
24
|
+
* A `null` `prev` yields a full delta (everything), for a fresh attach.
|
|
25
|
+
*/
|
|
26
|
+
export declare const diffScene: (prev: Scene | null, next: Scene) => SceneDelta | null;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
const sameChrome = (a, b) => a.tabBarRows === b.tabBarRows &&
|
|
3
|
+
a.statusBarRows === b.statusBarRows &&
|
|
4
|
+
(a.leftCols ?? 0) === (b.leftCols ?? 0) &&
|
|
5
|
+
(a.rightCols ?? 0) === (b.rightCols ?? 0) &&
|
|
6
|
+
(a.topRows ?? 0) === (b.topRows ?? 0) &&
|
|
7
|
+
(a.bottomRows ?? 0) === (b.bottomRows ?? 0);
|
|
8
|
+
const sameTabs = (a, b) => {
|
|
9
|
+
if (a.length !== b.length)
|
|
10
|
+
return false;
|
|
11
|
+
for (let i = 0; i < a.length; i++) {
|
|
12
|
+
if (a[i].id !== b[i].id || a[i].name !== b[i].name)
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
return true;
|
|
16
|
+
};
|
|
17
|
+
const samePane = (a, b) => a.id === b.id &&
|
|
18
|
+
a.kind === b.kind &&
|
|
19
|
+
a.title === b.title &&
|
|
20
|
+
a.focused === b.focused &&
|
|
21
|
+
a.exited === b.exited &&
|
|
22
|
+
a.geom.x === b.geom.x &&
|
|
23
|
+
a.geom.y === b.geom.y &&
|
|
24
|
+
a.geom.width === b.geom.width &&
|
|
25
|
+
a.geom.height === b.geom.height;
|
|
26
|
+
const samePanes = (a, b) => {
|
|
27
|
+
if (a.length !== b.length)
|
|
28
|
+
return false;
|
|
29
|
+
for (let i = 0; i < a.length; i++) {
|
|
30
|
+
if (!samePane(a[i], b[i]))
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
return true;
|
|
34
|
+
};
|
|
35
|
+
const sameViewport = (a, b) => a.cols === b.cols && a.rows === b.rows;
|
|
36
|
+
/**
|
|
37
|
+
* Compute the delta from `prev` to `next`, or `null` when nothing changed.
|
|
38
|
+
* A `null` `prev` yields a full delta (everything), for a fresh attach.
|
|
39
|
+
*/
|
|
40
|
+
export const diffScene = (prev, next) => {
|
|
41
|
+
if (prev === null) {
|
|
42
|
+
return {
|
|
43
|
+
mode: next.mode,
|
|
44
|
+
activeTab: next.activeTab,
|
|
45
|
+
focusedPane: next.focusedPane,
|
|
46
|
+
viewport: next.viewport,
|
|
47
|
+
tabs: next.tabs,
|
|
48
|
+
panes: next.panes,
|
|
49
|
+
chrome: next.chrome,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
const delta = {};
|
|
53
|
+
let changed = false;
|
|
54
|
+
if (prev.mode !== next.mode) {
|
|
55
|
+
delta.mode = next.mode;
|
|
56
|
+
changed = true;
|
|
57
|
+
}
|
|
58
|
+
if (prev.activeTab !== next.activeTab) {
|
|
59
|
+
delta.activeTab = next.activeTab;
|
|
60
|
+
changed = true;
|
|
61
|
+
}
|
|
62
|
+
if (prev.focusedPane !== next.focusedPane) {
|
|
63
|
+
delta.focusedPane = next.focusedPane;
|
|
64
|
+
changed = true;
|
|
65
|
+
}
|
|
66
|
+
if (!sameViewport(prev.viewport, next.viewport)) {
|
|
67
|
+
delta.viewport = next.viewport;
|
|
68
|
+
changed = true;
|
|
69
|
+
}
|
|
70
|
+
if (!sameTabs(prev.tabs, next.tabs)) {
|
|
71
|
+
delta.tabs = next.tabs;
|
|
72
|
+
changed = true;
|
|
73
|
+
}
|
|
74
|
+
if (!samePanes(prev.panes, next.panes)) {
|
|
75
|
+
delta.panes = next.panes;
|
|
76
|
+
changed = true;
|
|
77
|
+
}
|
|
78
|
+
if (!sameChrome(prev.chrome, next.chrome)) {
|
|
79
|
+
delta.chrome = next.chrome;
|
|
80
|
+
changed = true;
|
|
81
|
+
}
|
|
82
|
+
return changed ? delta : null;
|
|
83
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Frontend-neutral scene model and delta computation.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
export type { Scene, ScenePane, SceneTab } from "./scene.ts";
|
|
7
|
+
export { deriveScene } from "./scene.ts";
|
|
8
|
+
export type { SceneDelta } from "./diff.ts";
|
|
9
|
+
export { diffScene } from "./diff.ts";
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Chrome } from "../engine/geometry.ts";
|
|
2
|
+
import type { Geom, InputMode, MuxState, PaneId, PaneKind, Viewport } from "../engine/types.ts";
|
|
3
|
+
/** A pane as the frontend needs to place and label it. */
|
|
4
|
+
export type ScenePane = {
|
|
5
|
+
readonly id: PaneId;
|
|
6
|
+
readonly kind: PaneKind;
|
|
7
|
+
readonly title: string;
|
|
8
|
+
readonly geom: Geom;
|
|
9
|
+
readonly focused: boolean;
|
|
10
|
+
readonly exited: boolean;
|
|
11
|
+
};
|
|
12
|
+
/** A tab as the frontend needs to show it in the tab bar. */
|
|
13
|
+
export type SceneTab = {
|
|
14
|
+
readonly id: string;
|
|
15
|
+
readonly name: string;
|
|
16
|
+
};
|
|
17
|
+
/** Everything a frontend needs to lay out a frame (no ANSI, no raw bytes). */
|
|
18
|
+
export type Scene = {
|
|
19
|
+
readonly tabs: readonly SceneTab[];
|
|
20
|
+
readonly activeTab: number;
|
|
21
|
+
readonly mode: InputMode;
|
|
22
|
+
readonly viewport: Viewport;
|
|
23
|
+
readonly focusedPane: PaneId | null;
|
|
24
|
+
readonly panes: readonly ScenePane[];
|
|
25
|
+
/** Effective chrome the server laid panes out with; the frontend draws
|
|
26
|
+
* decorations into the same reserved regions so the two never disagree. */
|
|
27
|
+
readonly chrome: Chrome;
|
|
28
|
+
};
|
|
29
|
+
/** Derive the neutral scene for the active tab from engine state. */
|
|
30
|
+
export declare const deriveScene: (state: MuxState, chrome?: Chrome) => Scene;
|