@mak-98/tmux-ts 0.0.0-stage → 0.1.1

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/README.md CHANGED
@@ -1,3 +1,87 @@
1
- # Temporary Holding Version
1
+ # tmux-ts
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A TypeScript package for splitting the tmux pane containing a Node.js application and running a command in the new pane.
4
+
5
+ ## Requirements
6
+
7
+ - Node.js
8
+ - tmux installed and available on `PATH`
9
+ - The application must run inside a reachable tmux pane
10
+
11
+ ## Usage
12
+
13
+ Build the package, then install it into your application. For local development:
14
+
15
+ ```sh
16
+ # In the tmux-ts repository
17
+ npm install
18
+ npm run build
19
+
20
+ # In your application
21
+ npm install /path/to/tmux-ts
22
+ ```
23
+
24
+ Import `split` to open a new pane:
25
+
26
+ ```ts
27
+ import { split } from "tmux-ts";
28
+
29
+ const paneId = await split(
30
+ { direction: "right", focus: true },
31
+ { command: "nvim", args: ["./example.txt"] },
32
+ );
33
+
34
+ console.log(`Opened pane ${paneId}`);
35
+ ```
36
+
37
+ The original application stays running in its pane. The returned string is the new pane's tmux ID, such as `%3`.
38
+
39
+ ### Options
40
+
41
+ `split` takes two arguments:
42
+
43
+ - **Pane options**
44
+ - `direction`: `"left"`, `"right"`, `"up"`, or `"down"`.
45
+ - `focus`: Whether to focus the new pane. Defaults to `false`.
46
+ - **Terminal options**
47
+ - `command`: The executable to run.
48
+ - `args`: Optional command arguments.
49
+ - `cwd`: Working directory for the new pane. Defaults to the current process's working directory.
50
+
51
+ ### Errors
52
+
53
+ `split` throws an error named `SplitError` with one of these `code` values:
54
+
55
+ | Code | Meaning |
56
+ | --------------------- | ------------------------------------------------------------------ |
57
+ | `not-installed` | The tmux executable was not found. |
58
+ | `not-in-tmux` | The process does not have the expected tmux environment variables. |
59
+ | `pane-unreachable` | The process's tmux pane could not be reached. |
60
+ | `status-check-failed` | An unexpected error occurred while checking tmux status. |
61
+ | `split-failed` | The tmux split command failed. |
62
+
63
+ The underlying error is available as `cause` when applicable. A successful split returns a pane ID; it does not guarantee that the command running in the new pane will remain running.
64
+
65
+ ## Development CLI
66
+
67
+ From inside tmux, run:
68
+
69
+ ```sh
70
+ npm start
71
+ ```
72
+
73
+ This checks the current pane and prints its ID. To open a focused zsh pane on the right:
74
+
75
+ ```sh
76
+ npm start -- split
77
+ ```
78
+
79
+ Build the package and its TypeScript declarations with:
80
+
81
+ ```sh
82
+ npm run build
83
+ ```
84
+
85
+ ## License
86
+
87
+ MIT
package/dist/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/dist/cli.js ADDED
@@ -0,0 +1,18 @@
1
+ import { split } from "./index.js";
2
+ import { getPaneStatus } from "./infrastructure/get-pane-status.js";
3
+ /**
4
+ * File for testing. `index.ts` is the package entry point.
5
+ */
6
+ const status = await getPaneStatus();
7
+ if (!status.available) {
8
+ console.error(`tmux unavailable: ${status.reason}`);
9
+ process.exitCode = 1;
10
+ }
11
+ else if (process.argv[2] === "split") {
12
+ const paneId = await split({ direction: "right", focus: true }, { command: "zsh" });
13
+ console.log(`Opened pane ${paneId}`);
14
+ }
15
+ else {
16
+ console.log(`Running in tmux pane ${status.paneId}`);
17
+ console.log("Run with 'split' to open a zsh pane on the right.");
18
+ }
@@ -0,0 +1,6 @@
1
+ export type PaneUnavailableReason = "not-installed" | "not-in-tmux" | "pane-unreachable";
2
+ export type SplitError = Error & {
3
+ name: "SplitError";
4
+ code: PaneUnavailableReason | "status-check-failed" | "split-failed";
5
+ };
6
+ export declare function createSplitError(code: SplitError["code"], cause?: unknown): SplitError;
@@ -0,0 +1,3 @@
1
+ export function createSplitError(code, cause) {
2
+ return Object.assign(new Error(`Cannot split tmux pane: ${code}`, { cause }), { name: "SplitError", code });
3
+ }
@@ -0,0 +1,5 @@
1
+ export type PaneOptions = {
2
+ direction: "left" | "right" | "up" | "down";
3
+ focus?: boolean;
4
+ ratio?: number;
5
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ import type { PaneUnavailableReason } from "./errors/split-error.js";
2
+ export type PaneStatus = {
3
+ available: true;
4
+ paneId: string;
5
+ } | {
6
+ available: false;
7
+ reason: PaneUnavailableReason;
8
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ export type ShellAction = {
2
+ command: string;
3
+ args?: string[];
4
+ cwd?: string;
5
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ export type TerminalOptions = {
2
+ command: string;
3
+ args?: string[];
4
+ cwd?: string;
5
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,20 @@
1
+ import { ShellAction } from "./domain/shell-action.js";
2
+ import { PaneOptions } from "./domain/pane-options.js";
3
+ /**
4
+ * Splits the pane containing this process and runs a command in the new pane.
5
+ *
6
+ * @param options - Split direction, focus behavior, and optional size ratio.
7
+ * Focus defaults to false. The ratio specifies the fraction of the original
8
+ * pane allocated to the new pane and must be finite and strictly between 0
9
+ * and 1. It is rounded to a whole percentage and clamped to 1–99%; tmux
10
+ * allocates space in cells, so the resulting size is approximate. If omitted,
11
+ * tmux uses its default split size.
12
+ * @param action - Command, arguments, and working directory for the new pane.
13
+ * The working directory defaults to the current process directory.
14
+ * @returns The new pane's tmux ID, such as "%3".
15
+ * @throws {SplitError} If the status check or split fails, including when the
16
+ * pane is too small for the requested split.
17
+ * @throws {RangeError} If the ratio is not finite or is outside the exclusive
18
+ * range (0, 1).
19
+ */
20
+ export declare function split(options: PaneOptions, action: ShellAction): Promise<string>;
package/dist/index.js ADDED
@@ -0,0 +1,79 @@
1
+ import { execFile } from "node:child_process";
2
+ import { promisify } from "node:util";
3
+ import { getPaneStatus } from "./infrastructure/get-pane-status.js";
4
+ import { createSplitError } from "./domain/errors/split-error.js";
5
+ async function getPaneId() {
6
+ let status;
7
+ try {
8
+ status = await getPaneStatus();
9
+ }
10
+ catch (error) {
11
+ throw createSplitError("status-check-failed", error);
12
+ }
13
+ if (!status.available) {
14
+ throw createSplitError(status.reason);
15
+ }
16
+ return status.paneId;
17
+ }
18
+ /**
19
+ * Splits the pane containing this process and runs a command in the new pane.
20
+ *
21
+ * @param options - Split direction, focus behavior, and optional size ratio.
22
+ * Focus defaults to false. The ratio specifies the fraction of the original
23
+ * pane allocated to the new pane and must be finite and strictly between 0
24
+ * and 1. It is rounded to a whole percentage and clamped to 1–99%; tmux
25
+ * allocates space in cells, so the resulting size is approximate. If omitted,
26
+ * tmux uses its default split size.
27
+ * @param action - Command, arguments, and working directory for the new pane.
28
+ * The working directory defaults to the current process directory.
29
+ * @returns The new pane's tmux ID, such as "%3".
30
+ * @throws {SplitError} If the status check or split fails, including when the
31
+ * pane is too small for the requested split.
32
+ * @throws {RangeError} If the ratio is not finite or is outside the exclusive
33
+ * range (0, 1).
34
+ */
35
+ export async function split(options, action) {
36
+ const paneId = await getPaneId();
37
+ const { ratio } = options;
38
+ if (ratio !== undefined && (!Number.isFinite(ratio) || ratio <= 0 || ratio >= 1)) {
39
+ throw new RangeError("ratio must be a finite number between 0 and 1, exclusive");
40
+ }
41
+ // tmux accepts integer percentages. Clamp after rounding so valid ratios
42
+ // close to either endpoint do not become 0% or 100%.
43
+ const percentage = ratio === undefined
44
+ ? undefined
45
+ : Math.max(1, Math.min(99, Math.round(ratio * 100)));
46
+ // Derive horizontal/vertical and whether to keep the existing session "before" in tmux.
47
+ const horizontal = options.direction === "left" || options.direction === "right";
48
+ const before = options.direction === "left" || options.direction === "up";
49
+ const execFileAsync = promisify(execFile);
50
+ let stdout;
51
+ try {
52
+ ({ stdout } = await execFileAsync("tmux", [
53
+ "split-window",
54
+ // Direction:
55
+ horizontal ? "-h" : "-v",
56
+ ...(before ? ["-b"] : []),
57
+ ...(options.focus ? [] : ["-d"]),
58
+ ...(percentage === undefined ? [] : ["-l", `${percentage}%`]),
59
+ "-P",
60
+ "-F",
61
+ "#{pane_id}",
62
+ "-t",
63
+ paneId,
64
+ "-c",
65
+ action.cwd ?? process.cwd(),
66
+ "--",
67
+ "/bin/sh",
68
+ "-c",
69
+ 'exec "$@"',
70
+ "tmux-ts",
71
+ action.command,
72
+ ...(action.args ?? []),
73
+ ]));
74
+ }
75
+ catch (error) {
76
+ throw createSplitError("split-failed", error);
77
+ }
78
+ return stdout.trim();
79
+ }
@@ -0,0 +1,2 @@
1
+ import { PaneStatus } from "../domain/pane-status.js";
2
+ export declare function getPaneStatus(): Promise<PaneStatus>;
@@ -0,0 +1,34 @@
1
+ import { execFile } from "node:child_process";
2
+ import { promisify } from "node:util";
3
+ export async function getPaneStatus() {
4
+ const execFileAsync = promisify(execFile);
5
+ try {
6
+ await execFileAsync("tmux", ["-V"]);
7
+ }
8
+ catch (error) {
9
+ if (error.code === "ENOENT") {
10
+ return { available: false, reason: "not-installed" };
11
+ }
12
+ throw error;
13
+ }
14
+ const paneId = process.env.TMUX_PANE;
15
+ if (!process.env.TMUX || !paneId) {
16
+ return { available: false, reason: "not-in-tmux" };
17
+ }
18
+ try {
19
+ const { stdout } = await execFileAsync("tmux", [
20
+ "display-message",
21
+ "-p",
22
+ "-t",
23
+ paneId,
24
+ "#{pane_id}",
25
+ ]);
26
+ if (stdout.trim() !== paneId) {
27
+ return { available: false, reason: "pane-unreachable" };
28
+ }
29
+ }
30
+ catch {
31
+ return { available: false, reason: "pane-unreachable" };
32
+ }
33
+ return { available: true, paneId };
34
+ }
package/package.json CHANGED
@@ -1,6 +1,29 @@
1
1
  {
2
2
  "name": "@mak-98/tmux-ts",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
4
+ "private": false,
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "scripts": {
8
+ "start": "tsx src/cli.ts",
9
+ "format": "prettier . --write --log-level warn",
10
+ "build": "tsc",
11
+ "login": "npm login",
12
+ "deploy": "npm run build && npm publish --access public"
13
+ },
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "default": "./dist/index.js"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "devDependencies": {
24
+ "@types/node": "^26.6.6",
25
+ "tsx": "^4.23.15",
26
+ "typescript": "^7.0.2",
27
+ "prettier": "^3.8.4"
28
+ }
29
+ }