@termwire/tmux 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 CHANGED
@@ -1,5 +1,89 @@
1
1
  # @termwire/tmux
2
2
 
3
+ ## 0.7.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 59dbb4d: Option+click on a path in any workspace pane opens it in the workspace Neovim at that line. Turn it on with `termwire install --mouse` or `"mouse": { "openPath": "M-MouseDown1Pane" }` in the global config; `up` installs the tmux binding. The tmux adapter gains `listKeys`, `bindKey`, `unbindKey` and `setSessionOption`.
8
+
9
+ ## 0.6.0
10
+
11
+ ### Minor Changes
12
+
13
+ - cfdcc19: `termwire ls` and `termwire down`: the workspaces you have, and a way to put one away.
14
+
15
+ A tool that creates workspaces owes you a way to see them and a way to remove them.
16
+ Until now tearing one down was `tmux kill-session` plus `git worktree remove`, typed
17
+ by hand, and there was nothing at all that answered "what is running where".
18
+
19
+ ```
20
+ $ termwire ls
21
+ SESSION A BRANCH DIRECTORY PROCS RSS
22
+ termwire-dev * master ~/projects/termwire 7 1.2G
23
+ termwire-demo demo ~/projects/termwire-demo 4 780M
24
+ ```
25
+
26
+ One row per workspace, printed once and then done: `ls` is not a monitor. `A` marks an
27
+ attached session, `BRANCH` is the Git branch of the session directory — a short sha on a
28
+ detached `HEAD`, `-` outside a repository — and `PROCS` and `RSS` count the processes
29
+ still carrying `TERMWIRE_SESSION=<session>`, from the same scan the reap uses, so a
30
+ background agent job is counted as well as a pane. A session whose directory is gone is
31
+ listed and marked `(missing)` rather than crashing the command, and no workspaces prints
32
+ `no termwire workspaces` and exits 0. `--json` prints the same rows for machines, with
33
+ absolute directories.
34
+
35
+ A session counts as a workspace when its tmux session environment carries
36
+ `TERMWIRE_SESSION`, never because its name looks like `<project>-<name>`: you pick the
37
+ name and `up` sets the label, so a session of your own is never listed by its name alone.
38
+ Nothing is read from disk — the list is derived from tmux and Git, so identity stays
39
+ stateless and there is no file to go stale.
40
+
41
+ ```bash
42
+ termwire down dev # kill the workspace session
43
+ termwire down # kill the workspace this shell is in
44
+ termwire down dev -w # and remove the worktree it sits in
45
+ termwire down dev -w --force # even with uncommitted changes
46
+ ```
47
+
48
+ `down` resolves `[name]` exactly as `up` does, so `up dev` and `down dev` always mean one
49
+ session, and it hunts no processes: the `session-closed` hook already reaps that session.
50
+
51
+ Without a name the target is the workspace the command runs in, read from the inherited
52
+ `TERMWIRE_SESSION` — the same environment-based identity `open` uses. Inside a worktree
53
+ workspace that bare form is the only one that works, because there the Git root is the
54
+ worktree, so `down feat` would look for `<worktree>-feat` and find nothing. Outside a
55
+ workspace a bare `down` fails with `not inside a termwire workspace`.
56
+
57
+ `-w` removes the worktree the session was created in, and refuses the main checkout, the
58
+ directory the command is running in, and a worktree holding uncommitted changes unless
59
+ `--force` is given. A refusal removes nothing at all, the session included. The branch is
60
+ never deleted: a branch outlives its workspace. Ignored files are not uncommitted
61
+ changes, so a worktree carrying only `.env` or `node_modules` counts as clean and is
62
+ removed with them.
63
+
64
+ Tearing down your _own_ workspace with `-w` inverts the order: the worktree is removed
65
+ first and the session killed last, because the kill ends the process that would otherwise
66
+ do the removal — and the command steps out of the worktree before removing it, since a
67
+ spawn from a deleted working directory fails with ENOENT. The refusal to remove the
68
+ directory it runs in does not apply there: the shell standing in it dies with the session
69
+ a moment later.
70
+
71
+ The tmux adapter gained `listSessions` and `showEnvironment` on `createTmux`, and the
72
+ process scan now reports resident memory, so `ProcessEntry` carries `rss` in KiB and one
73
+ walk over the process table answers for every workspace at once.
74
+
75
+ Process scanning also became platform-agnostic above the seam. `process-scan.ts` keeps
76
+ only the `SessionScanner` type and the contract a platform has to honour, while
77
+ `process-scan-darwin.ts` and `process-scan-linux.ts` each take their own dependencies;
78
+ `runtime.ts` picks one. Supporting another platform is a new module plus a branch there,
79
+ and `reap` and `ls` do not change, because they depend on the type.
80
+
81
+ Moving the scanner reshapes `CliRuntime`: it gained `createScanner`, a lazy factory so an
82
+ unsupported platform fails the commands that scan instead of every command, and lost
83
+ `exec`, `host.platform`, `host.uid` and `fs.readdir`, which existed only to feed it. It also
84
+ gained `host.chdir`, and `ProgramDependencies` gained `ls`, `down` and `homedir` — so
85
+ anything that builds a runtime or the command set by hand has to follow.
86
+
3
87
  ## 0.5.0
