@wayofmono/wo-tui 1.0.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/README.md +18 -0
- package/dist/index.js +32 -0
- package/package.json +38 -0
- package/src/autocomplete.ts +783 -0
- package/src/components/box.ts +137 -0
- package/src/components/cancellable-loader.ts +40 -0
- package/src/components/editor.ts +2292 -0
- package/src/components/image.ts +121 -0
- package/src/components/input.ts +503 -0
- package/src/components/loader.ts +86 -0
- package/src/components/markdown.ts +797 -0
- package/src/components/select-list.ts +229 -0
- package/src/components/settings-list.ts +250 -0
- package/src/components/spacer.ts +28 -0
- package/src/components/text.ts +106 -0
- package/src/components/truncated-text.ts +65 -0
- package/src/editor-component.ts +74 -0
- package/src/fuzzy.ts +137 -0
- package/src/index.ts +106 -0
- package/src/keybindings.ts +244 -0
- package/src/keys.ts +1400 -0
- package/src/kill-ring.ts +46 -0
- package/src/stdin-buffer.ts +411 -0
- package/src/terminal-image.ts +423 -0
- package/src/terminal.ts +395 -0
- package/src/tui.ts +1319 -0
- package/src/undo-stack.ts +28 -0
- package/src/utils.ts +1140 -0
|
@@ -0,0 +1,423 @@
|
|
|
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
|
+
/** Kitty image ID. If provided, reuses/replaces existing image with this ID. */
|
|
24
|
+
imageId?: number;
|
|
25
|
+
/** Whether Kitty should apply its default cursor movement after placement. */
|
|
26
|
+
moveCursor?: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
let cachedCapabilities: TerminalCapabilities | null = null;
|
|
30
|
+
|
|
31
|
+
// Default cell dimensions - updated by TUI when terminal responds to query
|
|
32
|
+
let cellDimensions: CellDimensions = { widthPx: 9, heightPx: 18 };
|
|
33
|
+
|
|
34
|
+
export function getCellDimensions(): CellDimensions {
|
|
35
|
+
return cellDimensions;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function setCellDimensions(dims: CellDimensions): void {
|
|
39
|
+
cellDimensions = dims;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function detectCapabilities(): TerminalCapabilities {
|
|
43
|
+
const termProgram = process.env.TERM_PROGRAM?.toLowerCase() || "";
|
|
44
|
+
const term = process.env.TERM?.toLowerCase() || "";
|
|
45
|
+
const colorTerm = process.env.COLORTERM?.toLowerCase() || "";
|
|
46
|
+
|
|
47
|
+
// tmux and screen swallow OSC 8 by default (passthrough is opt-in and wraps
|
|
48
|
+
// sequences differently). Force hyperlinks off whenever we detect them, even
|
|
49
|
+
// when the outer terminal would otherwise support OSC 8. Image protocols are
|
|
50
|
+
// also unreliable under tmux/screen, so leave `images: null` for safety.
|
|
51
|
+
const inTmuxOrScreen = !!process.env.TMUX || term.startsWith("tmux") || term.startsWith("screen");
|
|
52
|
+
if (inTmuxOrScreen) {
|
|
53
|
+
const trueColor = colorTerm === "truecolor" || colorTerm === "24bit";
|
|
54
|
+
return { images: null, trueColor, hyperlinks: false };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (process.env.KITTY_WINDOW_ID || termProgram === "kitty") {
|
|
58
|
+
return { images: "kitty", trueColor: true, hyperlinks: true };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (termProgram === "ghostty" || term.includes("ghostty") || process.env.GHOSTTY_RESOURCES_DIR) {
|
|
62
|
+
return { images: "kitty", trueColor: true, hyperlinks: true };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (process.env.WEZTERM_PANE || termProgram === "wezterm") {
|
|
66
|
+
return { images: "kitty", trueColor: true, hyperlinks: true };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (process.env.ITERM_SESSION_ID || termProgram === "iterm.app") {
|
|
70
|
+
return { images: "iterm2", trueColor: true, hyperlinks: true };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (termProgram === "vscode") {
|
|
74
|
+
return { images: null, trueColor: true, hyperlinks: true };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (termProgram === "alacritty") {
|
|
78
|
+
return { images: null, trueColor: true, hyperlinks: true };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Unknown terminal: be conservative. OSC 8 is rendered invisibly as "just
|
|
82
|
+
// text" on terminals that swallow it, which means the URL disappears from
|
|
83
|
+
// the rendered output. Default to the legacy `text (url)` behavior unless we
|
|
84
|
+
// have positively identified a hyperlink-capable terminal above.
|
|
85
|
+
const trueColor = colorTerm === "truecolor" || colorTerm === "24bit";
|
|
86
|
+
return { images: null, trueColor, hyperlinks: false };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function getCapabilities(): TerminalCapabilities {
|
|
90
|
+
if (!cachedCapabilities) {
|
|
91
|
+
cachedCapabilities = detectCapabilities();
|
|
92
|
+
}
|
|
93
|
+
return cachedCapabilities;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function resetCapabilitiesCache(): void {
|
|
97
|
+
cachedCapabilities = null;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Override the cached capabilities. Useful in tests to exercise both code paths. */
|
|
101
|
+
export function setCapabilities(caps: TerminalCapabilities): void {
|
|
102
|
+
cachedCapabilities = caps;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const KITTY_PREFIX = "\x1b_G";
|
|
106
|
+
const ITERM2_PREFIX = "\x1b]1337;File=";
|
|
107
|
+
|
|
108
|
+
export function isImageLine(line: string): boolean {
|
|
109
|
+
// Fast path: sequence at line start (single-row images)
|
|
110
|
+
if (line.startsWith(KITTY_PREFIX) || line.startsWith(ITERM2_PREFIX)) {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
// Slow path: sequence elsewhere (multi-row images have cursor-up prefix)
|
|
114
|
+
return line.includes(KITTY_PREFIX) || line.includes(ITERM2_PREFIX);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Generate a random image ID for Kitty graphics protocol.
|
|
119
|
+
* Uses random IDs to avoid collisions between different module instances
|
|
120
|
+
* (e.g., main app vs extensions).
|
|
121
|
+
*/
|
|
122
|
+
export function allocateImageId(): number {
|
|
123
|
+
// Use random ID in range [1, 0xffffffff] to avoid collisions
|
|
124
|
+
return Math.floor(Math.random() * 0xfffffffe) + 1;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function encodeKitty(
|
|
128
|
+
base64Data: string,
|
|
129
|
+
options: {
|
|
130
|
+
columns?: number;
|
|
131
|
+
rows?: number;
|
|
132
|
+
imageId?: number;
|
|
133
|
+
/** Whether Kitty should apply its default cursor movement after placement. Default: true. */
|
|
134
|
+
moveCursor?: boolean;
|
|
135
|
+
} = {},
|
|
136
|
+
): string {
|
|
137
|
+
const CHUNK_SIZE = 4096;
|
|
138
|
+
|
|
139
|
+
const params: string[] = ["a=T", "f=100", "q=2"];
|
|
140
|
+
|
|
141
|
+
if (options.moveCursor === false) params.push("C=1");
|
|
142
|
+
if (options.columns) params.push(`c=${options.columns}`);
|
|
143
|
+
if (options.rows) params.push(`r=${options.rows}`);
|
|
144
|
+
if (options.imageId) params.push(`i=${options.imageId}`);
|
|
145
|
+
|
|
146
|
+
if (base64Data.length <= CHUNK_SIZE) {
|
|
147
|
+
return `\x1b_G${params.join(",")};${base64Data}\x1b\\`;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const chunks: string[] = [];
|
|
151
|
+
let offset = 0;
|
|
152
|
+
let isFirst = true;
|
|
153
|
+
|
|
154
|
+
while (offset < base64Data.length) {
|
|
155
|
+
const chunk = base64Data.slice(offset, offset + CHUNK_SIZE);
|
|
156
|
+
const isLast = offset + CHUNK_SIZE >= base64Data.length;
|
|
157
|
+
|
|
158
|
+
if (isFirst) {
|
|
159
|
+
chunks.push(`\x1b_G${params.join(",")},m=1;${chunk}\x1b\\`);
|
|
160
|
+
isFirst = false;
|
|
161
|
+
} else if (isLast) {
|
|
162
|
+
chunks.push(`\x1b_Gm=0;${chunk}\x1b\\`);
|
|
163
|
+
} else {
|
|
164
|
+
chunks.push(`\x1b_Gm=1;${chunk}\x1b\\`);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
offset += CHUNK_SIZE;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return chunks.join("");
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Delete a Kitty graphics image by ID.
|
|
175
|
+
* Uses uppercase 'I' to also free the image data.
|
|
176
|
+
*/
|
|
177
|
+
export function deleteKittyImage(imageId: number): string {
|
|
178
|
+
return `\x1b_Ga=d,d=I,i=${imageId},q=2\x1b\\`;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Delete all visible Kitty graphics images.
|
|
183
|
+
* Uses uppercase 'A' to also free the image data.
|
|
184
|
+
*/
|
|
185
|
+
export function deleteAllKittyImages(): string {
|
|
186
|
+
return "\x1b_Ga=d,d=A,q=2\x1b\\";
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function encodeITerm2(
|
|
190
|
+
base64Data: string,
|
|
191
|
+
options: {
|
|
192
|
+
width?: number | string;
|
|
193
|
+
height?: number | string;
|
|
194
|
+
name?: string;
|
|
195
|
+
preserveAspectRatio?: boolean;
|
|
196
|
+
inline?: boolean;
|
|
197
|
+
} = {},
|
|
198
|
+
): string {
|
|
199
|
+
const params: string[] = [`inline=${options.inline !== false ? 1 : 0}`];
|
|
200
|
+
|
|
201
|
+
if (options.width !== undefined) params.push(`width=${options.width}`);
|
|
202
|
+
if (options.height !== undefined) params.push(`height=${options.height}`);
|
|
203
|
+
if (options.name) {
|
|
204
|
+
const nameBase64 = Buffer.from(options.name).toString("base64");
|
|
205
|
+
params.push(`name=${nameBase64}`);
|
|
206
|
+
}
|
|
207
|
+
if (options.preserveAspectRatio === false) {
|
|
208
|
+
params.push("preserveAspectRatio=0");
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return `\x1b]1337;File=${params.join(";")}:${base64Data}\x07`;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export function calculateImageRows(
|
|
215
|
+
imageDimensions: ImageDimensions,
|
|
216
|
+
targetWidthCells: number,
|
|
217
|
+
cellDimensions: CellDimensions = { widthPx: 9, heightPx: 18 },
|
|
218
|
+
): number {
|
|
219
|
+
const targetWidthPx = targetWidthCells * cellDimensions.widthPx;
|
|
220
|
+
const scale = targetWidthPx / imageDimensions.widthPx;
|
|
221
|
+
const scaledHeightPx = imageDimensions.heightPx * scale;
|
|
222
|
+
const rows = Math.ceil(scaledHeightPx / cellDimensions.heightPx);
|
|
223
|
+
return Math.max(1, rows);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export function getPngDimensions(base64Data: string): ImageDimensions | null {
|
|
227
|
+
try {
|
|
228
|
+
const buffer = Buffer.from(base64Data, "base64");
|
|
229
|
+
|
|
230
|
+
if (buffer.length < 24) {
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (buffer[0] !== 0x89 || buffer[1] !== 0x50 || buffer[2] !== 0x4e || buffer[3] !== 0x47) {
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const width = buffer.readUInt32BE(16);
|
|
239
|
+
const height = buffer.readUInt32BE(20);
|
|
240
|
+
|
|
241
|
+
return { widthPx: width, heightPx: height };
|
|
242
|
+
} catch {
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export function getJpegDimensions(base64Data: string): ImageDimensions | null {
|
|
248
|
+
try {
|
|
249
|
+
const buffer = Buffer.from(base64Data, "base64");
|
|
250
|
+
|
|
251
|
+
if (buffer.length < 2) {
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (buffer[0] !== 0xff || buffer[1] !== 0xd8) {
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
let offset = 2;
|
|
260
|
+
while (offset < buffer.length - 9) {
|
|
261
|
+
if (buffer[offset] !== 0xff) {
|
|
262
|
+
offset++;
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const marker = buffer[offset + 1];
|
|
267
|
+
|
|
268
|
+
if (marker >= 0xc0 && marker <= 0xc2) {
|
|
269
|
+
const height = buffer.readUInt16BE(offset + 5);
|
|
270
|
+
const width = buffer.readUInt16BE(offset + 7);
|
|
271
|
+
return { widthPx: width, heightPx: height };
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (offset + 3 >= buffer.length) {
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
const length = buffer.readUInt16BE(offset + 2);
|
|
278
|
+
if (length < 2) {
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
offset += 2 + length;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return null;
|
|
285
|
+
} catch {
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export function getGifDimensions(base64Data: string): ImageDimensions | null {
|
|
291
|
+
try {
|
|
292
|
+
const buffer = Buffer.from(base64Data, "base64");
|
|
293
|
+
|
|
294
|
+
if (buffer.length < 10) {
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const sig = buffer.slice(0, 6).toString("ascii");
|
|
299
|
+
if (sig !== "GIF87a" && sig !== "GIF89a") {
|
|
300
|
+
return null;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const width = buffer.readUInt16LE(6);
|
|
304
|
+
const height = buffer.readUInt16LE(8);
|
|
305
|
+
|
|
306
|
+
return { widthPx: width, heightPx: height };
|
|
307
|
+
} catch {
|
|
308
|
+
return null;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export function getWebpDimensions(base64Data: string): ImageDimensions | null {
|
|
313
|
+
try {
|
|
314
|
+
const buffer = Buffer.from(base64Data, "base64");
|
|
315
|
+
|
|
316
|
+
if (buffer.length < 30) {
|
|
317
|
+
return null;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const riff = buffer.slice(0, 4).toString("ascii");
|
|
321
|
+
const webp = buffer.slice(8, 12).toString("ascii");
|
|
322
|
+
if (riff !== "RIFF" || webp !== "WEBP") {
|
|
323
|
+
return null;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const chunk = buffer.slice(12, 16).toString("ascii");
|
|
327
|
+
if (chunk === "VP8 ") {
|
|
328
|
+
if (buffer.length < 30) return null;
|
|
329
|
+
const width = buffer.readUInt16LE(26) & 0x3fff;
|
|
330
|
+
const height = buffer.readUInt16LE(28) & 0x3fff;
|
|
331
|
+
return { widthPx: width, heightPx: height };
|
|
332
|
+
} else if (chunk === "VP8L") {
|
|
333
|
+
if (buffer.length < 25) return null;
|
|
334
|
+
const bits = buffer.readUInt32LE(21);
|
|
335
|
+
const width = (bits & 0x3fff) + 1;
|
|
336
|
+
const height = ((bits >> 14) & 0x3fff) + 1;
|
|
337
|
+
return { widthPx: width, heightPx: height };
|
|
338
|
+
} else if (chunk === "VP8X") {
|
|
339
|
+
if (buffer.length < 30) return null;
|
|
340
|
+
const width = (buffer[24] | (buffer[25] << 8) | (buffer[26] << 16)) + 1;
|
|
341
|
+
const height = (buffer[27] | (buffer[28] << 8) | (buffer[29] << 16)) + 1;
|
|
342
|
+
return { widthPx: width, heightPx: height };
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
return null;
|
|
346
|
+
} catch {
|
|
347
|
+
return null;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
export function getImageDimensions(base64Data: string, mimeType: string): ImageDimensions | null {
|
|
352
|
+
if (mimeType === "image/png") {
|
|
353
|
+
return getPngDimensions(base64Data);
|
|
354
|
+
}
|
|
355
|
+
if (mimeType === "image/jpeg") {
|
|
356
|
+
return getJpegDimensions(base64Data);
|
|
357
|
+
}
|
|
358
|
+
if (mimeType === "image/gif") {
|
|
359
|
+
return getGifDimensions(base64Data);
|
|
360
|
+
}
|
|
361
|
+
if (mimeType === "image/webp") {
|
|
362
|
+
return getWebpDimensions(base64Data);
|
|
363
|
+
}
|
|
364
|
+
return null;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export function renderImage(
|
|
368
|
+
base64Data: string,
|
|
369
|
+
imageDimensions: ImageDimensions,
|
|
370
|
+
options: ImageRenderOptions = {},
|
|
371
|
+
): { sequence: string; rows: number; imageId?: number } | null {
|
|
372
|
+
const caps = getCapabilities();
|
|
373
|
+
|
|
374
|
+
if (!caps.images) {
|
|
375
|
+
return null;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
const maxWidth = options.maxWidthCells ?? 80;
|
|
379
|
+
const rows = calculateImageRows(imageDimensions, maxWidth, getCellDimensions());
|
|
380
|
+
|
|
381
|
+
if (caps.images === "kitty") {
|
|
382
|
+
const sequence = encodeKitty(base64Data, {
|
|
383
|
+
columns: maxWidth,
|
|
384
|
+
rows,
|
|
385
|
+
imageId: options.imageId,
|
|
386
|
+
moveCursor: options.moveCursor,
|
|
387
|
+
});
|
|
388
|
+
return { sequence, rows, imageId: options.imageId };
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
if (caps.images === "iterm2") {
|
|
392
|
+
const sequence = encodeITerm2(base64Data, {
|
|
393
|
+
width: maxWidth,
|
|
394
|
+
height: "auto",
|
|
395
|
+
preserveAspectRatio: options.preserveAspectRatio ?? true,
|
|
396
|
+
});
|
|
397
|
+
return { sequence, rows };
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
return null;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Wrap text in an OSC 8 hyperlink sequence.
|
|
405
|
+
* The text is rendered as a clickable hyperlink in terminals that support OSC 8
|
|
406
|
+
* (Ghostty, Kitty, WezTerm, iTerm2, VSCode, and others).
|
|
407
|
+
* In terminals that do not support OSC 8, the escape sequences are ignored
|
|
408
|
+
* and only the plain text is displayed.
|
|
409
|
+
*
|
|
410
|
+
* @param text - The visible text to display
|
|
411
|
+
* @param url - The URL to link to
|
|
412
|
+
*/
|
|
413
|
+
export function hyperlink(text: string, url: string): string {
|
|
414
|
+
return `\x1b]8;;${url}\x1b\\${text}\x1b]8;;\x1b\\`;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
export function imageFallback(mimeType: string, dimensions?: ImageDimensions, filename?: string): string {
|
|
418
|
+
const parts: string[] = [];
|
|
419
|
+
if (filename) parts.push(filename);
|
|
420
|
+
parts.push(`[${mimeType}]`);
|
|
421
|
+
if (dimensions) parts.push(`${dimensions.widthPx}x${dimensions.heightPx}`);
|
|
422
|
+
return `[Image: ${parts.join(" ")}]`;
|
|
423
|
+
}
|