tmux-ide 2.9.16 → 2.9.18

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.
Files changed (40) hide show
  1. package/bin/cli.js +913 -367
  2. package/package.json +1 -1
  3. package/packages/core/src/terminal-fnv64-wasm.ts +8 -1
  4. package/packages/core/src/terminal-replica-hash-cache.ts +108 -1
  5. package/packages/core/src/terminal-replica.ts +14 -0
  6. package/packages/daemon/dist/command-center/discovery.js +39 -6
  7. package/packages/daemon/dist/doctor.js +101 -2
  8. package/packages/daemon/dist/lib/catalog-fleet-read.js +10 -0
  9. package/packages/daemon/dist/lib/daemon-embed.js +18 -4
  10. package/packages/daemon/dist/lib/daemon-service.js +13 -5
  11. package/packages/daemon/dist/lib/native-split-capability.js +20 -0
  12. package/packages/daemon/dist/lib/tmux-server-owner.js +2 -1
  13. package/packages/daemon/dist/lib/tmux-server-owners.js +20 -1
  14. package/packages/daemon/dist/lib/tui-binary.js +83 -17
  15. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Info.plist +2 -2
  16. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/MacOS/tmux-ide-notifier +0 -0
  17. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Resources/AppIcon.icns +0 -0
  18. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Resources/Assets.car +0 -0
  19. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/_CodeSignature/CodeResources +4 -4
  20. package/packages/daemon/dist/terminal/mirror/control-channel.js +3 -2
  21. package/packages/daemon/dist/terminal/mirror/mirror-service.js +47 -11
  22. package/packages/daemon/dist/terminal/mirror/session-channel.js +19 -4
  23. package/packages/daemon/dist/terminal/mirror/window-split-authority.js +9 -2
  24. package/packages/daemon/dist/terminal/protocol/native-split-resize.js +16 -16
  25. package/packages/daemon/dist/terminal/protocol/stock-split-resize.js +194 -0
  26. package/packages/daemon/src/command-center/discovery.ts +72 -7
  27. package/packages/daemon/src/doctor.ts +104 -2
  28. package/packages/daemon/src/lib/catalog-fleet-read.ts +40 -0
  29. package/packages/daemon/src/lib/daemon-embed.ts +31 -12
  30. package/packages/daemon/src/lib/daemon-service.ts +17 -4
  31. package/packages/daemon/src/lib/native-split-capability.ts +21 -0
  32. package/packages/daemon/src/lib/tmux-server-owner.ts +2 -0
  33. package/packages/daemon/src/lib/tmux-server-owners.ts +17 -1
  34. package/packages/daemon/src/lib/tui-binary.ts +114 -22
  35. package/packages/daemon/src/terminal/mirror/control-channel.ts +3 -2
  36. package/packages/daemon/src/terminal/mirror/mirror-service.ts +51 -12
  37. package/packages/daemon/src/terminal/mirror/session-channel.ts +23 -4
  38. package/packages/daemon/src/terminal/mirror/window-split-authority.ts +15 -4
  39. package/packages/daemon/src/terminal/protocol/native-split-resize.ts +16 -16
  40. package/packages/daemon/src/terminal/protocol/stock-split-resize.ts +268 -0
