@nghyane/arcane-tui 0.1.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,393 @@
1
+ import { $env } from "@nghyane/arcane-utils";
2
+
3
+ export enum ImageProtocol {
4
+ Kitty = "\x1b_G",
5
+ Iterm2 = "\x1b]1337;File=",
6
+ }
7
+
8
+ export enum NotifyProtocol {
9
+ Bell = "\x07",
10
+ Osc99 = "\x1b]99;;",
11
+ Osc9 = "\x1b]9;",
12
+ }
13
+
14
+ export type TerminalId = "kitty" | "ghostty" | "wezterm" | "iterm2" | "vscode" | "alacritty" | "base" | "trueColor";
15
+
16
+ /** Terminal capability details used for rendering and protocol selection. */
17
+ export class TerminalInfo {
18
+ constructor(
19
+ public readonly id: TerminalId,
20
+ public readonly imageProtocol: ImageProtocol | null,
21
+ public readonly trueColor: boolean,
22
+ public readonly hyperlinks: boolean,
23
+ public readonly notifyProtocol: NotifyProtocol = NotifyProtocol.Bell,
24
+ ) {}
25
+
26
+ isImageLine(line: string): boolean {
27
+ if (!this.imageProtocol) return false;
28
+ return line.slice(0, 64).includes(this.imageProtocol);
29
+ }
30
+
31
+ formatNotification(message: string): string {
32
+ if (this.notifyProtocol === NotifyProtocol.Bell) {
33
+ return NotifyProtocol.Bell;
34
+ }
35
+ return `${this.notifyProtocol}${message}\x1b\\`;
36
+ }
37
+
38
+ sendNotification(message: string): void {
39
+ if (isNotificationSuppressed()) return;
40
+ process.stdout.write(this.formatNotification(message));
41
+ }
42
+ }
43
+
44
+ export function isNotificationSuppressed(): boolean {
45
+ const value = $env.ARCANE_NOTIFICATIONS;
46
+ if (!value) return false;
47
+ return value === "off" || value === "0" || value === "false";
48
+ }
49
+
50
+ const KNOWN_TERMINALS = Object.freeze({
51
+ // Fallback terminals
52
+ base: new TerminalInfo("base", null, false, true, NotifyProtocol.Bell),
53
+ trueColor: new TerminalInfo("trueColor", null, true, true, NotifyProtocol.Bell),
54
+ // Recognized terminals
55
+ kitty: new TerminalInfo("kitty", ImageProtocol.Kitty, true, true, NotifyProtocol.Osc99),
56
+ ghostty: new TerminalInfo("ghostty", ImageProtocol.Kitty, true, true, NotifyProtocol.Osc9),
57
+ wezterm: new TerminalInfo("wezterm", ImageProtocol.Kitty, true, true, NotifyProtocol.Osc9),
58
+ iterm2: new TerminalInfo("iterm2", ImageProtocol.Iterm2, true, true, NotifyProtocol.Osc9),
59
+ vscode: new TerminalInfo("vscode", null, true, true, NotifyProtocol.Bell),
60
+ alacritty: new TerminalInfo("alacritty", null, true, true, NotifyProtocol.Bell),
61
+ });
62
+
63
+ export const TERMINAL_ID: TerminalId = (() => {
64
+ function caseEq(a: string, b: string): boolean {
65
+ return a.toLowerCase() === b.toLowerCase(); // For compiler to pattern match
66
+ }
67
+
68
+ const {
69
+ KITTY_WINDOW_ID,
70
+ GHOSTTY_RESOURCES_DIR,
71
+ WEZTERM_PANE,
72
+ ITERM_SESSION_ID,
73
+ VSCODE_PID,
74
+ ALACRITTY_WINDOW_ID,
75
+ TERM_PROGRAM,
76
+ TERM,
77
+ COLORTERM,
78
+ } = Bun.env;
79
+
80
+ if (KITTY_WINDOW_ID) return "kitty";
81
+ if (GHOSTTY_RESOURCES_DIR) return "ghostty";
82
+ if (WEZTERM_PANE) return "wezterm";
83
+ if (ITERM_SESSION_ID) return "iterm2";
84
+ if (VSCODE_PID) return "vscode";
85
+ if (ALACRITTY_WINDOW_ID) return "alacritty";
86
+
87
+ if (TERM_PROGRAM) {
88
+ if (caseEq(TERM_PROGRAM, "kitty")) return "kitty";
89
+ if (caseEq(TERM_PROGRAM, "ghostty")) return "ghostty";
90
+ if (caseEq(TERM_PROGRAM, "wezterm")) return "wezterm";
91
+ if (caseEq(TERM_PROGRAM, "iterm.app")) return "iterm2";
92
+ if (caseEq(TERM_PROGRAM, "vscode")) return "vscode";
93
+ if (caseEq(TERM_PROGRAM, "alacritty")) return "alacritty";
94
+ }
95
+
96
+ if (!!TERM && TERM.toLowerCase().includes("ghostty")) return "ghostty";
97
+
98
+ if (COLORTERM) {
99
+ if (caseEq(COLORTERM, "truecolor") || caseEq(COLORTERM, "24bit")) return "trueColor";
100
+ }
101
+ return "base";
102
+ })();
103
+
104
+ export const TERMINAL = getTerminalInfo(TERMINAL_ID);
105
+
106
+ export function getTerminalInfo(terminalId: TerminalId): TerminalInfo {
107
+ return KNOWN_TERMINALS[terminalId];
108
+ }
109
+
110
+ export interface CellDimensions {
111
+ widthPx: number;
112
+ heightPx: number;
113
+ }
114
+
115
+ export interface ImageDimensions {
116
+ widthPx: number;
117
+ heightPx: number;
118
+ }
119
+
120
+ export interface ImageRenderOptions {
121
+ maxWidthCells?: number;
122
+ maxHeightCells?: number;
123
+ preserveAspectRatio?: boolean;
124
+ }
125
+
126
+ // Default cell dimensions - updated by TUI when terminal responds to query
127
+ let cellDimensions: CellDimensions = { widthPx: 9, heightPx: 18 };
128
+
129
+ export function getCellDimensions(): CellDimensions {
130
+ return cellDimensions;
131
+ }
132
+
133
+ export function setCellDimensions(dims: CellDimensions): void {
134
+ cellDimensions = dims;
135
+ }
136
+
137
+ export function encodeKitty(
138
+ base64Data: string,
139
+ options: {
140
+ columns?: number;
141
+ rows?: number;
142
+ imageId?: number;
143
+ } = {},
144
+ ): string {
145
+ const CHUNK_SIZE = 4096;
146
+
147
+ const params: string[] = ["a=T", "f=100", "q=2"];
148
+
149
+ if (options.columns) params.push(`c=${options.columns}`);
150
+ if (options.rows) params.push(`r=${options.rows}`);
151
+ if (options.imageId) params.push(`i=${options.imageId}`);
152
+
153
+ if (base64Data.length <= CHUNK_SIZE) {
154
+ return `\x1b_G${params.join(",")};${base64Data}\x1b\\`;
155
+ }
156
+
157
+ const chunks: string[] = [];
158
+ let offset = 0;
159
+ let isFirst = true;
160
+
161
+ while (offset < base64Data.length) {
162
+ const chunk = base64Data.slice(offset, offset + CHUNK_SIZE);
163
+ const isLast = offset + CHUNK_SIZE >= base64Data.length;
164
+
165
+ if (isFirst) {
166
+ chunks.push(`\x1b_G${params.join(",")},m=1;${chunk}\x1b\\`);
167
+ isFirst = false;
168
+ } else if (isLast) {
169
+ chunks.push(`\x1b_Gm=0;${chunk}\x1b\\`);
170
+ } else {
171
+ chunks.push(`\x1b_Gm=1;${chunk}\x1b\\`);
172
+ }
173
+
174
+ offset += CHUNK_SIZE;
175
+ }
176
+
177
+ return chunks.join("");
178
+ }
179
+
180
+ export function encodeITerm2(
181
+ base64Data: string,
182
+ options: {
183
+ width?: number | string;
184
+ height?: number | string;
185
+ name?: string;
186
+ preserveAspectRatio?: boolean;
187
+ inline?: boolean;
188
+ } = {},
189
+ ): string {
190
+ const params: string[] = [`inline=${options.inline !== false ? 1 : 0}`];
191
+
192
+ if (options.width !== undefined) params.push(`width=${options.width}`);
193
+ if (options.height !== undefined) params.push(`height=${options.height}`);
194
+ if (options.name) {
195
+ const nameBase64 = Buffer.from(options.name).toBase64();
196
+ params.push(`name=${nameBase64}`);
197
+ }
198
+ if (options.preserveAspectRatio === false) {
199
+ params.push("preserveAspectRatio=0");
200
+ }
201
+
202
+ return `\x1b]1337;File=${params.join(";")}:${base64Data}\x07`;
203
+ }
204
+
205
+ export function calculateImageRows(
206
+ imageDimensions: ImageDimensions,
207
+ targetWidthCells: number,
208
+ cellDimensions: CellDimensions = { widthPx: 9, heightPx: 18 },
209
+ ): number {
210
+ const targetWidthPx = targetWidthCells * cellDimensions.widthPx;
211
+ const scale = targetWidthPx / imageDimensions.widthPx;
212
+ const scaledHeightPx = imageDimensions.heightPx * scale;
213
+ const rows = Math.ceil(scaledHeightPx / cellDimensions.heightPx);
214
+ return Math.max(1, rows);
215
+ }
216
+
217
+ export function getPngDimensions(base64Data: string): ImageDimensions | null {
218
+ try {
219
+ const buffer = Buffer.from(base64Data, "base64");
220
+
221
+ if (buffer.length < 24) {
222
+ return null;
223
+ }
224
+
225
+ if (buffer[0] !== 0x89 || buffer[1] !== 0x50 || buffer[2] !== 0x4e || buffer[3] !== 0x47) {
226
+ return null;
227
+ }
228
+
229
+ const width = buffer.readUInt32BE(16);
230
+ const height = buffer.readUInt32BE(20);
231
+
232
+ return { widthPx: width, heightPx: height };
233
+ } catch {
234
+ return null;
235
+ }
236
+ }
237
+
238
+ export function getJpegDimensions(base64Data: string): ImageDimensions | null {
239
+ try {
240
+ const buffer = Buffer.from(base64Data, "base64");
241
+
242
+ if (buffer.length < 2) {
243
+ return null;
244
+ }
245
+
246
+ if (buffer[0] !== 0xff || buffer[1] !== 0xd8) {
247
+ return null;
248
+ }
249
+
250
+ let offset = 2;
251
+ while (offset < buffer.length - 9) {
252
+ if (buffer[offset] !== 0xff) {
253
+ offset++;
254
+ continue;
255
+ }
256
+
257
+ const marker = buffer[offset + 1];
258
+
259
+ if (marker >= 0xc0 && marker <= 0xc2) {
260
+ const height = buffer.readUInt16BE(offset + 5);
261
+ const width = buffer.readUInt16BE(offset + 7);
262
+ return { widthPx: width, heightPx: height };
263
+ }
264
+
265
+ if (offset + 3 >= buffer.length) {
266
+ return null;
267
+ }
268
+ const length = buffer.readUInt16BE(offset + 2);
269
+ if (length < 2) {
270
+ return null;
271
+ }
272
+ offset += 2 + length;
273
+ }
274
+
275
+ return null;
276
+ } catch {
277
+ return null;
278
+ }
279
+ }
280
+
281
+ export function getGifDimensions(base64Data: string): ImageDimensions | null {
282
+ try {
283
+ const buffer = Buffer.from(base64Data, "base64");
284
+
285
+ if (buffer.length < 10) {
286
+ return null;
287
+ }
288
+
289
+ const sig = buffer.slice(0, 6).toString("ascii");
290
+ if (sig !== "GIF87a" && sig !== "GIF89a") {
291
+ return null;
292
+ }
293
+
294
+ const width = buffer.readUInt16LE(6);
295
+ const height = buffer.readUInt16LE(8);
296
+
297
+ return { widthPx: width, heightPx: height };
298
+ } catch {
299
+ return null;
300
+ }
301
+ }
302
+
303
+ export function getWebpDimensions(base64Data: string): ImageDimensions | null {
304
+ try {
305
+ const buffer = Buffer.from(base64Data, "base64");
306
+
307
+ if (buffer.length < 30) {
308
+ return null;
309
+ }
310
+
311
+ const riff = buffer.slice(0, 4).toString("ascii");
312
+ const webp = buffer.slice(8, 12).toString("ascii");
313
+ if (riff !== "RIFF" || webp !== "WEBP") {
314
+ return null;
315
+ }
316
+
317
+ const chunk = buffer.slice(12, 16).toString("ascii");
318
+ if (chunk === "VP8 ") {
319
+ if (buffer.length < 30) return null;
320
+ const width = buffer.readUInt16LE(26) & 0x3fff;
321
+ const height = buffer.readUInt16LE(28) & 0x3fff;
322
+ return { widthPx: width, heightPx: height };
323
+ } else if (chunk === "VP8L") {
324
+ if (buffer.length < 25) return null;
325
+ const bits = buffer.readUInt32LE(21);
326
+ const width = (bits & 0x3fff) + 1;
327
+ const height = ((bits >> 14) & 0x3fff) + 1;
328
+ return { widthPx: width, heightPx: height };
329
+ } else if (chunk === "VP8X") {
330
+ if (buffer.length < 30) return null;
331
+ const width = (buffer[24] | (buffer[25] << 8) | (buffer[26] << 16)) + 1;
332
+ const height = (buffer[27] | (buffer[28] << 8) | (buffer[29] << 16)) + 1;
333
+ return { widthPx: width, heightPx: height };
334
+ }
335
+
336
+ return null;
337
+ } catch {
338
+ return null;
339
+ }
340
+ }
341
+
342
+ export function getImageDimensions(base64Data: string, mimeType: string): ImageDimensions | null {
343
+ if (mimeType === "image/png") {
344
+ return getPngDimensions(base64Data);
345
+ }
346
+ if (mimeType === "image/jpeg") {
347
+ return getJpegDimensions(base64Data);
348
+ }
349
+ if (mimeType === "image/gif") {
350
+ return getGifDimensions(base64Data);
351
+ }
352
+ if (mimeType === "image/webp") {
353
+ return getWebpDimensions(base64Data);
354
+ }
355
+ return null;
356
+ }
357
+
358
+ export function renderImage(
359
+ base64Data: string,
360
+ imageDimensions: ImageDimensions,
361
+ options: ImageRenderOptions = {},
362
+ ): { sequence: string; rows: number } | null {
363
+ if (!TERMINAL.imageProtocol) {
364
+ return null;
365
+ }
366
+
367
+ const maxWidth = options.maxWidthCells ?? 80;
368
+ const rows = calculateImageRows(imageDimensions, maxWidth, getCellDimensions());
369
+
370
+ if (TERMINAL.imageProtocol === ImageProtocol.Kitty) {
371
+ const sequence = encodeKitty(base64Data, { columns: maxWidth, rows });
372
+ return { sequence, rows };
373
+ }
374
+
375
+ if (TERMINAL.imageProtocol === ImageProtocol.Iterm2) {
376
+ const sequence = encodeITerm2(base64Data, {
377
+ width: maxWidth,
378
+ height: "auto",
379
+ preserveAspectRatio: options.preserveAspectRatio ?? true,
380
+ });
381
+ return { sequence, rows };
382
+ }
383
+
384
+ return null;
385
+ }
386
+
387
+ export function imageFallback(mimeType: string, dimensions?: ImageDimensions, filename?: string): string {
388
+ const parts: string[] = [];
389
+ if (filename) parts.push(filename);
390
+ parts.push(`[${mimeType}]`);
391
+ if (dimensions) parts.push(`${dimensions.widthPx}x${dimensions.heightPx}`);
392
+ return `[Image: ${parts.join(" ")}]`;
393
+ }