@termwire/cli 0.5.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 +96 -0
- package/README.md +125 -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/down.d.ts +40 -0
- package/dist/down.js +99 -0
- 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/process-scan-darwin.d.ts +24 -0
- package/dist/process-scan-darwin.js +102 -0
- package/dist/process-scan-linux.d.ts +13 -0
- package/dist/process-scan-linux.js +66 -0
- package/dist/process-scan.d.ts +37 -23
- package/dist/process-scan.js +30 -131
- package/dist/program.d.ts +14 -1
- package/dist/program.js +174 -38
- 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 +20 -5
- package/dist/runtime.js +44 -5
- 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/workspaces.d.ts +45 -0
- package/dist/workspaces.js +116 -0
- package/dist/worktree.d.ts +16 -1
- package/dist/worktree.js +66 -9
- package/package.json +21 -7
package/dist/down.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { dirname, resolve, sep } from "node:path";
|
|
2
|
+
import { createIdentity } from "./identity.js";
|
|
3
|
+
import { sessionVariable } from "./process-scan.js";
|
|
4
|
+
/**
|
|
5
|
+
* Teardown. A name goes through `createIdentity`, so `up foo` and `down foo` always
|
|
6
|
+
* mean one session; without a name the inherited `TERMWIRE_SESSION` names it, which is
|
|
7
|
+
* how a workspace tears itself down from the inside. The tmux listing knows where it
|
|
8
|
+
* lives.
|
|
9
|
+
*
|
|
10
|
+
* Processes are not hunted here: the global `session-closed` hook `up` installs runs
|
|
11
|
+
* `_reap` for exactly that.
|
|
12
|
+
*/
|
|
13
|
+
export async function down(request, { cwd, chdir, env, findGitRoot, listSessions, killSession, inspectWorktree, removeWorktree, }) {
|
|
14
|
+
const directory = cwd();
|
|
15
|
+
const target = await resolveSession(request, { cwd: directory, env, findGitRoot });
|
|
16
|
+
const sessions = await listSessions();
|
|
17
|
+
const session = sessions.find((entry) => entry.name === target);
|
|
18
|
+
if (session === undefined) {
|
|
19
|
+
throw new Error(`no workspace session named ${target}`);
|
|
20
|
+
}
|
|
21
|
+
if (request.worktree !== true) {
|
|
22
|
+
await killSession(target);
|
|
23
|
+
return { session: target };
|
|
24
|
+
}
|
|
25
|
+
/** Whether the session being torn down is the one this command is running in. */
|
|
26
|
+
const ownSession = env[sessionVariable]?.trim() === target;
|
|
27
|
+
const force = request.force === true;
|
|
28
|
+
// Everything is refused before anything is destroyed, so a refusal leaves the
|
|
29
|
+
// session running and the worktree untouched.
|
|
30
|
+
await assertRemovable({ path: session.path, cwd: directory, force, ownSession, inspectWorktree });
|
|
31
|
+
if (ownSession) {
|
|
32
|
+
// The kill ends this process with its pane, so it goes last. And the kill is a
|
|
33
|
+
// spawn, which fails with ENOENT from a deleted working directory, so step out of
|
|
34
|
+
// the worktree first; the parent of a worktree always outlives it.
|
|
35
|
+
if (contains(session.path, directory))
|
|
36
|
+
chdir(dirname(session.path));
|
|
37
|
+
await removeWorktree(session.path, { force });
|
|
38
|
+
await killSession(target);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
// Nothing of ours dies here, so the processes go first and their files after them.
|
|
42
|
+
await killSession(target);
|
|
43
|
+
await removeKilledWorktree({ session: target, path: session.path, force, removeWorktree });
|
|
44
|
+
}
|
|
45
|
+
return { session: target, worktree: session.path };
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* A name is resolved from the current directory, exactly as `up` resolves it. Without
|
|
49
|
+
* one, the session is the workspace this command is running in, which is the only way
|
|
50
|
+
* to name a worktree session from inside it: there the Git root is the worktree, so
|
|
51
|
+
* `createIdentity` would build `<worktree>-<name>` and find nothing.
|
|
52
|
+
*/
|
|
53
|
+
async function resolveSession(request, { cwd, env, findGitRoot }) {
|
|
54
|
+
if (request.name === undefined) {
|
|
55
|
+
const session = env[sessionVariable]?.trim();
|
|
56
|
+
if (!session) {
|
|
57
|
+
throw new Error("not inside a termwire workspace");
|
|
58
|
+
}
|
|
59
|
+
return session;
|
|
60
|
+
}
|
|
61
|
+
const gitRoot = await findGitRoot(cwd);
|
|
62
|
+
return createIdentity({ cwd, gitRoot, name: request.name }).session;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The session is already gone by the time the worktree is removed, so a failure here
|
|
66
|
+
* says so: `down` cannot be retried for a session that no longer exists, and the user
|
|
67
|
+
* needs to know that `git worktree remove` is what is left to do.
|
|
68
|
+
*/
|
|
69
|
+
async function removeKilledWorktree({ session, path, force, removeWorktree, }) {
|
|
70
|
+
try {
|
|
71
|
+
await removeWorktree(path, { force });
|
|
72
|
+
}
|
|
73
|
+
catch (cause) {
|
|
74
|
+
const message = cause instanceof Error ? cause.message : String(cause);
|
|
75
|
+
throw new Error(`session ${session} was killed, but its worktree was not removed: ${message}`, {
|
|
76
|
+
cause,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
async function assertRemovable({ path, cwd, force, ownSession, inspectWorktree, }) {
|
|
81
|
+
// Sawing off the branch you stand on, unless the shell standing there is itself part
|
|
82
|
+
// of what is being torn down: it dies with the session a moment later.
|
|
83
|
+
if (!ownSession && contains(path, cwd)) {
|
|
84
|
+
throw new Error(`refusing to remove the directory this command runs in: ${path}`);
|
|
85
|
+
}
|
|
86
|
+
const state = await inspectWorktree(path);
|
|
87
|
+
if (state.main) {
|
|
88
|
+
throw new Error(`refusing to remove the main checkout: ${path}`);
|
|
89
|
+
}
|
|
90
|
+
if (state.dirty && !force) {
|
|
91
|
+
throw new Error(`${path} has uncommitted changes: pass --force to remove it anyway`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/** Whether `inner` is `outer` itself or lies below it. */
|
|
95
|
+
function contains(outer, inner) {
|
|
96
|
+
const base = resolve(outer);
|
|
97
|
+
const nested = resolve(inner);
|
|
98
|
+
return nested === base || nested.startsWith(`${base}${sep}`);
|
|
99
|
+
}
|
package/dist/identity.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { basename } from "node:path";
|
|
2
|
+
/** Runs of anything tmux, a path, or a branch-derived name could choke on. */
|
|
3
|
+
const unsafeRun = /[^A-Za-z0-9_-]+/g;
|
|
4
|
+
const edgeDashes = /^-+|-+$/g;
|
|
2
5
|
export function sanitizeComponent(value, label) {
|
|
3
|
-
const sanitized = value.replace(
|
|
6
|
+
const sanitized = value.replace(unsafeRun, "-").replace(edgeDashes, "");
|
|
4
7
|
if (sanitized.length === 0) {
|
|
5
8
|
throw new Error(`${label} must contain a letter, number, _ or -`);
|
|
6
9
|
}
|
package/dist/install-prompt.js
CHANGED
|
@@ -67,6 +67,13 @@ export function createClackPrompter({ columns }) {
|
|
|
67
67
|
});
|
|
68
68
|
return isCancel(answer) ? cancelled() : answer;
|
|
69
69
|
},
|
|
70
|
+
async mouse({ enabled }) {
|
|
71
|
+
const answer = await confirm({
|
|
72
|
+
message: "Open paths in the editor on Option+click?",
|
|
73
|
+
initialValue: enabled,
|
|
74
|
+
});
|
|
75
|
+
return isCancel(answer) ? cancelled() : answer;
|
|
76
|
+
},
|
|
70
77
|
async replace({ path }) {
|
|
71
78
|
const answer = await confirm({
|
|
72
79
|
message: `Replace ${path}?`,
|
package/dist/install.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ export interface InstallRequest {
|
|
|
6
6
|
layout?: string;
|
|
7
7
|
project?: boolean;
|
|
8
8
|
agents?: readonly AgentId[];
|
|
9
|
+
/** Option+click in the global config: on, off, or left as it is when undefined. */
|
|
10
|
+
mouse?: boolean;
|
|
9
11
|
force?: boolean;
|
|
10
12
|
yes?: boolean;
|
|
11
13
|
}
|
|
@@ -31,6 +33,10 @@ export interface InstallPrompter {
|
|
|
31
33
|
agents: (options: {
|
|
32
34
|
candidates: AgentCandidate[];
|
|
33
35
|
}) => Promise<readonly AgentId[] | undefined>;
|
|
36
|
+
/** `enabled` is the current setting, offered as the default answer. */
|
|
37
|
+
mouse: (options: {
|
|
38
|
+
enabled: boolean;
|
|
39
|
+
}) => Promise<boolean | undefined>;
|
|
34
40
|
/** Asked only when a config is already there and no --force settled it. */
|
|
35
41
|
replace: (options: {
|
|
36
42
|
path: string;
|
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
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Exec } from "./exec.js";
|
|
2
|
+
import { type ProcessEntry, type SessionScanner } from "./process-scan.js";
|
|
3
|
+
export interface DarwinScanDependencies {
|
|
4
|
+
exec: Exec;
|
|
5
|
+
uid: number;
|
|
6
|
+
}
|
|
7
|
+
export interface DarwinProcessRow extends ProcessEntry {
|
|
8
|
+
/** The command line, followed by the environment when `ps -E` could read it. */
|
|
9
|
+
details: string;
|
|
10
|
+
}
|
|
11
|
+
export declare function parseDarwinProcesses(stdout: string): DarwinProcessRow[];
|
|
12
|
+
export declare function parseDarwinArguments(stdout: string): Map<number, string>;
|
|
13
|
+
/**
|
|
14
|
+
* True when `text` assigns `needle`, meaning the match is a whole space-separated
|
|
15
|
+
* token. A command line that merely mentions the variable never qualifies.
|
|
16
|
+
*/
|
|
17
|
+
export declare function containsAssignment(text: string, needle: string): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* macOS has no `/proc`, so the environment comes from `ps -E`, which appends it to
|
|
20
|
+
* the command column. A process that only mentions the label on its command line
|
|
21
|
+
* would look identical, so every candidate is re-read without `-E` and the command
|
|
22
|
+
* line is stripped off before the label is looked up in what remains.
|
|
23
|
+
*/
|
|
24
|
+
export declare function createDarwinScanner(dependencies: DarwinScanDependencies): SessionScanner;
|