@oh-my-pi/pi-tui 1.337.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.
@@ -0,0 +1,340 @@
1
+ export type ImageProtocol = "kitty" | "iterm2" | null;
2
+
3
+ export interface TerminalCapabilities {
4
+ images: ImageProtocol;
5
+ trueColor: boolean;
6
+ hyperlinks: boolean;
7
+ }
8
+
9
+ export interface CellDimensions {
10
+ widthPx: number;
11
+ heightPx: number;
12
+ }
13
+
14
+ export interface ImageDimensions {
15
+ widthPx: number;
16
+ heightPx: number;
17
+ }
18
+
19
+ export interface ImageRenderOptions {
20
+ maxWidthCells?: number;
21
+ maxHeightCells?: number;
22
+ preserveAspectRatio?: boolean;
23
+ }
24
+
25
+ let cachedCapabilities: TerminalCapabilities | null = null;
26
+
27
+ // Default cell dimensions - updated by TUI when terminal responds to query
28
+ let cellDimensions: CellDimensions = { widthPx: 9, heightPx: 18 };
29
+
30
+ export function getCellDimensions(): CellDimensions {
31
+ return cellDimensions;
32
+ }
33
+
34
+ export function setCellDimensions(dims: CellDimensions): void {
35
+ cellDimensions = dims;
36
+ }
37
+
38
+ export function detectCapabilities(): TerminalCapabilities {
39
+ const termProgram = process.env.TERM_PROGRAM?.toLowerCase() || "";
40
+ const term = process.env.TERM?.toLowerCase() || "";
41
+ const colorTerm = process.env.COLORTERM?.toLowerCase() || "";
42
+
43
+ if (process.env.KITTY_WINDOW_ID || termProgram === "kitty") {
44
+ return { images: "kitty", trueColor: true, hyperlinks: true };
45
+ }
46
+
47
+ if (termProgram === "ghostty" || term.includes("ghostty") || process.env.GHOSTTY_RESOURCES_DIR) {
48
+ return { images: "kitty", trueColor: true, hyperlinks: true };
49
+ }
50
+
51
+ if (process.env.WEZTERM_PANE || termProgram === "wezterm") {
52
+ return { images: "kitty", trueColor: true, hyperlinks: true };
53
+ }
54
+
55
+ if (process.env.ITERM_SESSION_ID || termProgram === "iterm.app") {
56
+ return { images: "iterm2", trueColor: true, hyperlinks: true };
57
+ }
58
+
59
+ if (termProgram === "vscode") {
60
+ return { images: null, trueColor: true, hyperlinks: true };
61
+ }
62
+
63
+ if (termProgram === "alacritty") {
64
+ return { images: null, trueColor: true, hyperlinks: true };
65
+ }
66
+
67
+ const trueColor = colorTerm === "truecolor" || colorTerm === "24bit";
68
+ return { images: null, trueColor, hyperlinks: true };
69
+ }
70
+
71
+ export function getCapabilities(): TerminalCapabilities {
72
+ if (!cachedCapabilities) {
73
+ cachedCapabilities = detectCapabilities();
74
+ }
75
+ return cachedCapabilities;
76
+ }
77
+
78
+ export function resetCapabilitiesCache(): void {
79
+ cachedCapabilities = null;
80
+ }
81
+
82
+ export function encodeKitty(
83
+ base64Data: string,
84
+ options: {
85
+ columns?: number;
86
+ rows?: number;
87
+ imageId?: number;
88
+ } = {},
89
+ ): string {
90
+ const CHUNK_SIZE = 4096;
91
+
92
+ const params: string[] = ["a=T", "f=100", "q=2"];
93
+
94
+ if (options.columns) params.push(`c=${options.columns}`);
95
+ if (options.rows) params.push(`r=${options.rows}`);
96
+ if (options.imageId) params.push(`i=${options.imageId}`);
97
+
98
+ if (base64Data.length <= CHUNK_SIZE) {
99
+ return `\x1b_G${params.join(",")};${base64Data}\x1b\\`;
100
+ }
101
+
102
+ const chunks: string[] = [];
103
+ let offset = 0;
104
+ let isFirst = true;
105
+
106
+ while (offset < base64Data.length) {
107
+ const chunk = base64Data.slice(offset, offset + CHUNK_SIZE);
108
+ const isLast = offset + CHUNK_SIZE >= base64Data.length;
109
+
110
+ if (isFirst) {
111
+ chunks.push(`\x1b_G${params.join(",")},m=1;${chunk}\x1b\\`);
112
+ isFirst = false;
113
+ } else if (isLast) {
114
+ chunks.push(`\x1b_Gm=0;${chunk}\x1b\\`);
115
+ } else {
116
+ chunks.push(`\x1b_Gm=1;${chunk}\x1b\\`);
117
+ }
118
+
119
+ offset += CHUNK_SIZE;
120
+ }
121
+
122
+ return chunks.join("");
123
+ }
124
+
125
+ export function encodeITerm2(
126
+ base64Data: string,
127
+ options: {
128
+ width?: number | string;
129
+ height?: number | string;
130
+ name?: string;
131
+ preserveAspectRatio?: boolean;
132
+ inline?: boolean;
133
+ } = {},
134
+ ): string {
135
+ const params: string[] = [`inline=${options.inline !== false ? 1 : 0}`];
136
+
137
+ if (options.width !== undefined) params.push(`width=${options.width}`);
138
+ if (options.height !== undefined) params.push(`height=${options.height}`);
139
+ if (options.name) {
140
+ const nameBase64 = Buffer.from(options.name).toString("base64");
141
+ params.push(`name=${nameBase64}`);
142
+ }
143
+ if (options.preserveAspectRatio === false) {
144
+ params.push("preserveAspectRatio=0");
145
+ }
146
+
147
+ return `\x1b]1337;File=${params.join(";")}:${base64Data}\x07`;
148
+ }
149
+
150
+ export function calculateImageRows(
151
+ imageDimensions: ImageDimensions,
152
+ targetWidthCells: number,
153
+ cellDimensions: CellDimensions = { widthPx: 9, heightPx: 18 },
154
+ ): number {
155
+ const targetWidthPx = targetWidthCells * cellDimensions.widthPx;
156
+ const scale = targetWidthPx / imageDimensions.widthPx;
157
+ const scaledHeightPx = imageDimensions.heightPx * scale;
158
+ const rows = Math.ceil(scaledHeightPx / cellDimensions.heightPx);
159
+ return Math.max(1, rows);
160
+ }
161
+
162
+ export function getPngDimensions(base64Data: string): ImageDimensions | null {
163
+ try {
164
+ const buffer = Buffer.from(base64Data, "base64");
165
+
166
+ if (buffer.length < 24) {
167
+ return null;
168
+ }
169
+
170
+ if (buffer[0] !== 0x89 || buffer[1] !== 0x50 || buffer[2] !== 0x4e || buffer[3] !== 0x47) {
171
+ return null;
172
+ }
173
+
174
+ const width = buffer.readUInt32BE(16);
175
+ const height = buffer.readUInt32BE(20);
176
+
177
+ return { widthPx: width, heightPx: height };
178
+ } catch {
179
+ return null;
180
+ }
181
+ }
182
+
183
+ export function getJpegDimensions(base64Data: string): ImageDimensions | null {
184
+ try {
185
+ const buffer = Buffer.from(base64Data, "base64");
186
+
187
+ if (buffer.length < 2) {
188
+ return null;
189
+ }
190
+
191
+ if (buffer[0] !== 0xff || buffer[1] !== 0xd8) {
192
+ return null;
193
+ }
194
+
195
+ let offset = 2;
196
+ while (offset < buffer.length - 9) {
197
+ if (buffer[offset] !== 0xff) {
198
+ offset++;
199
+ continue;
200
+ }
201
+
202
+ const marker = buffer[offset + 1];
203
+
204
+ if (marker >= 0xc0 && marker <= 0xc2) {
205
+ const height = buffer.readUInt16BE(offset + 5);
206
+ const width = buffer.readUInt16BE(offset + 7);
207
+ return { widthPx: width, heightPx: height };
208
+ }
209
+
210
+ if (offset + 3 >= buffer.length) {
211
+ return null;
212
+ }
213
+ const length = buffer.readUInt16BE(offset + 2);
214
+ if (length < 2) {
215
+ return null;
216
+ }
217
+ offset += 2 + length;
218
+ }
219
+
220
+ return null;
221
+ } catch {
222
+ return null;
223
+ }
224
+ }
225
+
226
+ export function getGifDimensions(base64Data: string): ImageDimensions | null {
227
+ try {
228
+ const buffer = Buffer.from(base64Data, "base64");
229
+
230
+ if (buffer.length < 10) {
231
+ return null;
232
+ }
233
+
234
+ const sig = buffer.slice(0, 6).toString("ascii");
235
+ if (sig !== "GIF87a" && sig !== "GIF89a") {
236
+ return null;
237
+ }
238
+
239
+ const width = buffer.readUInt16LE(6);
240
+ const height = buffer.readUInt16LE(8);
241
+
242
+ return { widthPx: width, heightPx: height };
243
+ } catch {
244
+ return null;
245
+ }
246
+ }
247
+
248
+ export function getWebpDimensions(base64Data: string): ImageDimensions | null {
249
+ try {
250
+ const buffer = Buffer.from(base64Data, "base64");
251
+
252
+ if (buffer.length < 30) {
253
+ return null;
254
+ }
255
+
256
+ const riff = buffer.slice(0, 4).toString("ascii");
257
+ const webp = buffer.slice(8, 12).toString("ascii");
258
+ if (riff !== "RIFF" || webp !== "WEBP") {
259
+ return null;
260
+ }
261
+
262
+ const chunk = buffer.slice(12, 16).toString("ascii");
263
+ if (chunk === "VP8 ") {
264
+ if (buffer.length < 30) return null;
265
+ const width = buffer.readUInt16LE(26) & 0x3fff;
266
+ const height = buffer.readUInt16LE(28) & 0x3fff;
267
+ return { widthPx: width, heightPx: height };
268
+ } else if (chunk === "VP8L") {
269
+ if (buffer.length < 25) return null;
270
+ const bits = buffer.readUInt32LE(21);
271
+ const width = (bits & 0x3fff) + 1;
272
+ const height = ((bits >> 14) & 0x3fff) + 1;
273
+ return { widthPx: width, heightPx: height };
274
+ } else if (chunk === "VP8X") {
275
+ if (buffer.length < 30) return null;
276
+ const width = (buffer[24] | (buffer[25] << 8) | (buffer[26] << 16)) + 1;
277
+ const height = (buffer[27] | (buffer[28] << 8) | (buffer[29] << 16)) + 1;
278
+ return { widthPx: width, heightPx: height };
279
+ }
280
+
281
+ return null;
282
+ } catch {
283
+ return null;
284
+ }
285
+ }
286
+
287
+ export function getImageDimensions(base64Data: string, mimeType: string): ImageDimensions | null {
288
+ if (mimeType === "image/png") {
289
+ return getPngDimensions(base64Data);
290
+ }
291
+ if (mimeType === "image/jpeg") {
292
+ return getJpegDimensions(base64Data);
293
+ }
294
+ if (mimeType === "image/gif") {
295
+ return getGifDimensions(base64Data);
296
+ }
297
+ if (mimeType === "image/webp") {
298
+ return getWebpDimensions(base64Data);
299
+ }
300
+ return null;
301
+ }
302
+
303
+ export function renderImage(
304
+ base64Data: string,
305
+ imageDimensions: ImageDimensions,
306
+ options: ImageRenderOptions = {},
307
+ ): { sequence: string; rows: number } | null {
308
+ const caps = getCapabilities();
309
+
310
+ if (!caps.images) {
311
+ return null;
312
+ }
313
+
314
+ const maxWidth = options.maxWidthCells ?? 80;
315
+ const rows = calculateImageRows(imageDimensions, maxWidth, getCellDimensions());
316
+
317
+ if (caps.images === "kitty") {
318
+ const sequence = encodeKitty(base64Data, { columns: maxWidth, rows });
319
+ return { sequence, rows };
320
+ }
321
+
322
+ if (caps.images === "iterm2") {
323
+ const sequence = encodeITerm2(base64Data, {
324
+ width: maxWidth,
325
+ height: "auto",
326
+ preserveAspectRatio: options.preserveAspectRatio ?? true,
327
+ });
328
+ return { sequence, rows };
329
+ }
330
+
331
+ return null;
332
+ }
333
+
334
+ export function imageFallback(mimeType: string, dimensions?: ImageDimensions, filename?: string): string {
335
+ const parts: string[] = [];
336
+ if (filename) parts.push(filename);
337
+ parts.push(`[${mimeType}]`);
338
+ if (dimensions) parts.push(`${dimensions.widthPx}x${dimensions.heightPx}`);
339
+ return `[Image: ${parts.join(" ")}]`;
340
+ }
@@ -0,0 +1,163 @@
1
+ /**
2
+ * Minimal terminal interface for TUI
3
+ */
4
+
5
+ // Track active terminal for emergency cleanup on crash
6
+ let activeTerminal: ProcessTerminal | null = null;
7
+
8
+ /**
9
+ * Emergency terminal restore - call this from signal/crash handlers
10
+ * Resets terminal state without requiring access to the ProcessTerminal instance
11
+ */
12
+ export function emergencyTerminalRestore(): void {
13
+ if (activeTerminal) {
14
+ activeTerminal.stop();
15
+ activeTerminal.showCursor();
16
+ activeTerminal = null;
17
+ } else {
18
+ // Blind restore if no instance tracked - covers edge cases
19
+ process.stdout.write(
20
+ "\x1b[?2004l" + // Disable bracketed paste
21
+ "\x1b[<u" + // Pop kitty keyboard protocol
22
+ "\x1b[?25h", // Show cursor
23
+ );
24
+ if (process.stdin.setRawMode) {
25
+ process.stdin.setRawMode(false);
26
+ }
27
+ }
28
+ }
29
+ export interface Terminal {
30
+ // Start the terminal with input and resize handlers
31
+ start(onInput: (data: string) => void, onResize: () => void): void;
32
+
33
+ // Stop the terminal and restore state
34
+ stop(): void;
35
+
36
+ // Write output to terminal
37
+ write(data: string): void;
38
+
39
+ // Get terminal dimensions
40
+ get columns(): number;
41
+ get rows(): number;
42
+
43
+ // Cursor positioning (relative to current position)
44
+ moveBy(lines: number): void; // Move cursor up (negative) or down (positive) by N lines
45
+
46
+ // Cursor visibility
47
+ hideCursor(): void; // Hide the cursor
48
+ showCursor(): void; // Show the cursor
49
+
50
+ // Clear operations
51
+ clearLine(): void; // Clear current line
52
+ clearFromCursor(): void; // Clear from cursor to end of screen
53
+ clearScreen(): void; // Clear entire screen and move cursor to (0,0)
54
+ }
55
+
56
+ /**
57
+ * Real terminal using process.stdin/stdout
58
+ */
59
+ export class ProcessTerminal implements Terminal {
60
+ private wasRaw = false;
61
+ private inputHandler?: (data: string) => void;
62
+ private resizeHandler?: () => void;
63
+
64
+ start(onInput: (data: string) => void, onResize: () => void): void {
65
+ this.inputHandler = onInput;
66
+ this.resizeHandler = onResize;
67
+
68
+ // Register for emergency cleanup
69
+ activeTerminal = this;
70
+
71
+ // Save previous state and enable raw mode
72
+ this.wasRaw = process.stdin.isRaw || false;
73
+ if (process.stdin.setRawMode) {
74
+ process.stdin.setRawMode(true);
75
+ }
76
+ process.stdin.setEncoding("utf8");
77
+ process.stdin.resume();
78
+
79
+ // Enable bracketed paste mode - terminal will wrap pastes in \x1b[200~ ... \x1b[201~
80
+ process.stdout.write("\x1b[?2004h");
81
+
82
+ // Enable Kitty keyboard protocol (disambiguate escape codes)
83
+ // This makes terminals like Ghostty, Kitty, WezTerm send enhanced key sequences
84
+ // e.g., Shift+Enter becomes \x1b[13;2u instead of just \r
85
+ // See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/
86
+ process.stdout.write("\x1b[>1u");
87
+
88
+ // Set up event handlers
89
+ process.stdin.on("data", this.inputHandler);
90
+ process.stdout.on("resize", this.resizeHandler);
91
+ }
92
+
93
+ stop(): void {
94
+ // Unregister from emergency cleanup
95
+ if (activeTerminal === this) {
96
+ activeTerminal = null;
97
+ }
98
+
99
+ // Disable bracketed paste mode
100
+ process.stdout.write("\x1b[?2004l");
101
+
102
+ // Disable Kitty keyboard protocol (pop the flags we pushed)
103
+ process.stdout.write("\x1b[<u");
104
+
105
+ // Remove event handlers
106
+ if (this.inputHandler) {
107
+ process.stdin.removeListener("data", this.inputHandler);
108
+ this.inputHandler = undefined;
109
+ }
110
+ if (this.resizeHandler) {
111
+ process.stdout.removeListener("resize", this.resizeHandler);
112
+ this.resizeHandler = undefined;
113
+ }
114
+
115
+ // Restore raw mode state
116
+ if (process.stdin.setRawMode) {
117
+ process.stdin.setRawMode(this.wasRaw);
118
+ }
119
+ }
120
+
121
+ write(data: string): void {
122
+ process.stdout.write(data);
123
+ }
124
+
125
+ get columns(): number {
126
+ return process.stdout.columns || 80;
127
+ }
128
+
129
+ get rows(): number {
130
+ return process.stdout.rows || 24;
131
+ }
132
+
133
+ moveBy(lines: number): void {
134
+ if (lines > 0) {
135
+ // Move down
136
+ process.stdout.write(`\x1b[${lines}B`);
137
+ } else if (lines < 0) {
138
+ // Move up
139
+ process.stdout.write(`\x1b[${-lines}A`);
140
+ }
141
+ // lines === 0: no movement
142
+ }
143
+
144
+ hideCursor(): void {
145
+ process.stdout.write("\x1b[?25l");
146
+ }
147
+
148
+ showCursor(): void {
149
+ process.stdout.write("\x1b[?25h");
150
+ }
151
+
152
+ clearLine(): void {
153
+ process.stdout.write("\x1b[K");
154
+ }
155
+
156
+ clearFromCursor(): void {
157
+ process.stdout.write("\x1b[J");
158
+ }
159
+
160
+ clearScreen(): void {
161
+ process.stdout.write("\x1b[2J\x1b[H"); // Clear screen and move to home (1,1)
162
+ }
163
+ }