@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
package/LICENSE
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright 2023-present Eser Ozvataf and other contributors.
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# 🪟 [@eserstack/mux](./)
|
|
2
|
+
|
|
3
|
+
> **Note:** JSR is frozen at 4.5.x. From 5.0.0, `@eserstack/mux` is published on
|
|
4
|
+
> npm under the same name (`pnpm add @eserstack/mux`); the JSR versions stay
|
|
5
|
+
> available but get no updates.
|
|
6
|
+
|
|
7
|
+
`@eserstack/mux` is the core of a terminal multiplexer: tabs, split panes,
|
|
8
|
+
keybinding modes and the processes running in them. A pure engine (split tree,
|
|
9
|
+
geometry, reducer) holds the state, a server owns the pseudo-terminals and turns
|
|
10
|
+
reducer effects into I/O, and frontends draw what the server sends. The server
|
|
11
|
+
speaks one neutral protocol over any transport, so the same session can drive
|
|
12
|
+
the ANSI terminal frontend in this package or a browser frontend over a
|
|
13
|
+
WebSocket.
|
|
14
|
+
|
|
15
|
+
Install it with `pnpm add @eserstack/mux`.
|
|
16
|
+
|
|
17
|
+
## Quick Start
|
|
18
|
+
|
|
19
|
+
Run the server and the terminal frontend in one process, with your shell in the
|
|
20
|
+
first pane:
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import * as mux from "@eserstack/mux";
|
|
24
|
+
|
|
25
|
+
const exitCode = await mux.runInProcess();
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Run it in a real terminal. Press `ctrl+p` then `v` or `s` to split, `h`, `j`,
|
|
29
|
+
`k`, `l` to move focus and `x` to close a pane; `ctrl+t` then `n` for a new tab
|
|
30
|
+
and `h` or `l` to switch tabs; `ctrl+q` to quit. A pane gets a real
|
|
31
|
+
pseudo-terminal when the `@eserstack/ajan` native library is available and falls
|
|
32
|
+
back to the `script` command on macOS and Linux otherwise.
|
|
33
|
+
|
|
34
|
+
`runInProcess` accepts `shell`, `shellArgs` and `cwd` for terminal panes, or a
|
|
35
|
+
`resolveSpawn` function that decides what every pane runs.
|
|
36
|
+
|
|
37
|
+
## Composing the Pieces
|
|
38
|
+
|
|
39
|
+
`runInProcess` wires the parts below together; wire them yourself to change the
|
|
40
|
+
keymap, the chrome or the transport.
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import * as mux from "@eserstack/mux";
|
|
44
|
+
|
|
45
|
+
const server = mux.createServer({
|
|
46
|
+
initialState: mux.engine.createInitialState({ cols: 120, rows: 40 }),
|
|
47
|
+
resolveSpawn: () => ({ command: "/bin/sh", args: [] }),
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const [serverSide, frontendSide] = mux.createInProcessPair<
|
|
51
|
+
mux.ServerToFrontend,
|
|
52
|
+
mux.FrontendToServer
|
|
53
|
+
>();
|
|
54
|
+
server.connect(serverSide);
|
|
55
|
+
|
|
56
|
+
const keymap = mux.keybindings.mergeOverrides(mux.keybindings.defaultKeymap, {
|
|
57
|
+
normal: { "ctrl+b": { type: "switchMode", mode: "pane" } },
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const frontend = mux.createTuiFrontend({ transport: frontendSide, keymap });
|
|
61
|
+
server.dispatch({ type: "newTab" });
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
await frontend.run();
|
|
65
|
+
} finally {
|
|
66
|
+
frontend.stop();
|
|
67
|
+
await server.dispose();
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
| Part | Role |
|
|
72
|
+
| --------------------- | ------------------------------------------------------------------ |
|
|
73
|
+
| `createServer` | Owns engine state and the pane processes, broadcasts scene updates |
|
|
74
|
+
| `createTuiFrontend` | Draws the scene in the terminal and turns keys into actions |
|
|
75
|
+
| `createInProcessPair` | Two connected transports for a server and frontend in one process |
|
|
76
|
+
| `wsTransport` | A transport over a `WebSocket`, with JSON messages |
|
|
77
|
+
| `createPtyHost` | The default pseudo-terminal backend the server uses |
|
|
78
|
+
| `createScheduler` | Coalesces render requests into frames |
|
|
79
|
+
|
|
80
|
+
`createServer` options include `chrome` (rows and columns reserved for tab bar,
|
|
81
|
+
status bar and sidebars), `exitOnEmpty` (default `true`; set `false` for a
|
|
82
|
+
long-lived host that keeps an empty session when the last tab closes),
|
|
83
|
+
`onPaneExit`, and `createHost` to replace the pseudo-terminal backend in tests.
|
|
84
|
+
`server.dispatch(action)` drives the session from code, for example from an
|
|
85
|
+
agent.
|
|
86
|
+
|
|
87
|
+
## Remote Frontends
|
|
88
|
+
|
|
89
|
+
A message from a remote client can carry an action, and `newTab` and `newPane`
|
|
90
|
+
actions can name a command, arguments and a working directory. Wrap every
|
|
91
|
+
transport that crosses a network in `engine.sanitizeRemoteTransport`, which
|
|
92
|
+
strips those fields so the server's `resolveSpawn` alone decides what runs:
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
import * as mux from "@eserstack/mux";
|
|
96
|
+
|
|
97
|
+
/** Attach a WebSocket whose client your server has already authenticated. */
|
|
98
|
+
export const attachRemote = (
|
|
99
|
+
server: mux.MuxServer,
|
|
100
|
+
socket: WebSocket,
|
|
101
|
+
): void => {
|
|
102
|
+
const transport = mux.engine.sanitizeRemoteTransport(
|
|
103
|
+
mux.wsTransport<mux.ServerToFrontend, mux.FrontendToServer>(socket, {
|
|
104
|
+
onClose: () => server.disconnect(transport),
|
|
105
|
+
}),
|
|
106
|
+
);
|
|
107
|
+
server.connect(transport);
|
|
108
|
+
};
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Sanitizing is a second line of defence, not authentication: anyone who can open
|
|
112
|
+
the socket can type into your shells. The server serves one frontend at a time;
|
|
113
|
+
connecting a new transport closes the previous one, and the panes keep running
|
|
114
|
+
across reconnects.
|
|
115
|
+
|
|
116
|
+
## Keybindings
|
|
117
|
+
|
|
118
|
+
Keys are read per mode: `normal`, `pane`, `tab`, `resize`, `scroll` and
|
|
119
|
+
`locked`. In `normal` mode the reserved chords switch modes (`ctrl+p` pane,
|
|
120
|
+
`ctrl+t` tab, `ctrl+n` resize, `ctrl+s` scroll, `ctrl+g` locked) and every other
|
|
121
|
+
key goes to the focused pane. In `locked` mode only `ctrl+g` is intercepted, so
|
|
122
|
+
full-screen programs receive every key.
|
|
123
|
+
|
|
124
|
+
`keybindings.buildKeymap(table)` compiles a table of chords to actions,
|
|
125
|
+
`mergeOverrides(base, table)` layers your chords over a keymap, and
|
|
126
|
+
`lookup(keymap, mode, chord)` returns the actions bound to a chord.
|
|
127
|
+
|
|
128
|
+
## Engine
|
|
129
|
+
|
|
130
|
+
The engine is pure and has no I/O. `reduce(state, action)` returns the next
|
|
131
|
+
state and a list of effects (`spawnPty`, `writePty`, `killPty`, `scrollPane`,
|
|
132
|
+
`render`, `detach`, `exit`) for the server to carry out:
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
import * as mux from "@eserstack/mux";
|
|
136
|
+
|
|
137
|
+
const initial = mux.engine.createInitialState({ cols: 80, rows: 24 });
|
|
138
|
+
const { state, effects } = mux.engine.reduce(initial, { type: "newTab" });
|
|
139
|
+
|
|
140
|
+
console.log(state.tabs.length); // 1
|
|
141
|
+
console.log(effects.map((effect) => effect.type)); // includes "spawnPty"
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
`engine.tree` and `engine.geometry` hold the split-tree operations and the pane
|
|
145
|
+
layout math, and `scene.deriveScene(state)` and `scene.diffScene(prev, next)`
|
|
146
|
+
build the frontend-neutral view and the delta the server sends.
|
|
147
|
+
|
|
148
|
+
## 🔗 Links
|
|
149
|
+
|
|
150
|
+
- [npm package](https://www.npmjs.com/package/@eserstack/mux)
|
|
151
|
+
- [eser/stack on GitHub](https://github.com/eser/stack)
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Effect descriptors emitted by the reducer.
|
|
3
|
+
*
|
|
4
|
+
* The reducer stays pure: instead of performing I/O it returns a list of
|
|
5
|
+
* effects that the (impure) server interprets — spawning/killing PTYs, writing
|
|
6
|
+
* input, requesting a render, or exiting. Pane resizing is NOT an effect: the
|
|
7
|
+
* server recomputes geometry after each reduce and resizes any pane whose rect
|
|
8
|
+
* changed, so layout changes need no explicit effect.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
import type { PaneId, ScrollAction } from "./types.ts";
|
|
13
|
+
export type Effect =
|
|
14
|
+
/** Spawn the process for a newly-created pane (server reads the descriptor). */
|
|
15
|
+
{
|
|
16
|
+
readonly type: "spawnPty";
|
|
17
|
+
readonly pane: PaneId;
|
|
18
|
+
}
|
|
19
|
+
/** Write raw bytes to a pane's process (focused-terminal passthrough). */
|
|
20
|
+
| {
|
|
21
|
+
readonly type: "writePty";
|
|
22
|
+
readonly pane: PaneId;
|
|
23
|
+
readonly data: string;
|
|
24
|
+
}
|
|
25
|
+
/** Kill a pane's process (pane/tab closed). */
|
|
26
|
+
| {
|
|
27
|
+
readonly type: "killPty";
|
|
28
|
+
readonly pane: PaneId;
|
|
29
|
+
}
|
|
30
|
+
/** Scroll a pane's scrollback viewport. */
|
|
31
|
+
| {
|
|
32
|
+
readonly type: "scrollPane";
|
|
33
|
+
readonly pane: PaneId;
|
|
34
|
+
readonly action: ScrollAction;
|
|
35
|
+
}
|
|
36
|
+
/** Ask the scheduler to composite and broadcast a frame. */
|
|
37
|
+
| {
|
|
38
|
+
readonly type: "render";
|
|
39
|
+
}
|
|
40
|
+
/** Detach the current client(s) but keep the session alive. */
|
|
41
|
+
| {
|
|
42
|
+
readonly type: "detach";
|
|
43
|
+
}
|
|
44
|
+
/** Tear down the session and exit. */
|
|
45
|
+
| {
|
|
46
|
+
readonly type: "exit";
|
|
47
|
+
};
|
|
48
|
+
/** Result of a single reduce step: the next state plus effects to run. */
|
|
49
|
+
export type ReducerResult = {
|
|
50
|
+
readonly state: import("./types.ts").MuxState;
|
|
51
|
+
readonly effects: readonly Effect[];
|
|
52
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Geom, PaneId, Tab, Viewport } from "./types.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Screen regions reserved around the pane area.
|
|
4
|
+
*
|
|
5
|
+
* `tabBarRows`/`statusBarRows` are the full-width top/bottom strips. The optional
|
|
6
|
+
* `leftCols`/`rightCols`/`topRows`/`bottomRows` reserve space for consumer
|
|
7
|
+
* "decorations" (e.g. a spec-list sidebar or a status monitor); panes lay out
|
|
8
|
+
* inside what's left. The same Chrome must be given to the server (for geometry)
|
|
9
|
+
* and the frontend (for drawing the decorations) so they agree.
|
|
10
|
+
*/
|
|
11
|
+
export type Chrome = {
|
|
12
|
+
readonly tabBarRows: number;
|
|
13
|
+
readonly statusBarRows: number;
|
|
14
|
+
readonly leftCols?: number;
|
|
15
|
+
readonly rightCols?: number;
|
|
16
|
+
readonly topRows?: number;
|
|
17
|
+
readonly bottomRows?: number;
|
|
18
|
+
};
|
|
19
|
+
/** One row for the tab bar, one for the status bar — used by the reducer and compositor alike. */
|
|
20
|
+
export declare const DEFAULT_CHROME: Chrome;
|
|
21
|
+
/**
|
|
22
|
+
* Drop decoration reservations that would starve the pane area, so a small
|
|
23
|
+
* terminal still shows panes instead of going blank. The server lays out with
|
|
24
|
+
* this (and broadcasts it in the scene) so frontends draw decorations to match.
|
|
25
|
+
*/
|
|
26
|
+
export declare const effectiveChrome: (viewport: Viewport, chrome: Chrome) => Chrome;
|
|
27
|
+
/** A 1-based screen rectangle (ready to pass to box/ansi drawing helpers). */
|
|
28
|
+
export type Region = {
|
|
29
|
+
readonly x: number;
|
|
30
|
+
readonly y: number;
|
|
31
|
+
readonly width: number;
|
|
32
|
+
readonly height: number;
|
|
33
|
+
};
|
|
34
|
+
/** Reserved decoration regions for a viewport, in 1-based screen coordinates. */
|
|
35
|
+
export type DecorationRegions = {
|
|
36
|
+
readonly left?: Region;
|
|
37
|
+
readonly right?: Region;
|
|
38
|
+
readonly top?: Region;
|
|
39
|
+
readonly bottom?: Region;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Compute the 1-based decoration rectangles implied by a Chrome. The left/right
|
|
43
|
+
* sidebars span the full content band; the top/bottom strips sit between the
|
|
44
|
+
* sidebars (right of left, left of right).
|
|
45
|
+
*/
|
|
46
|
+
export declare const decorationRegions: (viewport: Viewport, chrome: Chrome) => DecorationRegions;
|
|
47
|
+
/**
|
|
48
|
+
* Compute absolute geometry for every pane in a tab.
|
|
49
|
+
*
|
|
50
|
+
* When a pane is fullscreen, only it is returned, filling the whole content area.
|
|
51
|
+
*/
|
|
52
|
+
export declare const computeTabGeoms: (tab: Tab, viewport: Viewport, chrome: Chrome) => Map<PaneId, Geom>;
|
|
53
|
+
/**
|
|
54
|
+
* Pick the pane spatially adjacent to `from` in `direction`, using computed
|
|
55
|
+
* geoms. Returns null when there is no neighbour that way.
|
|
56
|
+
*/
|
|
57
|
+
export declare const neighborInDirection: (geoms: Map<PaneId, Geom>, from: PaneId, direction: "up" | "down" | "left" | "right") => PaneId | null;
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* Maps the binary split tree onto the existing flex-layout engine.
|
|
4
|
+
*
|
|
5
|
+
* Rather than write a second layout solver, each {@link SplitNode} is converted
|
|
6
|
+
* to a `FlexNode` and handed to `flexLayout.computeLayout`. Split ratios become
|
|
7
|
+
* flex grow weights. Coordinates are 0-based; the y-axis is offset by the tab
|
|
8
|
+
* bar, and the compositor adds per-pane border insets on top.
|
|
9
|
+
*
|
|
10
|
+
* Direction mapping (see {@link SplitDirection}): `"vertical"` → flex `"row"`
|
|
11
|
+
* (side by side), `"horizontal"` → flex `"column"` (stacked).
|
|
12
|
+
*
|
|
13
|
+
* @module
|
|
14
|
+
*/
|
|
15
|
+
import * as tui from "@eserstack/shell/tui";
|
|
16
|
+
/** One row for the tab bar, one for the status bar — used by the reducer and compositor alike. */
|
|
17
|
+
export const DEFAULT_CHROME = { tabBarRows: 1, statusBarRows: 1 };
|
|
18
|
+
const MIN_PANE_COLS = 10;
|
|
19
|
+
const MIN_PANE_ROWS = 3;
|
|
20
|
+
/**
|
|
21
|
+
* Drop decoration reservations that would starve the pane area, so a small
|
|
22
|
+
* terminal still shows panes instead of going blank. The server lays out with
|
|
23
|
+
* this (and broadcasts it in the scene) so frontends draw decorations to match.
|
|
24
|
+
*/
|
|
25
|
+
export const effectiveChrome = (viewport, chrome) => {
|
|
26
|
+
let leftCols = chrome.leftCols ?? 0;
|
|
27
|
+
let rightCols = chrome.rightCols ?? 0;
|
|
28
|
+
let topRows = chrome.topRows ?? 0;
|
|
29
|
+
let bottomRows = chrome.bottomRows ?? 0;
|
|
30
|
+
if (viewport.cols - leftCols - rightCols < MIN_PANE_COLS) {
|
|
31
|
+
leftCols = 0;
|
|
32
|
+
rightCols = 0;
|
|
33
|
+
}
|
|
34
|
+
const verticalChrome = chrome.tabBarRows + chrome.statusBarRows + topRows + bottomRows;
|
|
35
|
+
if (viewport.rows - verticalChrome < MIN_PANE_ROWS) {
|
|
36
|
+
topRows = 0;
|
|
37
|
+
bottomRows = 0;
|
|
38
|
+
}
|
|
39
|
+
return { ...chrome, leftCols, rightCols, topRows, bottomRows };
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Compute the 1-based decoration rectangles implied by a Chrome. The left/right
|
|
43
|
+
* sidebars span the full content band; the top/bottom strips sit between the
|
|
44
|
+
* sidebars (right of left, left of right).
|
|
45
|
+
*/
|
|
46
|
+
export const decorationRegions = (viewport, chrome) => {
|
|
47
|
+
const left = chrome.leftCols ?? 0;
|
|
48
|
+
const right = chrome.rightCols ?? 0;
|
|
49
|
+
const top = chrome.topRows ?? 0;
|
|
50
|
+
const bottom = chrome.bottomRows ?? 0;
|
|
51
|
+
const bandY = chrome.tabBarRows + 1; // first content row, 1-based
|
|
52
|
+
const bandH = Math.max(0, viewport.rows - chrome.tabBarRows - chrome.statusBarRows);
|
|
53
|
+
const innerX = left + 1; // first column right of the left sidebar
|
|
54
|
+
const innerW = Math.max(0, viewport.cols - left - right);
|
|
55
|
+
const regions = {};
|
|
56
|
+
if (left > 0)
|
|
57
|
+
regions.left = { x: 1, y: bandY, width: left, height: bandH };
|
|
58
|
+
if (right > 0) {
|
|
59
|
+
regions.right = {
|
|
60
|
+
x: viewport.cols - right + 1,
|
|
61
|
+
y: bandY,
|
|
62
|
+
width: right,
|
|
63
|
+
height: bandH,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
if (top > 0) {
|
|
67
|
+
regions.top = { x: innerX, y: bandY, width: innerW, height: top };
|
|
68
|
+
}
|
|
69
|
+
if (bottom > 0) {
|
|
70
|
+
regions.bottom = {
|
|
71
|
+
x: innerX,
|
|
72
|
+
y: viewport.rows - chrome.statusBarRows - bottom + 1,
|
|
73
|
+
width: innerW,
|
|
74
|
+
height: bottom,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return regions;
|
|
78
|
+
};
|
|
79
|
+
const RATIO_SCALE = 100;
|
|
80
|
+
const splitToFlex = (node) => {
|
|
81
|
+
if (node.kind === "leaf") {
|
|
82
|
+
return { id: node.pane, size: { type: "flex", grow: 1 } };
|
|
83
|
+
}
|
|
84
|
+
const direction = node.direction === "vertical" ? "row" : "column";
|
|
85
|
+
const firstGrow = Math.max(1, Math.round(node.ratio * RATIO_SCALE));
|
|
86
|
+
const secondGrow = Math.max(1, Math.round((1 - node.ratio) * RATIO_SCALE));
|
|
87
|
+
return {
|
|
88
|
+
direction,
|
|
89
|
+
size: { type: "flex", grow: 1 },
|
|
90
|
+
children: [
|
|
91
|
+
{ ...splitToFlex(node.first), size: { type: "flex", grow: firstGrow } },
|
|
92
|
+
{ ...splitToFlex(node.second), size: { type: "flex", grow: secondGrow } },
|
|
93
|
+
],
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Compute absolute geometry for every pane in a tab.
|
|
98
|
+
*
|
|
99
|
+
* When a pane is fullscreen, only it is returned, filling the whole content area.
|
|
100
|
+
*/
|
|
101
|
+
export const computeTabGeoms = (tab, viewport, chrome) => {
|
|
102
|
+
const left = chrome.leftCols ?? 0;
|
|
103
|
+
const right = chrome.rightCols ?? 0;
|
|
104
|
+
const top = chrome.topRows ?? 0;
|
|
105
|
+
const bottom = chrome.bottomRows ?? 0;
|
|
106
|
+
const xOffset = left;
|
|
107
|
+
const yOffset = chrome.tabBarRows + top;
|
|
108
|
+
const cols = Math.max(0, viewport.cols - left - right);
|
|
109
|
+
const contentRows = Math.max(0, viewport.rows - chrome.tabBarRows - chrome.statusBarRows - top - bottom);
|
|
110
|
+
const result = new Map();
|
|
111
|
+
if (tab.fullscreenPane !== undefined) {
|
|
112
|
+
result.set(tab.fullscreenPane, {
|
|
113
|
+
x: xOffset,
|
|
114
|
+
y: yOffset,
|
|
115
|
+
width: cols,
|
|
116
|
+
height: contentRows,
|
|
117
|
+
});
|
|
118
|
+
return result;
|
|
119
|
+
}
|
|
120
|
+
const panels = tui.flexLayout.computeLayout(splitToFlex(tab.root), cols, contentRows);
|
|
121
|
+
for (const p of panels) {
|
|
122
|
+
if (p.id === undefined)
|
|
123
|
+
continue;
|
|
124
|
+
result.set(p.id, {
|
|
125
|
+
x: p.x + xOffset,
|
|
126
|
+
y: p.y + yOffset,
|
|
127
|
+
width: p.width,
|
|
128
|
+
height: p.height,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
return result;
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* Pick the pane spatially adjacent to `from` in `direction`, using computed
|
|
135
|
+
* geoms. Returns null when there is no neighbour that way.
|
|
136
|
+
*/
|
|
137
|
+
export const neighborInDirection = (geoms, from, direction) => {
|
|
138
|
+
const self = geoms.get(from);
|
|
139
|
+
if (self === undefined)
|
|
140
|
+
return null;
|
|
141
|
+
const horizontal = direction === "left" || direction === "right";
|
|
142
|
+
const selfCx = self.x + self.width / 2;
|
|
143
|
+
const selfCy = self.y + self.height / 2;
|
|
144
|
+
let best = null;
|
|
145
|
+
let bestDist = Infinity;
|
|
146
|
+
for (const [id, g] of geoms) {
|
|
147
|
+
if (id === from)
|
|
148
|
+
continue;
|
|
149
|
+
// Require overlap on the perpendicular axis so focus never jumps diagonally
|
|
150
|
+
// to a non-adjacent pane.
|
|
151
|
+
const overlaps = horizontal
|
|
152
|
+
? g.y < self.y + self.height && g.y + g.height > self.y
|
|
153
|
+
: g.x < self.x + self.width && g.x + g.width > self.x;
|
|
154
|
+
if (!overlaps)
|
|
155
|
+
continue;
|
|
156
|
+
const center = horizontal ? g.x + g.width / 2 : g.y + g.height / 2;
|
|
157
|
+
const selfCenter = horizontal ? selfCx : selfCy;
|
|
158
|
+
const onSide = direction === "left" || direction === "up"
|
|
159
|
+
? center < selfCenter
|
|
160
|
+
: center > selfCenter;
|
|
161
|
+
if (!onSide)
|
|
162
|
+
continue;
|
|
163
|
+
const dist = Math.abs(center - selfCenter);
|
|
164
|
+
if (dist < bestDist) {
|
|
165
|
+
bestDist = dist;
|
|
166
|
+
best = id;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return best;
|
|
170
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure multiplexer engine: data model, split-tree ops, geometry, and the
|
|
3
|
+
* action reducer. No I/O — the server interprets the emitted effects.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
export type { Action, Direction, Geom, InputMode, MuxState, Pane, PaneId, PaneKind, ScrollAction, SplitDirection, SplitNode, Tab, TabId, Viewport, } from "./types.ts";
|
|
8
|
+
export type { Effect, ReducerResult } from "./effects.ts";
|
|
9
|
+
export * as tree from "./split-tree.ts";
|
|
10
|
+
export * as geometry from "./geometry.ts";
|
|
11
|
+
export { createInitialState, reduce } from "./reducer.ts";
|
|
12
|
+
export { sanitizeRemoteAction, sanitizeRemoteMessage, sanitizeRemoteTransport, } from "./sanitize.ts";
|
|
13
|
+
export type { InboundTransport } from "./sanitize.ts";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
export * as tree from "./split-tree.js";
|
|
3
|
+
export * as geometry from "./geometry.js";
|
|
4
|
+
export { createInitialState, reduce } from "./reducer.js";
|
|
5
|
+
export { sanitizeRemoteAction, sanitizeRemoteMessage, sanitizeRemoteTransport, } from "./sanitize.js";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pure engine reducer: `(state, action) -> { state, effects }`.
|
|
3
|
+
*
|
|
4
|
+
* It is the single place that interprets actions. It performs no I/O; side
|
|
5
|
+
* effects (spawn/write/kill PTY, render, exit) are returned as descriptors for
|
|
6
|
+
* the server to run. Pane resizing on layout change is handled by the server
|
|
7
|
+
* diffing geometry, so it is not emitted here.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
import type { ReducerResult } from "./effects.ts";
|
|
12
|
+
import type { Action, MuxState, Viewport } from "./types.ts";
|
|
13
|
+
/** Create an empty engine state; dispatch `newTab` to open the first tab. */
|
|
14
|
+
export declare const createInitialState: (viewport: Viewport) => MuxState;
|
|
15
|
+
export declare const reduce: (state: MuxState, action: Action) => ReducerResult;
|