4
88
 
5
89
  ### Minor Changes
@@ -1,4 +1,5 @@
1
1
  import { setHook, showHooks, unsetHook } from "./hook.js";
2
+ import { bindKey, unbindKey } from "./key.js";
2
3
  import { respawnPane, sendKeys, splitPane } from "./pane.js";
3
4
  import { type Exec } from "./process.js";
4
5
  import { newSession } from "./session.js";
@@ -9,9 +10,12 @@ export interface CreateTmuxOptions {
9
10
  }
10
11
  export declare function createTmux({ exec, env }?: CreateTmuxOptions): {
11
12
  hasSession: (session: string) => Promise<boolean>;
13
+ listSessions: () => Promise<import("./session.js").SessionSummary[]>;
14
+ showEnvironment: (session: string) => Promise<Record<string, string>>;
12
15
  newSession: (options: Parameters<typeof newSession>[1]) => Promise<import("./command.js").WindowPaneIds>;
13
16
  newWindow: (options: Parameters<typeof newWindow>[1]) => Promise<import("./command.js").WindowPaneIds>;
14
17
  setEnvironment: (session: string, key: string, value: string) => Promise<void>;
18
+ setSessionOption: (session: string, name: string, value: string) => Promise<void>;
15
19
  setSessionTitle: (session: string) => Promise<void>;
16
20
  killSession: (session: string) => Promise<void>;
17
21
  respawnPane: (options: Parameters<typeof respawnPane>[1]) => Promise<void>;
@@ -24,4 +28,7 @@ export declare function createTmux({ exec, env }?: CreateTmuxOptions): {
24
28
  showHooks: (options?: Parameters<typeof showHooks>[1]) => Promise<import("./hook.js").HookEntry[]>;
25
29
  setHook: (options: Parameters<typeof setHook>[1]) => Promise<void>;
26
30
  unsetHook: (options: Parameters<typeof unsetHook>[1]) => Promise<void>;
31
+ listKeys: (table: string) => Promise<import("./key.js").KeyBinding[]>;
32
+ bindKey: (options: Parameters<typeof bindKey>[1]) => Promise<void>;
33
+ unbindKey: (options: Parameters<typeof unbindKey>[1]) => Promise<void>;
27
34
  };
@@ -1,15 +1,19 @@
1
1
  import { attach } from "./client.js";
2
2
  import { setHook, showHooks, unsetHook } from "./hook.js";
3
+ import { bindKey, listKeys, unbindKey } from "./key.js";
3
4
  import { respawnPane, selectPane, sendKeys, splitPane } from "./pane.js";
4
5
  import { spawnExec } from "./process.js";
5
- import { hasSession, killSession, newSession, setEnvironment, setSessionTitle } from "./session.js";
6
+ import { hasSession, killSession, listSessions, newSession, setEnvironment, setSessionOption, setSessionTitle, showEnvironment, } from "./session.js";
6
7
  import { newWindow, selectLayout, selectWindow } from "./window.js";
7
8
  export function createTmux({ exec = spawnExec, env = process.env } = {}) {
8
9
  return {
9
10
  hasSession: (session) => hasSession(exec, session),
11
+ listSessions: () => listSessions(exec),
12
+ showEnvironment: (session) => showEnvironment(exec, session),
10
13
  newSession: (options) => newSession(exec, options),
11
14
  newWindow: (options) => newWindow(exec, options),
12
15
  setEnvironment: (session, key, value) => setEnvironment(exec, session, key, value),
16
+ setSessionOption: (session, name, value) => setSessionOption(exec, session, name, value),
13
17
  setSessionTitle: (session) => setSessionTitle(exec, session),
14
18
  killSession: (session) => killSession(exec, session),
15
19
  respawnPane: (options) => respawnPane(exec, options),
@@ -22,5 +26,8 @@ export function createTmux({ exec = spawnExec, env = process.env } = {}) {
22
26
  showHooks: (options) => showHooks(exec, options),
23
27
  setHook: (options) => setHook(exec, options),
24
28
  unsetHook: (options) => unsetHook(exec, options),
29
+ listKeys: (table) => listKeys(exec, table),
30
+ bindKey: (options) => bindKey(exec, options),
31
+ unbindKey: (options) => unbindKey(exec, options),
25
32
  };
26
33
  }
package/dist/hook.js CHANGED
@@ -1,5 +1,7 @@
1
1
  import { CommandError, execute } from "./process.js";
2
2
  import { assertNotEmpty } from "./validation.js";
3
+ /** `show-hooks` prints an array hook as `name[index]` and a plain one as `name`. */
4
+ const hookKey = /^([A-Za-z0-9_-]+)(?:\[(\d+)\])?$/;
3
5
  export function parseHooks(stdout) {
4
6
  const entries = [];
5
7
  for (const line of stdout.split("\n")) {
@@ -9,7 +11,7 @@ export function parseHooks(stdout) {
9
11
  const separator = trimmed.indexOf(" ");
10
12
  const key = separator === -1 ? trimmed : trimmed.slice(0, separator);
11
13
  const command = separator === -1 ? "" : trimmed.slice(separator + 1);
12
- const parsed = /^([A-Za-z0-9_-]+)(?:\[(\d+)\])?$/.exec(key);
14
+ const parsed = hookKey.exec(key);
13
15
  if (!parsed)
14
16
  continue;
15
17
  const index = parsed[2];
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@ export type { WindowPaneIds } from "./command.js";
2
2
  export type { CreateTmuxOptions } from "./create-tmux.js";
3
3
  export { createTmux } from "./create-tmux.js";
4
4
  export type { HookEntry, SetHookOptions, ShowHooksOptions, UnsetHookOptions } from "./hook.js";
5
+ export type { BindKeyOptions, KeyBinding, UnbindKeyOptions } from "./key.js";
5
6
  export type { RespawnPaneOptions, SplitPaneOptions } from "./pane.js";
6
7
  export type { Exec, ExecOptions, ExecResult } from "./process.js";
7
8
  export { CommandError } from "./process.js";
package/dist/key.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ import { type Exec } from "./process.js";
2
+ export interface KeyBinding {
3
+ key: string;
4
+ /** The command as `list-keys` prints it, re-quoted by tmux. */
5
+ command: string;
6
+ }
7
+ export interface BindKeyOptions {
8
+ table: string;
9
+ key: string;
10
+ /** One tmux command and its arguments, passed as argv so tmux never re-parses them. */
11
+ command: readonly string[];
12
+ }
13
+ export interface UnbindKeyOptions {
14
+ table: string;
15
+ key: string;
16
+ }
17
+ export declare function parseKeys(stdout: string): KeyBinding[];
18
+ export declare function listKeys(exec: Exec, table: string): Promise<KeyBinding[]>;
19
+ export declare function bindKey(exec: Exec, options: BindKeyOptions): Promise<void>;
20
+ export declare function unbindKey(exec: Exec, options: UnbindKeyOptions): Promise<void>;
package/dist/key.js ADDED
@@ -0,0 +1,43 @@
1
+ import { CommandError, execute } from "./process.js";
2
+ import { assertNotEmpty, ValidationError } from "./validation.js";
3
+ /** `list-keys` prints `bind-key [-r] -T <table> <key> <command>`, padded for alignment. */
4
+ const bindingLine = /^bind-key\s+(?:-r\s+)?-T\s+\S+\s+(\S+)\s+(.*)$/;
5
+ export function parseKeys(stdout) {
6
+ const bindings = [];
7
+ for (const line of stdout.split("\n")) {
8
+ const parsed = bindingLine.exec(line.trim());
9
+ if (parsed)
10
+ bindings.push({ key: parsed[1], command: parsed[2] });
11
+ }
12
+ return bindings;
13
+ }
14
+ export async function listKeys(exec, table) {
15
+ assertNotEmpty("table", table);
16
+ const command = ["tmux", "list-keys", "-T", table];
17
+ const execution = await execute(exec, command);
18
+ if (execution.exitCode !== 0) {
19
+ throw CommandError.from(command, execution);
20
+ }
21
+ return parseKeys(execution.stdout);
22
+ }
23
+ export async function bindKey(exec, options) {
24
+ assertNotEmpty("table", options.table);
25
+ assertNotEmpty("key", options.key);
26
+ if (options.command.length === 0) {
27
+ throw new ValidationError("command", "command must not be empty");
28
+ }
29
+ const command = ["tmux", "bind-key", "-T", options.table, options.key, ...options.command];
30
+ const execution = await execute(exec, command);
31
+ if (execution.exitCode !== 0) {
32
+ throw CommandError.from(command, execution);
33
+ }
34
+ }
35
+ export async function unbindKey(exec, options) {
36
+ assertNotEmpty("table", options.table);
37
+ assertNotEmpty("key", options.key);
38
+ const command = ["tmux", "unbind-key", "-T", options.table, options.key];
39
+ const execution = await execute(exec, command);
40
+ if (execution.exitCode !== 0) {
41
+ throw CommandError.from(command, execution);
42
+ }
43
+ }
package/dist/session.d.ts CHANGED
@@ -9,6 +9,18 @@ export interface NewSessionOptions {
9
9
  }
10
10
  export declare function hasSession(exec: Exec, session: string): Promise<boolean>;
11
11
  export declare function setEnvironment(exec: Exec, session: string, key: string, value: string): Promise<void>;
12
+ /** Sets a session option; a name starting with `@` is a user option formats can read. */
13
+ export declare function setSessionOption(exec: Exec, session: string, name: string, value: string): Promise<void>;
12
14
  export declare function setSessionTitle(exec: Exec, session: string): Promise<void>;
13
15
  export declare function killSession(exec: Exec, session: string): Promise<void>;
14
16
  export declare function newSession(exec: Exec, options: NewSessionOptions): Promise<WindowPaneIds>;
17
+ export interface SessionSummary {
18
+ name: string;
19
+ attached: boolean;
20
+ path: string;
21
+ }
22
+ export declare function parseSessionList(stdout: string): SessionSummary[];
23
+ export declare function listSessions(exec: Exec): Promise<SessionSummary[]>;
24
+ /** `show-environment` prints `NAME=value` per line, and `-NAME` for a removed one. */
25
+ export declare function parseEnvironment(stdout: string): Record<string, string>;
26
+ export declare function showEnvironment(exec: Exec, session: string): Promise<Record<string, string>>;
package/dist/session.js CHANGED
@@ -21,6 +21,16 @@ export async function setEnvironment(exec, session, key, value) {
21
21
  throw CommandError.from(command, execution);
22
22
  }
23
23
  }
24
+ /** Sets a session option; a name starting with `@` is a user option formats can read. */
25
+ export async function setSessionOption(exec, session, name, value) {
26
+ assertNotEmpty("session", session);
27
+ assertNotEmpty("name", name);
28
+ const command = ["tmux", "set-option", "-t", `=${session}:`, name, value];
29
+ const execution = await execute(exec, command);
30
+ if (execution.exitCode !== 0) {
31
+ throw CommandError.from(command, execution);
32
+ }
33
+ }
24
34
  export async function setSessionTitle(exec, session) {
25
35
  assertNotEmpty("session", session);
26
36
  const setTitlesCommand = ["tmux", "set-option", "-t", session, "set-titles", "on"];
@@ -87,3 +97,59 @@ export async function newSession(exec, options) {
87
97
  throw error;
88
98
  }
89
99
  }
100
+ /** The session path comes last, so a tab inside it cannot shift the other fields. */
101
+ const sessionFormat = "#{session_name}\t#{session_attached}\t#{session_path}";
102
+ export function parseSessionList(stdout) {
103
+ const sessions = [];
104
+ for (const line of stdout.split("\n")) {
105
+ const fields = line.split("\t");
106
+ // Three fields or it is not a session row; the path keeps any tab of its own.
107
+ if (fields.length < 3)
108
+ continue;
109
+ const [name, attached, ...path] = fields;
110
+ if (name.length === 0)
111
+ continue;
112
+ sessions.push({ name, attached: Number(attached) > 0, path: path.join("\t") });
113
+ }
114
+ return sessions;
115
+ }
116
+ export async function listSessions(exec) {
117
+ const command = ["tmux", "list-sessions", "-F", sessionFormat];
118
+ const execution = await execute(exec, command);
119
+ // No server is no sessions, not a failure. The message differs by version — "no
120
+ // server running on <socket>" on older tmux, "error connecting to <socket> (No such
121
+ // file or directory)" on 3.6 — so the exit code decides. The cost is that an
122
+ // unreadable socket exits 1 the same way and reads as an empty list.
123
+ if (execution.exitCode === 1)
124
+ return [];
125
+ if (execution.exitCode !== 0) {
126
+ throw CommandError.from(command, execution);
127
+ }
128
+ return parseSessionList(execution.stdout);
129
+ }
130
+ /** `show-environment` prints `NAME=value` per line, and `-NAME` for a removed one. */
131
+ export function parseEnvironment(stdout) {
132
+ const environment = {};
133
+ for (const line of stdout.split("\n")) {
134
+ if (line.startsWith("-"))
135
+ continue;
136
+ const separator = line.indexOf("=");
137
+ if (separator <= 0)
138
+ continue;
139
+ environment[line.slice(0, separator)] = line.slice(separator + 1);
140
+ }
141
+ return environment;
142
+ }
143
+ export async function showEnvironment(exec, session) {
144
+ assertNotEmpty("session", session);
145
+ const command = ["tmux", "show-environment", "-t", `=${session}`];
146
+ const execution = await execute(exec, command);
147
+ // Exit 1 is "no such session": it closed between being listed and being read, and a
148
+ // session that no longer exists has no environment rather than an error.
149
+ if (execution.exitCode === 1)
150
+ return {};
151
+ if (execution.exitCode !== 0) {
152
+ throw CommandError.from(command, execution);
153
+ }
154
+ return parseEnvironment(execution.stdout);
155
+ }
package/package.json CHANGED
@@ -1,6 +1,16 @@
1
1
  {
2
2
  "name": "@termwire/tmux",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
+ "description": "A thin, typed adapter for tmux 3.2 or newer.",
5
+ "keywords": [
6
+ "tmux",
7
+ "adapter",
8
+ "terminal",
9
+ "session",
10
+ "pane",
11
+ "typescript",
12
+ "cli"
13
+ ],
4
14
  "type": "module",
5
15
  "main": "./dist/index.js",
6
16
  "types": "./dist/index.d.ts",