@oh-my-pi/pi-tui 12.8.1 → 12.9.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/package.json +3 -3
- package/src/index.ts +2 -0
- package/src/ttyid.ts +66 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oh-my-pi/pi-tui",
|
|
3
|
-
"version": "12.
|
|
3
|
+
"version": "12.9.0",
|
|
4
4
|
"description": "Terminal User Interface library with differential rendering for efficient text-based applications",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -52,8 +52,8 @@
|
|
|
52
52
|
"bun": ">=1.3.7"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@oh-my-pi/pi-natives": "12.
|
|
56
|
-
"@oh-my-pi/pi-utils": "12.
|
|
55
|
+
"@oh-my-pi/pi-natives": "12.9.0",
|
|
56
|
+
"@oh-my-pi/pi-utils": "12.9.0",
|
|
57
57
|
"@types/mime-types": "^3.0.1",
|
|
58
58
|
"chalk": "^5.6.2",
|
|
59
59
|
"marked": "^17.0.2",
|
package/src/index.ts
CHANGED
|
@@ -60,6 +60,8 @@ export type { BoxSymbols, SymbolTheme } from "./symbols";
|
|
|
60
60
|
export { emergencyTerminalRestore, ProcessTerminal, type Terminal } from "./terminal";
|
|
61
61
|
// Terminal image support
|
|
62
62
|
export * from "./terminal-capabilities";
|
|
63
|
+
// TTY ID
|
|
64
|
+
export { getTerminalId, getTtyPath } from "./ttyid";
|
|
63
65
|
export { type Component, Container, type OverlayHandle, type SizeValue, TUI } from "./tui";
|
|
64
66
|
// Utilities
|
|
65
67
|
export { Ellipsis, padding, replaceTabs, truncateToWidth, visibleWidth, wrapTextWithAnsi } from "./utils";
|
package/src/ttyid.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { CString, dlopen, FFIType } from "bun:ffi";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import * as os from "node:os";
|
|
4
|
+
|
|
5
|
+
/** Resolve the TTY device path for stdin (fd 0) via POSIX `ttyname(3)`. */
|
|
6
|
+
export function getTtyPath(): string | null {
|
|
7
|
+
if (os.platform() === "linux") {
|
|
8
|
+
// Linux: /proc/self/fd/0 is a symlink to /dev/pts/N
|
|
9
|
+
try {
|
|
10
|
+
const ttyPath = fs.readlinkSync("/proc/self/fd/0");
|
|
11
|
+
if (ttyPath.startsWith("/dev/")) {
|
|
12
|
+
return ttyPath;
|
|
13
|
+
}
|
|
14
|
+
} catch {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
} else if (os.platform() !== "win32") {
|
|
18
|
+
try {
|
|
19
|
+
const libName = os.platform() === "darwin" ? "libSystem.B.dylib" : "libc.so.6";
|
|
20
|
+
const lib = dlopen(libName, {
|
|
21
|
+
ttyname: { args: [FFIType.i32], returns: FFIType.ptr },
|
|
22
|
+
});
|
|
23
|
+
try {
|
|
24
|
+
const result = lib.symbols.ttyname(0);
|
|
25
|
+
return result ? new CString(result).toString() : null;
|
|
26
|
+
} finally {
|
|
27
|
+
lib.close();
|
|
28
|
+
}
|
|
29
|
+
} catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Get a stable identifier for the current terminal.
|
|
37
|
+
* Uses the TTY device path (e.g., /dev/pts/3), falling back to environment
|
|
38
|
+
* variables for terminal multiplexers or terminal emulators.
|
|
39
|
+
* Returns null if no terminal can be identified (e.g., piped input).
|
|
40
|
+
*/
|
|
41
|
+
export function getTerminalId(): string | null {
|
|
42
|
+
// TTY device path — most reliable, unique per terminal tab
|
|
43
|
+
if (process.stdin.isTTY) {
|
|
44
|
+
try {
|
|
45
|
+
const ttyPath = getTtyPath();
|
|
46
|
+
if (ttyPath?.startsWith("/dev/")) {
|
|
47
|
+
return ttyPath.slice(5).replace(/\//g, "-"); // /dev/pts/3 -> pts-3
|
|
48
|
+
}
|
|
49
|
+
} catch {}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Fallback to terminal-specific env vars
|
|
53
|
+
const kittyId = process.env.KITTY_WINDOW_ID;
|
|
54
|
+
if (kittyId) return `kitty-${kittyId}`;
|
|
55
|
+
|
|
56
|
+
const tmuxPane = process.env.TMUX_PANE;
|
|
57
|
+
if (tmuxPane) return `tmux-${tmuxPane}`;
|
|
58
|
+
|
|
59
|
+
const terminalSessionId = process.env.TERM_SESSION_ID; // macOS Terminal.app
|
|
60
|
+
if (terminalSessionId) return `apple-${terminalSessionId}`;
|
|
61
|
+
|
|
62
|
+
const wtSession = process.env.WT_SESSION; // Windows Terminal
|
|
63
|
+
if (wtSession) return `wt-${wtSession}`;
|
|
64
|
+
|
|
65
|
+
return null;
|
|
66
|
+
}
|