@f5xc-salesdemos/pi-tui 14.0.2
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 +682 -0
- package/README.md +704 -0
- package/package.json +71 -0
- package/src/autocomplete.ts +787 -0
- package/src/bracketed-paste.ts +47 -0
- package/src/components/box.ts +144 -0
- package/src/components/cancellable-loader.ts +40 -0
- package/src/components/editor.ts +2563 -0
- package/src/components/image.ts +90 -0
- package/src/components/input.ts +439 -0
- package/src/components/loader.ts +67 -0
- package/src/components/markdown.ts +914 -0
- package/src/components/select-list.ts +249 -0
- package/src/components/settings-list.ts +195 -0
- package/src/components/spacer.ts +28 -0
- package/src/components/tab-bar.ts +175 -0
- package/src/components/text.ts +110 -0
- package/src/components/truncated-text.ts +61 -0
- package/src/editor-component.ts +71 -0
- package/src/fuzzy.ts +143 -0
- package/src/index.ts +39 -0
- package/src/keybindings.ts +279 -0
- package/src/keys.ts +404 -0
- package/src/kill-ring.ts +46 -0
- package/src/stdin-buffer.ts +385 -0
- package/src/symbols.ts +24 -0
- package/src/terminal-capabilities.ts +525 -0
- package/src/terminal.ts +630 -0
- package/src/ttyid.ts +66 -0
- package/src/tui.ts +1328 -0
- package/src/utils.ts +301 -0
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
import { encodeSixel } from "@f5xc-salesdemos/pi-natives";
|
|
2
|
+
import { $env } from "@f5xc-salesdemos/pi-utils";
|
|
3
|
+
|
|
4
|
+
export enum ImageProtocol {
|
|
5
|
+
Kitty = "\x1b_G",
|
|
6
|
+
Iterm2 = "\x1b]1337;File=",
|
|
7
|
+
Sixel = "\x1bPq",
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export enum NotifyProtocol {
|
|
11
|
+
Bell = "\x07",
|
|
12
|
+
Osc99 = "\x1b]99;;",
|
|
13
|
+
Osc9 = "\x1b]9;",
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type TerminalId = "kitty" | "ghostty" | "wezterm" | "iterm2" | "vscode" | "alacritty" | "base" | "trueColor";
|
|
17
|
+
|
|
18
|
+
const SIXEL_DCS_START_REGEX = /\x1bP(?:[0-9;]*)q/u;
|
|
19
|
+
/** Terminal capability details used for rendering and protocol selection. */
|
|
20
|
+
export class TerminalInfo {
|
|
21
|
+
constructor(
|
|
22
|
+
public readonly id: TerminalId,
|
|
23
|
+
public readonly imageProtocol: ImageProtocol | null,
|
|
24
|
+
public readonly trueColor: boolean,
|
|
25
|
+
public readonly hyperlinks: boolean,
|
|
26
|
+
public readonly notifyProtocol: NotifyProtocol = NotifyProtocol.Bell,
|
|
27
|
+
) {}
|
|
28
|
+
|
|
29
|
+
isImageLine(line: string): boolean {
|
|
30
|
+
if (!this.imageProtocol) return false;
|
|
31
|
+
if (this.imageProtocol === ImageProtocol.Sixel) {
|
|
32
|
+
return SIXEL_DCS_START_REGEX.test(line.slice(0, 128));
|
|
33
|
+
}
|
|
34
|
+
return line.slice(0, 64).includes(this.imageProtocol);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
formatNotification(message: string): string {
|
|
38
|
+
if (this.notifyProtocol === NotifyProtocol.Bell) {
|
|
39
|
+
return NotifyProtocol.Bell;
|
|
40
|
+
}
|
|
41
|
+
return `${this.notifyProtocol}${message}\x1b\\`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
sendNotification(message: string): void {
|
|
45
|
+
if (isNotificationSuppressed()) return;
|
|
46
|
+
process.stdout.write(this.formatNotification(message));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function isNotificationSuppressed(): boolean {
|
|
51
|
+
const value = $env.PI_NOTIFICATIONS;
|
|
52
|
+
if (!value) return false;
|
|
53
|
+
return value === "off" || value === "0" || value === "false";
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function getForcedImageProtocol(): ImageProtocol | null | undefined {
|
|
57
|
+
const raw = $env.PI_FORCE_IMAGE_PROTOCOL?.trim().toLowerCase();
|
|
58
|
+
if (!raw) return undefined;
|
|
59
|
+
if (raw === "kitty") return ImageProtocol.Kitty;
|
|
60
|
+
if (raw === "iterm2" || raw === "iterm") return ImageProtocol.Iterm2;
|
|
61
|
+
if (raw === "sixel") return ImageProtocol.Sixel;
|
|
62
|
+
if (raw === "off" || raw === "none" || raw === "0" || raw === "false") return null;
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function parseMajorMinorVersion(versionRaw?: string): { major: number; minor: number } | null {
|
|
67
|
+
if (!versionRaw) return null;
|
|
68
|
+
const match = /^(\d+)\.(\d+)/u.exec(versionRaw.trim());
|
|
69
|
+
if (!match) return null;
|
|
70
|
+
const major = Number.parseInt(match[1] ?? "", 10);
|
|
71
|
+
const minor = Number.parseInt(match[2] ?? "", 10);
|
|
72
|
+
if (!Number.isFinite(major) || !Number.isFinite(minor)) return null;
|
|
73
|
+
return { major, minor };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Returns true when running in Windows Terminal with known SIXEL support.
|
|
78
|
+
*
|
|
79
|
+
* Windows Terminal introduced SIXEL support in preview 1.22.
|
|
80
|
+
*/
|
|
81
|
+
export function isWindowsTerminalPreviewSixelSupported(
|
|
82
|
+
env: NodeJS.ProcessEnv = Bun.env,
|
|
83
|
+
platform: NodeJS.Platform = process.platform,
|
|
84
|
+
): boolean {
|
|
85
|
+
if (platform !== "win32") return false;
|
|
86
|
+
if (!env.WT_SESSION) return false;
|
|
87
|
+
if (env.TERM_PROGRAM && env.TERM_PROGRAM.toLowerCase() !== "windows_terminal") {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
const version = parseMajorMinorVersion(env.TERM_PROGRAM_VERSION);
|
|
91
|
+
if (!version) return false;
|
|
92
|
+
return version.major > 1 || (version.major === 1 && version.minor >= 22);
|
|
93
|
+
}
|
|
94
|
+
function getFallbackImageProtocol(terminalId: TerminalId): ImageProtocol | null {
|
|
95
|
+
if (!process.stdout.isTTY) return null;
|
|
96
|
+
if (terminalId === "vscode" || terminalId === "alacritty") return null;
|
|
97
|
+
const term = Bun.env.TERM?.toLowerCase() ?? "";
|
|
98
|
+
if (term.includes("screen") || term.includes("tmux") || term.includes("ghostty")) {
|
|
99
|
+
return ImageProtocol.Kitty;
|
|
100
|
+
}
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
const KNOWN_TERMINALS = Object.freeze({
|
|
104
|
+
// Fallback terminals
|
|
105
|
+
base: new TerminalInfo("base", null, false, true, NotifyProtocol.Bell),
|
|
106
|
+
trueColor: new TerminalInfo("trueColor", null, true, true, NotifyProtocol.Bell),
|
|
107
|
+
// Recognized terminals
|
|
108
|
+
kitty: new TerminalInfo("kitty", ImageProtocol.Kitty, true, true, NotifyProtocol.Osc99),
|
|
109
|
+
ghostty: new TerminalInfo("ghostty", ImageProtocol.Kitty, true, true, NotifyProtocol.Osc9),
|
|
110
|
+
wezterm: new TerminalInfo("wezterm", ImageProtocol.Kitty, true, true, NotifyProtocol.Osc9),
|
|
111
|
+
iterm2: new TerminalInfo("iterm2", ImageProtocol.Iterm2, true, true, NotifyProtocol.Osc9),
|
|
112
|
+
vscode: new TerminalInfo("vscode", null, true, true, NotifyProtocol.Bell),
|
|
113
|
+
alacritty: new TerminalInfo("alacritty", null, true, true, NotifyProtocol.Bell),
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
export const TERMINAL_ID: TerminalId = (() => {
|
|
117
|
+
function caseEq(a: string, b: string): boolean {
|
|
118
|
+
return a.toLowerCase() === b.toLowerCase(); // For compiler to pattern match
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const {
|
|
122
|
+
KITTY_WINDOW_ID,
|
|
123
|
+
GHOSTTY_RESOURCES_DIR,
|
|
124
|
+
WEZTERM_PANE,
|
|
125
|
+
ITERM_SESSION_ID,
|
|
126
|
+
VSCODE_PID,
|
|
127
|
+
ALACRITTY_WINDOW_ID,
|
|
128
|
+
TERM_PROGRAM,
|
|
129
|
+
TERM,
|
|
130
|
+
COLORTERM,
|
|
131
|
+
} = Bun.env;
|
|
132
|
+
|
|
133
|
+
if (KITTY_WINDOW_ID) return "kitty";
|
|
134
|
+
if (GHOSTTY_RESOURCES_DIR) return "ghostty";
|
|
135
|
+
if (WEZTERM_PANE) return "wezterm";
|
|
136
|
+
if (ITERM_SESSION_ID) return "iterm2";
|
|
137
|
+
if (VSCODE_PID) return "vscode";
|
|
138
|
+
if (ALACRITTY_WINDOW_ID) return "alacritty";
|
|
139
|
+
|
|
140
|
+
if (TERM_PROGRAM) {
|
|
141
|
+
if (caseEq(TERM_PROGRAM, "kitty")) return "kitty";
|
|
142
|
+
if (caseEq(TERM_PROGRAM, "ghostty")) return "ghostty";
|
|
143
|
+
if (caseEq(TERM_PROGRAM, "wezterm")) return "wezterm";
|
|
144
|
+
if (caseEq(TERM_PROGRAM, "iterm.app")) return "iterm2";
|
|
145
|
+
if (caseEq(TERM_PROGRAM, "vscode")) return "vscode";
|
|
146
|
+
if (caseEq(TERM_PROGRAM, "alacritty")) return "alacritty";
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (!!TERM && TERM.toLowerCase().includes("ghostty")) return "ghostty";
|
|
150
|
+
|
|
151
|
+
if (COLORTERM) {
|
|
152
|
+
if (caseEq(COLORTERM, "truecolor") || caseEq(COLORTERM, "24bit")) return "trueColor";
|
|
153
|
+
}
|
|
154
|
+
return "base";
|
|
155
|
+
})();
|
|
156
|
+
|
|
157
|
+
export const TERMINAL = (() => {
|
|
158
|
+
const terminal = getTerminalInfo(TERMINAL_ID);
|
|
159
|
+
const forcedImageProtocol = getForcedImageProtocol();
|
|
160
|
+
if (forcedImageProtocol !== undefined) {
|
|
161
|
+
return new TerminalInfo(
|
|
162
|
+
terminal.id,
|
|
163
|
+
forcedImageProtocol,
|
|
164
|
+
terminal.trueColor,
|
|
165
|
+
terminal.hyperlinks,
|
|
166
|
+
terminal.notifyProtocol,
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
if (!terminal.imageProtocol) {
|
|
170
|
+
const fallbackImageProtocol = getFallbackImageProtocol(terminal.id);
|
|
171
|
+
if (fallbackImageProtocol) {
|
|
172
|
+
return new TerminalInfo(
|
|
173
|
+
terminal.id,
|
|
174
|
+
fallbackImageProtocol,
|
|
175
|
+
terminal.trueColor,
|
|
176
|
+
terminal.hyperlinks,
|
|
177
|
+
terminal.notifyProtocol,
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return terminal;
|
|
182
|
+
})();
|
|
183
|
+
|
|
184
|
+
type MutableTerminalInfo = {
|
|
185
|
+
imageProtocol: ImageProtocol | null;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Override terminal image protocol at runtime after capability probes complete.
|
|
190
|
+
*/
|
|
191
|
+
export function setTerminalImageProtocol(imageProtocol: ImageProtocol | null): void {
|
|
192
|
+
(TERMINAL as unknown as MutableTerminalInfo).imageProtocol = imageProtocol;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export function getTerminalInfo(terminalId: TerminalId): TerminalInfo {
|
|
196
|
+
return KNOWN_TERMINALS[terminalId];
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export interface CellDimensions {
|
|
200
|
+
widthPx: number;
|
|
201
|
+
heightPx: number;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export interface ImageDimensions {
|
|
205
|
+
widthPx: number;
|
|
206
|
+
heightPx: number;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export interface ImageRenderOptions {
|
|
210
|
+
maxWidthCells?: number;
|
|
211
|
+
maxHeightCells?: number;
|
|
212
|
+
preserveAspectRatio?: boolean;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Default cell dimensions - updated by TUI when terminal responds to query
|
|
216
|
+
let cellDimensions: CellDimensions = { widthPx: 9, heightPx: 18 };
|
|
217
|
+
|
|
218
|
+
export function getCellDimensions(): CellDimensions {
|
|
219
|
+
return cellDimensions;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export function setCellDimensions(dims: CellDimensions): void {
|
|
223
|
+
cellDimensions = dims;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export function encodeKitty(
|
|
227
|
+
base64Data: string,
|
|
228
|
+
options: {
|
|
229
|
+
columns?: number;
|
|
230
|
+
rows?: number;
|
|
231
|
+
imageId?: number;
|
|
232
|
+
} = {},
|
|
233
|
+
): string {
|
|
234
|
+
const CHUNK_SIZE = 4096;
|
|
235
|
+
|
|
236
|
+
const params: string[] = ["a=T", "f=100", "q=2"];
|
|
237
|
+
|
|
238
|
+
if (options.columns) params.push(`c=${options.columns}`);
|
|
239
|
+
if (options.rows) params.push(`r=${options.rows}`);
|
|
240
|
+
if (options.imageId) params.push(`i=${options.imageId}`);
|
|
241
|
+
|
|
242
|
+
if (base64Data.length <= CHUNK_SIZE) {
|
|
243
|
+
return `\x1b_G${params.join(",")};${base64Data}\x1b\\`;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const chunks: string[] = [];
|
|
247
|
+
let offset = 0;
|
|
248
|
+
let isFirst = true;
|
|
249
|
+
|
|
250
|
+
while (offset < base64Data.length) {
|
|
251
|
+
const chunk = base64Data.slice(offset, offset + CHUNK_SIZE);
|
|
252
|
+
const isLast = offset + CHUNK_SIZE >= base64Data.length;
|
|
253
|
+
|
|
254
|
+
if (isFirst) {
|
|
255
|
+
chunks.push(`\x1b_G${params.join(",")},m=1;${chunk}\x1b\\`);
|
|
256
|
+
isFirst = false;
|
|
257
|
+
} else if (isLast) {
|
|
258
|
+
chunks.push(`\x1b_Gm=0;${chunk}\x1b\\`);
|
|
259
|
+
} else {
|
|
260
|
+
chunks.push(`\x1b_Gm=1;${chunk}\x1b\\`);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
offset += CHUNK_SIZE;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return chunks.join("");
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export function encodeITerm2(
|
|
270
|
+
base64Data: string,
|
|
271
|
+
options: {
|
|
272
|
+
width?: number | string;
|
|
273
|
+
height?: number | string;
|
|
274
|
+
name?: string;
|
|
275
|
+
preserveAspectRatio?: boolean;
|
|
276
|
+
inline?: boolean;
|
|
277
|
+
} = {},
|
|
278
|
+
): string {
|
|
279
|
+
const params: string[] = [`inline=${options.inline !== false ? 1 : 0}`];
|
|
280
|
+
|
|
281
|
+
if (options.width !== undefined) params.push(`width=${options.width}`);
|
|
282
|
+
if (options.height !== undefined) params.push(`height=${options.height}`);
|
|
283
|
+
if (options.name) {
|
|
284
|
+
const nameBase64 = Buffer.from(options.name).toBase64();
|
|
285
|
+
params.push(`name=${nameBase64}`);
|
|
286
|
+
}
|
|
287
|
+
if (options.preserveAspectRatio === false) {
|
|
288
|
+
params.push("preserveAspectRatio=0");
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
return `\x1b]1337;File=${params.join(";")}:${base64Data}\x07`;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export function calculateImageRows(
|
|
295
|
+
imageDimensions: ImageDimensions,
|
|
296
|
+
targetWidthCells: number,
|
|
297
|
+
cellDimensions: CellDimensions = { widthPx: 9, heightPx: 18 },
|
|
298
|
+
): number {
|
|
299
|
+
const targetWidthPx = targetWidthCells * cellDimensions.widthPx;
|
|
300
|
+
const scale = targetWidthPx / imageDimensions.widthPx;
|
|
301
|
+
const scaledHeightPx = imageDimensions.heightPx * scale;
|
|
302
|
+
const rows = Math.ceil(scaledHeightPx / cellDimensions.heightPx);
|
|
303
|
+
return Math.max(1, rows);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function calculateImageFit(
|
|
307
|
+
imageDimensions: ImageDimensions,
|
|
308
|
+
options: ImageRenderOptions,
|
|
309
|
+
cellDims: CellDimensions,
|
|
310
|
+
): { columns: number; rows: number } {
|
|
311
|
+
const maxColumns = options.maxWidthCells !== undefined ? Math.max(1, Math.floor(options.maxWidthCells)) : undefined;
|
|
312
|
+
const maxRows = options.maxHeightCells !== undefined ? Math.max(1, Math.floor(options.maxHeightCells)) : undefined;
|
|
313
|
+
|
|
314
|
+
if (maxColumns === undefined && maxRows === undefined) {
|
|
315
|
+
const columns = Math.max(1, Math.ceil(imageDimensions.widthPx / cellDims.widthPx));
|
|
316
|
+
const rows = Math.max(1, Math.ceil(imageDimensions.heightPx / cellDims.heightPx));
|
|
317
|
+
return { columns, rows };
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const maxWidthPx = maxColumns !== undefined ? maxColumns * cellDims.widthPx : Number.POSITIVE_INFINITY;
|
|
321
|
+
const maxHeightPx = maxRows !== undefined ? maxRows * cellDims.heightPx : Number.POSITIVE_INFINITY;
|
|
322
|
+
const scale = Math.min(maxWidthPx / imageDimensions.widthPx, maxHeightPx / imageDimensions.heightPx);
|
|
323
|
+
const fittedWidthPx = imageDimensions.widthPx * scale;
|
|
324
|
+
const fittedHeightPx = imageDimensions.heightPx * scale;
|
|
325
|
+
|
|
326
|
+
const columns = Math.max(1, Math.floor(fittedWidthPx / cellDims.widthPx));
|
|
327
|
+
const rows = Math.max(1, Math.ceil(fittedHeightPx / cellDims.heightPx));
|
|
328
|
+
|
|
329
|
+
return {
|
|
330
|
+
columns: maxColumns !== undefined ? Math.min(columns, maxColumns) : columns,
|
|
331
|
+
rows: maxRows !== undefined ? Math.min(rows, maxRows) : rows,
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export function getPngDimensions(base64Data: string): ImageDimensions | null {
|
|
336
|
+
try {
|
|
337
|
+
const buffer = Buffer.from(base64Data, "base64");
|
|
338
|
+
|
|
339
|
+
if (buffer.length < 24) {
|
|
340
|
+
return null;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
if (buffer[0] !== 0x89 || buffer[1] !== 0x50 || buffer[2] !== 0x4e || buffer[3] !== 0x47) {
|
|
344
|
+
return null;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const width = buffer.readUInt32BE(16);
|
|
348
|
+
const height = buffer.readUInt32BE(20);
|
|
349
|
+
|
|
350
|
+
return { widthPx: width, heightPx: height };
|
|
351
|
+
} catch {
|
|
352
|
+
return null;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
export function getJpegDimensions(base64Data: string): ImageDimensions | null {
|
|
357
|
+
try {
|
|
358
|
+
const buffer = Buffer.from(base64Data, "base64");
|
|
359
|
+
|
|
360
|
+
if (buffer.length < 2) {
|
|
361
|
+
return null;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
if (buffer[0] !== 0xff || buffer[1] !== 0xd8) {
|
|
365
|
+
return null;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
let offset = 2;
|
|
369
|
+
while (offset < buffer.length - 9) {
|
|
370
|
+
if (buffer[offset] !== 0xff) {
|
|
371
|
+
offset++;
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const marker = buffer[offset + 1];
|
|
376
|
+
|
|
377
|
+
if (marker >= 0xc0 && marker <= 0xc2) {
|
|
378
|
+
const height = buffer.readUInt16BE(offset + 5);
|
|
379
|
+
const width = buffer.readUInt16BE(offset + 7);
|
|
380
|
+
return { widthPx: width, heightPx: height };
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
if (offset + 3 >= buffer.length) {
|
|
384
|
+
return null;
|
|
385
|
+
}
|
|
386
|
+
const length = buffer.readUInt16BE(offset + 2);
|
|
387
|
+
if (length < 2) {
|
|
388
|
+
return null;
|
|
389
|
+
}
|
|
390
|
+
offset += 2 + length;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
return null;
|
|
394
|
+
} catch {
|
|
395
|
+
return null;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
export function getGifDimensions(base64Data: string): ImageDimensions | null {
|
|
400
|
+
try {
|
|
401
|
+
const buffer = Buffer.from(base64Data, "base64");
|
|
402
|
+
|
|
403
|
+
if (buffer.length < 10) {
|
|
404
|
+
return null;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const sig = buffer.slice(0, 6).toString("ascii");
|
|
408
|
+
if (sig !== "GIF87a" && sig !== "GIF89a") {
|
|
409
|
+
return null;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
const width = buffer.readUInt16LE(6);
|
|
413
|
+
const height = buffer.readUInt16LE(8);
|
|
414
|
+
|
|
415
|
+
return { widthPx: width, heightPx: height };
|
|
416
|
+
} catch {
|
|
417
|
+
return null;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
export function getWebpDimensions(base64Data: string): ImageDimensions | null {
|
|
422
|
+
try {
|
|
423
|
+
const buffer = Buffer.from(base64Data, "base64");
|
|
424
|
+
|
|
425
|
+
if (buffer.length < 30) {
|
|
426
|
+
return null;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const riff = buffer.slice(0, 4).toString("ascii");
|
|
430
|
+
const webp = buffer.slice(8, 12).toString("ascii");
|
|
431
|
+
if (riff !== "RIFF" || webp !== "WEBP") {
|
|
432
|
+
return null;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const chunk = buffer.slice(12, 16).toString("ascii");
|
|
436
|
+
if (chunk === "VP8 ") {
|
|
437
|
+
if (buffer.length < 30) return null;
|
|
438
|
+
const width = buffer.readUInt16LE(26) & 0x3fff;
|
|
439
|
+
const height = buffer.readUInt16LE(28) & 0x3fff;
|
|
440
|
+
return { widthPx: width, heightPx: height };
|
|
441
|
+
} else if (chunk === "VP8L") {
|
|
442
|
+
if (buffer.length < 25) return null;
|
|
443
|
+
const bits = buffer.readUInt32LE(21);
|
|
444
|
+
const width = (bits & 0x3fff) + 1;
|
|
445
|
+
const height = ((bits >> 14) & 0x3fff) + 1;
|
|
446
|
+
return { widthPx: width, heightPx: height };
|
|
447
|
+
} else if (chunk === "VP8X") {
|
|
448
|
+
if (buffer.length < 30) return null;
|
|
449
|
+
const width = (buffer[24] | (buffer[25] << 8) | (buffer[26] << 16)) + 1;
|
|
450
|
+
const height = (buffer[27] | (buffer[28] << 8) | (buffer[29] << 16)) + 1;
|
|
451
|
+
return { widthPx: width, heightPx: height };
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
return null;
|
|
455
|
+
} catch {
|
|
456
|
+
return null;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
export function getImageDimensions(base64Data: string, mimeType: string): ImageDimensions | null {
|
|
461
|
+
if (mimeType === "image/png") {
|
|
462
|
+
return getPngDimensions(base64Data);
|
|
463
|
+
}
|
|
464
|
+
if (mimeType === "image/jpeg") {
|
|
465
|
+
return getJpegDimensions(base64Data);
|
|
466
|
+
}
|
|
467
|
+
if (mimeType === "image/gif") {
|
|
468
|
+
return getGifDimensions(base64Data);
|
|
469
|
+
}
|
|
470
|
+
if (mimeType === "image/webp") {
|
|
471
|
+
return getWebpDimensions(base64Data);
|
|
472
|
+
}
|
|
473
|
+
return null;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
export function renderImage(
|
|
477
|
+
base64Data: string,
|
|
478
|
+
imageDimensions: ImageDimensions,
|
|
479
|
+
options: ImageRenderOptions = {},
|
|
480
|
+
): { sequence: string; rows: number } | null {
|
|
481
|
+
if (!TERMINAL.imageProtocol) {
|
|
482
|
+
return null;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
const cellDims = getCellDimensions();
|
|
486
|
+
const fit = calculateImageFit(imageDimensions, options, cellDims);
|
|
487
|
+
|
|
488
|
+
if (TERMINAL.imageProtocol === ImageProtocol.Kitty) {
|
|
489
|
+
const sequence = encodeKitty(base64Data, {
|
|
490
|
+
columns: fit.columns,
|
|
491
|
+
rows: fit.rows,
|
|
492
|
+
});
|
|
493
|
+
return { sequence, rows: fit.rows };
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
if (TERMINAL.imageProtocol === ImageProtocol.Sixel) {
|
|
497
|
+
try {
|
|
498
|
+
const targetWidthPx = Math.max(1, fit.columns * cellDims.widthPx);
|
|
499
|
+
const targetHeightPx = Math.max(1, fit.rows * cellDims.heightPx);
|
|
500
|
+
const decoded = new Uint8Array(Buffer.from(base64Data, "base64"));
|
|
501
|
+
const sequence = encodeSixel(decoded, targetWidthPx, targetHeightPx);
|
|
502
|
+
return { sequence, rows: fit.rows };
|
|
503
|
+
} catch {
|
|
504
|
+
return null;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
if (TERMINAL.imageProtocol === ImageProtocol.Iterm2) {
|
|
508
|
+
const sequence = encodeITerm2(base64Data, {
|
|
509
|
+
width: fit.columns,
|
|
510
|
+
height: "auto",
|
|
511
|
+
preserveAspectRatio: options.preserveAspectRatio ?? true,
|
|
512
|
+
});
|
|
513
|
+
return { sequence, rows: fit.rows };
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
return null;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
export function imageFallback(mimeType: string, dimensions?: ImageDimensions, filename?: string): string {
|
|
520
|
+
const parts: string[] = [];
|
|
521
|
+
if (filename) parts.push(filename);
|
|
522
|
+
parts.push(`[${mimeType}]`);
|
|
523
|
+
if (dimensions) parts.push(`${dimensions.widthPx}x${dimensions.heightPx}`);
|
|
524
|
+
return `[Image: ${parts.join(" ")}]`;
|
|
525
|
+
}
|