@heyhuynhgiabuu/pi-pretty 0.5.3 → 0.6.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/bun.lock +598 -0
- package/package.json +6 -8
- package/src/autocomplete.ts +96 -0
- package/src/config.ts +250 -0
- package/src/fff.ts +147 -0
- package/src/helpers.ts +124 -0
- package/src/image.ts +129 -0
- package/src/index.ts +163 -2161
- package/src/render.ts +402 -0
- package/src/tools/bash.ts +115 -0
- package/src/tools/find.ts +87 -0
- package/src/tools/grep.ts +125 -0
- package/src/tools/ls.ts +66 -0
- package/src/tools/metrics.ts +40 -0
- package/src/tools/multi-grep.ts +196 -0
- package/src/tools/read.ts +142 -0
- package/src/types.ts +227 -0
- package/test/bash-rendering.test.ts +3 -3
package/src/image.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pi-pretty: Terminal image rendering (iTerm2 / Kitty / Ghostty inline image protocols).
|
|
3
|
+
* Handles tmux passthrough for image protocols.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { execFileSync } from "node:child_process";
|
|
7
|
+
|
|
8
|
+
type ImageProtocol = "iterm2" | "kitty" | "none";
|
|
9
|
+
|
|
10
|
+
let _tmuxClientTermCache: string | null | undefined;
|
|
11
|
+
let _tmuxAllowPassthroughCache: boolean | null | undefined;
|
|
12
|
+
let _tmuxClientTermOverrideForTests: string | null | undefined;
|
|
13
|
+
let _tmuxAllowPassthroughOverrideForTests: boolean | null | undefined;
|
|
14
|
+
|
|
15
|
+
function isTmuxSession(): boolean {
|
|
16
|
+
return !!process.env.TMUX || /^(tmux|screen)/.test(process.env.TERM ?? "");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function normalizeTerminalName(term: string): string {
|
|
20
|
+
const t = term.toLowerCase();
|
|
21
|
+
if (t.includes("kitty")) return "kitty";
|
|
22
|
+
if (t.includes("ghostty")) return "ghostty";
|
|
23
|
+
if (t.includes("wezterm")) return "WezTerm";
|
|
24
|
+
if (t.includes("iterm")) return "iTerm.app";
|
|
25
|
+
if (t.includes("mintty")) return "mintty";
|
|
26
|
+
return term;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function readTmuxClientTerm(): string | null {
|
|
30
|
+
if (_tmuxClientTermOverrideForTests !== undefined) {
|
|
31
|
+
return _tmuxClientTermOverrideForTests ? normalizeTerminalName(_tmuxClientTermOverrideForTests) : null;
|
|
32
|
+
}
|
|
33
|
+
if (!isTmuxSession()) return null;
|
|
34
|
+
if (_tmuxClientTermCache !== undefined) return _tmuxClientTermCache;
|
|
35
|
+
try {
|
|
36
|
+
const term = execFileSync("tmux", ["display-message", "-p", "#{client_termname}"], {
|
|
37
|
+
encoding: "utf8",
|
|
38
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
39
|
+
timeout: 200,
|
|
40
|
+
}).trim();
|
|
41
|
+
_tmuxClientTermCache = term ? normalizeTerminalName(term) : null;
|
|
42
|
+
} catch {
|
|
43
|
+
_tmuxClientTermCache = null;
|
|
44
|
+
}
|
|
45
|
+
return _tmuxClientTermCache;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function getOuterTerminal(): string {
|
|
49
|
+
if (process.env.LC_TERMINAL === "iTerm2") return "iTerm.app";
|
|
50
|
+
if (process.env.GHOSTTY_RESOURCES_DIR) return "ghostty";
|
|
51
|
+
if (process.env.KITTY_WINDOW_ID || process.env.KITTY_PID) return "kitty";
|
|
52
|
+
if (process.env.WEZTERM_EXECUTABLE || process.env.WEZTERM_CONFIG_DIR || process.env.WEZTERM_CONFIG_FILE) {
|
|
53
|
+
return "WezTerm";
|
|
54
|
+
}
|
|
55
|
+
const termProgram = process.env.TERM_PROGRAM ?? "";
|
|
56
|
+
if (termProgram && termProgram !== "tmux" && termProgram !== "screen") {
|
|
57
|
+
return normalizeTerminalName(termProgram);
|
|
58
|
+
}
|
|
59
|
+
const tmuxClientTerm = readTmuxClientTerm();
|
|
60
|
+
if (tmuxClientTerm) return tmuxClientTerm;
|
|
61
|
+
const term = process.env.TERM ?? "";
|
|
62
|
+
if (term) return normalizeTerminalName(term);
|
|
63
|
+
if (process.env.COLORTERM === "truecolor" || process.env.COLORTERM === "24bit") return "unknown-modern";
|
|
64
|
+
return termProgram;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function detectImageProtocol(): ImageProtocol {
|
|
68
|
+
const forced = (process.env.PRETTY_IMAGE_PROTOCOL ?? "").toLowerCase();
|
|
69
|
+
if (forced === "kitty" || forced === "iterm2" || forced === "none") return forced;
|
|
70
|
+
const term = getOuterTerminal();
|
|
71
|
+
if (term === "ghostty" || term === "kitty") return "kitty";
|
|
72
|
+
if (["iTerm.app", "WezTerm", "mintty"].includes(term)) return "iterm2";
|
|
73
|
+
if (process.env.LC_TERMINAL === "iTerm2") return "iterm2";
|
|
74
|
+
return "none";
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function tmuxAllowsPassthrough(): boolean | null {
|
|
78
|
+
if (_tmuxAllowPassthroughOverrideForTests !== undefined) return _tmuxAllowPassthroughOverrideForTests;
|
|
79
|
+
if (!isTmuxSession()) return null;
|
|
80
|
+
if (_tmuxAllowPassthroughCache !== undefined) return _tmuxAllowPassthroughCache;
|
|
81
|
+
try {
|
|
82
|
+
const value = execFileSync("tmux", ["show-options", "-gv", "allow-passthrough"], {
|
|
83
|
+
encoding: "utf8",
|
|
84
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
85
|
+
timeout: 200,
|
|
86
|
+
})
|
|
87
|
+
.trim()
|
|
88
|
+
.toLowerCase();
|
|
89
|
+
_tmuxAllowPassthroughCache = value === "on" || value === "all";
|
|
90
|
+
} catch {
|
|
91
|
+
_tmuxAllowPassthroughCache = null;
|
|
92
|
+
}
|
|
93
|
+
return _tmuxAllowPassthroughCache;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function getTmuxPassthroughWarning(protocol: ImageProtocol): string | null {
|
|
97
|
+
if (!isTmuxSession() || protocol === "none") return null;
|
|
98
|
+
if (tmuxAllowsPassthrough() === false) {
|
|
99
|
+
return 'tmux allow-passthrough is off. Run: tmux set -g allow-passthrough on';
|
|
100
|
+
}
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function tmuxWrap(seq: string): string {
|
|
105
|
+
if (!isTmuxSession()) return seq;
|
|
106
|
+
const escaped = seq.split("\x1b").join("\x1b\x1b");
|
|
107
|
+
return `\x1bPtmux;${escaped}\x1b\\`;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export const __imageInternals = {
|
|
111
|
+
isTmuxSession,
|
|
112
|
+
getOuterTerminal,
|
|
113
|
+
detectImageProtocol,
|
|
114
|
+
tmuxWrap,
|
|
115
|
+
tmuxAllowsPassthrough,
|
|
116
|
+
getTmuxPassthroughWarning,
|
|
117
|
+
setTmuxClientTermOverrideForTests: (value: string | null | undefined) => {
|
|
118
|
+
_tmuxClientTermOverrideForTests = value;
|
|
119
|
+
},
|
|
120
|
+
setTmuxAllowPassthroughOverrideForTests: (value: boolean | null | undefined) => {
|
|
121
|
+
_tmuxAllowPassthroughOverrideForTests = value;
|
|
122
|
+
},
|
|
123
|
+
resetCachesForTests: () => {
|
|
124
|
+
_tmuxClientTermCache = undefined;
|
|
125
|
+
_tmuxAllowPassthroughCache = undefined;
|
|
126
|
+
_tmuxClientTermOverrideForTests = undefined;
|
|
127
|
+
_tmuxAllowPassthroughOverrideForTests = undefined;
|
|
128
|
+
},
|
|
129
|
+
};
|