@xynogen/pix-display 0.4.2 → 0.4.4
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 +3 -3
- package/src/code-blocks.ts +34 -35
- package/src/directives.ts +9 -5
- package/src/image.ts +123 -4
- package/src/math.ts +5 -37
- package/src/mermaid.ts +16 -19
- package/src/tables.ts +17 -20
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xynogen/pix-display",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.4",
|
|
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,8 +38,8 @@
|
|
|
38
38
|
"access": "public"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@xynogen/pix-pretty": "^3.0
|
|
42
|
-
"@xynogen/pix-runtime": "^0.22.
|
|
41
|
+
"@xynogen/pix-pretty": "^3.1.0",
|
|
42
|
+
"@xynogen/pix-runtime": "^0.22.4"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@mathjax/src": "4.1.3",
|
package/src/code-blocks.ts
CHANGED
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
visibleWidth,
|
|
6
6
|
} from "@xynogen/pix-pretty/tui";
|
|
7
7
|
import type { ExtensionAPI } from "@xynogen/pix-runtime/host";
|
|
8
|
+
import { wrapMethod } from "@xynogen/pix-runtime/patch";
|
|
8
9
|
|
|
9
10
|
const OPENING_FENCE_RE = /^```([^`]*)$/;
|
|
10
11
|
const CLOSING_FENCE_RE = /^```\s*$/;
|
|
@@ -12,17 +13,10 @@ const DEFAULT_LABEL = "code";
|
|
|
12
13
|
const ANSI_RE = /\x1b\[[0-?]*[ -/]*[@-~]/g;
|
|
13
14
|
const BACKGROUND_ANSI_RE = /\x1b\[(?:4[0-9]|48)(?:;[^m]*)?m/g;
|
|
14
15
|
const OSC_RE = /\x1b\][^\x07]*(?:\x07|\x1b\\)/g;
|
|
15
|
-
const PATCHED = Symbol.for("@xynogen/pix-display:code-block-renderer");
|
|
16
16
|
|
|
17
17
|
type CodeFrameTheme = Pick<Theme, "bold" | "fg">;
|
|
18
18
|
type RenderMode = "tui" | "rpc" | "json" | "print";
|
|
19
19
|
|
|
20
|
-
type PatchablePrototype = {
|
|
21
|
-
[PATCHED]?: boolean;
|
|
22
|
-
render(width: number): readonly string[];
|
|
23
|
-
invalidate(): void;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
20
|
let activeTheme: CodeFrameTheme | undefined;
|
|
27
21
|
|
|
28
22
|
/**
|
|
@@ -138,13 +132,9 @@ export function renderCodeFences(
|
|
|
138
132
|
}
|
|
139
133
|
|
|
140
134
|
function patchAssistantRenderer(): void {
|
|
141
|
-
const prototype = AssistantMessageComponent.prototype
|
|
142
|
-
if (prototype[PATCHED]) return;
|
|
143
|
-
|
|
144
|
-
const nativeRender = prototype.render;
|
|
145
|
-
const nativeInvalidate = prototype.invalidate;
|
|
135
|
+
const prototype = AssistantMessageComponent.prototype;
|
|
146
136
|
const cache = new WeakMap<
|
|
147
|
-
|
|
137
|
+
object,
|
|
148
138
|
{
|
|
149
139
|
width: number;
|
|
150
140
|
theme: CodeFrameTheme;
|
|
@@ -152,28 +142,37 @@ function patchAssistantRenderer(): void {
|
|
|
152
142
|
output: readonly string[];
|
|
153
143
|
}
|
|
154
144
|
>();
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
145
|
+
wrapMethod(
|
|
146
|
+
prototype,
|
|
147
|
+
"invalidate",
|
|
148
|
+
(native) =>
|
|
149
|
+
function invalidateCodeFrames(this: object): void {
|
|
150
|
+
cache.delete(this);
|
|
151
|
+
native.call(this);
|
|
152
|
+
},
|
|
153
|
+
);
|
|
154
|
+
wrapMethod(
|
|
155
|
+
prototype,
|
|
156
|
+
"render",
|
|
157
|
+
(native) =>
|
|
158
|
+
function renderWithCodeFrames(this: object, width: number): readonly string[] {
|
|
159
|
+
const lines: readonly string[] = native.call(this, width);
|
|
160
|
+
if (!activeTheme) return lines;
|
|
161
|
+
const previous = cache.get(this);
|
|
162
|
+
if (
|
|
163
|
+
previous &&
|
|
164
|
+
previous.width === width &&
|
|
165
|
+
previous.theme === activeTheme &&
|
|
166
|
+
previous.input.length === lines.length &&
|
|
167
|
+
lines.every((line, i) => line === previous.input[i])
|
|
168
|
+
) {
|
|
169
|
+
return previous.output;
|
|
170
|
+
}
|
|
171
|
+
const output = renderCodeFences(lines, width, activeTheme);
|
|
172
|
+
cache.set(this, { width, theme: activeTheme, input: [...lines], output });
|
|
173
|
+
return output;
|
|
174
|
+
},
|
|
175
|
+
);
|
|
177
176
|
}
|
|
178
177
|
|
|
179
178
|
export default function codeBlocksExtension(pi: ExtensionAPI): void {
|
package/src/directives.ts
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
* inline code stay literal.
|
|
14
14
|
*/
|
|
15
15
|
import { BOLD, FG_BLUE, FG_DIM, FG_GREEN, FG_RED, FG_YELLOW, RST } from "@xynogen/pix-pretty/ansi";
|
|
16
|
+
import { setMarkdownTransform } from "@xynogen/pix-pretty/markdown-transform";
|
|
16
17
|
import {
|
|
17
18
|
getCapabilities,
|
|
18
19
|
getMarkdownTheme,
|
|
@@ -477,13 +478,16 @@ export function renderDirectives(markdown: string, width: number): string {
|
|
|
477
478
|
return out.join("\n");
|
|
478
479
|
}
|
|
479
480
|
|
|
481
|
+
/** The display pipeline for assistant Markdown: Mermaid, math, then directives. */
|
|
482
|
+
export function renderDisplayMarkdown(markdown: string, width: number): string {
|
|
483
|
+
return renderDirectives(renderMathBlocks(renderMermaidBlocks(markdown, width), width), width);
|
|
484
|
+
}
|
|
485
|
+
|
|
480
486
|
export default function directivesExtension(pi: ExtensionAPI): void {
|
|
481
487
|
pi.registerMarkdownTransformer?.(
|
|
482
|
-
(markdown, { availableWidth }) =>
|
|
483
|
-
renderDirectives(
|
|
484
|
-
renderMathBlocks(renderMermaidBlocks(markdown, availableWidth), availableWidth),
|
|
485
|
-
availableWidth,
|
|
486
|
-
),
|
|
488
|
+
(markdown, { availableWidth }) => renderDisplayMarkdown(markdown, availableWidth),
|
|
487
489
|
{ messageTypes: ["assistant"] },
|
|
488
490
|
);
|
|
491
|
+
// Entry renderers (the /btw card) bypass Pi's chain; share the same pipeline.
|
|
492
|
+
setMarkdownTransform(renderDisplayMarkdown);
|
|
489
493
|
}
|
package/src/image.ts
CHANGED
|
@@ -7,9 +7,14 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { basename } from "node:path";
|
|
9
9
|
import { FG_DIM, RST } from "@xynogen/pix-pretty/ansi";
|
|
10
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
getCapabilities,
|
|
12
|
+
getCellDimensions,
|
|
13
|
+
getImageDimensions,
|
|
14
|
+
Image,
|
|
15
|
+
} from "@xynogen/pix-pretty/tui";
|
|
11
16
|
import { readFileSync, statSync } from "@xynogen/pix-runtime/fs";
|
|
12
|
-
import { localFilePath } from "@xynogen/pix-runtime/paths";
|
|
17
|
+
import { localFilePath, requireFrom } from "@xynogen/pix-runtime/paths";
|
|
13
18
|
|
|
14
19
|
const IMAGE = /^::image\[([^\]\n]*)\](?:\{([^}\n]*)\})?\s*$/;
|
|
15
20
|
// Read synchronously during render, so bound the bytes one directive can load.
|
|
@@ -83,6 +88,7 @@ export function renderImage(image: ImageDirective, width: number): string[] {
|
|
|
83
88
|
}
|
|
84
89
|
|
|
85
90
|
type RowOptions = { maxWidthCells: number; maxHeightCells?: number; filename?: string };
|
|
91
|
+
type Dimensions = { widthPx: number; heightPx: number };
|
|
86
92
|
|
|
87
93
|
/** Image rows through Pi's `Image`; blank rows become NBSP so Markdown keeps the height. */
|
|
88
94
|
export function renderImageRows(
|
|
@@ -91,8 +97,121 @@ export function renderImageRows(
|
|
|
91
97
|
width: number,
|
|
92
98
|
options: RowOptions,
|
|
93
99
|
): string[] {
|
|
94
|
-
const
|
|
95
|
-
|
|
100
|
+
const source = getImageDimensions(data, mime) ?? undefined;
|
|
101
|
+
const png =
|
|
102
|
+
source && getCapabilities().images ? fitToCells(data, source, width, options) : undefined;
|
|
103
|
+
const [shown, type] = png ? [png, "image/png"] : [data, mime];
|
|
104
|
+
const dims = getImageDimensions(shown, type) ?? undefined;
|
|
105
|
+
return new Image(shown, type, { fallbackColor: (s) => `${FG_DIM}${s}${RST}` }, options, dims)
|
|
96
106
|
.render(width)
|
|
97
107
|
.map((row) => row || NBSP);
|
|
98
108
|
}
|
|
109
|
+
|
|
110
|
+
type PhotonImage = { get_bytes(): Uint8Array; free(): void };
|
|
111
|
+
type Photon = {
|
|
112
|
+
PhotonImage: { new_from_byteslice(bytes: Uint8Array): PhotonImage };
|
|
113
|
+
resize(image: PhotonImage, width: number, height: number, filter: number): PhotonImage;
|
|
114
|
+
};
|
|
115
|
+
// photon's SamplingFilter.Lanczos3, the filter Pi's own image resize uses.
|
|
116
|
+
const LANCZOS3 = 5;
|
|
117
|
+
let photon: Photon | null | undefined;
|
|
118
|
+
|
|
119
|
+
/** Pi's photon-node (a Pi dependency, not ours): the running CLI's copy, else the peer install's. */
|
|
120
|
+
function loadPhoton(): Photon | null {
|
|
121
|
+
if (photon !== undefined) return photon;
|
|
122
|
+
photon = null;
|
|
123
|
+
const hosts = [
|
|
124
|
+
() => process.argv[1] ?? "",
|
|
125
|
+
// ESM-only package (no `require` export): resolve it the ESM way.
|
|
126
|
+
() => import.meta.resolve("@earendil-works/pi-coding-agent"),
|
|
127
|
+
];
|
|
128
|
+
for (const host of hosts) {
|
|
129
|
+
try {
|
|
130
|
+
// SAFETY: photon-node is CommonJS with this API; Pi's utils/photon.ts makes the same calls.
|
|
131
|
+
photon = requireFrom(host())("@silvia-odwyer/photon-node") as Photon;
|
|
132
|
+
break;
|
|
133
|
+
} catch {
|
|
134
|
+
// Not resolvable from this host: try the next one.
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return photon;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The image as a PNG no larger than the pixels its cells show, or undefined when it
|
|
142
|
+
* already fits or cannot be resized. A terminal shows no more than that anyway, and
|
|
143
|
+
* herdr drops a Kitty image whose decoded RGBA exceeds its transfer budget (~30 MB).
|
|
144
|
+
* The box mirrors Pi's `Image`: `width - 2` columns, square-ish rows by default. One
|
|
145
|
+
* extra cell each way absorbs callers' rounding, so a raster sized to its cells stays.
|
|
146
|
+
* ponytail: synchronous (render is sync) and no EXIF rotation; a huge photo costs ~1 s
|
|
147
|
+
* once per cache entry. Upgrade path: Pi's async `resizeImage` plus a transcript redraw.
|
|
148
|
+
*/
|
|
149
|
+
function fitToCells(data: string, source: Dimensions, width: number, options: RowOptions) {
|
|
150
|
+
const cell = getCellDimensions();
|
|
151
|
+
const cols = Math.max(1, Math.min(width - 2, options.maxWidthCells));
|
|
152
|
+
const rows = options.maxHeightCells ?? Math.ceil((cols * cell.widthPx) / cell.heightPx);
|
|
153
|
+
if (source.widthPx <= (cols + 1) * cell.widthPx && source.heightPx <= (rows + 1) * cell.heightPx)
|
|
154
|
+
return undefined;
|
|
155
|
+
const scale = Math.min(
|
|
156
|
+
(cols * cell.widthPx) / source.widthPx,
|
|
157
|
+
(rows * cell.heightPx) / source.heightPx,
|
|
158
|
+
);
|
|
159
|
+
const lib = loadPhoton();
|
|
160
|
+
if (!lib) return undefined;
|
|
161
|
+
let image: PhotonImage | undefined;
|
|
162
|
+
let out: PhotonImage | undefined;
|
|
163
|
+
try {
|
|
164
|
+
image = lib.PhotonImage.new_from_byteslice(Buffer.from(data, "base64"));
|
|
165
|
+
const w = Math.max(1, Math.round(source.widthPx * scale));
|
|
166
|
+
const h = Math.max(1, Math.round(source.heightPx * scale));
|
|
167
|
+
out = lib.resize(image, w, h, LANCZOS3);
|
|
168
|
+
return Buffer.from(out.get_bytes()).toString("base64");
|
|
169
|
+
} catch {
|
|
170
|
+
// Undecodable source: show it unchanged and let Pi's Image fall back.
|
|
171
|
+
return undefined;
|
|
172
|
+
} finally {
|
|
173
|
+
out?.free();
|
|
174
|
+
image?.free();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Space between an image and its frame: columns left/right, rows above/below.
|
|
179
|
+
const PAD_X = 2;
|
|
180
|
+
const PAD_Y = 1;
|
|
181
|
+
/** Columns the frame adds around an image. */
|
|
182
|
+
export const FRAME_COLS = 2 + 2 * PAD_X;
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Box image rows in a dim rounded frame, like the `::card` cards, when it fits in
|
|
186
|
+
* `width`. Kitty only: it draws without moving the cursor, so each row writes spaces
|
|
187
|
+
* over the image cells to reach the right border. Spaces, not a cursor jump: a jump
|
|
188
|
+
* leaves the cells unpainted, so a card background has a hole there and old text
|
|
189
|
+
* shows through a transparent image. Kitty keeps an image when text is written
|
|
190
|
+
* under it. iTerm2 draws from the last row with its own cursor movement, so it
|
|
191
|
+
* stays unframed.
|
|
192
|
+
*/
|
|
193
|
+
export function frameImage(image: string[], width: number): string[] {
|
|
194
|
+
// The columns Pi's Image actually drew, which can differ from the requested cap.
|
|
195
|
+
const cols = Number(/[;,]c=(\d+)/.exec(image[0] ?? "")?.[1]);
|
|
196
|
+
if (getCapabilities().images !== "kitty" || !(cols > 0) || cols + FRAME_COLS > width) {
|
|
197
|
+
return image;
|
|
198
|
+
}
|
|
199
|
+
const pad = " ".repeat(PAD_X);
|
|
200
|
+
const under = " ".repeat(cols);
|
|
201
|
+
const side = `${FG_DIM}│${RST}`;
|
|
202
|
+
const rule = "─".repeat(cols + 2 * PAD_X);
|
|
203
|
+
const blank = `${side}${pad}${under}${pad}${side}`;
|
|
204
|
+
// Markdown indents every row by its paddingX except image rows, so indent that one here.
|
|
205
|
+
// ponytail: assumes Markdown spans its parent with equal paddingX on both sides (Pi's
|
|
206
|
+
// transcript, and cards that pad their Markdown rather than their Box).
|
|
207
|
+
const margin = " ".repeat(Math.max(0, ((process.stdout.columns ?? width) - width) >> 1));
|
|
208
|
+
return [
|
|
209
|
+
`${FG_DIM}╭${rule}╮${RST}`,
|
|
210
|
+
...Array<string>(PAD_Y).fill(blank),
|
|
211
|
+
...image.map((row, i) =>
|
|
212
|
+
i === 0 ? `${margin}${side}${pad}${row}${under}${pad}${side}` : blank,
|
|
213
|
+
),
|
|
214
|
+
...Array<string>(PAD_Y).fill(blank),
|
|
215
|
+
`${FG_DIM}╰${rule}╯${RST}`,
|
|
216
|
+
];
|
|
217
|
+
}
|
package/src/math.ts
CHANGED
|
@@ -12,13 +12,12 @@
|
|
|
12
12
|
* support, engine not installed or loaded, a TeX error, or a formula wider than the screen.
|
|
13
13
|
*/
|
|
14
14
|
import { dirname, join } from "node:path";
|
|
15
|
-
import { FG_DIM, RST } from "@xynogen/pix-pretty/ansi";
|
|
16
15
|
import { type ConfirmUI, confirmOverlay } from "@xynogen/pix-pretty/confirm";
|
|
17
16
|
import { getCapabilities, getCellDimensions } from "@xynogen/pix-pretty/tui";
|
|
18
17
|
import { readFile } from "@xynogen/pix-runtime/fs";
|
|
19
18
|
import { installIntoPrefix } from "@xynogen/pix-runtime/npm-prefix";
|
|
20
19
|
import { agentDir, importFile, requireFrom } from "@xynogen/pix-runtime/paths";
|
|
21
|
-
import { renderImageRows } from "./image.ts";
|
|
20
|
+
import { frameImage, renderImageRows } from "./image.ts";
|
|
22
21
|
|
|
23
22
|
/** SVG plus its natural size in `ex`, the unit MathJax lays math out in. */
|
|
24
23
|
type Typeset = (tex: string) => { svg: string; widthEx: number; heightEx: number } | undefined;
|
|
@@ -43,9 +42,6 @@ const INK = "#e6e6e6";
|
|
|
43
42
|
// One `ex` is about half a text row, so text-size math is `heightEx / 2` rows tall.
|
|
44
43
|
const ROWS_PER_EX = 0.5;
|
|
45
44
|
const MAX_ROWS = 8;
|
|
46
|
-
// Space between the formula and its frame: columns left/right, rows above/below.
|
|
47
|
-
const PAD_X = 2;
|
|
48
|
-
const PAD_Y = 1;
|
|
49
45
|
const MAX_CACHE = 32;
|
|
50
46
|
const cache = new Map<string, string[]>();
|
|
51
47
|
|
|
@@ -180,35 +176,6 @@ export async function offerMathInstall(
|
|
|
180
176
|
);
|
|
181
177
|
}
|
|
182
178
|
|
|
183
|
-
/**
|
|
184
|
-
* Box the image in a dim rounded frame, like the `::card` cards. Kitty only: it
|
|
185
|
-
* draws without moving the cursor, so each row jumps the cursor past the image
|
|
186
|
-
* (`CSI n C`) to reach the right border. iTerm2 draws from the last row with its
|
|
187
|
-
* own cursor movement, so it stays unframed.
|
|
188
|
-
*/
|
|
189
|
-
function frame(image: string[], width: number): string[] {
|
|
190
|
-
// The columns Pi's Image actually drew, which can differ from the requested cap.
|
|
191
|
-
const cols = Number(/[;,]c=(\d+)/.exec(image[0] ?? "")?.[1]);
|
|
192
|
-
if (getCapabilities().images !== "kitty" || !(cols > 0)) return image;
|
|
193
|
-
const pad = " ".repeat(PAD_X);
|
|
194
|
-
const skip = `\x1b[${cols}C`;
|
|
195
|
-
const side = `${FG_DIM}│${RST}`;
|
|
196
|
-
const rule = "─".repeat(cols + 2 * PAD_X);
|
|
197
|
-
const blank = `${side}${pad}${skip}${pad}${side}`;
|
|
198
|
-
// Markdown indents every row by Pi's outputPad except image rows, so indent that one here.
|
|
199
|
-
// ponytail: assumes the reply spans the terminal (true for Pi's transcript).
|
|
200
|
-
const margin = " ".repeat(Math.max(0, ((process.stdout.columns ?? width) - width) >> 1));
|
|
201
|
-
return [
|
|
202
|
-
`${FG_DIM}╭${rule}╮${RST}`,
|
|
203
|
-
...Array<string>(PAD_Y).fill(blank),
|
|
204
|
-
...image.map((row, i) =>
|
|
205
|
-
i === 0 ? `${margin}${side}${pad}${row}${skip}${pad}${side}` : blank,
|
|
206
|
-
),
|
|
207
|
-
...Array<string>(PAD_Y).fill(blank),
|
|
208
|
-
`${FG_DIM}╰${rule}╯${RST}`,
|
|
209
|
-
];
|
|
210
|
-
}
|
|
211
|
-
|
|
212
179
|
/** Image rows for one display formula, or undefined to keep Pi's own rendering. */
|
|
213
180
|
export function renderMath(tex: string, width: number): string[] | undefined {
|
|
214
181
|
if (!engine || !getCapabilities().images) return undefined;
|
|
@@ -227,13 +194,14 @@ export function renderMath(tex: string, width: number): string[] | undefined {
|
|
|
227
194
|
const rows = Math.max(1, Math.min(MAX_ROWS, Math.round(math.heightEx * ROWS_PER_EX)));
|
|
228
195
|
const cols = Math.ceil((rows * cell.heightPx * math.widthEx) / math.heightEx / cell.widthPx);
|
|
229
196
|
if (cols > width) return undefined;
|
|
230
|
-
// Rasterise at
|
|
231
|
-
|
|
197
|
+
// Rasterise at the displayed size: cell pixels are the terminal's own resolution,
|
|
198
|
+
// and renderImageRows would shrink anything larger back down.
|
|
199
|
+
const png = engine.rasterise(math.svg, rows * cell.heightPx);
|
|
232
200
|
const image = renderImageRows(Buffer.from(png).toString("base64"), "image/png", width, {
|
|
233
201
|
maxWidthCells: cols,
|
|
234
202
|
maxHeightCells: rows,
|
|
235
203
|
});
|
|
236
|
-
const lines =
|
|
204
|
+
const lines = frameImage(image, width);
|
|
237
205
|
cache.set(key, lines);
|
|
238
206
|
if (cache.size > MAX_CACHE) cache.delete(cache.keys().next().value ?? "");
|
|
239
207
|
return lines;
|
package/src/mermaid.ts
CHANGED
|
@@ -16,11 +16,11 @@
|
|
|
16
16
|
import { join } from "node:path";
|
|
17
17
|
import { type ConfirmUI, confirmOverlay } from "@xynogen/pix-pretty/confirm";
|
|
18
18
|
import { getCapabilities, getCellDimensions } from "@xynogen/pix-pretty/tui";
|
|
19
|
-
import { writeFileAtomicSync } from "@xynogen/pix-runtime/atomic-write";
|
|
20
19
|
import { existsSync, readFile, readFileSync } from "@xynogen/pix-runtime/fs";
|
|
21
20
|
import { installIntoPrefix } from "@xynogen/pix-runtime/npm-prefix";
|
|
22
21
|
import { agentDir, importFile, requireFrom } from "@xynogen/pix-runtime/paths";
|
|
23
|
-
import {
|
|
22
|
+
import { setPiSettings } from "@xynogen/pix-runtime/pi-settings";
|
|
23
|
+
import { FRAME_COLS, frameImage, renderImageRows } from "./image.ts";
|
|
24
24
|
|
|
25
25
|
type Svg = (source: string) => string;
|
|
26
26
|
type Text = (source: string) => string;
|
|
@@ -51,8 +51,9 @@ const COLORS = {
|
|
|
51
51
|
accent: "#7aa2f7",
|
|
52
52
|
muted: "#565f89",
|
|
53
53
|
};
|
|
54
|
-
// Node labels are 13px in beautiful-mermaid's layout
|
|
54
|
+
// Node labels are 13px in beautiful-mermaid's layout; ZOOM sets how many text rows that is.
|
|
55
55
|
const TEXT_PX = 13;
|
|
56
|
+
const ZOOM = 1;
|
|
56
57
|
const MAX_ROWS = 40;
|
|
57
58
|
const MAX_CACHE = 16;
|
|
58
59
|
const cache = new Map<string, string[]>();
|
|
@@ -150,8 +151,8 @@ async function load(): Promise<void> {
|
|
|
150
151
|
rasterise: (svg, heightPx) =>
|
|
151
152
|
new resvg.Resvg(svg, {
|
|
152
153
|
fitTo: { mode: "height", value: heightPx },
|
|
153
|
-
//
|
|
154
|
-
background
|
|
154
|
+
// Transparent, like math: frameImage writes spaces under the image, so the cells
|
|
155
|
+
// hold the surrounding background (a card's color) and no old text.
|
|
155
156
|
font: { fontBuffers, defaultFontFamily: "Inter", loadSystemFonts: false },
|
|
156
157
|
})
|
|
157
158
|
.render()
|
|
@@ -187,14 +188,8 @@ export function hasMermaid(messages: readonly TextMessage[]): boolean {
|
|
|
187
188
|
* this extension. Returns "changed", "already" when it was off, or "failed".
|
|
188
189
|
*/
|
|
189
190
|
function disablePiMermaid(): "changed" | "already" | "failed" {
|
|
190
|
-
const path = join(agentDir(), "settings.json");
|
|
191
191
|
try {
|
|
192
|
-
|
|
193
|
-
const settings = JSON.parse(text) as { markdown?: Record<string, unknown> };
|
|
194
|
-
if (settings.markdown?.mermaid === "off") return "already";
|
|
195
|
-
settings.markdown = { ...settings.markdown, mermaid: "off" };
|
|
196
|
-
writeFileAtomicSync(path, `${JSON.stringify(settings, null, 2)}\n`);
|
|
197
|
-
return "changed";
|
|
192
|
+
return setPiSettings({ markdown: { mermaid: "off" } }) ? "changed" : "already";
|
|
198
193
|
} catch {
|
|
199
194
|
return "failed";
|
|
200
195
|
}
|
|
@@ -276,26 +271,28 @@ export function renderMermaidImage(source: string, width: number): string[] | un
|
|
|
276
271
|
}
|
|
277
272
|
const [, , w, h] = (/viewBox="([^"]+)"/.exec(svg)?.[1] ?? "").split(/\s+/).map(Number);
|
|
278
273
|
if (!(w && h && w > 0 && h > 0)) return undefined;
|
|
279
|
-
// Size from the diagram, like math.ts: its label text lands at
|
|
274
|
+
// Size from the diagram, like math.ts: its label text lands at ZOOM text rows.
|
|
280
275
|
const cell = getCellDimensions();
|
|
281
|
-
const scale = cell.heightPx / TEXT_PX;
|
|
276
|
+
const scale = (ZOOM * cell.heightPx) / TEXT_PX;
|
|
282
277
|
let cols = Math.ceil((w * scale) / cell.widthPx);
|
|
283
278
|
let rows = Math.ceil((h * scale) / cell.heightPx);
|
|
284
|
-
// Too wide or tall: shrink to fit; the text gets smaller than a row.
|
|
285
|
-
const shrink = Math.min(1, width / cols, MAX_ROWS / rows);
|
|
279
|
+
// Too wide or tall: shrink to fit, frame included; the text gets smaller than a row.
|
|
280
|
+
const shrink = Math.min(1, (width - FRAME_COLS) / cols, MAX_ROWS / rows);
|
|
286
281
|
cols = Math.max(1, Math.floor(cols * shrink));
|
|
287
282
|
rows = Math.max(1, Math.floor(rows * shrink));
|
|
288
|
-
// Rasterise at
|
|
283
|
+
// Rasterise at the displayed size: cell pixels are the terminal's own resolution,
|
|
284
|
+
// and renderImageRows would shrink anything larger back down.
|
|
289
285
|
let png: Uint8Array;
|
|
290
286
|
try {
|
|
291
|
-
png = engine.rasterise(svg, rows * cell.heightPx
|
|
287
|
+
png = engine.rasterise(svg, rows * cell.heightPx);
|
|
292
288
|
} catch {
|
|
293
289
|
return undefined;
|
|
294
290
|
}
|
|
295
|
-
const
|
|
291
|
+
const image = renderImageRows(Buffer.from(png).toString("base64"), "image/png", width, {
|
|
296
292
|
maxWidthCells: cols,
|
|
297
293
|
maxHeightCells: rows,
|
|
298
294
|
});
|
|
295
|
+
const lines = frameImage(image, width);
|
|
299
296
|
cache.set(key, lines);
|
|
300
297
|
if (cache.size > MAX_CACHE) cache.delete(cache.keys().next().value ?? "");
|
|
301
298
|
return lines;
|
package/src/tables.ts
CHANGED
|
@@ -1,23 +1,18 @@
|
|
|
1
1
|
import { Markdown, sliceByColumn, type Theme, visibleWidth } from "@xynogen/pix-pretty/tui";
|
|
2
2
|
import type { ExtensionAPI } from "@xynogen/pix-runtime/host";
|
|
3
|
+
import { wrapMethod } from "@xynogen/pix-runtime/patch";
|
|
3
4
|
import { themeAfterSessionStart } from "./code-blocks.ts";
|
|
4
5
|
|
|
5
6
|
type TableTheme = Pick<Theme, "bold" | "fg">;
|
|
6
7
|
type Align = "left" | "center" | "right" | null;
|
|
7
|
-
type RenderTable = (
|
|
8
|
-
this: unknown,
|
|
9
|
-
token: { align?: readonly Align[] },
|
|
10
|
-
...rest: unknown[]
|
|
11
|
-
) => string[];
|
|
12
8
|
|
|
13
|
-
const NATIVE = Symbol.for("@xynogen/pix-display:native-render-table");
|
|
14
9
|
const CORNERS: Record<string, string> = { "┌": "╭", "┐": "╮", "└": "╰", "┘": "╯" };
|
|
15
10
|
const HEAVY: Record<string, string> = { "├": "┝", "─": "━", "┼": "┿", "┤": "┥" };
|
|
16
11
|
// Pi pads each cell with plain spaces, sometimes inside its closing style codes.
|
|
17
12
|
const TRAILING_PAD_RE = /(?: |\x1b\[[0-9;]*m)+$/;
|
|
18
|
-
//
|
|
19
|
-
// style close that sits exactly on the column edge.
|
|
20
|
-
const STYLE_RESET = "\x1b[22;23;24;25;27;28;29;
|
|
13
|
+
// Pi's text-style reset between wrapped cell fragments, plus the background: the slice
|
|
14
|
+
// drops a style close that sits exactly on the column edge, such as a badge's tint.
|
|
15
|
+
const STYLE_RESET = "\x1b[22;23;24;25;27;28;29;39;49m";
|
|
21
16
|
|
|
22
17
|
let activeTheme: TableTheme | undefined;
|
|
23
18
|
|
|
@@ -69,18 +64,20 @@ export function styleTable(
|
|
|
69
64
|
}
|
|
70
65
|
|
|
71
66
|
function patchTableRenderer(): void {
|
|
72
|
-
const prototype = Markdown.prototype;
|
|
73
|
-
// Pi's own renderTable, kept on the prototype: /reload loads a new module that must
|
|
74
|
-
// replace the previous wrapper, not wrap it or skip patching.
|
|
75
|
-
const native: unknown = Reflect.get(prototype, NATIVE) ?? Reflect.get(prototype, "renderTable");
|
|
76
67
|
// A Pi release without this private method keeps its own table rendering.
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
68
|
+
wrapMethod(
|
|
69
|
+
Markdown.prototype,
|
|
70
|
+
"renderTable",
|
|
71
|
+
(native) =>
|
|
72
|
+
function renderStyledTable(
|
|
73
|
+
this: unknown,
|
|
74
|
+
token: { align?: readonly Align[] },
|
|
75
|
+
...rest: unknown[]
|
|
76
|
+
) {
|
|
77
|
+
const lines: string[] = native.call(this, token, ...rest);
|
|
78
|
+
return activeTheme ? styleTable(lines, token.align ?? [], activeTheme) : lines;
|
|
79
|
+
},
|
|
80
|
+
);
|
|
84
81
|
}
|
|
85
82
|
|
|
86
83
|
export default function tablesExtension(pi: ExtensionAPI): void {
|