@each1024/pi-mini-mode 1.3.14 → 2.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 +53 -43
- package/README.zh-CN.md +53 -46
- package/extensions/footer-status.ts +194 -113
- package/lib/file-links.ts +36 -17
- package/lib/input-enhancements.ts +330 -50
- package/lib/settings-web.ts +7 -2
- package/lib/settings.html +110 -48
- package/lib/terminal-capabilities.ts +45 -0
- package/lib/transcript-adapter.ts +42 -0
- package/package.json +3 -2
|
@@ -11,19 +11,23 @@
|
|
|
11
11
|
* and call cleanup.refresh() when the setting changes; call cleanup from
|
|
12
12
|
* `session_shutdown`.
|
|
13
13
|
*/
|
|
14
|
-
import { CustomEditor, stripFrontmatter, type ExtensionAPI, type ExtensionContext, type InputEvent, type InputEventResult } from "@earendil-works/pi-coding-agent";
|
|
14
|
+
import { CustomEditor, resizeImage, stripFrontmatter, type ExtensionAPI, type ExtensionContext, type InputEvent, type InputEventResult } from "@earendil-works/pi-coding-agent";
|
|
15
15
|
import {
|
|
16
|
-
allocateImageId, getImageDimensions, renderImage, getCapabilities,
|
|
16
|
+
CURSOR_MARKER, allocateImageId, deleteKittyImage, encodeKitty, getCellDimensions, getImageDimensions, renderImage, getCapabilities, getOsc8LinkAtColumn, matchesKey, sliceByColumn, stripTerminalSequences, truncateToWidth, visibleWidth,
|
|
17
17
|
type AutocompleteItem, type AutocompleteProvider, type EditorComponent, type TuiMouseEvent, type TUI, type OverlayHandle, type OverlayOptions,
|
|
18
18
|
} from "@earendil-works/pi-tui";
|
|
19
|
-
import { filePaths, inputCapabilities, linkRenderedPath, localPath } from "./file-links.ts";
|
|
19
|
+
import { filePaths, inputCapabilities, linkRenderedPath, localPath, webPaths } from "./file-links.ts";
|
|
20
20
|
import { open, readFile } from "node:fs/promises";
|
|
21
|
+
import { existsSync } from "node:fs";
|
|
21
22
|
import { homedir } from "node:os";
|
|
22
23
|
import { dirname, extname, isAbsolute, join, resolve } from "node:path";
|
|
23
24
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
24
25
|
|
|
25
26
|
export const INPUT_ENHANCEMENTS_SETTING = "pi-mini-mode-input-enhancements";
|
|
26
27
|
export const MAX_PREVIEW_BYTES = 20 * 1024 * 1024;
|
|
28
|
+
const MAX_PREVIEW_ENCODED_BYTES = 1024 * 1024;
|
|
29
|
+
const MAX_PREVIEW_WIDTH_PX = 1200;
|
|
30
|
+
const MAX_PREVIEW_HEIGHT_PX = 800;
|
|
27
31
|
|
|
28
32
|
const IMAGE_MIME: Record<string, string> = {
|
|
29
33
|
".png": "image/png",
|
|
@@ -55,9 +59,13 @@ function absolutePath(path: string, cwd: string): string {
|
|
|
55
59
|
export function imagePathsInLine(line: string): ImagePathMatch[] {
|
|
56
60
|
const plain = stripTerminalSequences(line);
|
|
57
61
|
const matches: ImagePathMatch[] = [];
|
|
58
|
-
|
|
62
|
+
// ponytail: accept existing absolute clipboard paths with spaces; extend to cwd-aware relative paths if needed.
|
|
63
|
+
const spaced = [...plain.matchAll(/(?:^|[\s"'`])((?:\/|~\/|\.\.?\/)[^\n"'`<>()[\]{},。;!?]+?\.(?:png|jpe?g|gif|webp))(?=$|[\s"'`<>()[\]{},。;!?])/gi)]
|
|
64
|
+
.map(match => ({ path: match[1], start: match.index + match[0].length - match[1].length, end: match.index + match[0].length }))
|
|
65
|
+
.filter(match => isAbsolute(match.path) && match.path.includes(" ") && existsSync(match.path));
|
|
66
|
+
for (const { path, start, end } of [...spaced, ...filePaths(plain).filter(match => !spaced.some(full => match.start >= full.start && match.start < full.end))]) {
|
|
59
67
|
if (!IMAGE_MIME[extname(path).toLowerCase()]) continue;
|
|
60
|
-
matches.push({ path, start, end
|
|
68
|
+
matches.push({ path, start, end, startCol: visibleWidth(plain.slice(0, start)), endCol: visibleWidth(plain.slice(0, end)) });
|
|
61
69
|
}
|
|
62
70
|
return matches;
|
|
63
71
|
}
|
|
@@ -79,6 +87,8 @@ export interface PreviewImage {
|
|
|
79
87
|
path: string;
|
|
80
88
|
mimeType: string;
|
|
81
89
|
base64: string;
|
|
90
|
+
widthPx: number;
|
|
91
|
+
heightPx: number;
|
|
82
92
|
}
|
|
83
93
|
export interface PreviewNote {
|
|
84
94
|
kind: "note";
|
|
@@ -91,7 +101,7 @@ export type Preview = PreviewImage | PreviewNote;
|
|
|
91
101
|
* Read a preview for an image path. Returns undefined when there is nothing to show
|
|
92
102
|
* (unknown extension, missing file, directory) so typing never raises a hint.
|
|
93
103
|
*/
|
|
94
|
-
export async function readPreview(path: string, cwd: string): Promise<Preview | undefined> {
|
|
104
|
+
export async function readPreview(path: string, cwd: string, bounds = { widthPx: MAX_PREVIEW_WIDTH_PX, heightPx: MAX_PREVIEW_HEIGHT_PX }): Promise<Preview | undefined> {
|
|
95
105
|
const absolute = absolutePath(path, cwd);
|
|
96
106
|
const mimeType = IMAGE_MIME[extname(absolute).toLowerCase()];
|
|
97
107
|
if (!mimeType) return undefined;
|
|
@@ -108,7 +118,21 @@ export async function readPreview(path: string, cwd: string): Promise<Preview |
|
|
|
108
118
|
if (!bytesRead) break;
|
|
109
119
|
offset += bytesRead;
|
|
110
120
|
}
|
|
111
|
-
|
|
121
|
+
const data = bytes.subarray(0, offset);
|
|
122
|
+
const base64 = data.toString("base64");
|
|
123
|
+
const dimensions = getImageDimensions(base64, mimeType);
|
|
124
|
+
if (!dimensions) return { kind: "note", path, note: "无法解析图片尺寸" };
|
|
125
|
+
const maxWidth = Math.max(1, Math.min(MAX_PREVIEW_WIDTH_PX, Math.floor(bounds.widthPx)));
|
|
126
|
+
const maxHeight = Math.max(1, Math.min(MAX_PREVIEW_HEIGHT_PX, Math.floor(bounds.heightPx)));
|
|
127
|
+
// renderImage controls cell placement, not pixels; its Kitty f=100 payload must be PNG.
|
|
128
|
+
const resized = dimensions.widthPx <= maxWidth && dimensions.heightPx <= maxHeight && base64.length < MAX_PREVIEW_ENCODED_BYTES
|
|
129
|
+
? { data: base64, mimeType } : await resizeImage(data, mimeType, { maxWidth, maxHeight, maxBytes: MAX_PREVIEW_ENCODED_BYTES });
|
|
130
|
+
if (!resized) return { kind: "note", path, note: "无法缩小图片预览" };
|
|
131
|
+
const png = resized.mimeType === "image/png" ? resized
|
|
132
|
+
: await (await import("@earendil-works/pi-coding-agent")).convertToPng(resized.data, resized.mimeType);
|
|
133
|
+
if (!png) return { kind: "note", path, note: "无法转换图片预览" };
|
|
134
|
+
if (png.data.length > MAX_PREVIEW_ENCODED_BYTES) return { kind: "note", path, note: "图片预览过大" };
|
|
135
|
+
return { kind: "image", path, mimeType: png.mimeType, base64: png.data, widthPx: dimensions.widthPx, heightPx: dimensions.heightPx };
|
|
112
136
|
} catch {
|
|
113
137
|
return { kind: "note", path, note: "读取失败" };
|
|
114
138
|
} finally { await file.close(); }
|
|
@@ -232,8 +256,24 @@ export interface PreviewState {
|
|
|
232
256
|
graphic?: NonNullable<ReturnType<typeof renderImage>>;
|
|
233
257
|
}
|
|
234
258
|
|
|
259
|
+
function positionedGraphic(sequence: string, up: number, right: number): string {
|
|
260
|
+
// Pi <=0.87's ANSI slicer needs an SGR terminator after relative motion.
|
|
261
|
+
// Otty places Kitty graphics against the last committed cursor while DEC 2026
|
|
262
|
+
// buffers text/cursor updates. Commit the target before the APC, then resume
|
|
263
|
+
// Pi's synchronized frame. Other terminals retain their atomic rendering.
|
|
264
|
+
const flush = process.env.TERM_PROGRAM?.toLowerCase() === "otty"
|
|
265
|
+
&& !process.env.TMUX && !process.env.STY && !/^(tmux|screen)/i.test(process.env.TERM ?? "")
|
|
266
|
+
&& sequence.startsWith("\x1b_G");
|
|
267
|
+
return `\x1b[s\x1b[${up}A\x1b[${right}C\x1b[0m${flush ? "\x1b[?2026l\x1b[0m" : ""}${sequence}\x1b[u${flush ? "\x1b[?2026h" : ""}\x1b[0m`;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function imageLayoutKey(): string {
|
|
271
|
+
const cells = getCellDimensions();
|
|
272
|
+
return `${getCapabilities().images}:${cells.widthPx}:${cells.heightPx}`;
|
|
273
|
+
}
|
|
274
|
+
|
|
235
275
|
/** A tightly sized image frame. Draw after clearing its interior, never a path-only tooltip. */
|
|
236
|
-
export function previewLines(preview: PreviewState, theme: ExtensionContext["ui"]["theme"], width: number
|
|
276
|
+
export function previewLines(preview: PreviewState, theme: ExtensionContext["ui"]["theme"], width: number): string[] {
|
|
237
277
|
if (!preview.path) return [];
|
|
238
278
|
const graphic = preview.graphic;
|
|
239
279
|
const innerWidth = Math.max(1, Math.min(width - 2, graphic?.columns ?? 26));
|
|
@@ -242,8 +282,7 @@ export function previewLines(preview: PreviewState, theme: ExtensionContext["ui"
|
|
|
242
282
|
if (graphic) {
|
|
243
283
|
for (let row = 0; row < graphic.rows; row++) lines.push(border("│") + " ".repeat(innerWidth) + border("│"));
|
|
244
284
|
} else lines.push(border("│") + truncateToWidth(preview.note ?? "无法预览图片", innerWidth, "", true) + border("│"));
|
|
245
|
-
|
|
246
|
-
const draw = graphic ? `\x1b[${origin.row + 2};${origin.col + 2}H${graphic.sequence}\x1b[${origin.row + graphic.rows + 2};${origin.col + 1}H` : "";
|
|
285
|
+
const draw = graphic ? positionedGraphic(graphic.sequence, graphic.rows, 1) : "";
|
|
247
286
|
lines.push(draw + border(`╰${"─".repeat(innerWidth)}╯`));
|
|
248
287
|
return lines;
|
|
249
288
|
}
|
|
@@ -264,6 +303,8 @@ interface Enhancements {
|
|
|
264
303
|
theme?: ExtensionContext["ui"]["theme"];
|
|
265
304
|
generation?: unknown;
|
|
266
305
|
preview: PreviewState;
|
|
306
|
+
previewSource?: Preview;
|
|
307
|
+
previewLayout?: string;
|
|
267
308
|
previewToken: number;
|
|
268
309
|
overlayReady: boolean;
|
|
269
310
|
previewTui?: { requestRender(): void };
|
|
@@ -272,7 +313,12 @@ interface Enhancements {
|
|
|
272
313
|
requestedPath?: string;
|
|
273
314
|
pointer?: boolean;
|
|
274
315
|
previewAnchor?: { x: number; y: number };
|
|
316
|
+
hoverLink?: { row: number; start: number; end: number; path: string };
|
|
275
317
|
previewOptions?: OverlayOptions;
|
|
318
|
+
previewMode: () => "hover" | "inline";
|
|
319
|
+
inlineImages: Map<string, Preview | undefined>;
|
|
320
|
+
inlinePending: Map<string, Promise<Preview | undefined>>;
|
|
321
|
+
inlineGraphics: Map<string, { layout: string } & NonNullable<ReturnType<typeof renderImage>>>;
|
|
276
322
|
baseFactory?: EditorFactoryFunction;
|
|
277
323
|
editor?: EditorComponent;
|
|
278
324
|
editorFactory?: EditorFactoryFunction;
|
|
@@ -322,29 +368,44 @@ function liveUI(ctx?: ExtensionContext): ExtensionContext["ui"] | undefined {
|
|
|
322
368
|
}
|
|
323
369
|
|
|
324
370
|
/** Compact image attachments without losing their paths on submit, reload, or external editing. */
|
|
325
|
-
function wrapEditor(inner: EditorComponent, cwd: string, isEnabled: () => boolean, onCursor: () => void, onHoverEditor: (editor: EditorComponent, event: TuiMouseEvent) => void): EditorComponent {
|
|
371
|
+
function wrapEditor(inner: EditorComponent, cwd: string, isEnabled: () => boolean, isInline: () => boolean, theme: ExtensionContext["ui"]["theme"], inlineState: Enhancements, onCursor: () => void, onHoverEditor: (editor: EditorComponent, event: TuiMouseEvent) => void, onEditorRender: () => void = () => {}): EditorComponent {
|
|
326
372
|
const attachments = new Map<string, string>();
|
|
327
|
-
const
|
|
373
|
+
const sources = new Map<string, string>();
|
|
374
|
+
const expand = (text: string) => text.replace(/\[img\d+\]/g, (label, index) => {
|
|
375
|
+
const path = sources.get(label) ?? attachments.get(label);
|
|
376
|
+
const next = /^\[img\d+\]/.exec(text.slice(index + label.length));
|
|
377
|
+
return path === undefined ? label : path + (next && attachments.has(next[0]) ? " " : "");
|
|
378
|
+
});
|
|
328
379
|
const compact = (text: string) => {
|
|
329
380
|
if (!isEnabled()) return text;
|
|
330
381
|
const replacements = imagePathsInLine(text).map(match => {
|
|
331
|
-
|
|
332
|
-
|
|
382
|
+
const source = text.slice(match.start, match.end);
|
|
383
|
+
let label = [...attachments].find(([label, path]) => path === match.path && (sources.get(label) ?? path) === source)?.[0];
|
|
384
|
+
if (!label) {
|
|
385
|
+
label = `[img${attachments.size + 1}]`;
|
|
386
|
+
attachments.set(label, match.path);
|
|
387
|
+
sources.set(label, source);
|
|
388
|
+
}
|
|
333
389
|
return { ...match, label };
|
|
334
390
|
});
|
|
335
391
|
for (const match of replacements.reverse()) text = text.slice(0, match.start) + match.label + text.slice(match.end);
|
|
336
392
|
return text;
|
|
337
393
|
};
|
|
338
|
-
// ponytail:
|
|
339
|
-
const core = inner as EditorComponent & { expandPasteMarkers?(text: string): string; segment?(text: string, mode?: string): Array<{ segment: string; index: number; input: string }>; imageAttachments?: Map<string, string
|
|
394
|
+
// ponytail: Pi 0.87 image paste calls the private inserter, bypassing the public proxy; replace with a public attachment API when available.
|
|
395
|
+
const core = inner as EditorComponent & { expandPasteMarkers?(text: string): string; insertTextAtCursorInternal?(text: string): void; setTextInternal?(text: string, placement?: "start" | "end"): void; segment?(text: string, mode?: string): Array<{ segment: string; index: number; input: string }>; imageAttachments?: Map<string, string>; state?: { lines: string[] } };
|
|
340
396
|
core.imageAttachments = attachments;
|
|
397
|
+
// Rendering reads the stored lines directly, so the submit expansion must not put the temp path back on screen.
|
|
398
|
+
if (core.state) core.state.lines = compact(core.state.lines.join("\n")).split("\n");
|
|
341
399
|
if (core.expandPasteMarkers) { const original = core.expandPasteMarkers.bind(core); core.expandPasteMarkers = text => expand(original(text)); }
|
|
400
|
+
if (core.insertTextAtCursorInternal) { const insert = core.insertTextAtCursorInternal.bind(core); core.insertTextAtCursorInternal = text => insert(compact(text)); }
|
|
401
|
+
// Native history navigation bypasses setText; compact the restored draft before its onChange/render.
|
|
402
|
+
if (core.setTextInternal) { const restore = core.setTextInternal.bind(core); core.setTextInternal = (text, placement) => restore(compact(text), placement); }
|
|
342
403
|
const pasteEditor = inner as EditorComponent & { handlePaste?(text: string): void };
|
|
343
404
|
if (pasteEditor.handlePaste) { const paste = pasteEditor.handlePaste.bind(inner); pasteEditor.handlePaste = text => paste(compact(text)); }
|
|
344
405
|
if (core.segment) {
|
|
345
406
|
const original = core.segment.bind(core);
|
|
346
407
|
core.segment = (text, mode) => {
|
|
347
|
-
const spans = [...text.matchAll(/\[
|
|
408
|
+
const spans = [...text.matchAll(/\[img\d+\]/g)].filter(match => attachments.has(match[0]));
|
|
348
409
|
return [...original(text, mode)].flatMap(segment => {
|
|
349
410
|
const span = spans.find(match => segment.index >= match.index && segment.index < match.index + match[0].length);
|
|
350
411
|
return !span ? [segment] : segment.index === span.index ? [{ segment: span[0], index: span.index, input: text }] : [];
|
|
@@ -355,18 +416,28 @@ function wrapEditor(inner: EditorComponent, cwd: string, isEnabled: () => boolea
|
|
|
355
416
|
return new Proxy(inner, {
|
|
356
417
|
get(target, property, receiver) {
|
|
357
418
|
if (property === "getText") return () => expand(target.getText());
|
|
358
|
-
if (property === "setText" || property === "insertTextAtCursor") return (text: string) => target[property]?.(compact(text));
|
|
419
|
+
if (property === "setText" || property === "insertTextAtCursor" || property === "insertTextAtCursorInternal") return (text: string) => { const insert = (target as EditorComponent & { insertTextAtCursorInternal?(value: string): void })[property] as ((value: string) => void) | undefined; insert?.call(target, compact(text)); onEditorRender(); };
|
|
359
420
|
if (property === "render") {
|
|
360
421
|
return (width: number) => {
|
|
361
422
|
const lines = target.render(width);
|
|
362
|
-
|
|
423
|
+
onEditorRender();
|
|
424
|
+
const linked = isEnabled() ? linkImagePaths(lines, target, cwd, width) : lines;
|
|
425
|
+
if (!isInline()) return linked;
|
|
426
|
+
const cards = inlinePreviewLines(inlineState, theme, width);
|
|
427
|
+
if (!cards.length) return linked;
|
|
428
|
+
// ponytail: Pi's editor has no rich image nodes; keep its cursor/text layout and mask owned tokens only in render. Replace with native attachment nodes when available.
|
|
429
|
+
const labels = new Set(attachments.keys());
|
|
430
|
+
const body = linked.map((line, row) => row > 0 && row <= ((target as EditorComponent & { renderedVisibleLineCount?: number }).renderedVisibleLineCount ?? 0) ? hideImageLabels(line, labels, theme, lines[row]) : line);
|
|
431
|
+
return [...cards, ...body];
|
|
363
432
|
};
|
|
364
433
|
}
|
|
365
434
|
if (property === "handleMouse") {
|
|
366
435
|
return (event: TuiMouseEvent) => {
|
|
367
|
-
const
|
|
368
|
-
|
|
369
|
-
|
|
436
|
+
const offset = isInline() ? inlinePreviewLines(inlineState, theme, event.width).length : 0;
|
|
437
|
+
const adjusted = offset ? { ...event, y: event.y - offset } : event;
|
|
438
|
+
const result = !offset || event.y >= offset ? target.handleMouse?.(adjusted) : undefined;
|
|
439
|
+
if (event.type === "move") onHoverEditor(target, adjusted);
|
|
440
|
+
else if (event.type === "click") void requestPreview(inlineState, undefined);
|
|
370
441
|
return result;
|
|
371
442
|
};
|
|
372
443
|
}
|
|
@@ -389,6 +460,7 @@ function wrapEditor(inner: EditorComponent, cwd: string, isEnabled: () => boolea
|
|
|
389
460
|
onChange = value as ((text: string) => void) | undefined;
|
|
390
461
|
return Reflect.set(target, property, (text: string) => {
|
|
391
462
|
onChange?.(expand(text));
|
|
463
|
+
onEditorRender();
|
|
392
464
|
onCursor();
|
|
393
465
|
});
|
|
394
466
|
}
|
|
@@ -401,14 +473,14 @@ function wrapEditor(inner: EditorComponent, cwd: string, isEnabled: () => boolea
|
|
|
401
473
|
function editorImagePaths(editor: EditorComponent, text: string): ImagePathMatch[] {
|
|
402
474
|
const attachments = (editor as EditorComponent & { imageAttachments?: Map<string, string> }).imageAttachments;
|
|
403
475
|
const paths = imagePathsInLine(text);
|
|
404
|
-
for (const match of text.matchAll(/\[
|
|
476
|
+
for (const match of text.matchAll(/\[img\d+\]/g)) {
|
|
405
477
|
const path = attachments?.get(match[0]);
|
|
406
478
|
if (path) paths.push({ path, start: match.index, end: match.index + match[0].length, startCol: visibleWidth(text.slice(0, match.index)), endCol: visibleWidth(text.slice(0, match.index + match[0].length)) });
|
|
407
479
|
}
|
|
408
480
|
return paths.sort((a, b) => a.start - b.start);
|
|
409
481
|
}
|
|
410
482
|
|
|
411
|
-
function editorPaths(editor: EditorComponent, row: number, width: number) {
|
|
483
|
+
function editorPaths(editor: EditorComponent, row: number, width: number, includeWeb = false, compactImages = false) {
|
|
412
484
|
const core = editor as EditorComponent & { lastWidth?: number; scrollOffset?: number; renderedVisibleLineCount?: number; getPaddingX?(): number; buildVisualLineMap?(width: number): Array<{ logicalLine: number; startCol: number; length: number }> };
|
|
413
485
|
if (!core.buildVisualLineMap || !core.lastWidth) return imagePathsInLine(editor.render(width)[row] ?? "");
|
|
414
486
|
if (row < 1 || row > (core.renderedVisibleLineCount ?? 0)) return [];
|
|
@@ -416,29 +488,68 @@ function editorPaths(editor: EditorComponent, row: number, width: number) {
|
|
|
416
488
|
if (!chunk) return [];
|
|
417
489
|
const source = editor.getText().split("\n")[chunk.logicalLine] ?? "";
|
|
418
490
|
const padding = Math.min(core.getPaddingX?.() ?? 0, Math.max(0, Math.floor((width - 1) / 2)));
|
|
419
|
-
return editorImagePaths(editor, source).filter(match => match.end > chunk.startCol && match.start < chunk.startCol + chunk.length).map(match => ({
|
|
491
|
+
return [...editorImagePaths(editor, source), ...(includeWeb ? webPaths(source) : [])].filter(match => match.end > chunk.startCol && match.start < chunk.startCol + chunk.length).map(match => ({
|
|
420
492
|
...match,
|
|
421
|
-
startCol: padding + visibleWidth(source.slice(chunk.startCol, Math.max(chunk.startCol, match.start)))
|
|
422
|
-
|
|
493
|
+
startCol: padding + visibleWidth(source.slice(chunk.startCol, Math.max(chunk.startCol, match.start)))
|
|
494
|
+
+ (compactImages && /^\[img\d+\]$/.test(source.slice(match.start, match.end)) ? 1 : 0),
|
|
495
|
+
endCol: padding + visibleWidth(source.slice(chunk.startCol, Math.min(chunk.startCol + chunk.length, compactImages && /^\[img\d+\]$/.test(source.slice(match.start, match.end))
|
|
496
|
+
? match.start + 1 + visibleWidth(` #${source.slice(match.start + 4, match.end - 1)}`) : match.end))),
|
|
423
497
|
}));
|
|
424
498
|
}
|
|
425
499
|
|
|
500
|
+
function hideImageLabels(line: string, labels: Set<string>, theme: ExtensionContext["ui"]["theme"], original: string): string {
|
|
501
|
+
const markerIndex = original.indexOf(CURSOR_MARKER);
|
|
502
|
+
const cursorCol = markerIndex < 0 ? -1 : visibleWidth(original.slice(0, markerIndex));
|
|
503
|
+
const plain = stripTerminalSequences(line);
|
|
504
|
+
const clean = line.replaceAll(CURSOR_MARKER, "").replaceAll("\x1b[7m", "");
|
|
505
|
+
const reset = "\x1b]8;;\x07\x1b[0m";
|
|
506
|
+
const caret = (cell: string) => reset + CURSOR_MARKER + `\x1b[7m${cell}\x1b[27m`;
|
|
507
|
+
const textSlice = (start: number, length: number) => {
|
|
508
|
+
const part = sliceByColumn(clean, start, length, true);
|
|
509
|
+
if (getOsc8LinkAtColumn(clean, start)) return part;
|
|
510
|
+
return part.replace(/^(?:\x1b\[[0-?]*[ -/]*[@-~]|\x1b[\]_][^\x07\x1b]*(?:\x07|\x1b\\))*/,
|
|
511
|
+
leading => leading.replace(/\x1b\]8;[^\x07\x1b]*(?:\x07|\x1b\\)/g, ""));
|
|
512
|
+
};
|
|
513
|
+
const textSpan = (start: number, end: number) => {
|
|
514
|
+
if (cursorCol < start || cursorCol >= end) return textSlice(start, end - start);
|
|
515
|
+
const cell = stripTerminalSequences(sliceByColumn(clean, cursorCol, 1));
|
|
516
|
+
return textSlice(start, cursorCol - start) + caret(cell)
|
|
517
|
+
+ textSlice(cursorCol + visibleWidth(cell), end - cursorCol - visibleWidth(cell));
|
|
518
|
+
};
|
|
519
|
+
const parts: string[] = [];
|
|
520
|
+
let previous = 0;
|
|
521
|
+
for (const match of plain.matchAll(/\[img\d+\]/g)) {
|
|
522
|
+
if (!labels.has(match[0])) continue;
|
|
523
|
+
const start = visibleWidth(plain.slice(0, match.index));
|
|
524
|
+
const end = start + visibleWidth(match[0]);
|
|
525
|
+
const icon = theme.fg("accent", ` #${match[0].slice(4, -1)}`);
|
|
526
|
+
const url = getOsc8LinkAtColumn(line, start);
|
|
527
|
+
const chip = url ? `\x1b]8;;${url}\x07${icon}\x1b]8;;\x07` : icon;
|
|
528
|
+
// [imgN] fits exactly one insertion cell plus the visible chip; no hidden padding.
|
|
529
|
+
// Emit the caret before styling the chip: slicing styled output replays stale ANSI resets.
|
|
530
|
+
parts.push(textSpan(previous, start), reset, cursorCol === start ? caret(" ") : " ", chip);
|
|
531
|
+
previous = end;
|
|
532
|
+
}
|
|
533
|
+
parts.push(textSpan(previous, visibleWidth(plain)), reset);
|
|
534
|
+
return parts.join("");
|
|
535
|
+
}
|
|
536
|
+
|
|
426
537
|
function linkImagePaths(lines: string[], editor: EditorComponent, cwd: string, width: number): string[] {
|
|
427
538
|
if (!inputCapabilities().hyperlinks) return lines;
|
|
428
539
|
return lines.map((line, row) => {
|
|
429
|
-
for (const match of editorPaths(editor, row, width).reverse()) line = linkRenderedPath(line, match.startCol, match.endCol, localPath(match.path, cwd));
|
|
540
|
+
for (const match of editorPaths(editor, row, width, true).reverse()) line = linkRenderedPath(line, match.startCol, match.endCol, /^https?:\/\//i.test(match.path) ? match.path : localPath(match.path, cwd));
|
|
430
541
|
// Isolate editor rows from inherited terminal link/color state.
|
|
431
542
|
const reset = "\x1b]8;;\x07\x1b[0m";
|
|
432
543
|
return reset + line + reset;
|
|
433
544
|
});
|
|
434
545
|
}
|
|
435
546
|
|
|
436
|
-
export function installInputEnhancements(pi: ExtensionAPI, ctx: ExtensionContext, isEnabled: boolean | (() => boolean)): InputEnhancementsCleanup {
|
|
437
|
-
if (process.env.TERM_PROGRAM?.toLowerCase() === "otty") setCapabilities(inputCapabilities());
|
|
547
|
+
export function installInputEnhancements(pi: ExtensionAPI, ctx: ExtensionContext, isEnabled: boolean | (() => boolean), previewMode: () => "hover" | "inline" = () => "hover"): InputEnhancementsCleanup {
|
|
438
548
|
const enabled = typeof isEnabled === "function" ? isEnabled : () => isEnabled;
|
|
439
549
|
if (installed && installed.pi !== pi) installed.cleanup();
|
|
440
550
|
const state = installed?.pi === pi ? installed : (installed = createEnhancements(pi));
|
|
441
551
|
state.enabled = enabled;
|
|
552
|
+
state.previewMode = previewMode;
|
|
442
553
|
state.ctx = ctx;
|
|
443
554
|
state.cwd = ctx.cwd;
|
|
444
555
|
state.theme = ctx.hasUI ? ctx.ui.theme : undefined;
|
|
@@ -446,12 +557,17 @@ export function installInputEnhancements(pi: ExtensionAPI, ctx: ExtensionContext
|
|
|
446
557
|
const generation = ctx.sessionManager;
|
|
447
558
|
if (state.generation !== generation) {
|
|
448
559
|
state.generation = generation;
|
|
560
|
+
void requestPreview(state, undefined);
|
|
561
|
+
state.inlinePending.clear();
|
|
562
|
+
state.inlineImages.clear();
|
|
563
|
+
clearInlineGraphics(state);
|
|
449
564
|
state.unsubscribeInput?.();
|
|
450
565
|
state.unsubscribeInput = ctx.ui.onTerminalInput?.((data) => handleTerminalInput(state, data)) ?? undefined;
|
|
451
566
|
ctx.ui.addAutocompleteProvider((current) => createSkillAutocompleteProvider(current, () => readSkillCommands(pi), () => state.enabled()));
|
|
452
567
|
}
|
|
568
|
+
// /reload clears the editor before session_start, so the saved factory can still be current while the live editor is unwrapped.
|
|
453
569
|
const active = ctx.ui.getEditorComponent();
|
|
454
|
-
if (active !== state.editorFactory || !state.editor) installEditor(ctx, state, active);
|
|
570
|
+
if (active !== state.editorFactory || !state.editor) installEditor(ctx, state, active === state.editorFactory ? state.baseFactory : active);
|
|
455
571
|
}
|
|
456
572
|
return state.cleanup;
|
|
457
573
|
}
|
|
@@ -462,6 +578,10 @@ function createEnhancements(pi: ExtensionAPI): Enhancements {
|
|
|
462
578
|
enabled: () => true,
|
|
463
579
|
preview: {},
|
|
464
580
|
previewToken: 0,
|
|
581
|
+
previewMode: () => "hover",
|
|
582
|
+
inlineImages: new Map(),
|
|
583
|
+
inlinePending: new Map(),
|
|
584
|
+
inlineGraphics: new Map(),
|
|
465
585
|
overlayReady: false,
|
|
466
586
|
cleanup: (() => {}) as InputEnhancementsCleanup,
|
|
467
587
|
};
|
|
@@ -473,7 +593,14 @@ function createEnhancements(pi: ExtensionAPI): Enhancements {
|
|
|
473
593
|
state.restoreViewportInput = undefined;
|
|
474
594
|
state.requestedPath = undefined;
|
|
475
595
|
state.previewToken++;
|
|
596
|
+
if (state.preview.graphic?.imageId) state.tui?.terminal.write(deleteKittyImage(state.preview.graphic.imageId));
|
|
476
597
|
state.preview = {};
|
|
598
|
+
state.previewSource = undefined;
|
|
599
|
+
state.previewLayout = undefined;
|
|
600
|
+
state.hoverLink = undefined;
|
|
601
|
+
state.inlineImages.clear();
|
|
602
|
+
state.inlinePending.clear();
|
|
603
|
+
clearInlineGraphics(state);
|
|
477
604
|
setPointer(state, false);
|
|
478
605
|
state.previewHandle?.hide();
|
|
479
606
|
state.previewHandle = undefined;
|
|
@@ -489,6 +616,8 @@ function createEnhancements(pi: ExtensionAPI): Enhancements {
|
|
|
489
616
|
},
|
|
490
617
|
{
|
|
491
618
|
refresh: () => {
|
|
619
|
+
syncInlineImages(state);
|
|
620
|
+
if (state.previewMode() === "inline") { void requestPreview(state, undefined); setPointer(state, false); }
|
|
492
621
|
if (!state.enabled()) {
|
|
493
622
|
void requestPreview(state, undefined);
|
|
494
623
|
setPointer(state, false);
|
|
@@ -521,23 +650,26 @@ function installEditor(ctx: ExtensionContext, state: Enhancements, base?: Editor
|
|
|
521
650
|
const factory: EditorFactoryFunction = (tui, theme, keybindings) => {
|
|
522
651
|
state.tui = tui;
|
|
523
652
|
state.previewTui = tui;
|
|
524
|
-
// Otty is detected after fullscreen startup; synchronize its cached protocol for placement cleanup.
|
|
525
|
-
const renderer = tui as TUI & { imageProtocol?: string | null };
|
|
526
|
-
if (renderer.imageProtocol === null && getCapabilities().images === "kitty") renderer.imageProtocol = "kitty";
|
|
527
653
|
// ponytail: fullscreen's first input listener consumes mouse events before extension listeners.
|
|
528
654
|
// Observe its entry point until Pi exposes a pre-dispatch mouse subscription.
|
|
529
655
|
state.restoreViewportInput = hookViewportInput(tui, (data) => handleTerminalInput(state, data));
|
|
530
656
|
const inner: CursorEditor = create(tui, theme, keybindings);
|
|
531
|
-
const editor = wrapEditor(inner, state.cwd ?? process.cwd(), () => state.enabled(), () => {
|
|
657
|
+
const editor = wrapEditor(inner, state.cwd ?? process.cwd(), () => state.enabled(), () => state.enabled() && state.previewMode() === "inline", state.theme!, state, () => {
|
|
532
658
|
const cursor = inner.getCursor?.();
|
|
533
659
|
const path = cursor ? editorImagePaths(inner, inner.getText().split("\n")[cursor.line] ?? "").find(match => cursor.col >= match.start && cursor.col <= match.end)?.path : undefined;
|
|
534
|
-
void requestPreview(state, state.enabled() ? path : undefined);
|
|
660
|
+
if (state.previewMode() === "hover") void requestPreview(state, state.enabled() ? path : undefined);
|
|
535
661
|
}, (target, event) => {
|
|
536
|
-
const match = state.enabled() ? editorPaths(target, event.y, event.width).find(path => event.x >= path.startCol && event.x < path.endCol) : undefined;
|
|
537
|
-
|
|
662
|
+
const match = state.enabled() && event.y >= 1 ? editorPaths(target, event.y, event.width, false, state.previewMode() === "inline").find(path => event.x >= path.startCol && event.x < path.endCol) : undefined;
|
|
663
|
+
const screenStart = match ? event.screenX - event.x + match.startCol : 0;
|
|
664
|
+
const screenEnd = match ? event.screenX - event.x + match.endCol : 0;
|
|
665
|
+
if (match && (match.path !== state.requestedPath || state.previewAnchor?.y !== event.screenY
|
|
666
|
+
|| state.previewAnchor.x < screenStart || state.previewAnchor.x >= screenEnd)) {
|
|
667
|
+
state.previewAnchor = { x: event.screenX, y: event.screenY };
|
|
668
|
+
if (match.path === state.requestedPath && state.preview.path) { positionPreview(state); state.previewTui?.requestRender(); }
|
|
669
|
+
}
|
|
538
670
|
setPointer(state, !!match);
|
|
539
671
|
void requestPreview(state, match?.path);
|
|
540
|
-
});
|
|
672
|
+
}, () => syncInlineImages(state));
|
|
541
673
|
state.editor = editor;
|
|
542
674
|
return editor;
|
|
543
675
|
};
|
|
@@ -546,6 +678,114 @@ function installEditor(ctx: ExtensionContext, state: Enhancements, base?: Editor
|
|
|
546
678
|
ctx.ui.setEditorComponent(factory);
|
|
547
679
|
}
|
|
548
680
|
|
|
681
|
+
function draftImagePaths(editor: EditorComponent): string[] {
|
|
682
|
+
const source = (editor as EditorComponent & { state?: { lines: string[] } }).state?.lines ?? editor.getText().split("\n");
|
|
683
|
+
return source.flatMap(line => editorImagePaths(editor, line).map(match => match.path));
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
function clearInlineGraphics(state: Pick<Enhancements, "inlineGraphics" | "tui">): void {
|
|
687
|
+
for (const graphic of state.inlineGraphics.values()) {
|
|
688
|
+
if (graphic.imageId) state.tui?.terminal.write(deleteKittyImage(graphic.imageId));
|
|
689
|
+
}
|
|
690
|
+
state.inlineGraphics.clear();
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
/** Only the editing draft is shown here; history hover remains the separate mode. */
|
|
694
|
+
function syncInlineImages(state: Enhancements): void {
|
|
695
|
+
const paths = state.editor && state.enabled() && state.previewMode() === "inline"
|
|
696
|
+
? [...new Set(draftImagePaths(state.editor))].slice(0, 15) : [];
|
|
697
|
+
if (!paths.length) clearInlineGraphics(state);
|
|
698
|
+
for (const path of state.inlineImages.keys()) if (!paths.includes(path)) { state.inlineImages.delete(path); clearInlineGraphics(state); }
|
|
699
|
+
for (const path of state.inlinePending.keys()) if (!paths.includes(path)) state.inlinePending.delete(path);
|
|
700
|
+
for (const path of paths) {
|
|
701
|
+
if (state.inlineImages.has(path) || state.inlinePending.has(path)) continue;
|
|
702
|
+
const cwd = state.cwd ?? process.cwd();
|
|
703
|
+
// Cell placement scales the image; keep a bounded copy independent of startup cell metrics.
|
|
704
|
+
const pending = readPreview(path, cwd).catch(() => undefined);
|
|
705
|
+
state.inlinePending.set(path, pending);
|
|
706
|
+
void pending.then(preview => {
|
|
707
|
+
if (state.inlinePending.get(path) !== pending) return;
|
|
708
|
+
state.inlinePending.delete(path);
|
|
709
|
+
if (!state.editor || !state.enabled() || state.previewMode() !== "inline" || !draftImagePaths(state.editor).includes(path)) return;
|
|
710
|
+
state.inlineImages.set(path, preview);
|
|
711
|
+
state.tui?.requestRender();
|
|
712
|
+
});
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
export function inlinePreviewLines(state: Pick<Enhancements, "editor" | "enabled" | "previewMode" | "inlineImages" | "inlinePending" | "inlineGraphics" | "tui">, theme: ExtensionContext["ui"]["theme"], width: number): string[] {
|
|
717
|
+
const paths = state.editor && state.enabled() && state.previewMode() === "inline" && width >= 12 ? draftImagePaths(state.editor) : [];
|
|
718
|
+
if (!paths.length) { clearInlineGraphics(state); return []; }
|
|
719
|
+
const cardWidth = Math.min(14, width);
|
|
720
|
+
// ponytail: cards are capped at one row; beyond viewport width show compact numbered tags, upgrade to a gallery when scrolling is supported.
|
|
721
|
+
const count = Math.max(1, Math.floor((width + 2) / (cardWidth + 2)));
|
|
722
|
+
const protocol = getCapabilities().images;
|
|
723
|
+
const layout = `${cardWidth}:${imageLayoutKey()}`;
|
|
724
|
+
const keys = paths.slice(0, count).map((path, index) => `${index}:${path}`);
|
|
725
|
+
if ([...state.inlineGraphics].some(([key, graphic]) => graphic.layout !== layout || !keys.includes(key))) clearInlineGraphics(state);
|
|
726
|
+
const rows: string[] = [];
|
|
727
|
+
const color = (index: number, text: string) => theme.fg((["warning", "accent", "success", "error"] as const)[index % 4], text);
|
|
728
|
+
{
|
|
729
|
+
const cards = paths.slice(0, count).map((path, index) => {
|
|
730
|
+
const key = `${index}:${path}`;
|
|
731
|
+
const preview = state.inlineImages.get(path);
|
|
732
|
+
const dimensions = preview?.kind === "image" ? getImageDimensions(preview.base64, preview.mimeType) : undefined;
|
|
733
|
+
let graphic = state.inlineGraphics.get(key);
|
|
734
|
+
if (preview?.kind === "image" && dimensions && protocol && !graphic) {
|
|
735
|
+
const cells = getCellDimensions();
|
|
736
|
+
const scale = Math.min((cardWidth - 2) * cells.widthPx / dimensions.widthPx, 4 * cells.heightPx / dimensions.heightPx);
|
|
737
|
+
const columns = Math.min(cardWidth - 2, Math.ceil(dimensions.widthPx * scale / cells.widthPx));
|
|
738
|
+
const rows = Math.min(4, Math.ceil(dimensions.heightPx * scale / cells.heightPx));
|
|
739
|
+
const imageId = protocol === "kitty" ? allocateImageId() : undefined;
|
|
740
|
+
// ponytail: bottom-row placement must not register top-row crop metadata; use Pi's encoder until its layout API supports positioned graphics.
|
|
741
|
+
const rendered = protocol === "kitty"
|
|
742
|
+
? { sequence: encodeKitty(preview.base64, { imageId, columns, rows, moveCursor: false }), imageId, rows, columns }
|
|
743
|
+
: renderImage(preview.base64, dimensions, { maxWidthCells: cardWidth - 2, maxHeightCells: 4, moveCursor: false });
|
|
744
|
+
if (rendered) { graphic = { layout, ...rendered }; state.inlineGraphics.set(key, graphic); }
|
|
745
|
+
}
|
|
746
|
+
const frame = (text: string) => color(index, text);
|
|
747
|
+
const rim = (text: string, top: boolean) => {
|
|
748
|
+
const caption = truncateToWidth(text, cardWidth - 4, "…");
|
|
749
|
+
const fill = cardWidth - 4 - visibleWidth(caption);
|
|
750
|
+
return frame(`${top ? "╭" : "╰"}${"─".repeat(Math.floor(fill / 2))} ${caption} ${"─".repeat(Math.ceil(fill / 2))}${top ? "╮" : "╯"}`);
|
|
751
|
+
};
|
|
752
|
+
const top = rim(` #${index + 1}`, true);
|
|
753
|
+
const size = preview?.kind === "image" ? `${preview.widthPx}x${preview.heightPx}` : state.inlinePending.has(path) ? "加载中" : "无法预览";
|
|
754
|
+
return { graphic, preview, top, size, rim, frame };
|
|
755
|
+
});
|
|
756
|
+
// OMP's 12x4 inner grid; never grow a tall empty box for a wide image.
|
|
757
|
+
const bodyHeight = 4;
|
|
758
|
+
rows.push(cards.map(card => card.top).join(" "));
|
|
759
|
+
for (let row = 0; row < bodyHeight; row++) rows.push(cards.map(card => {
|
|
760
|
+
const content = row === 0 && !card.graphic ? truncateToWidth(card.preview?.kind === "note" ? card.preview.note : card.size === "加载中" ? "加载中" : "无法预览", cardWidth - 4, "…") : "";
|
|
761
|
+
const left = card.graphic ? Math.floor((cardWidth - 2 - card.graphic.columns) / 2) : 0;
|
|
762
|
+
return card.frame("│") + " ".repeat(left) + " ".repeat(cardWidth - 2 - left - visibleWidth(content)) + content + card.frame("│");
|
|
763
|
+
}).join(" "));
|
|
764
|
+
const bottom = cards.map(card => card.rim(card.size, false)).join(" ");
|
|
765
|
+
// Draw the cleared card first, then place images over its interior from the final row.
|
|
766
|
+
const drawings = cards.map((card, index) => {
|
|
767
|
+
if (!card.graphic) return "";
|
|
768
|
+
const topPad = Math.floor((bodyHeight - card.graphic.rows) / 2);
|
|
769
|
+
const leftPad = Math.floor((cardWidth - 2 - card.graphic.columns) / 2);
|
|
770
|
+
const up = bodyHeight - topPad;
|
|
771
|
+
const right = index * (cardWidth + 2) + leftPad + 1;
|
|
772
|
+
return positionedGraphic(card.graphic.sequence, up, right);
|
|
773
|
+
}).join("");
|
|
774
|
+
rows.push(drawings + bottom);
|
|
775
|
+
}
|
|
776
|
+
if (paths.length > count) {
|
|
777
|
+
rows.push("");
|
|
778
|
+
let line = "";
|
|
779
|
+
for (let index = count; index < paths.length; index++) {
|
|
780
|
+
const tag = color(index, ` #${index + 1}`);
|
|
781
|
+
if (visibleWidth(line) + visibleWidth(tag) + 2 > width) { rows.push(line); line = ""; }
|
|
782
|
+
line += (line ? " " : "") + tag;
|
|
783
|
+
}
|
|
784
|
+
if (line) rows.push(line);
|
|
785
|
+
}
|
|
786
|
+
return rows;
|
|
787
|
+
}
|
|
788
|
+
|
|
549
789
|
function setPointer(state: Enhancements, pointer: boolean): void {
|
|
550
790
|
if (state.pointer === pointer) return;
|
|
551
791
|
if (pointer || state.pointer) state.tui?.terminal.write(`\x1b]22;${pointer ? "pointer" : "default"}\x07`);
|
|
@@ -553,17 +793,32 @@ function setPointer(state: Enhancements, pointer: boolean): void {
|
|
|
553
793
|
}
|
|
554
794
|
|
|
555
795
|
function handleTerminalInput(state: Enhancements, data: string): { consume?: boolean } | undefined {
|
|
796
|
+
if (data === "\x1b[O") { setPointer(state, false); void requestPreview(state, undefined); }
|
|
556
797
|
const mouse = /^\x1b\[<(\d+);(\d+);(\d+)([Mm])$/.exec(data);
|
|
557
798
|
if (mouse) {
|
|
558
799
|
const x = Number(mouse[2]) - 1, y = Number(mouse[3]) - 1;
|
|
559
800
|
// ponytail: Pi 0.85 has no public screen hit-test API; inspect its last rendered OSC 8 cells.
|
|
560
801
|
const screen = (state.tui as TUI & { previousScreen?: string[] } | undefined)?.previousScreen;
|
|
561
|
-
const
|
|
802
|
+
const moving = !!(Number(mouse[1]) & 32) && mouse[4] === "M";
|
|
803
|
+
if (!moving) { setPointer(state, false); void requestPreview(state, undefined); return undefined; }
|
|
804
|
+
const cached = state.hoverLink;
|
|
805
|
+
const sameTarget = cached?.row === y && x >= cached.start && x < cached.end;
|
|
806
|
+
const url = state.enabled()
|
|
807
|
+
? sameTarget ? pathToFileURL(cached.path).href : getOsc8LinkAtColumn(screen?.[y] ?? "", x)
|
|
808
|
+
: undefined;
|
|
562
809
|
let path: string | undefined;
|
|
563
810
|
if (url?.startsWith("file:")) {
|
|
564
811
|
try { const file = fileURLToPath(url); if (IMAGE_MIME[extname(file).toLowerCase()]) path = file; } catch { /* Reject malformed or remote file URLs. */ }
|
|
565
812
|
}
|
|
566
|
-
if (path
|
|
813
|
+
if (path && (path !== state.requestedPath || !sameTarget)) {
|
|
814
|
+
state.previewAnchor = { x, y };
|
|
815
|
+
if (path === state.requestedPath && state.preview.path) { positionPreview(state); state.previewTui?.requestRender(); }
|
|
816
|
+
let start = x, end = x + 1;
|
|
817
|
+
const line = screen?.[y] ?? "";
|
|
818
|
+
while (start > 0 && getOsc8LinkAtColumn(line, start - 1) === url) start--;
|
|
819
|
+
while (end < visibleWidth(line) && getOsc8LinkAtColumn(line, end) === url) end++;
|
|
820
|
+
state.hoverLink = { row: y, start, end, path };
|
|
821
|
+
} else if (!path) state.hoverLink = undefined;
|
|
567
822
|
setPointer(state, !!path);
|
|
568
823
|
void requestPreview(state, path);
|
|
569
824
|
}
|
|
@@ -573,23 +828,36 @@ function handleTerminalInput(state: Enhancements, data: string): { consume?: boo
|
|
|
573
828
|
}
|
|
574
829
|
|
|
575
830
|
async function requestPreview(state: Enhancements, path: string | undefined): Promise<void> {
|
|
576
|
-
if (path && state.enabled() && path === state.requestedPath)
|
|
831
|
+
if (path && state.enabled() && path === state.requestedPath) return;
|
|
832
|
+
if (!path || !state.enabled()) state.hoverLink = undefined;
|
|
833
|
+
if (!path && !state.requestedPath && !state.preview.path) return;
|
|
577
834
|
state.requestedPath = state.enabled() ? path : undefined;
|
|
578
835
|
const token = ++state.previewToken;
|
|
579
836
|
if (!path || !state.enabled()) {
|
|
580
837
|
setPreview(state, undefined);
|
|
581
838
|
return;
|
|
582
839
|
}
|
|
583
|
-
const
|
|
840
|
+
const cells = getCellDimensions();
|
|
841
|
+
const preview = await readPreview(path, state.cwd ?? process.cwd(), {
|
|
842
|
+
widthPx: Math.max(1, (state.tui?.terminal.columns ?? 80) - 2) * cells.widthPx,
|
|
843
|
+
heightPx: Math.max(1, (state.tui?.terminal.rows ?? 24) - 2) * cells.heightPx,
|
|
844
|
+
});
|
|
584
845
|
if (token !== state.previewToken || !state.enabled()) return;
|
|
585
846
|
setPreview(state, preview);
|
|
586
847
|
}
|
|
587
848
|
|
|
588
849
|
function setPreview(state: Enhancements, preview: Preview | undefined): void {
|
|
850
|
+
// Overlay visibility only erases text; explicitly free the image owned by this preview.
|
|
851
|
+
if (state.preview.graphic?.imageId) state.tui?.terminal.write(deleteKittyImage(state.preview.graphic.imageId));
|
|
852
|
+
state.previewSource = preview;
|
|
853
|
+
state.previewLayout = imageLayoutKey();
|
|
589
854
|
const dimensions = preview?.kind === "image" ? getImageDimensions(preview.base64, preview.mimeType) : undefined;
|
|
855
|
+
const bandWidth = state.previewMode() === "inline" && state.editor && state.theme
|
|
856
|
+
? visibleWidth(inlinePreviewLines(state, state.theme, state.tui?.terminal.columns ?? 80)[0] ?? "") : 0;
|
|
857
|
+
const available = (state.tui?.terminal.columns ?? 80) - (bandWidth ? bandWidth + 4 : 2);
|
|
590
858
|
const graphic = preview?.kind === "image" && dimensions ? renderImage(preview.base64, dimensions, {
|
|
591
|
-
maxWidthCells: Math.max(1, Math.min(
|
|
592
|
-
maxHeightCells: Math.max(1,
|
|
859
|
+
maxWidthCells: Math.max(1, Math.min(Math.floor(dimensions.widthPx / getCellDimensions().widthPx), available)),
|
|
860
|
+
maxHeightCells: Math.max(1, (state.tui?.terminal.rows ?? 24) - 2),
|
|
593
861
|
imageId: allocateImageId(), moveCursor: false,
|
|
594
862
|
}) ?? undefined : undefined;
|
|
595
863
|
state.preview = {
|
|
@@ -604,9 +872,12 @@ function setPreview(state: Enhancements, preview: Preview | undefined): void {
|
|
|
604
872
|
function showOverlay(state: Enhancements): void {
|
|
605
873
|
if (!state.tui || !state.theme) return;
|
|
606
874
|
state.overlayReady = true;
|
|
607
|
-
state.previewOptions = { width: 60, maxHeight: "
|
|
875
|
+
state.previewOptions = { width: 60, maxHeight: "100%", nonCapturing: true, visible: () => state.enabled() && state.preview.path !== undefined };
|
|
608
876
|
state.previewHandle = state.tui.showOverlay({
|
|
609
|
-
render: width =>
|
|
877
|
+
render: width => {
|
|
878
|
+
if (state.previewLayout !== imageLayoutKey()) setPreview(state, state.previewSource);
|
|
879
|
+
return previewLines(state.preview, state.theme!, width);
|
|
880
|
+
},
|
|
610
881
|
invalidate: () => {},
|
|
611
882
|
}, state.previewOptions);
|
|
612
883
|
}
|
|
@@ -615,8 +886,17 @@ function positionPreview(state: Enhancements): void {
|
|
|
615
886
|
if (!state.previewOptions || !state.tui || !state.theme) return;
|
|
616
887
|
const { rows, columns } = state.tui.terminal;
|
|
617
888
|
const width = Math.max(3, Math.min((state.preview.graphic?.columns ?? 26) + 2, columns || 80));
|
|
618
|
-
const height = Math.min(
|
|
889
|
+
const height = Math.min(rows, previewLines(state.preview, state.theme, width).length);
|
|
619
890
|
const { x, y } = state.previewAnchor ?? { x: 0, y: rows - 2 };
|
|
620
|
-
|
|
621
|
-
|
|
891
|
+
const previewRow = y + 1 + height <= rows ? y + 1 : Math.max(0, y - height);
|
|
892
|
+
// Keep hover previews out of the draft attachment band. If there is no safe
|
|
893
|
+
// vertical gap, move to the side rather than painting over the thumbnail.
|
|
894
|
+
const band = state.previewMode() === "inline" && state.editor && state.theme
|
|
895
|
+
? inlinePreviewLines(state, state.theme, columns).length : 0;
|
|
896
|
+
const overlapsBand = band > 0 && previewRow < y && previewRow + height > y - band - 1;
|
|
897
|
+
const bandWidth = band && state.theme && state.editor ? visibleWidth(inlinePreviewLines(state, state.theme, columns)[0] ?? "") : 0;
|
|
898
|
+
const side = Math.min(columns - width, bandWidth + 2);
|
|
899
|
+
const beside = overlapsBand && side >= bandWidth + 2;
|
|
900
|
+
Object.assign(state.previewOptions, { width, col: beside ? side : Math.max(0, Math.min(x, columns - width)),
|
|
901
|
+
row: beside ? Math.max(0, Math.min(y + 1, rows - height)) : previewRow });
|
|
622
902
|
}
|