@termwire/tmux 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ # @termwire/tmux
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 5d73919: Initial public release with compiled Bun packages and npm-ready metadata.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Termwire contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # @termwire/tmux
2
+
3
+ ```bash
4
+ bun add @termwire/tmux
5
+ ```
6
+
7
+ ```ts
8
+ import { createTmux } from "@termwire/tmux";
9
+
10
+ const tmux = createTmux();
11
+ ```
12
+
13
+ A thin, typed adapter for tmux 3.2. Create it with `createTmux({ exec, env })`; production uses
14
+ `Bun.spawn`, while tests can inject a fake `exec` without requiring a tmux binary.
15
+
16
+ ## API
17
+
18
+ The factory exposes `hasSession`, `newSession`, `newWindow`, `splitPane`, `sendKeys`,
19
+ `selectWindow`, `selectPane`, and `attach`. Attach uses `attach-session` outside tmux and
20
+ `switch-client` when `TMUX` is set.
21
+
22
+ ## Boundary
23
+
24
+ This package owns tmux commands only. The CLI owns runtime layout interpretation and editor roles;
25
+ no OpenCode, workspace orchestration, or Neovim logic belongs here.
@@ -0,0 +1,2 @@
1
+ import { type Exec } from "./process";
2
+ export declare function attach(exec: Exec, env: Record<string, string | undefined>, session: string): Promise<void>;
package/dist/client.js ADDED
@@ -0,0 +1,14 @@
1
+ import { CommandError, execute } from "./process";
2
+ import { assertNotEmpty } from "./validation";
3
+ export async function attach(exec, env, session) {
4
+ assertNotEmpty("session", session);
5
+ const internal = env.TMUX !== undefined && env.TMUX.length > 0;
6
+ const command = internal
7
+ ? ["tmux", "switch-client", "-t", `=${session}`]
8
+ : ["tmux", "attach-session", "-t", `=${session}`];
9
+ const options = internal ? undefined : { stdio: "inherit" };
10
+ const execution = await execute(exec, command, options);
11
+ if (execution.exitCode !== 0) {
12
+ throw CommandError.from(command, execution);
13
+ }
14
+ }
@@ -0,0 +1,8 @@
1
+ export interface WindowPaneIds {
2
+ windowId: string;
3
+ paneId: string;
4
+ }
5
+ export declare function appendEnvironment(argv: string[], environment?: Record<string, string | undefined>): void;
6
+ export declare function appendCommand(argv: string[], command?: readonly string[]): void;
7
+ export declare function readPaneId(stdout: string): string;
8
+ export declare function readWindowPaneIds(stdout: string): WindowPaneIds;
@@ -0,0 +1,34 @@
1
+ import { assertNotEmpty, ValidationError } from "./validation";
2
+ export function appendEnvironment(argv, environment) {
3
+ if (!environment)
4
+ return;
5
+ for (const [name, value] of Object.entries(environment)) {
6
+ if (value !== undefined) {
7
+ argv.push("-e", `${name}=${value}`);
8
+ }
9
+ }
10
+ }
11
+ export function appendCommand(argv, command) {
12
+ if (!command)
13
+ return;
14
+ if (command.length === 0) {
15
+ throw new ValidationError("command", "command must not be empty");
16
+ }
17
+ assertNotEmpty("command", command[0]);
18
+ argv.push(...command);
19
+ }
20
+ export function readPaneId(stdout) {
21
+ const paneId = stdout.trim();
22
+ assertNotEmpty("paneId", paneId);
23
+ return paneId;
24
+ }
25
+ export function readWindowPaneIds(stdout) {
26
+ const values = stdout.trim().split("\t");
27
+ if (values.length !== 2) {
28
+ throw new ValidationError("windowPaneIds", "expected a window and pane id");
29
+ }
30
+ const [windowId, paneId] = values.map((value) => value.trim());
31
+ assertNotEmpty("windowId", windowId);
32
+ assertNotEmpty("paneId", paneId);
33
+ return { windowId, paneId };
34
+ }
@@ -0,0 +1,21 @@
1
+ import { respawnPane, sendKeys, splitPane } from "./pane";
2
+ import { type Exec } from "./process";
3
+ import { newSession } from "./session";
4
+ import { newWindow } from "./window";
5
+ export interface CreateTmuxOptions {
6
+ exec?: Exec;
7
+ env?: Record<string, string | undefined>;
8
+ }
9
+ export declare function createTmux({ exec, env }?: CreateTmuxOptions): {
10
+ hasSession: (session: string) => Promise<boolean>;
11
+ newSession: (options: Parameters<typeof newSession>[1]) => Promise<import("./command").WindowPaneIds>;
12
+ newWindow: (options: Parameters<typeof newWindow>[1]) => Promise<import("./command").WindowPaneIds>;
13
+ setEnvironment: (session: string, key: string, value: string) => Promise<void>;
14
+ killSession: (session: string) => Promise<void>;
15
+ respawnPane: (options: Parameters<typeof respawnPane>[1]) => Promise<void>;
16
+ splitPane: (options: Parameters<typeof splitPane>[1]) => Promise<string>;
17
+ sendKeys: (target: string, keys: readonly string[], options?: Parameters<typeof sendKeys>[3]) => Promise<void>;
18
+ selectWindow: (target: string) => Promise<void>;
19
+ selectPane: (target: string) => Promise<void>;
20
+ attach: (session: string) => Promise<void>;
21
+ };
@@ -0,0 +1,20 @@
1
+ import { attach } from "./client";
2
+ import { respawnPane, selectPane, sendKeys, splitPane } from "./pane";
3
+ import { bunExec } from "./process";
4
+ import { hasSession, killSession, newSession, setEnvironment } from "./session";
5
+ import { newWindow, selectWindow } from "./window";
6
+ export function createTmux({ exec = bunExec, env = process.env } = {}) {
7
+ return {
8
+ hasSession: (session) => hasSession(exec, session),
9
+ newSession: (options) => newSession(exec, options),
10
+ newWindow: (options) => newWindow(exec, options),
11
+ setEnvironment: (session, key, value) => setEnvironment(exec, session, key, value),
12
+ killSession: (session) => killSession(exec, session),
13
+ respawnPane: (options) => respawnPane(exec, options),
14
+ splitPane: (options) => splitPane(exec, options),
15
+ sendKeys: (target, keys, options) => sendKeys(exec, target, keys, options),
16
+ selectWindow: (target) => selectWindow(exec, target),
17
+ selectPane: (target) => selectPane(exec, target),
18
+ attach: (session) => attach(exec, env, session),
19
+ };
20
+ }
@@ -0,0 +1,11 @@
1
+ export type { WindowPaneIds } from "./command";
2
+ export type { CreateTmuxOptions } from "./create-tmux";
3
+ export { createTmux } from "./create-tmux";
4
+ export type { RespawnPaneOptions, SplitPaneOptions } from "./pane";
5
+ export { respawnPane } from "./pane";
6
+ export type { Exec, ExecOptions, ExecResult } from "./process";
7
+ export { CommandError } from "./process";
8
+ export type { NewSessionOptions } from "./session";
9
+ export { killSession, setEnvironment } from "./session";
10
+ export { ValidationError } from "./validation";
11
+ export type { NewWindowOptions } from "./window";
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export { createTmux } from "./create-tmux";
2
+ export { respawnPane } from "./pane";
3
+ export { CommandError } from "./process";
4
+ export { killSession, setEnvironment } from "./session";
5
+ export { ValidationError } from "./validation";
package/dist/pane.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ import { type Exec } from "./process";
2
+ export interface SplitPaneOptions {
3
+ target: string;
4
+ direction: "horizontal" | "vertical";
5
+ sizePercent?: number;
6
+ cwd?: string;
7
+ command?: readonly string[];
8
+ environment?: Record<string, string | undefined>;
9
+ }
10
+ export interface RespawnPaneOptions {
11
+ target: string;
12
+ cwd?: string;
13
+ command: readonly string[];
14
+ environment?: Record<string, string | undefined>;
15
+ }
16
+ export declare function respawnPane(exec: Exec, options: RespawnPaneOptions): Promise<void>;
17
+ export declare function splitPane(exec: Exec, options: SplitPaneOptions): Promise<string>;
18
+ export declare function sendKeys(exec: Exec, target: string, keys: readonly string[], options?: {
19
+ literal?: boolean;
20
+ }): Promise<void>;
21
+ export declare function selectPane(exec: Exec, target: string): Promise<void>;
package/dist/pane.js ADDED
@@ -0,0 +1,69 @@
1
+ import { appendCommand, appendEnvironment, readPaneId } from "./command";
2
+ import { CommandError, execute } from "./process";
3
+ import { assertNotEmpty, ValidationError } from "./validation";
4
+ export async function respawnPane(exec, options) {
5
+ assertNotEmpty("target", options.target);
6
+ if (options.cwd !== undefined)
7
+ assertNotEmpty("cwd", options.cwd);
8
+ const command = ["tmux", "respawn-pane", "-k", "-t", options.target];
9
+ if (options.cwd !== undefined)
10
+ command.push("-c", options.cwd);
11
+ appendEnvironment(command, options.environment);
12
+ appendCommand(command, options.command);
13
+ const execution = await execute(exec, command);
14
+ if (execution.exitCode !== 0) {
15
+ throw CommandError.from(command, execution);
16
+ }
17
+ }
18
+ export async function splitPane(exec, options) {
19
+ assertNotEmpty("target", options.target);
20
+ if (options.cwd !== undefined)
21
+ assertNotEmpty("cwd", options.cwd);
22
+ if (options.sizePercent !== undefined &&
23
+ (!Number.isInteger(options.sizePercent) || options.sizePercent < 1 || options.sizePercent > 100)) {
24
+ throw new ValidationError("sizePercent", "sizePercent must be an integer from 1 to 100");
25
+ }
26
+ const command = [
27
+ "tmux",
28
+ "split-window",
29
+ "-d",
30
+ "-t",
31
+ options.target,
32
+ options.direction === "horizontal" ? "-h" : "-v",
33
+ ...(options.sizePercent === undefined ? [] : ["-p", String(options.sizePercent)]),
34
+ "-P",
35
+ "-F",
36
+ "#{pane_id}",
37
+ ];
38
+ if (options.cwd !== undefined)
39
+ command.push("-c", options.cwd);
40
+ appendEnvironment(command, options.environment);
41
+ appendCommand(command, options.command);
42
+ const execution = await execute(exec, command);
43
+ if (execution.exitCode !== 0) {
44
+ throw CommandError.from(command, execution);
45
+ }
46
+ return readPaneId(execution.stdout);
47
+ }
48
+ export async function sendKeys(exec, target, keys, options = {}) {
49
+ assertNotEmpty("target", target);
50
+ if (keys.length === 0) {
51
+ throw new ValidationError("keys", "keys must not be empty");
52
+ }
53
+ const command = ["tmux", "send-keys", "-t", target];
54
+ if (options.literal)
55
+ command.push("-l", "--");
56
+ command.push(...keys);
57
+ const execution = await execute(exec, command);
58
+ if (execution.exitCode !== 0) {
59
+ throw CommandError.from(command, execution);
60
+ }
61
+ }
62
+ export async function selectPane(exec, target) {
63
+ assertNotEmpty("target", target);
64
+ const command = ["tmux", "select-pane", "-t", target];
65
+ const execution = await execute(exec, command);
66
+ if (execution.exitCode !== 0) {
67
+ throw CommandError.from(command, execution);
68
+ }
69
+ }
@@ -0,0 +1,18 @@
1
+ export interface ExecResult {
2
+ exitCode: number;
3
+ stdout: string;
4
+ stderr: string;
5
+ }
6
+ export interface ExecOptions {
7
+ stdio?: "inherit";
8
+ }
9
+ export type Exec = (argv: readonly string[], options?: ExecOptions) => Promise<ExecResult>;
10
+ export declare class CommandError extends Error {
11
+ readonly argv: readonly string[];
12
+ readonly exitCode: number | null;
13
+ readonly stderr: string;
14
+ constructor(argv: readonly string[], exitCode: number | null, stderr: string, options?: ErrorOptions);
15
+ static from(argv: readonly string[], result: ExecResult): CommandError;
16
+ }
17
+ export declare function execute(exec: Exec, argv: readonly string[], options?: ExecOptions): Promise<ExecResult>;
18
+ export declare function bunExec(argv: readonly string[], options?: ExecOptions): Promise<ExecResult>;
@@ -0,0 +1,40 @@
1
+ export class CommandError extends Error {
2
+ argv;
3
+ exitCode;
4
+ stderr;
5
+ constructor(argv, exitCode, stderr, options) {
6
+ const status = exitCode === null ? "unknown" : String(exitCode);
7
+ const details = stderr.trim();
8
+ super(`Command failed (exit ${status}): ${argv.join(" ")}${details ? `: ${details}` : ""}`, options);
9
+ this.argv = argv;
10
+ this.exitCode = exitCode;
11
+ this.stderr = stderr;
12
+ this.name = "CommandError";
13
+ }
14
+ static from(argv, result) {
15
+ return new CommandError(argv, result.exitCode, result.stderr);
16
+ }
17
+ }
18
+ export async function execute(exec, argv, options) {
19
+ try {
20
+ return await exec(argv, options);
21
+ }
22
+ catch (cause) {
23
+ throw new CommandError(argv, null, "", { cause });
24
+ }
25
+ }
26
+ export async function bunExec(argv, options) {
27
+ const inherited = options?.stdio === "inherit";
28
+ const proc = Bun.spawn([...argv], inherited
29
+ ? { stdin: "inherit", stdout: "inherit", stderr: "inherit" }
30
+ : { stdout: "pipe", stderr: "pipe" });
31
+ const exitCode = await proc.exited;
32
+ if (inherited) {
33
+ return { exitCode, stdout: "", stderr: "" };
34
+ }
35
+ const [stdout, stderr] = await Promise.all([
36
+ new Response(proc.stdout).text(),
37
+ new Response(proc.stderr).text(),
38
+ ]);
39
+ return { exitCode, stdout, stderr };
40
+ }
@@ -0,0 +1,13 @@
1
+ import { type WindowPaneIds } from "./command";
2
+ import { type Exec } from "./process";
3
+ export interface NewSessionOptions {
4
+ session: string;
5
+ name?: string;
6
+ cwd?: string;
7
+ command?: readonly string[];
8
+ environment?: Record<string, string | undefined>;
9
+ }
10
+ export declare function hasSession(exec: Exec, session: string): Promise<boolean>;
11
+ export declare function setEnvironment(exec: Exec, session: string, key: string, value: string): Promise<void>;
12
+ export declare function killSession(exec: Exec, session: string): Promise<void>;
13
+ export declare function newSession(exec: Exec, options: NewSessionOptions): Promise<WindowPaneIds>;
@@ -0,0 +1,69 @@
1
+ import { appendCommand, appendEnvironment, readWindowPaneIds } from "./command";
2
+ import { CommandError, execute } from "./process";
3
+ import { assertNotEmpty } from "./validation";
4
+ export async function hasSession(exec, session) {
5
+ assertNotEmpty("session", session);
6
+ const command = ["tmux", "has-session", "-t", `=${session}`];
7
+ const execution = await execute(exec, command);
8
+ if (execution.exitCode === 0)
9
+ return true;
10
+ if (execution.exitCode === 1)
11
+ return false;
12
+ throw CommandError.from(command, execution);
13
+ }
14
+ export async function setEnvironment(exec, session, key, value) {
15
+ assertNotEmpty("session", session);
16
+ assertNotEmpty("key", key);
17
+ assertNotEmpty("value", value);
18
+ const command = ["tmux", "set-environment", "-t", `=${session}`, key, value];
19
+ const execution = await execute(exec, command);
20
+ if (execution.exitCode !== 0) {
21
+ throw CommandError.from(command, execution);
22
+ }
23
+ }
24
+ export async function killSession(exec, session) {
25
+ assertNotEmpty("session", session);
26
+ const command = ["tmux", "kill-session", "-t", `=${session}`];
27
+ const execution = await execute(exec, command);
28
+ if (execution.exitCode !== 0) {
29
+ throw CommandError.from(command, execution);
30
+ }
31
+ }
32
+ export async function newSession(exec, options) {
33
+ assertNotEmpty("session", options.session);
34
+ if (options.name !== undefined)
35
+ assertNotEmpty("name", options.name);
36
+ if (options.cwd !== undefined)
37
+ assertNotEmpty("cwd", options.cwd);
38
+ const command = [
39
+ "tmux",
40
+ "new-session",
41
+ "-d",
42
+ "-s",
43
+ options.session,
44
+ ...(options.name === undefined ? [] : ["-n", options.name]),
45
+ "-P",
46
+ "-F",
47
+ "#{window_id}\t#{pane_id}",
48
+ ];
49
+ if (options.cwd !== undefined)
50
+ command.push("-c", options.cwd);
51
+ appendEnvironment(command, options.environment);
52
+ appendCommand(command, options.command);
53
+ const execution = await execute(exec, command);
54
+ if (execution.exitCode !== 0) {
55
+ throw CommandError.from(command, execution);
56
+ }
57
+ try {
58
+ return readWindowPaneIds(execution.stdout);
59
+ }
60
+ catch (error) {
61
+ try {
62
+ await killSession(exec, options.session);
63
+ }
64
+ catch {
65
+ // Preserve the original output parsing error.
66
+ }
67
+ throw error;
68
+ }
69
+ }
@@ -0,0 +1,5 @@
1
+ export declare class ValidationError extends TypeError {
2
+ readonly field: string;
3
+ constructor(field: string, message: string);
4
+ }
5
+ export declare function assertNotEmpty(field: string, value: string): void;
@@ -0,0 +1,13 @@
1
+ export class ValidationError extends TypeError {
2
+ field;
3
+ constructor(field, message) {
4
+ super(message);
5
+ this.field = field;
6
+ this.name = "ValidationError";
7
+ }
8
+ }
9
+ export function assertNotEmpty(field, value) {
10
+ if (value.trim().length === 0) {
11
+ throw new ValidationError(field, `${field} must not be empty`);
12
+ }
13
+ }
@@ -0,0 +1,11 @@
1
+ import { type WindowPaneIds } from "./command";
2
+ import { type Exec } from "./process";
3
+ export interface NewWindowOptions {
4
+ target: string;
5
+ name?: string;
6
+ cwd?: string;
7
+ command?: readonly string[];
8
+ environment?: Record<string, string | undefined>;
9
+ }
10
+ export declare function newWindow(exec: Exec, options: NewWindowOptions): Promise<WindowPaneIds>;
11
+ export declare function selectWindow(exec: Exec, target: string): Promise<void>;
package/dist/window.js ADDED
@@ -0,0 +1,38 @@
1
+ import { appendCommand, appendEnvironment, readWindowPaneIds } from "./command";
2
+ import { CommandError, execute } from "./process";
3
+ import { assertNotEmpty } from "./validation";
4
+ export async function newWindow(exec, options) {
5
+ assertNotEmpty("target", options.target);
6
+ if (options.name !== undefined)
7
+ assertNotEmpty("name", options.name);
8
+ if (options.cwd !== undefined)
9
+ assertNotEmpty("cwd", options.cwd);
10
+ const command = [
11
+ "tmux",
12
+ "new-window",
13
+ "-d",
14
+ "-t",
15
+ `=${options.target}`,
16
+ ...(options.name === undefined ? [] : ["-n", options.name]),
17
+ "-P",
18
+ "-F",
19
+ "#{window_id}\t#{pane_id}",
20
+ ];
21
+ if (options.cwd !== undefined)
22
+ command.push("-c", options.cwd);
23
+ appendEnvironment(command, options.environment);
24
+ appendCommand(command, options.command);
25
+ const execution = await execute(exec, command);
26
+ if (execution.exitCode !== 0) {
27
+ throw CommandError.from(command, execution);
28
+ }
29
+ return readWindowPaneIds(execution.stdout);
30
+ }
31
+ export async function selectWindow(exec, target) {
32
+ assertNotEmpty("target", target);
33
+ const command = ["tmux", "select-window", "-t", target];
34
+ const execution = await execute(exec, command);
35
+ if (execution.exitCode !== 0) {
36
+ throw CommandError.from(command, execution);
37
+ }
38
+ }
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@termwire/tmux",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js",
11
+ "default": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "README.md",
17
+ "LICENSE",
18
+ "CHANGELOG.md"
19
+ ],
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "license": "MIT",
24
+ "engines": {
25
+ "bun": ">=1.3.14"
26
+ },
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "git+https://github.com/max-konin/termwire.git",
30
+ "directory": "packages/tmux"
31
+ },
32
+ "homepage": "https://github.com/max-konin/termwire#readme",
33
+ "bugs": {
34
+ "url": "https://github.com/max-konin/termwire/issues"
35
+ }
36
+ }