@termwire/cli 0.6.0 → 0.7.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/CHANGELOG.md +12 -0
- package/README.md +59 -4
- package/dist/click.d.ts +52 -0
- package/dist/click.js +138 -0
- package/dist/config-schema.d.ts +7 -0
- package/dist/config-schema.js +19 -0
- package/dist/config-validation.d.ts +2 -0
- package/dist/config-validation.js +14 -0
- package/dist/config-writer.d.ts +4 -2
- package/dist/config-writer.js +15 -3
- package/dist/identity.js +4 -1
- package/dist/install-prompt.js +7 -0
- package/dist/install.d.ts +6 -0
- package/dist/install.js +101 -25
- package/dist/mouse-binding.d.ts +60 -0
- package/dist/mouse-binding.js +91 -0
- package/dist/mouse-config.d.ts +17 -0
- package/dist/mouse-config.js +63 -0
- package/dist/program.d.ts +5 -0
- package/dist/program.js +98 -28
- package/dist/reap-hook.js +1 -19
- package/dist/reap.d.ts +0 -5
- package/dist/reap.js +3 -10
- package/dist/runtime.d.ts +5 -0
- package/dist/runtime.js +22 -1
- package/dist/shell-quote.d.ts +6 -0
- package/dist/shell-quote.js +19 -0
- package/dist/state-log.d.ts +8 -0
- package/dist/state-log.js +12 -0
- package/dist/up.d.ts +5 -2
- package/dist/up.js +5 -5
- package/dist/worktree.d.ts +1 -1
- package/dist/worktree.js +1 -2
- package/package.json +21 -7
package/dist/install.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { join } from "node:path";
|
|
2
2
|
import { agentDefinitions, findAgentDefinition, skillName, } from "./agents.js";
|
|
3
|
+
import { isMouseEnabled, readMouseSetting, setMouseEnabled, setOpenPath } from "./mouse-config.js";
|
|
3
4
|
import { skillMarker } from "./skill.js";
|
|
4
5
|
import { findLayoutTemplate, layoutTemplates } from "./templates.js";
|
|
5
6
|
export const noLayout = "none";
|
|
@@ -8,7 +9,7 @@ export async function install(request, dependencies) {
|
|
|
8
9
|
// A confirmation is only worth asking for what the user has not already said.
|
|
9
10
|
const asked = { any: false };
|
|
10
11
|
if (!interactive && !hasExplicitRequest(request)) {
|
|
11
|
-
throw new Error("install has nothing to do and no terminal to ask: pass --yes, --layout or --
|
|
12
|
+
throw new Error("install has nothing to do and no terminal to ask: pass --yes, --layout, --agents or --mouse");
|
|
12
13
|
}
|
|
13
14
|
const configPath = await resolveConfigPath(request, dependencies);
|
|
14
15
|
const exists = await dependencies.pathExists(configPath);
|
|
@@ -33,13 +34,76 @@ export async function install(request, dependencies) {
|
|
|
33
34
|
const agents = await planAgents({ selected, candidates, force: request.force === true, interactive, skill, asked }, dependencies);
|
|
34
35
|
if (agents === undefined)
|
|
35
36
|
return cancelled;
|
|
37
|
+
const mouse = await resolveMouse(request, dependencies, interactive, asked);
|
|
38
|
+
if (mouse === undefined)
|
|
39
|
+
return cancelled;
|
|
36
40
|
if (interactive && dependencies.prompt && asked.any) {
|
|
37
|
-
const summary = planSummary({ configPath, layout, exists, force, agents });
|
|
41
|
+
const summary = planSummary({ configPath, layout, exists, force, agents, mouse });
|
|
38
42
|
const accepted = await dependencies.prompt.confirm({ summary });
|
|
39
43
|
if (accepted === undefined || !accepted)
|
|
40
44
|
return cancelled;
|
|
41
45
|
}
|
|
42
|
-
return await apply({ configPath, layout, force, agents, skill }, dependencies);
|
|
46
|
+
return await apply({ configPath, layout, force, agents, skill, mouse }, dependencies);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Like agents, Option+click is never turned on by a default: `--yes` alone leaves it
|
|
50
|
+
* as it is, and only `--mouse` or a yes to the question turns it on. The question joins
|
|
51
|
+
* a run that is asking anyway, so flags that settled everything else never block.
|
|
52
|
+
*/
|
|
53
|
+
async function resolveMouse(request, dependencies, interactive, asked) {
|
|
54
|
+
if (request.mouse !== undefined)
|
|
55
|
+
return request.mouse;
|
|
56
|
+
if (!interactive || dependencies.prompt === undefined || !asked.any)
|
|
57
|
+
return "leave";
|
|
58
|
+
const enabled = isMouseEnabled(await readGlobalConfig(dependencies));
|
|
59
|
+
const answer = await dependencies.prompt.mouse({ enabled });
|
|
60
|
+
if (answer === undefined)
|
|
61
|
+
return undefined;
|
|
62
|
+
// Accepting what is already there is not a change, so the config is not touched.
|
|
63
|
+
return answer === enabled ? "leave" : answer;
|
|
64
|
+
}
|
|
65
|
+
async function readGlobalConfig(dependencies) {
|
|
66
|
+
const path = dependencies.globalConfigPath();
|
|
67
|
+
return (await dependencies.pathExists(path)) ? await dependencies.readFile(path) : undefined;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Edits one key of the global config, which is the only one `up` reads it from,
|
|
71
|
+
* whichever config the layout went to. It runs after the layout, which already
|
|
72
|
+
* carried over the setting of a config it replaced.
|
|
73
|
+
*/
|
|
74
|
+
async function applyMouse(enabled, result, dependencies) {
|
|
75
|
+
try {
|
|
76
|
+
dependencies.write(await writeMouse(enabled, dependencies));
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
80
|
+
result.failures.push(message);
|
|
81
|
+
dependencies.write(`Option+click not changed: ${message}\n`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/** Turns Option+click on or off in the global config and says what happened. */
|
|
85
|
+
async function writeMouse(enabled, dependencies) {
|
|
86
|
+
const path = dependencies.globalConfigPath();
|
|
87
|
+
const state = enabled ? "on" : "off";
|
|
88
|
+
const current = await readGlobalConfig(dependencies);
|
|
89
|
+
const next = setMouseEnabled(current, enabled, path);
|
|
90
|
+
if (next === current || next === undefined) {
|
|
91
|
+
return `Option+click is already ${state} in ${path}\n`;
|
|
92
|
+
}
|
|
93
|
+
await dependencies.writeConfig({ path, contents: next, force: true });
|
|
94
|
+
return `Turned Option+click ${state} in ${path}; it applies on the next \`termwire up\`\n`;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* The template to write, carrying the `mouse.openPath` of the global config it is
|
|
98
|
+
* about to replace. A forced write replaces the whole file, and the setting goes into
|
|
99
|
+
* the same write rather than a second one that could fail after the first.
|
|
100
|
+
*/
|
|
101
|
+
async function composeLayout(template, plan, dependencies) {
|
|
102
|
+
const replacesGlobal = plan.force && plan.configPath === dependencies.globalConfigPath();
|
|
103
|
+
if (!replacesGlobal)
|
|
104
|
+
return template.contents;
|
|
105
|
+
const setting = readMouseSetting(await readGlobalConfig(dependencies));
|
|
106
|
+
return setting === undefined ? template.contents : setOpenPath(template.contents, setting);
|
|
43
107
|
}
|
|
44
108
|
/**
|
|
45
109
|
* A config that is already there is only replaced when the user said so: `--force`, or a
|
|
@@ -71,7 +135,10 @@ function isInteractive(request, dependencies) {
|
|
|
71
135
|
return dependencies.isTerminal;
|
|
72
136
|
}
|
|
73
137
|
function hasExplicitRequest(request) {
|
|
74
|
-
return request.yes === true ||
|
|
138
|
+
return (request.yes === true ||
|
|
139
|
+
request.layout !== undefined ||
|
|
140
|
+
request.agents !== undefined ||
|
|
141
|
+
request.mouse !== undefined);
|
|
75
142
|
}
|
|
76
143
|
async function resolveConfigPath(request, dependencies) {
|
|
77
144
|
if (request.project !== true)
|
|
@@ -93,9 +160,9 @@ async function resolveLayout(request, dependencies, interactive, path, exists, a
|
|
|
93
160
|
}
|
|
94
161
|
return template.id;
|
|
95
162
|
}
|
|
96
|
-
// Asked for agents and nothing else: do not create a
|
|
163
|
+
// Asked for agents or the mouse and nothing else: do not create a layout nobody mentioned.
|
|
97
164
|
if (!interactive || dependencies.prompt === undefined) {
|
|
98
|
-
return request.agents === undefined ? "default" : noLayout;
|
|
165
|
+
return request.agents === undefined && request.mouse === undefined ? "default" : noLayout;
|
|
99
166
|
}
|
|
100
167
|
asked.any = true;
|
|
101
168
|
return await dependencies.prompt.layout({ templates: layoutTemplates(), path, exists });
|
|
@@ -206,6 +273,9 @@ async function planAgents(options, dependencies) {
|
|
|
206
273
|
}
|
|
207
274
|
function planSummary(plan) {
|
|
208
275
|
const lines = [];
|
|
276
|
+
if (plan.mouse !== "leave") {
|
|
277
|
+
lines.push(`Option+click opens paths: ${plan.mouse ? "on" : "off"}`);
|
|
278
|
+
}
|
|
209
279
|
if (plan.layout === noLayout) {
|
|
210
280
|
lines.push("Layout: leave the configuration alone");
|
|
211
281
|
}
|
|
@@ -237,6 +307,28 @@ function summariseAction(agent) {
|
|
|
237
307
|
return `replace ${agent.path}`;
|
|
238
308
|
return `write ${agent.path}`;
|
|
239
309
|
}
|
|
310
|
+
/** Writes the chosen layout template; a failure is recorded, and the run goes on. */
|
|
311
|
+
async function writeLayout(plan, result, dependencies) {
|
|
312
|
+
if (plan.layout === noLayout)
|
|
313
|
+
return;
|
|
314
|
+
const template = findLayoutTemplate(plan.layout);
|
|
315
|
+
if (template === undefined)
|
|
316
|
+
throw new Error(`unknown layout: ${plan.layout}`);
|
|
317
|
+
try {
|
|
318
|
+
await dependencies.writeConfig({
|
|
319
|
+
path: plan.configPath,
|
|
320
|
+
contents: await composeLayout(template, plan, dependencies),
|
|
321
|
+
force: plan.force,
|
|
322
|
+
});
|
|
323
|
+
result.configWritten = true;
|
|
324
|
+
dependencies.write(`Wrote ${plan.configPath} (${template.id})\n`);
|
|
325
|
+
}
|
|
326
|
+
catch (error) {
|
|
327
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
328
|
+
result.failures.push(message);
|
|
329
|
+
dependencies.write(`Config not written: ${message}\n`);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
240
332
|
async function apply(plan, dependencies) {
|
|
241
333
|
const result = {
|
|
242
334
|
cancelled: false,
|
|
@@ -245,25 +337,9 @@ async function apply(plan, dependencies) {
|
|
|
245
337
|
agents: [],
|
|
246
338
|
failures: [],
|
|
247
339
|
};
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
throw new Error(`unknown layout: ${plan.layout}`);
|
|
252
|
-
try {
|
|
253
|
-
await dependencies.writeConfig({
|
|
254
|
-
path: plan.configPath,
|
|
255
|
-
contents: template.contents,
|
|
256
|
-
force: plan.force,
|
|
257
|
-
});
|
|
258
|
-
result.configWritten = true;
|
|
259
|
-
dependencies.write(`Wrote ${plan.configPath} (${template.id})\n`);
|
|
260
|
-
}
|
|
261
|
-
catch (error) {
|
|
262
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
263
|
-
result.failures.push(message);
|
|
264
|
-
dependencies.write(`Config not written: ${message}\n`);
|
|
265
|
-
}
|
|
266
|
-
}
|
|
340
|
+
await writeLayout(plan, result, dependencies);
|
|
341
|
+
if (plan.mouse !== "leave")
|
|
342
|
+
await applyMouse(plan.mouse, result, dependencies);
|
|
267
343
|
for (const agent of plan.agents) {
|
|
268
344
|
result.agents.push(await runAgentPlan(agent, plan.skill, dependencies));
|
|
269
345
|
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { BindKeyOptions, KeyBinding, UnbindKeyOptions } from "@termwire/tmux";
|
|
2
|
+
export declare const clickCommandName = "_click";
|
|
3
|
+
/**
|
|
4
|
+
* Identifies our binding among the user's own, the way the reap hook's marker does,
|
|
5
|
+
* and is a harmless environment assignment in the shell command that carries it.
|
|
6
|
+
*/
|
|
7
|
+
export declare const clickMarker = "TERMWIRE_CLICK=1";
|
|
8
|
+
export interface MouseBindingTmux {
|
|
9
|
+
listKeys: (table: string) => Promise<KeyBinding[]>;
|
|
10
|
+
bindKey: (options: BindKeyOptions) => Promise<void>;
|
|
11
|
+
unbindKey: (options: UnbindKeyOptions) => Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
/** `blocked`: the key carries a binding that is not ours, which is left alone. */
|
|
14
|
+
export type MouseBindingOutcome = "bound" | "blocked" | "removed" | "none";
|
|
15
|
+
/**
|
|
16
|
+
* The session option `up` sets on a workspace. The binding reads it as a format, so
|
|
17
|
+
* deciding whose click this is starts no process.
|
|
18
|
+
*/
|
|
19
|
+
export declare const workspaceOption = "@termwire";
|
|
20
|
+
/**
|
|
21
|
+
* What a tab in the screen line is replaced with before it reaches sh. `#{q:}` leaves
|
|
22
|
+
* a tab bare, and sh would split the line there into extra arguments.
|
|
23
|
+
*/
|
|
24
|
+
export declare const tabMark = "\u2409";
|
|
25
|
+
/**
|
|
26
|
+
* The tmux arguments after the key.
|
|
27
|
+
*
|
|
28
|
+
* A root binding takes the key from every pane on the server, so it is guarded: only
|
|
29
|
+
* a session `up` marked with `@termwire` runs the click, and anywhere else
|
|
30
|
+
* `send-keys -M` hands the click to the application as if the key were unbound.
|
|
31
|
+
*
|
|
32
|
+
* `run-shell -t =` expands the formats against the pane that was clicked, not the
|
|
33
|
+
* active one, and `-b` keeps the click from blocking the client.
|
|
34
|
+
*
|
|
35
|
+
* `mouse_line` is whatever text is on screen, so it reaches sh only through `#{q:}`,
|
|
36
|
+
* which escapes every sh special character but the tab, replaced first. The `""` in
|
|
37
|
+
* front of each value keeps an empty expansion as one argument; it is a separate word
|
|
38
|
+
* part, so it never wraps the escaped text in quotes that would disarm the escaping.
|
|
39
|
+
* The `--` keeps a line that starts with a dash, a diff header for one, from being
|
|
40
|
+
* read as an option.
|
|
41
|
+
*
|
|
42
|
+
* A click that opens nothing exits 0 and only logs. The process failing to run at all
|
|
43
|
+
* — a stale interpreter path, say — would otherwise swallow every click in silence, so
|
|
44
|
+
* that one case shows a message in the status line.
|
|
45
|
+
*/
|
|
46
|
+
export declare function createClickCommand(paths: {
|
|
47
|
+
execPath: string;
|
|
48
|
+
scriptPath: string;
|
|
49
|
+
}): string[];
|
|
50
|
+
/**
|
|
51
|
+
* Binds `key` to the click command, or only removes ours when `key` is undefined.
|
|
52
|
+
* Our binding on any other key is removed, so changing or dropping the key in the
|
|
53
|
+
* config moves or removes it. Rebinding ours is idempotent and picks up a new path.
|
|
54
|
+
*/
|
|
55
|
+
export declare function ensureMouseBinding(options: {
|
|
56
|
+
tmux: MouseBindingTmux;
|
|
57
|
+
key: string | undefined;
|
|
58
|
+
execPath: string;
|
|
59
|
+
scriptPath: string;
|
|
60
|
+
}): Promise<MouseBindingOutcome>;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { shellQuote } from "./shell-quote.js";
|
|
2
|
+
export const clickCommandName = "_click";
|
|
3
|
+
/**
|
|
4
|
+
* Identifies our binding among the user's own, the way the reap hook's marker does,
|
|
5
|
+
* and is a harmless environment assignment in the shell command that carries it.
|
|
6
|
+
*/
|
|
7
|
+
export const clickMarker = "TERMWIRE_CLICK=1";
|
|
8
|
+
const table = "root";
|
|
9
|
+
/**
|
|
10
|
+
* The session option `up` sets on a workspace. The binding reads it as a format, so
|
|
11
|
+
* deciding whose click this is starts no process.
|
|
12
|
+
*/
|
|
13
|
+
export const workspaceOption = "@termwire";
|
|
14
|
+
/**
|
|
15
|
+
* What a tab in the screen line is replaced with before it reaches sh. `#{q:}` leaves
|
|
16
|
+
* a tab bare, and sh would split the line there into extra arguments.
|
|
17
|
+
*/
|
|
18
|
+
export const tabMark = "\u2409";
|
|
19
|
+
const failureMessage = "termwire: Option+click could not run; run `termwire up` to rebind it";
|
|
20
|
+
/**
|
|
21
|
+
* The tmux arguments after the key.
|
|
22
|
+
*
|
|
23
|
+
* A root binding takes the key from every pane on the server, so it is guarded: only
|
|
24
|
+
* a session `up` marked with `@termwire` runs the click, and anywhere else
|
|
25
|
+
* `send-keys -M` hands the click to the application as if the key were unbound.
|
|
26
|
+
*
|
|
27
|
+
* `run-shell -t =` expands the formats against the pane that was clicked, not the
|
|
28
|
+
* active one, and `-b` keeps the click from blocking the client.
|
|
29
|
+
*
|
|
30
|
+
* `mouse_line` is whatever text is on screen, so it reaches sh only through `#{q:}`,
|
|
31
|
+
* which escapes every sh special character but the tab, replaced first. The `""` in
|
|
32
|
+
* front of each value keeps an empty expansion as one argument; it is a separate word
|
|
33
|
+
* part, so it never wraps the escaped text in quotes that would disarm the escaping.
|
|
34
|
+
* The `--` keeps a line that starts with a dash, a diff header for one, from being
|
|
35
|
+
* read as an option.
|
|
36
|
+
*
|
|
37
|
+
* A click that opens nothing exits 0 and only logs. The process failing to run at all
|
|
38
|
+
* — a stale interpreter path, say — would otherwise swallow every click in silence, so
|
|
39
|
+
* that one case shows a message in the status line.
|
|
40
|
+
*/
|
|
41
|
+
export function createClickCommand(paths) {
|
|
42
|
+
const execPath = shellQuote(paths.execPath, "interpreter path");
|
|
43
|
+
const scriptPath = shellQuote(paths.scriptPath, "script path");
|
|
44
|
+
const values = ["session_name", "pane_current_path", "session_path", "mouse_x"]
|
|
45
|
+
.map((format) => `""#{q:${format}}`)
|
|
46
|
+
.concat(`""#{s/\t/${tabMark}/;q:mouse_line}`)
|
|
47
|
+
.join(" ");
|
|
48
|
+
const click = `${clickMarker} ${execPath} ${scriptPath} ${clickCommandName} -- ${values}`;
|
|
49
|
+
const shell = `${click} >/dev/null 2>&1 || tmux display-message '${failureMessage}'`;
|
|
50
|
+
return [
|
|
51
|
+
"if-shell",
|
|
52
|
+
"-F",
|
|
53
|
+
"-t",
|
|
54
|
+
"=",
|
|
55
|
+
`#{${workspaceOption}}`,
|
|
56
|
+
`run-shell -b -t = ${tmuxQuote(shell)}`,
|
|
57
|
+
"send-keys -M -t =",
|
|
58
|
+
];
|
|
59
|
+
}
|
|
60
|
+
/** One tmux word: nothing is special inside single quotes, and `'\\''` carries a quote. */
|
|
61
|
+
function tmuxQuote(value) {
|
|
62
|
+
return `'${value.replaceAll("'", "'\\''")}'`;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Binds `key` to the click command, or only removes ours when `key` is undefined.
|
|
66
|
+
* Our binding on any other key is removed, so changing or dropping the key in the
|
|
67
|
+
* config moves or removes it. Rebinding ours is idempotent and picks up a new path.
|
|
68
|
+
*/
|
|
69
|
+
export async function ensureMouseBinding(options) {
|
|
70
|
+
const { tmux, key } = options;
|
|
71
|
+
const bindings = await tmux.listKeys(table);
|
|
72
|
+
const removed = await removeStrayBindings(tmux, bindings, key);
|
|
73
|
+
if (key === undefined)
|
|
74
|
+
return removed ? "removed" : "none";
|
|
75
|
+
const current = bindings.find((binding) => binding.key === key);
|
|
76
|
+
if (current !== undefined && !isOurs(current))
|
|
77
|
+
return "blocked";
|
|
78
|
+
await tmux.bindKey({ table, key, command: createClickCommand(options) });
|
|
79
|
+
return "bound";
|
|
80
|
+
}
|
|
81
|
+
/** Unbinds ours from every key but `key`, and says whether there was one. */
|
|
82
|
+
async function removeStrayBindings(tmux, bindings, key) {
|
|
83
|
+
const stray = bindings.filter((binding) => isOurs(binding) && binding.key !== key);
|
|
84
|
+
for (const binding of stray) {
|
|
85
|
+
await tmux.unbindKey({ table, key: binding.key });
|
|
86
|
+
}
|
|
87
|
+
return stray.length > 0;
|
|
88
|
+
}
|
|
89
|
+
function isOurs(binding) {
|
|
90
|
+
return binding.command.includes(clickMarker);
|
|
91
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What this config text sets `mouse.openPath` to, when the schema accepts it: a key,
|
|
3
|
+
* `false`, or nothing. A key `up` would reject counts as nothing, so turning
|
|
4
|
+
* Option+click on replaces it instead of reporting it as already on.
|
|
5
|
+
*/
|
|
6
|
+
export declare function readMouseSetting(text: string | undefined): string | false | undefined;
|
|
7
|
+
/** Whether this config text turns Option+click on. Text that does not parse does not. */
|
|
8
|
+
export declare function isMouseEnabled(text: string | undefined): boolean;
|
|
9
|
+
/** The config text with `mouse.openPath` set, everything else as the user wrote it. */
|
|
10
|
+
export declare function setOpenPath(text: string, value: string | false): string;
|
|
11
|
+
/**
|
|
12
|
+
* The config text with Option+click turned on or off, or the same text when it already
|
|
13
|
+
* is. It edits the one key in place with jsonc-parser, so the user's comments, layout
|
|
14
|
+
* and formatting survive. A config that does not parse is refused rather than
|
|
15
|
+
* rewritten around its error.
|
|
16
|
+
*/
|
|
17
|
+
export declare function setMouseEnabled(text: string | undefined, enabled: boolean, path: string): string | undefined;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { applyEdits, modify, parse } from "jsonc-parser";
|
|
2
|
+
import { defaultMouseKey, mouseSchema } from "./config-schema.js";
|
|
3
|
+
/**
|
|
4
|
+
* A new key goes first in the object: jsonc-parser reformats the property it inserts
|
|
5
|
+
* after, which would reflow the user's last property and move its comments.
|
|
6
|
+
*/
|
|
7
|
+
const editOptions = {
|
|
8
|
+
formattingOptions: { insertSpaces: true, tabSize: 2 },
|
|
9
|
+
getInsertionIndex: () => 0,
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* What this config text sets `mouse.openPath` to, when the schema accepts it: a key,
|
|
13
|
+
* `false`, or nothing. A key `up` would reject counts as nothing, so turning
|
|
14
|
+
* Option+click on replaces it instead of reporting it as already on.
|
|
15
|
+
*/
|
|
16
|
+
export function readMouseSetting(text) {
|
|
17
|
+
const openPath = readOpenPath(text);
|
|
18
|
+
const valid = mouseSchema.safeParse({ openPath }).success;
|
|
19
|
+
return valid ? openPath : undefined;
|
|
20
|
+
}
|
|
21
|
+
/** Whether this config text turns Option+click on. Text that does not parse does not. */
|
|
22
|
+
export function isMouseEnabled(text) {
|
|
23
|
+
return typeof readMouseSetting(text) === "string";
|
|
24
|
+
}
|
|
25
|
+
/** The config text with `mouse.openPath` set, everything else as the user wrote it. */
|
|
26
|
+
export function setOpenPath(text, value) {
|
|
27
|
+
return applyEdits(text, modify(text, ["mouse", "openPath"], value, editOptions));
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The config text with Option+click turned on or off, or the same text when it already
|
|
31
|
+
* is. It edits the one key in place with jsonc-parser, so the user's comments, layout
|
|
32
|
+
* and formatting survive. A config that does not parse is refused rather than
|
|
33
|
+
* rewritten around its error.
|
|
34
|
+
*/
|
|
35
|
+
export function setMouseEnabled(text, enabled, path) {
|
|
36
|
+
if (text === undefined) {
|
|
37
|
+
return enabled
|
|
38
|
+
? `{\n "version": 1,\n "mouse": { "openPath": "${defaultMouseKey}" },\n}\n`
|
|
39
|
+
: undefined;
|
|
40
|
+
}
|
|
41
|
+
const errors = [];
|
|
42
|
+
parse(text, errors, { allowTrailingComma: true });
|
|
43
|
+
if (errors.length > 0) {
|
|
44
|
+
throw new Error(`${path} is not valid JSONC; fix it before turning Option+click on or off`);
|
|
45
|
+
}
|
|
46
|
+
// A string the schema rejects is neither: on replaces it, and off still has to.
|
|
47
|
+
const settled = enabled ? isMouseEnabled(text) : typeof readOpenPath(text) !== "string";
|
|
48
|
+
if (settled)
|
|
49
|
+
return text;
|
|
50
|
+
// Off is written as `false` rather than a removed key: it says what was decided.
|
|
51
|
+
return setOpenPath(text, enabled && defaultMouseKey);
|
|
52
|
+
}
|
|
53
|
+
function readOpenPath(text) {
|
|
54
|
+
if (text === undefined)
|
|
55
|
+
return undefined;
|
|
56
|
+
const value = parse(text, [], { allowTrailingComma: true });
|
|
57
|
+
if (typeof value !== "object" || value === null)
|
|
58
|
+
return undefined;
|
|
59
|
+
const mouse = value.mouse;
|
|
60
|
+
if (typeof mouse !== "object" || mouse === null)
|
|
61
|
+
return undefined;
|
|
62
|
+
return mouse.openPath;
|
|
63
|
+
}
|
package/dist/program.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Command } from "commander";
|
|
2
|
+
import { type ClickRequest } from "./click.js";
|
|
2
3
|
import { type DownRequest, type DownResult } from "./down.js";
|
|
3
4
|
import { type InstallRequest, type InstallResult } from "./install.js";
|
|
4
5
|
import { type OpenRequest, type OpenResult } from "./open.js";
|
|
@@ -11,9 +12,12 @@ export interface ProgramDependencies {
|
|
|
11
12
|
down: (request: DownRequest) => Promise<DownResult>;
|
|
12
13
|
open: (request: OpenRequest) => Promise<OpenResult>;
|
|
13
14
|
reap: (session: string) => Promise<void>;
|
|
15
|
+
/** Never throws: a click that opens nothing is logged, not shown in tmux. */
|
|
16
|
+
click: (request: ClickRequest) => Promise<void>;
|
|
14
17
|
install: (request: InstallRequest) => Promise<InstallResult>;
|
|
15
18
|
/** Only the table shortens `$HOME` to `~`; the JSON rows keep absolute paths. */
|
|
16
19
|
homedir: () => string;
|
|
20
|
+
version: string;
|
|
17
21
|
writeError: (message: string) => void;
|
|
18
22
|
writeOutput: (message: string) => void;
|
|
19
23
|
}
|
|
@@ -23,6 +27,7 @@ export declare function createLs({ fs, git, tmux, createScanner }: CliRuntime):
|
|
|
23
27
|
export declare function createDown({ host, git, tmux, }: CliRuntime): (request: DownRequest) => Promise<DownResult>;
|
|
24
28
|
export declare function createOpen({ host, nvim, tmux, }: CliRuntime): (request: OpenRequest) => Promise<OpenResult>;
|
|
25
29
|
export declare function createReap({ fs, host, createScanner, }: CliRuntime): (session: string) => Promise<void>;
|
|
30
|
+
export declare function createClick({ fs, host, nvim, tmux, }: CliRuntime): (request: ClickRequest) => Promise<void>;
|
|
26
31
|
export declare function createInstall({ fs, host, git, createPrompter, }: CliRuntime): (request: InstallRequest) => Promise<InstallResult>;
|
|
27
32
|
/** Binds every command to one runtime. The only caller that needs the real one. */
|
|
28
33
|
export declare function createCommands(runtime: CliRuntime): ProgramDependencies;
|
package/dist/program.js
CHANGED
|
@@ -1,30 +1,25 @@
|
|
|
1
|
-
import { readFileSync } from "node:fs";
|
|
2
1
|
import { dirname, resolve as resolvePath } from "node:path";
|
|
3
2
|
import { Command, CommanderError } from "commander";
|
|
4
3
|
import { agentIds, parseAgentIds } from "./agents.js";
|
|
5
4
|
import { prepareBranch } from "./branch.js";
|
|
5
|
+
import { click } from "./click.js";
|
|
6
6
|
import { configHome, createConfigLoader, globalConfigPath } from "./config-loader.js";
|
|
7
|
-
import { resolveLayout } from "./config-validation.js";
|
|
7
|
+
import { resolveLayout, resolveMouseKey } from "./config-validation.js";
|
|
8
8
|
import { writeConfigFile } from "./config-writer.js";
|
|
9
9
|
import { down } from "./down.js";
|
|
10
10
|
import { install, noLayout } from "./install.js";
|
|
11
11
|
import { createLayout } from "./layout.js";
|
|
12
|
+
import { clickCommandName, ensureMouseBinding, workspaceOption } from "./mouse-binding.js";
|
|
12
13
|
import { open } from "./open.js";
|
|
13
14
|
import { toSingleSession } from "./process-scan.js";
|
|
14
|
-
import { formatReapFailure, formatReapReport, parseTmuxServerPid,
|
|
15
|
+
import { formatReapFailure, formatReapReport, parseTmuxServerPid, reapSession } from "./reap.js";
|
|
15
16
|
import { ensureReapHook, reapCommandName } from "./reap-hook.js";
|
|
16
17
|
import { skillSourcePath } from "./skill.js";
|
|
18
|
+
import { stateLogPath } from "./state-log.js";
|
|
17
19
|
import { layoutTemplateIds } from "./templates.js";
|
|
18
20
|
import { up } from "./up.js";
|
|
19
21
|
import { collectWorkspaces, formatWorkspacesJson, formatWorkspaceTable, } from "./workspaces.js";
|
|
20
22
|
import { findGitRoot, inspectWorktree, prepareWorktree, removeWorktree } from "./worktree.js";
|
|
21
|
-
/**
|
|
22
|
-
* Read at runtime rather than baked in at build time, the way `@termwire/mcp`
|
|
23
|
-
* reports its own version. `dist/program.js` and `src/program.ts` both sit one
|
|
24
|
-
* level below the package root, so the same specifier resolves in a published
|
|
25
|
-
* install and from a checkout.
|
|
26
|
-
*/
|
|
27
|
-
const manifest = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
28
23
|
export async function removeStaleSocket(path, unlink) {
|
|
29
24
|
try {
|
|
30
25
|
await unlink(path);
|
|
@@ -42,20 +37,31 @@ export function createUp({ fs, host, git, tmux, }) {
|
|
|
42
37
|
exists: fs.exists,
|
|
43
38
|
readFile: (path) => fs.readFile(path),
|
|
44
39
|
});
|
|
45
|
-
return (request) =>
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
40
|
+
return (request) => {
|
|
41
|
+
// One read per `up`: the layout and the mouse binding both want the global config.
|
|
42
|
+
let globalConfig;
|
|
43
|
+
const loadGlobalConfig = () => {
|
|
44
|
+
globalConfig ??= loader.loadGlobal();
|
|
45
|
+
return globalConfig;
|
|
46
|
+
};
|
|
47
|
+
return up(request, {
|
|
48
|
+
cwd: host.cwd,
|
|
49
|
+
findGitRoot: (directory) => findGitRoot(git, directory),
|
|
50
|
+
prepareBranch: ({ cwd: directory, name }) => prepareBranch(git, directory, name),
|
|
51
|
+
prepareWorktree: (options) => prepareWorktree({ ...options, exec: git, pathExists: fs.exists, realpath: fs.realpath }),
|
|
52
|
+
mkdir: fs.mkdir,
|
|
53
|
+
removeFile: (path) => removeStaleSocket(path, fs.unlink),
|
|
54
|
+
tmux,
|
|
55
|
+
installServerHooks: async (session) => {
|
|
56
|
+
await installReapHook({ host, tmux });
|
|
57
|
+
await installMouseBinding({ host, tmux }, session, loadGlobalConfig);
|
|
58
|
+
},
|
|
59
|
+
loadGlobalConfig,
|
|
60
|
+
loadProjectConfig: loader.loadProject,
|
|
61
|
+
resolveLayout,
|
|
62
|
+
createLayout,
|
|
63
|
+
});
|
|
64
|
+
};
|
|
59
65
|
}
|
|
60
66
|
/**
|
|
61
67
|
* Installing the cleanup hook is best effort: a workspace is still usable when the
|
|
@@ -74,6 +80,34 @@ async function installReapHook({ host, tmux }) {
|
|
|
74
80
|
host.writeError(`termwire: session cleanup hook not installed: ${message}\n`);
|
|
75
81
|
}
|
|
76
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* Binds, moves or removes the Option+click binding to match the global config, and
|
|
85
|
+
* marks `session` as a workspace the binding may act in. Best
|
|
86
|
+
* effort like the reap hook; a key someone else bound is reported and left alone.
|
|
87
|
+
*/
|
|
88
|
+
async function installMouseBinding({ host, tmux }, session, loadGlobalConfig) {
|
|
89
|
+
try {
|
|
90
|
+
const key = resolveMouseKey(await loadGlobalConfig());
|
|
91
|
+
const outcome = await ensureMouseBinding({
|
|
92
|
+
tmux,
|
|
93
|
+
key,
|
|
94
|
+
execPath: host.execPath,
|
|
95
|
+
scriptPath: resolveScriptPath(host.scriptPath),
|
|
96
|
+
});
|
|
97
|
+
// The binding only acts in a session carrying this option, so every `up` sets it:
|
|
98
|
+
// a workspace older than the option gets it the next time it is attached to.
|
|
99
|
+
if (key !== undefined)
|
|
100
|
+
await tmux.setSessionOption(session, workspaceOption, "1");
|
|
101
|
+
if (outcome === "blocked") {
|
|
102
|
+
host.writeError(`termwire: tmux already binds ${key} to something else; ` +
|
|
103
|
+
"unbind it or choose another mouse.openPath\n");
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
108
|
+
host.writeError(`termwire: mouse binding not updated: ${message}\n`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
77
111
|
function resolveScriptPath(scriptPath) {
|
|
78
112
|
if (scriptPath === undefined) {
|
|
79
113
|
throw new Error("the termwire script path is unknown");
|
|
@@ -107,7 +141,7 @@ export function createOpen({ host, nvim, tmux, }) {
|
|
|
107
141
|
return (request) => open(request, { cwd: host.cwd, env: host.env, nvim, tmux });
|
|
108
142
|
}
|
|
109
143
|
export function createReap({ fs, host, createScanner, }) {
|
|
110
|
-
const record = (line) => appendQuietly(
|
|
144
|
+
const record = (line) => appendQuietly(stateLogPath({ env: host.env, homedir: host.homedir }, "reap.log"), line, fs);
|
|
111
145
|
return async (session) => {
|
|
112
146
|
try {
|
|
113
147
|
const serverPid = parseTmuxServerPid(host.env.TMUX);
|
|
@@ -128,6 +162,29 @@ export function createReap({ fs, host, createScanner, }) {
|
|
|
128
162
|
}
|
|
129
163
|
};
|
|
130
164
|
}
|
|
165
|
+
export function createClick({ fs, host, nvim, tmux, }) {
|
|
166
|
+
return async (request) => {
|
|
167
|
+
let outcome;
|
|
168
|
+
try {
|
|
169
|
+
outcome = await click(request, {
|
|
170
|
+
showEnvironment: (session) => tmux.showEnvironment(session),
|
|
171
|
+
isFile: fs.isFile,
|
|
172
|
+
homedir: host.homedir,
|
|
173
|
+
open: async (target, context) => {
|
|
174
|
+
await open({
|
|
175
|
+
target: target.path,
|
|
176
|
+
...(target.line === undefined ? {} : { line: String(target.line) }),
|
|
177
|
+
}, { cwd: () => context.cwd, env: context.env, nvim, tmux });
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
outcome = error instanceof Error ? error.message : String(error);
|
|
183
|
+
}
|
|
184
|
+
const line = `${host.now().toISOString()} ${request.session}: ${outcome}\n`;
|
|
185
|
+
await appendQuietly(stateLogPath({ env: host.env, homedir: host.homedir }, "click.log"), line, fs);
|
|
186
|
+
};
|
|
187
|
+
}
|
|
131
188
|
/** A log that cannot be written must not hide what the reap did. */
|
|
132
189
|
async function appendQuietly(path, line, fs) {
|
|
133
190
|
try {
|
|
@@ -167,8 +224,10 @@ export function createCommands(runtime) {
|
|
|
167
224
|
down: createDown(runtime),
|
|
168
225
|
open: createOpen(runtime),
|
|
169
226
|
reap: createReap(runtime),
|
|
227
|
+
click: createClick(runtime),
|
|
170
228
|
install: createInstall(runtime),
|
|
171
229
|
homedir: runtime.host.homedir,
|
|
230
|
+
version: runtime.host.version,
|
|
172
231
|
writeError: runtime.host.writeError,
|
|
173
232
|
writeOutput: runtime.host.writeOutput,
|
|
174
233
|
};
|
|
@@ -180,7 +239,7 @@ export function createProgram(dependencies) {
|
|
|
180
239
|
});
|
|
181
240
|
program.exitOverride();
|
|
182
241
|
program.showHelpAfterError();
|
|
183
|
-
program.version(
|
|
242
|
+
program.version(dependencies.version, "-V, --version", "print the installed termwire version");
|
|
184
243
|
program
|
|
185
244
|
.command("up <name>")
|
|
186
245
|
.description("create or attach to the workspace tmux session <project>-<name>")
|
|
@@ -276,14 +335,17 @@ Target syntax:
|
|
|
276
335
|
.option("-l, --layout <name>", `layout template: ${[...layoutTemplateIds, noLayout].join(", ")}`)
|
|
277
336
|
.option("-p, --project", "write .termwire.jsonc in this repository instead of the global config")
|
|
278
337
|
.option("-a, --agents <list>", `comma-separated: ${agentIds.join(", ")}, or none`)
|
|
338
|
+
.option("--mouse", "open the path under Option+click in the editor (global config)")
|
|
339
|
+
.option("--no-mouse", "turn Option+click off again")
|
|
279
340
|
.option("-f, --force", "replace an existing config, or a skill file you own")
|
|
280
|
-
.option("-y, --yes", "never prompt: default layout,
|
|
341
|
+
.option("-y, --yes", "never prompt: default layout, no agents or mouse unless asked for")
|
|
281
342
|
.addHelpText("after", `
|
|
282
343
|
Run it before installing anything:
|
|
283
344
|
npx @termwire/cli install
|
|
284
345
|
|
|
285
346
|
Interactive by default, and silent once the flags leave nothing to ask: --yes, or
|
|
286
|
-
--layout together with --agents.
|
|
347
|
+
--layout together with --agents. Option+click is asked about along the way, and never
|
|
348
|
+
turned on by --yes alone. The skill teaches an agent to run ${"`termwire open`"}, which costs
|
|
287
349
|
no process, unlike the ${"`@termwire/mcp`"} server it replaces. It only ever adds its own file
|
|
288
350
|
to an agent's skills directory and never edits another tool's configuration.
|
|
289
351
|
`)
|
|
@@ -292,6 +354,7 @@ to an agent's skills directory and never edits another tool's configuration.
|
|
|
292
354
|
...(options.layout === undefined ? {} : { layout: options.layout }),
|
|
293
355
|
...(options.project === undefined ? {} : { project: true }),
|
|
294
356
|
...(options.agents === undefined ? {} : { agents: parseAgentIds(options.agents) }),
|
|
357
|
+
...(options.mouse === undefined ? {} : { mouse: options.mouse }),
|
|
295
358
|
...(options.force === undefined ? {} : { force: true }),
|
|
296
359
|
...(options.yes === undefined ? {} : { yes: true }),
|
|
297
360
|
});
|
|
@@ -307,6 +370,13 @@ to an agent's skills directory and never edits another tool's configuration.
|
|
|
307
370
|
.action(async (session) => {
|
|
308
371
|
await dependencies.reap(session);
|
|
309
372
|
});
|
|
373
|
+
// Run by the tmux mouse binding `up` installs, with values straight from formats.
|
|
374
|
+
program
|
|
375
|
+
.command(`${clickCommandName} <session> <pane-cwd> <root> <x> <text>`, { hidden: true })
|
|
376
|
+
.description("open the path under the mouse in the workspace editor")
|
|
377
|
+
.action(async (session, paneCwd, root, x, text) => {
|
|
378
|
+
await dependencies.click({ session, paneCwd, root, x, text });
|
|
379
|
+
});
|
|
310
380
|
return program;
|
|
311
381
|
}
|
|
312
382
|
/** The composition root's entry: one runtime in, an exit code out. */
|