@xynogen/pix-display 0.8.0 → 0.10.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/package.json +2 -2
- package/src/directives.ts +12 -4
- package/src/image.ts +83 -3
- package/src/index.ts +6 -0
- package/src/tables.ts +39 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xynogen/pix-display",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Pi core extension — inline chips, thinking blocks, and framed code rendering",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.ts",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"access": "public"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@xynogen/pix-pretty": "^3.
|
|
41
|
+
"@xynogen/pix-pretty": "^3.5.0",
|
|
42
42
|
"@xynogen/pix-runtime": "^0.24.0"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
package/src/directives.ts
CHANGED
|
@@ -24,7 +24,7 @@ import {
|
|
|
24
24
|
} from "@xynogen/pix-pretty/tui";
|
|
25
25
|
import type { ExtensionAPI } from "@xynogen/pix-runtime/host";
|
|
26
26
|
import { foldsOpen, renderFolds } from "./fold.ts";
|
|
27
|
-
import { parseImage,
|
|
27
|
+
import { parseImage, renderImageBlock } from "./image.ts";
|
|
28
28
|
import { renderMathBlocks } from "./math.ts";
|
|
29
29
|
import { renderMermaidBlocks } from "./mermaid.ts";
|
|
30
30
|
|
|
@@ -54,6 +54,8 @@ const MAX_BAR_LABEL = 20;
|
|
|
54
54
|
const MAX_V_COL = 10;
|
|
55
55
|
const ATTR = /(\w+)=(?:"((?:[^"\\]|\\.)*)"|([^\s"}]+))/g;
|
|
56
56
|
const FENCE = /^\s*(```|~~~)/;
|
|
57
|
+
// /loop's own control directive: the model writes it, the reader never needs to see it.
|
|
58
|
+
const LOOP_DIRECTIVE = /^[ \t]*::loop\[/;
|
|
57
59
|
const MIN_CARD = 16;
|
|
58
60
|
const MAX_CHART = 72;
|
|
59
61
|
// NBSP pads the axis gutter: Markdown would strip or code-block leading spaces.
|
|
@@ -81,6 +83,8 @@ const PATH_ONLY = new RegExp(`^${PATH}$`);
|
|
|
81
83
|
const INLINE = new RegExp(
|
|
82
84
|
[
|
|
83
85
|
String.raw`(?<ticks>\`+)(?<code>[^\`]*?)\k<ticks>`,
|
|
86
|
+
// A table-cell image stays raw text until the table renderer replaces it.
|
|
87
|
+
String.raw`(?<cellImage>::image\[[^\]\n]*\](?:\{[^}\n]*\})?)`,
|
|
84
88
|
String.raw`(?<![\w:]):(?<name>red|green|yellow|blue|spark)\[(?<text>[^\]\n]+)\]`,
|
|
85
89
|
String.raw`(?<![\w:]):badge\[(?<badge>[^\]\n]+)\](?:\{(?<badgeAttrs>[^}\n]*)\})?`,
|
|
86
90
|
String.raw`(?<!!)\[(?<linkText>[^\]\n]+)\]\((?<href>https?:\/\/[^\s)]+)\)`,
|
|
@@ -131,6 +135,7 @@ type InlineGroups = Partial<Record<string, string>>;
|
|
|
131
135
|
|
|
132
136
|
/** One inline match: directives, plus links, bare URLs and file paths as badges. */
|
|
133
137
|
function inlineMatch(match: string, g: InlineGroups): string {
|
|
138
|
+
if (g.cellImage) return match;
|
|
134
139
|
if (g.ticks) return g.code && PATH_ONLY.test(g.code) ? badge(literal(g.code), "green") : match;
|
|
135
140
|
if (g.badge) return badge(g.badge, attrs(g.badgeAttrs).color ?? "");
|
|
136
141
|
if (g.path) return badge(literal(g.path), "green");
|
|
@@ -156,7 +161,7 @@ function inlineLines(lines: readonly string[]): string[] {
|
|
|
156
161
|
});
|
|
157
162
|
}
|
|
158
163
|
|
|
159
|
-
function attrs(raw: string | undefined): Record<string, string> {
|
|
164
|
+
export function attrs(raw: string | undefined): Record<string, string> {
|
|
160
165
|
const out: Record<string, string> = {};
|
|
161
166
|
for (const [, key, quoted, bare] of (raw ?? "").matchAll(ATTR)) {
|
|
162
167
|
// Only `\"` is an escape, so a Windows path keeps its backslashes.
|
|
@@ -420,7 +425,8 @@ export function renderCards(cards: readonly Card[], width: number): string[] {
|
|
|
420
425
|
|
|
421
426
|
export function renderDirectives(markdown: string, width: number): string {
|
|
422
427
|
// Cheap pre-check: most replies have no directive, link or path at all.
|
|
423
|
-
if (!/::(?:card|bar|stacked|image)|:[a-z]+\[|https?:\/\/|\w\//.test(markdown))
|
|
428
|
+
if (!/::(?:card|bar|stacked|image|loop)|:[a-z]+\[|https?:\/\/|\w\//.test(markdown))
|
|
429
|
+
return markdown;
|
|
424
430
|
const out: string[] = [];
|
|
425
431
|
let fenced = false;
|
|
426
432
|
let cards: Card[] = [];
|
|
@@ -438,6 +444,8 @@ export function renderDirectives(markdown: string, width: number): string {
|
|
|
438
444
|
parts = [];
|
|
439
445
|
};
|
|
440
446
|
for (const line of markdown.split("\n")) {
|
|
447
|
+
// ::loop[...] is /loop's control channel for the model, not reader content.
|
|
448
|
+
if (!fenced && LOOP_DIRECTIVE.test(line)) continue;
|
|
441
449
|
// Lines right after a card, up to a blank line or the next block directive, are
|
|
442
450
|
// its body; a fence in the body stays whole.
|
|
443
451
|
const last = cards.at(-1);
|
|
@@ -466,7 +474,7 @@ export function renderDirectives(markdown: string, width: number): string {
|
|
|
466
474
|
continue;
|
|
467
475
|
}
|
|
468
476
|
const image = fenced ? undefined : parseImage(line, attrs);
|
|
469
|
-
const block = image ?
|
|
477
|
+
const block = image ? renderImageBlock(image, width) : undefined;
|
|
470
478
|
if (block) {
|
|
471
479
|
flush();
|
|
472
480
|
out.push("", ...block, "");
|
package/src/image.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `::image[alt]{src="file:///abs/path.png" width=
|
|
3
|
-
* assistant reply.
|
|
2
|
+
* `::image[alt]{src="file:///abs/path.png" width=50%}` — a local image in an
|
|
3
|
+
* assistant reply. `width` is a percentage of the space it sits in (the reply, or a
|
|
4
|
+
* table column), or a bare number of terminal columns.
|
|
5
|
+
* Display only, like the other directives.
|
|
4
6
|
*
|
|
5
7
|
* Only `file://` URLs and absolute paths: a remote image is downloaded first by a
|
|
6
8
|
* visible tool call, so rendering never touches the network.
|
|
@@ -27,8 +29,14 @@ const MIME: Record<string, string> = {
|
|
|
27
29
|
webp: "image/webp",
|
|
28
30
|
};
|
|
29
31
|
const NBSP = "\u00a0";
|
|
32
|
+
// Braille blank: one column wide and not whitespace, so Pi lays it out as one unbroken word.
|
|
33
|
+
const CELL_MARK = "\u2800";
|
|
34
|
+
const CELL_MARKS = /\u2800+/g;
|
|
35
|
+
// Default image width inside a table cell, in columns.
|
|
36
|
+
const CELL_COLS = 24;
|
|
30
37
|
|
|
31
|
-
|
|
38
|
+
/** `width` counts terminal columns; `percent` (1-100) is a share of the space, set instead. */
|
|
39
|
+
export type ImageDirective = { alt: string; src: string; width?: number; percent?: number };
|
|
32
40
|
|
|
33
41
|
export function parseImage(
|
|
34
42
|
line: string,
|
|
@@ -38,10 +46,21 @@ export function parseImage(
|
|
|
38
46
|
if (!match) return undefined;
|
|
39
47
|
const { src, width } = attrs(match[2]);
|
|
40
48
|
if (!src) return undefined;
|
|
49
|
+
const percent = /^(\d+(?:\.\d+)?)%$/.exec(width?.trim() ?? "")?.[1];
|
|
50
|
+
if (percent)
|
|
51
|
+
return { alt: match[1] ?? "", src, percent: Math.min(100, Number(percent)) || undefined };
|
|
41
52
|
const cells = Number(width);
|
|
42
53
|
return { alt: match[1] ?? "", src, width: cells > 0 ? cells : undefined };
|
|
43
54
|
}
|
|
44
55
|
|
|
56
|
+
/** Columns an image may use in `space` columns: its percentage, its width, or `fallback`. */
|
|
57
|
+
function imageCols(image: ImageDirective, space: number, fallback: number): number {
|
|
58
|
+
const want = image.percent
|
|
59
|
+
? Math.round((space * image.percent) / 100)
|
|
60
|
+
: (image.width ?? fallback);
|
|
61
|
+
return Math.max(1, Math.min(want, space));
|
|
62
|
+
}
|
|
63
|
+
|
|
45
64
|
// Pi may re-render a message many times; keep the encoded rows per file version and width.
|
|
46
65
|
// Insertion-ordered, so dropping the first key evicts the oldest entry.
|
|
47
66
|
const cache = new Map<string, string[]>();
|
|
@@ -87,6 +106,67 @@ export function renderImage(image: ImageDirective, width: number): string[] {
|
|
|
87
106
|
return rows;
|
|
88
107
|
}
|
|
89
108
|
|
|
109
|
+
/** A standalone `::image`, boxed like math and Mermaid when the terminal draws Kitty images. */
|
|
110
|
+
export function renderImageBlock(image: ImageDirective, width: number): string[] {
|
|
111
|
+
const cols = imageCols(image, width - FRAME_COLS, 60);
|
|
112
|
+
return frameImage(renderImage({ ...image, width: cols }, width), width);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export type CellImage = { seq: string; cols: number };
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* An `::image` table cell: placeholder text for Pi's table layout, plus the Kitty image that
|
|
119
|
+
* replaces it after layout. Other terminals get a short label: iTerm2 draws upward from the
|
|
120
|
+
* cursor, so its image cannot sit inside a cell.
|
|
121
|
+
*/
|
|
122
|
+
export function cellImage(
|
|
123
|
+
image: ImageDirective,
|
|
124
|
+
maxCols: number,
|
|
125
|
+
): { text: string; image?: CellImage } {
|
|
126
|
+
const label = { text: `${FG_DIM}[image: ${image.alt || basename(image.src)}]${RST}` };
|
|
127
|
+
if (getCapabilities().images !== "kitty") return label;
|
|
128
|
+
const cols = imageCols(image, maxCols, CELL_COLS);
|
|
129
|
+
const rows = renderImage({ ...image, width: cols }, cols + 2);
|
|
130
|
+
const seq = rows[0] ?? "";
|
|
131
|
+
const drawn = Number(/[;,]c=(\d+)/.exec(seq)?.[1]);
|
|
132
|
+
// A one-row note (missing, unsupported) explains itself better than the bare label.
|
|
133
|
+
if (!(drawn > 0)) return rows.length === 1 ? { text: seq } : label;
|
|
134
|
+
return {
|
|
135
|
+
text: CELL_MARK.repeat(drawn) + "\n".repeat(rows.length - 1),
|
|
136
|
+
image: { seq, cols: drawn },
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Put each image over its placeholder in reading order, or undefined when Pi wrapped a
|
|
142
|
+
* placeholder apart. Markdown skips its left padding on image lines, so `indent` adds it back.
|
|
143
|
+
* Spaces, not a cursor jump, cover the cells: Kitty keeps an image when text is written under it.
|
|
144
|
+
*/
|
|
145
|
+
export function placeCellImages(
|
|
146
|
+
lines: readonly string[],
|
|
147
|
+
images: readonly CellImage[],
|
|
148
|
+
indent: string,
|
|
149
|
+
): string[] | undefined {
|
|
150
|
+
let next = 0;
|
|
151
|
+
let broken = false;
|
|
152
|
+
const out = lines.map((line) => {
|
|
153
|
+
let placed = false;
|
|
154
|
+
const row = line.replace(CELL_MARKS, (run) => {
|
|
155
|
+
const image = images[next++];
|
|
156
|
+
if (image?.cols !== run.length) broken = true;
|
|
157
|
+
placed = true;
|
|
158
|
+
return `${image?.seq ?? ""}${" ".repeat(run.length)}`;
|
|
159
|
+
});
|
|
160
|
+
return placed ? indent + row : row;
|
|
161
|
+
});
|
|
162
|
+
return broken || next !== images.length ? undefined : out;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Plain spaces in place of image placeholders, for a table Pi wrapped too narrow to hold them. */
|
|
166
|
+
export function blankCellImages(lines: readonly string[]): string[] {
|
|
167
|
+
return lines.map((line) => line.replace(CELL_MARKS, (run) => " ".repeat(run.length)));
|
|
168
|
+
}
|
|
169
|
+
|
|
90
170
|
type RowOptions = { maxWidthCells: number; maxHeightCells?: number; filename?: string };
|
|
91
171
|
type Dimensions = { widthPx: number; heightPx: number };
|
|
92
172
|
|
package/src/index.ts
CHANGED
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
import { registerChips } from "@xynogen/pix-pretty/chips";
|
|
18
|
+
import { deleteAllKittyImages, getCapabilities } from "@xynogen/pix-pretty/tui";
|
|
18
19
|
import type { ExtensionAPI } from "@xynogen/pix-runtime/host";
|
|
19
20
|
import { once } from "@xynogen/pix-runtime/once";
|
|
20
21
|
|
|
@@ -37,6 +38,11 @@ export default async function pixDisplayExtension(pi: ExtensionAPI): Promise<voi
|
|
|
37
38
|
directivesExtension(pi);
|
|
38
39
|
workingExtension(pi);
|
|
39
40
|
followFoldInput();
|
|
41
|
+
pi.on("session_shutdown", (event, ctx) => {
|
|
42
|
+
if (event.reason === "quit" && ctx.mode === "tui" && getCapabilities().images === "kitty") {
|
|
43
|
+
process.stdout.write(deleteAllKittyImages());
|
|
44
|
+
}
|
|
45
|
+
});
|
|
40
46
|
pi.on("session_start", (_event, ctx) => {
|
|
41
47
|
if (ctx.mode !== "tui") return;
|
|
42
48
|
initFolds(ctx.ui);
|
package/src/tables.ts
CHANGED
|
@@ -8,6 +8,14 @@ import {
|
|
|
8
8
|
import type { ExtensionAPI } from "@xynogen/pix-runtime/host";
|
|
9
9
|
import { wrapMethod } from "@xynogen/pix-runtime/patch";
|
|
10
10
|
import { themeAfterSessionStart } from "./code-blocks.ts";
|
|
11
|
+
import { attrs } from "./directives.ts";
|
|
12
|
+
import {
|
|
13
|
+
blankCellImages,
|
|
14
|
+
type CellImage,
|
|
15
|
+
cellImage,
|
|
16
|
+
parseImage,
|
|
17
|
+
placeCellImages,
|
|
18
|
+
} from "./image.ts";
|
|
11
19
|
|
|
12
20
|
type TableTheme = Pick<Theme, "bold" | "fg">;
|
|
13
21
|
type Align = "left" | "center" | "right" | null;
|
|
@@ -202,18 +210,46 @@ export function styleTable(
|
|
|
202
210
|
});
|
|
203
211
|
}
|
|
204
212
|
|
|
213
|
+
/** Cells that hold only `::image[...]`: placeholder text now, the image after Pi's layout. */
|
|
214
|
+
function imageCells(token: TableToken, width: number): { token: TableToken; images: CellImage[] } {
|
|
215
|
+
const images: CellImage[] = [];
|
|
216
|
+
let changed = false;
|
|
217
|
+
// An even share of the width per column, minus its borders.
|
|
218
|
+
const maxCols = Math.max(1, Math.floor(width / Math.max(1, token.header.length)) - 3);
|
|
219
|
+
const rows = token.rows.map((row) =>
|
|
220
|
+
row.map((cell) => {
|
|
221
|
+
const image = parseImage(cell.text.trim(), attrs);
|
|
222
|
+
if (!image) return cell;
|
|
223
|
+
const shown = cellImage(image, maxCols);
|
|
224
|
+
if (shown.image) images.push(shown.image);
|
|
225
|
+
changed = true;
|
|
226
|
+
return { ...cell, tokens: [{ type: "text", raw: "", text: shown.text }] };
|
|
227
|
+
}),
|
|
228
|
+
);
|
|
229
|
+
return { token: changed ? { ...token, rows } : token, images };
|
|
230
|
+
}
|
|
231
|
+
|
|
205
232
|
function patchTableRenderer(): void {
|
|
206
233
|
// A Pi release without this private method keeps its own table rendering.
|
|
207
234
|
wrapMethod(
|
|
208
235
|
Markdown.prototype,
|
|
209
236
|
"renderTable",
|
|
210
237
|
(native) =>
|
|
211
|
-
function renderStyledTable(
|
|
238
|
+
function renderStyledTable(
|
|
239
|
+
this: object,
|
|
240
|
+
token: TableToken,
|
|
241
|
+
width: number,
|
|
242
|
+
...rest: unknown[]
|
|
243
|
+
) {
|
|
212
244
|
const sort = token.raw ? sorts.get(token.raw) : undefined;
|
|
213
|
-
const
|
|
245
|
+
const cells = imageCells(sortableToken(token, sort), width);
|
|
246
|
+
const lines: string[] = native.call(this, cells.token, width, ...rest);
|
|
214
247
|
const styled = activeTheme ? styleTable(lines, token.align ?? [], activeTheme) : lines;
|
|
215
248
|
rememberHeader(this, token, styled);
|
|
216
|
-
return styled;
|
|
249
|
+
if (cells.images.length === 0) return styled;
|
|
250
|
+
const indent = " ".repeat(Number(Reflect.get(this, "paddingX")) || 0);
|
|
251
|
+
// A wrapped placeholder cannot carry an image: keep the table, drop the pictures.
|
|
252
|
+
return placeCellImages(styled, cells.images, indent) ?? blankCellImages(styled);
|
|
217
253
|
},
|
|
218
254
|
);
|
|
219
255
|
// Markdown has no mouse handler of its own; Pi routes clicks to any component with one.
|