@@ -0,0 +1,268 @@
1
+ /**
2
+ * Moves one split boundary on a stock tmux server, which has neither
3
+ * `tmux-ide-resize-split` nor the operation journal.
4
+ *
5
+ * `resize-pane` names a pane, and tmux applies it to that pane's nearest
6
+ * same-axis ancestor, so the target is a leaf that reaches the split's parent
7
+ * through opposite-axis splits only. The boundary is clamped to its two
8
+ * neighbours, because tmux takes space from further siblings once they run out.
9
+ * The move is relative, because an absolute -y adds one on the status line's edge.
10
+ *
11
+ * The guard and the resize are one `if-shell -F`: tmux evaluates the format
12
+ * inline and runs the commands it queues before any other client's command, so
13
+ * a change from outside cannot land between the comparison and the resize. Send
14
+ * it on a control connection, where if-shell and each branch command answer in
15
+ * their own block, three in all, the last holding the answer. On tmux 3.8 only control clients read
16
+ * `#{window_layout}` in the format the mirror holds; other clients read JSON.
17
+ */
18
+ import { randomUUID } from "node:crypto";
19
+ import { parseLayoutTree, type LayoutTreeNode } from "./layout-parse.ts";
20
+ import {
21
+ locateSplit,
22
+ preservesSplitTree,
23
+ splitEdge,
24
+ validSplitTree,
25
+ type NativeSplitResizeRequest,
26
+ type NativeSplitResizeResult,
27
+ } from "./native-split-resize.ts";
28
+
29
+ type Axis = "cols" | "rows";
30
+ const span = (node: LayoutTreeNode, axis: Axis) => (axis === "cols" ? node.width : node.height);
31
+ const start = (node: LayoutTreeNode, axis: Axis) => (axis === "cols" ? node.left : node.top);
32
+
33
+ /** The window options that decide how small tmux lets a pane get. */
34
+ export interface StockSplitSizes {
35
+ readonly borderStatus: string;
36
+ /** Columns a pane scrollbar takes, or 0 with scrollbars off. */
37
+ readonly scrollbar: number;
38
+ }
39
+ /** tmux's own minimum (`layout_resize_check`): a leaf holds one cell, plus the pane
40
+ * status line on the window's top or bottom edge, and any scrollbar. */
41
+ function minimum(
42
+ node: LayoutTreeNode,
43
+ axis: Axis,
44
+ sizes: StockSplitSizes,
45
+ root: LayoutTreeNode,
46
+ ): number {
47
+ if (node.kind === "leaf") {
48
+ if (axis === "cols") return 1 + sizes.scrollbar;
49
+ const onEdge =
50
+ (sizes.borderStatus === "top" && node.top === root.top) ||
51
+ (sizes.borderStatus === "bottom" && node.top + node.height === root.top + root.height);
52
+ return onEdge ? 2 : 1;
53
+ }
54
+ const mins = node.children.map((child) => minimum(child, axis, sizes, root));
55
+ return node.axis === axis
56
+ ? mins.reduce((sum, size) => sum + size, node.children.length - 1)
57
+ : Math.max(...mins);
58
+ }
59
+ /** A pane whose nearest ancestor on `axis` is the cell directly above `node`. */
60
+ function reachingLeaf(node: LayoutTreeNode, axis: Axis): string | null {
61
+ if (node.kind === "leaf") return node.id;
62
+ if (node.axis === axis) return null;
63
+ for (const child of node.children) {
64
+ const leaf = reachingLeaf(child, axis);
65
+ if (leaf) return leaf;
66
+ }
67
+ return null;
68
+ }
69
+ /** The pane to name in `resize-pane` for the boundary after the cell at `path`.
70
+ * Null means tmux has no way to address that boundary. In its last cell a pane
71
+ * acts on the boundary before it, so either side's pane moves it the same way. */
72
+ export function stockSplitTarget(
73
+ tree: LayoutTreeNode,
74
+ path: readonly number[],
75
+ axis: Axis,
76
+ ): string | null {
77
+ const located = locateSplit(tree, path);
78
+ if (!located || located.parent.axis !== axis) return null;
79
+ const leading = reachingLeaf(located.node, axis);
80
+ if (leading) return leading;
81
+ const index = path.at(-1)!;
82
+ if (index + 1 !== located.parent.children.length - 1) return null;
83
+ return reachingLeaf(located.parent.children[index + 1]!, axis);
84
+ }
85
+ /** Where the boundary may go without tmux taking space from further siblings. */
86
+ export function stockSplitRange(
87
+ tree: LayoutTreeNode,
88
+ path: readonly number[],
89
+ axis: Axis,
90
+ sizes: StockSplitSizes,
91
+ ): { readonly low: number; readonly high: number } | null {
92
+ const located = locateSplit(tree, path);
93
+ if (!located) return null;
94
+ const after = located.parent.children[path.at(-1)! + 1]!;
95
+ const low = start(located.node, axis) + minimum(located.node, axis, sizes, tree);
96
+ const high = start(after, axis) + span(after, axis) - minimum(after, axis, sizes, tree) - 1;
97
+ return { low, high };
98
+ }
99
+ /** Whether a handle on this boundary could move it at all. */
100
+ export function stockSplitMovable(
101
+ tree: LayoutTreeNode,
102
+ path: readonly number[],
103
+ axis: Axis,
104
+ sizes: StockSplitSizes,
105
+ ): boolean {
106
+ const range = stockSplitRange(tree, path, axis, sizes);
107
+ return stockSplitTarget(tree, path, axis) !== null && range !== null && range.high > range.low;
108
+ }
109
+ export interface StockSplitPlan {
110
+ readonly paneId: string;
111
+ readonly boundary: number;
112
+ /** How far the boundary moves; negative is left or up. */
113
+ readonly delta: number;
114
+ }
115
+ export function planStockSplitResize(
116
+ tree: LayoutTreeNode,
117
+ path: readonly number[],
118
+ axis: Axis,
119
+ boundary: number,
120
+ sizes: StockSplitSizes,
121
+ ): StockSplitPlan | null {
122
+ const paneId = stockSplitTarget(tree, path, axis);
123
+ const range = stockSplitRange(tree, path, axis, sizes);
124
+ const located = locateSplit(tree, path);
125
+ if (!paneId || !range || !located || range.high < range.low) return null;
126
+ const clamped = Math.min(range.high, Math.max(range.low, boundary));
127
+ return { paneId, boundary: clamped, delta: clamped - splitEdge(located.node, axis) };
128
+ }
129
+ /** Escapes a layout for the right side of a format comparison. */
130
+ function formatLiteral(layout: string): string {
131
+ return layout.replaceAll(",", "#,").replaceAll("}", "#}");
132
+ }
133
+ function leafCount(node: LayoutTreeNode): number {
134
+ return node.kind === "leaf" ? 1 : node.children.reduce((sum, child) => sum + leafCount(child), 0);
135
+ }
136
+ export function stockSplitCommand(
137
+ request: NativeSplitResizeRequest,
138
+ sessionCreated: string,
139
+ plan: StockSplitPlan,
140
+ panes: number,
141
+ options: StockSplitOptions,
142
+ marker: string,
143
+ ): string {
144
+ const equal = (name: string, value: string | number) => `#{==:#{${name}},${value}}`;
145
+ const condition = [
146
+ equal("window_id", request.windowId),
147
+ equal("session_id", request.sessionId),
148
+ equal("session_created", sessionCreated),
149
+ equal("window_zoomed_flag", 0),
150
+ // Control clients' layouts leave out floating panes; this count includes them.
151
+ equal("window_panes", panes),
152
+ equal("pane-border-status", options.borderStatus),
153
+ equal("pane-scrollbars", options.scrollbars),
154
+ equal("window_layout", formatLiteral(request.expectedLayout)),
155
+ ].reduceRight((rest, next) => `#{&&:${next},${rest}}`);
156
+ // Relative, because tmux adds one to an absolute -y on the status line's edge.
157
+ const flag =
158
+ request.axis === "cols" ? (plan.delta > 0 ? "-R" : "-L") : plan.delta > 0 ? "-D" : "-U";
159
+ const move =
160
+ plan.delta === 0
161
+ ? `display-message -p ""`
162
+ : `resize-pane -t ${plan.paneId} ${flag} ${Math.abs(plan.delta)}`;
163
+ const readback = `display-message -p -t ${request.windowId} "${marker} #{window_layout}"`;
164
+ return `if-shell -F -t '${request.sessionId}:${request.windowId}' '${condition}' '${move} ; ${readback}' 'display-message -p ${marker} ; display-message -p ${marker}'`;
165
+ }
166
+ export type StockSplitResizeResult =
167
+ | NativeSplitResizeResult
168
+ | { status: "refused"; reason: "stale-layout" };
169
+
170
+ /** The format that reads a window's sizing options, and its parser. */
171
+ export const STOCK_SPLIT_OPTIONS_FORMAT =
172
+ "#{pane-border-status}\t#{pane-scrollbars}\t#{pane-scrollbars-style}";
173
+ export interface StockSplitOptions extends StockSplitSizes {
174
+ readonly scrollbars: string;
175
+ }
176
+ export function parseStockSplitOptions(line: string | undefined): StockSplitOptions | null {
177
+ const [borderStatus, scrollbars, style] = (line ?? "").split("\t");
178
+ if (!/^(off|top|bottom)$/u.test(borderStatus ?? "") || !/^[a-z]+$/u.test(scrollbars ?? ""))
179
+ return null;
180
+ const field = (name: string) =>
181
+ Number(new RegExp(`\\b${name}=(\\d+)`, "u").exec(style ?? "")?.[1] ?? 0);
182
+ return {
183
+ borderStatus: borderStatus!,
184
+ scrollbars: scrollbars!,
185
+ scrollbar: scrollbars === "off" ? 0 : Math.max(field("width"), 1) + field("pad"),
186
+ };
187
+ }
188
+ export interface StockSplitIo {
189
+ readonly readOptions: (windowId: string) => Promise<string[]>;
190
+ readonly send: (command: string) => Promise<string[]>;
191
+ }
192
+
193
+ /** Authorizes, then sends the command without awaiting in between. A sent command
194
+ * that does not answer as expected is uncertain: the caller must not retry it. */
195
+ export async function resizeStockSplit(
196
+ request: NativeSplitResizeRequest,
197
+ sessionCreated: string,
198
+ authorizeBeforeEffect: () => void,
199
+ io: StockSplitIo,
200
+ ): Promise<StockSplitResizeResult> {
201
+ const before = parseLayoutTree(request.expectedLayout);
202
+ if (
203
+ !before ||
204
+ !validSplitTree(before) ||
205
+ !/^\$\d+$/u.test(request.sessionId) ||
206
+ !/^@\d+$/u.test(request.windowId) ||
207
+ !/^[1-9]\d*$/u.test(sessionCreated)
208
+ )
209
+ return { status: "refused", reason: "invalid-request" };
210
+ let options: StockSplitOptions | null;
211
+ try {
212
+ options = parseStockSplitOptions((await io.readOptions(request.windowId))[0]);
213
+ } catch {
214
+ return { status: "refused", reason: "unsupported" };
215
+ }
216
+ if (!options) return { status: "refused", reason: "unsupported" };
217
+ const plan = planStockSplitResize(before, request.path, request.axis, request.boundary, options);
218
+ if (!plan) return { status: "refused", reason: "invalid-request" };
219
+ const marker = `tmux-ide-split-${randomUUID()}`;
220
+ const command = stockSplitCommand(
221
+ request,
222
+ sessionCreated,
223
+ plan,
224
+ leafCount(before),
225
+ options,
226
+ marker,
227
+ );
228
+ authorizeBeforeEffect();
229
+ let reply: string[];
230
+ try {
231
+ reply = await io.send(command);
232
+ } catch {
233
+ return { status: "uncertain", reason: "command-failed" };
234
+ }
235
+ const answer = reply.join("\n");
236
+ if (answer === marker) return { status: "refused", reason: "stale-layout" };
237
+ if (reply.length !== 1 || !answer.startsWith(`${marker} `))
238
+ return { status: "uncertain", reason: "invalid-receipt" };
239
+ const layout = answer.slice(marker.length + 1);
240
+ const after = parseLayoutTree(layout);
241
+ const original = locateSplit(before, request.path)!;
242
+ const actual = after && locateSplit(after, request.path);
243
+ if (
244
+ !after ||
245
+ !validSplitTree(after) ||
246
+ !actual ||
247
+ !preservesSplitTree(before, after, original.parent, request.axis)
248
+ )
249
+ return { status: "uncertain", reason: "invalid-receipt" };
250
+ // Only this boundary may have moved: its parent's other children keep their edges.
251
+ const edges = (parent: typeof original.parent) =>
252
+ parent.children.map((child) => splitEdge(child, request.axis));
253
+ const moved = request.path.at(-1)!;
254
+ const others = edges(actual.parent);
255
+ if (edges(original.parent).some((edge, index) => index !== moved && edge !== others[index]))
256
+ return { status: "uncertain", reason: "invalid-receipt" };
257
+ const boundary = splitEdge(actual.node, request.axis);
258
+ const previous = splitEdge(original.node, request.axis);
259
+ if (boundary < Math.min(previous, plan.boundary) || boundary > Math.max(previous, plan.boundary))
260
+ return { status: "uncertain", reason: "invalid-receipt" };
261
+ return {
262
+ status: "applied",
263
+ boundary,
264
+ layout,
265
+ tree: after,
266
+ changed: layout !== request.expectedLayout,
267
+ };
268
+ }