@xynogen/pix-display 0.5.0 → 0.7.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 +3 -3
- package/src/code-blocks.ts +50 -17
- package/src/code-frame.ts +85 -0
- package/src/fold.ts +3 -2
- package/src/index.ts +5 -1
- package/src/tables.ts +13 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xynogen/pix-display",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.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,8 +38,8 @@
|
|
|
38
38
|
"access": "public"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@xynogen/pix-pretty": "^3.
|
|
42
|
-
"@xynogen/pix-runtime": "^0.
|
|
41
|
+
"@xynogen/pix-pretty": "^3.3.0",
|
|
42
|
+
"@xynogen/pix-runtime": "^0.24.0"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@mathjax/src": "4.1.3",
|
package/src/code-blocks.ts
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
|
+
import { RST } from "@xynogen/pix-pretty/ansi";
|
|
1
2
|
import {
|
|
2
3
|
AssistantMessageComponent,
|
|
3
4
|
type Theme,
|
|
4
5
|
truncateToWidth,
|
|
5
6
|
visibleWidth,
|
|
6
7
|
} from "@xynogen/pix-pretty/tui";
|
|
8
|
+
import { gutterWidth, lineGutter } from "@xynogen/pix-pretty/utils";
|
|
7
9
|
import type { ExtensionAPI } from "@xynogen/pix-runtime/host";
|
|
8
10
|
import { wrapMethod } from "@xynogen/pix-runtime/patch";
|
|
11
|
+
import {
|
|
12
|
+
COPY_BUTTON,
|
|
13
|
+
type CodeFrame,
|
|
14
|
+
FOLD_ARROW,
|
|
15
|
+
forgetFrames,
|
|
16
|
+
isFolded,
|
|
17
|
+
rememberFrames,
|
|
18
|
+
} from "./code-frame.ts";
|
|
9
19
|
|
|
10
20
|
const OPENING_FENCE_RE = /^```([^`]*)$/;
|
|
11
21
|
const CLOSING_FENCE_RE = /^```\s*$/;
|
|
@@ -67,12 +77,23 @@ function fenceLabel(line: string): string | undefined {
|
|
|
67
77
|
return info.split(/\s+/, 1)[0] || DEFAULT_LABEL;
|
|
68
78
|
}
|
|
69
79
|
|
|
70
|
-
function topRule(
|
|
71
|
-
|
|
80
|
+
function topRule(
|
|
81
|
+
width: number,
|
|
82
|
+
language: string,
|
|
83
|
+
theme: CodeFrameTheme,
|
|
84
|
+
lines: number,
|
|
85
|
+
folded: boolean,
|
|
86
|
+
): string {
|
|
87
|
+
const meta = folded ? theme.fg("dim", `· ${lines} lines `) : "";
|
|
88
|
+
const available = Math.max(1, width - 4 - COPY_BUTTON.length - visibleWidth(meta));
|
|
72
89
|
const displayLanguage = truncateToWidth(language, available, "…");
|
|
73
90
|
const label = theme.bold(theme.fg("accent", ` ${displayLanguage} `));
|
|
74
|
-
const ruleWidth = Math.max(
|
|
75
|
-
|
|
91
|
+
const ruleWidth = Math.max(
|
|
92
|
+
0,
|
|
93
|
+
width - FOLD_ARROW - visibleWidth(label) - visibleWidth(meta) - COPY_BUTTON.length,
|
|
94
|
+
);
|
|
95
|
+
const arrow = theme.fg("accent", folded ? "▸" : "▾");
|
|
96
|
+
return `${arrow}${label}${meta}${theme.fg("borderMuted", "─".repeat(ruleWidth))}${theme.fg("dim", COPY_BUTTON)}`;
|
|
76
97
|
}
|
|
77
98
|
|
|
78
99
|
function bottomRule(width: number, theme: CodeFrameTheme): string {
|
|
@@ -80,10 +101,9 @@ function bottomRule(width: number, theme: CodeFrameTheme): string {
|
|
|
80
101
|
}
|
|
81
102
|
|
|
82
103
|
function bodyLine(line: string, width: number, layoutIndent: number): string {
|
|
83
|
-
|
|
84
|
-
//
|
|
85
|
-
|
|
86
|
-
return content;
|
|
104
|
+
// Code column only; the caller prepends the same numbered gutter as the
|
|
105
|
+
// file-content renderer.
|
|
106
|
+
return truncateToWidth(stripLayoutWhitespace(line, layoutIndent), width, "…");
|
|
87
107
|
}
|
|
88
108
|
|
|
89
109
|
/**
|
|
@@ -94,6 +114,7 @@ export function renderCodeFences(
|
|
|
94
114
|
lines: readonly string[],
|
|
95
115
|
width: number,
|
|
96
116
|
theme: CodeFrameTheme,
|
|
117
|
+
frames: CodeFrame[] = [],
|
|
97
118
|
): readonly string[] {
|
|
98
119
|
if (width < 12) return lines;
|
|
99
120
|
|
|
@@ -110,22 +131,31 @@ export function renderCodeFences(
|
|
|
110
131
|
|
|
111
132
|
const frameWidth = width;
|
|
112
133
|
const body = out.slice(start + 1, end);
|
|
134
|
+
const numberWidth = Math.max(3, String(body.length).length);
|
|
135
|
+
const codeWidth = Math.max(1, width - gutterWidth(numberWidth));
|
|
113
136
|
const bodyIndents = body
|
|
114
137
|
.map((line) => plainText(line))
|
|
115
138
|
.filter((line) => line.trim().length > 0)
|
|
116
139
|
.map((line) => leadingSpaces(line));
|
|
117
140
|
const bodyIndent = bodyIndents.length > 0 ? Math.min(...bodyIndents) : 0;
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
141
|
+
const source = body
|
|
142
|
+
.map((line) => plainText(stripLayoutWhitespace(line, bodyIndent)))
|
|
143
|
+
.join("\n");
|
|
144
|
+
const isFoldedBlock = isFolded(source);
|
|
145
|
+
const framed: string[] = [
|
|
146
|
+
`${oscSequences(out[start] ?? "")}${topRule(frameWidth, language, theme, body.length, isFoldedBlock)}`,
|
|
147
|
+
];
|
|
148
|
+
if (!isFoldedBlock) {
|
|
149
|
+
for (let index = start + 1; index < end; index++) {
|
|
150
|
+
framed.push(
|
|
151
|
+
`${oscSequences(out[index] ?? "")}${lineGutter(index - start, numberWidth)}${bodyLine(out[index] ?? "", codeWidth, bodyIndent)}${RST}`,
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
framed.push(`${oscSequences(out[end] ?? "")}${bottomRule(frameWidth, theme)}`);
|
|
125
155
|
}
|
|
126
|
-
framed.push(`${oscSequences(out[end] ?? "")}${bottomRule(frameWidth, theme)}`);
|
|
127
156
|
|
|
128
157
|
out.splice(start, end - start + 1, ...framed);
|
|
158
|
+
frames.push({ row: start, text: source });
|
|
129
159
|
start += framed.length - 1;
|
|
130
160
|
}
|
|
131
161
|
return out;
|
|
@@ -148,6 +178,7 @@ function patchAssistantRenderer(): void {
|
|
|
148
178
|
(native) =>
|
|
149
179
|
function invalidateCodeFrames(this: object): void {
|
|
150
180
|
cache.delete(this);
|
|
181
|
+
forgetFrames(this);
|
|
151
182
|
native.call(this);
|
|
152
183
|
},
|
|
153
184
|
);
|
|
@@ -168,7 +199,9 @@ function patchAssistantRenderer(): void {
|
|
|
168
199
|
) {
|
|
169
200
|
return previous.output;
|
|
170
201
|
}
|
|
171
|
-
const
|
|
202
|
+
const frames: CodeFrame[] = [];
|
|
203
|
+
const output = renderCodeFences(lines, width, activeTheme, frames);
|
|
204
|
+
rememberFrames(this, frames);
|
|
172
205
|
cache.set(this, { width, theme: activeTheme, input: [...lines], output });
|
|
173
206
|
return output;
|
|
174
207
|
},
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interactions on a code frame's header row: the arrow at the left folds the block,
|
|
3
|
+
* the `copy` button at the right end writes its source to the system clipboard.
|
|
4
|
+
*
|
|
5
|
+
* Rows come from the same render that drew the frames, so a hit test cannot drift
|
|
6
|
+
* from what the user sees. Display only: the session keeps the raw text.
|
|
7
|
+
*/
|
|
8
|
+
import { showTransientMessage, type TransientErrorUI } from "@xynogen/pix-pretty/transient-error";
|
|
9
|
+
import { getErrorMessage } from "@xynogen/pix-runtime/errors";
|
|
10
|
+
import { copyToClipboard } from "@xynogen/pix-runtime/host";
|
|
11
|
+
|
|
12
|
+
/** Right-aligned header label. Its width is the click target at the row's end. */
|
|
13
|
+
export const COPY_BUTTON = " copy ";
|
|
14
|
+
/** Columns taken by the fold arrow at the row's start. */
|
|
15
|
+
export const FOLD_ARROW = 1;
|
|
16
|
+
|
|
17
|
+
/** One framed block: its header row in the rendered lines, and the source it holds. */
|
|
18
|
+
export type CodeFrame = { row: number; text: string };
|
|
19
|
+
|
|
20
|
+
type Click = { type: string; button: string; x: number; y: number; width: number };
|
|
21
|
+
type Owner = { invalidate?(): void };
|
|
22
|
+
|
|
23
|
+
// Keyed by the message component that rendered the frames; rebuilt on every render.
|
|
24
|
+
const frames = new WeakMap<object, readonly CodeFrame[]>();
|
|
25
|
+
// Blocks the user folded, by source text, so the state survives re-renders.
|
|
26
|
+
const folded = new Set<string>();
|
|
27
|
+
|
|
28
|
+
let ui: TransientErrorUI | undefined;
|
|
29
|
+
let write = copyToClipboard;
|
|
30
|
+
|
|
31
|
+
/** Remember the UI for feedback and the clipboard writer (tests pass a fake). */
|
|
32
|
+
export function initCodeFrames(target: TransientErrorUI, copy = copyToClipboard): void {
|
|
33
|
+
ui = target;
|
|
34
|
+
write = copy;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function isFolded(text: string): boolean {
|
|
38
|
+
return folded.has(text);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Pi's expand key opens every block again. Returns whether anything was folded. */
|
|
42
|
+
export function setCodeFramesExpanded(expanded: boolean): boolean {
|
|
43
|
+
if (!expanded || folded.size === 0) return false;
|
|
44
|
+
folded.clear();
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function rememberFrames(owner: object, list: readonly CodeFrame[]): void {
|
|
49
|
+
frames.set(owner, list);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function forgetFrames(owner: object): void {
|
|
53
|
+
frames.delete(owner);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function notify(message: string, level: "info" | "warning"): void {
|
|
57
|
+
if (ui) showTransientMessage(ui, message, level);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function copyFrame(frame: CodeFrame): void {
|
|
61
|
+
const lines = frame.text.split("\n").length;
|
|
62
|
+
void write(frame.text).then(
|
|
63
|
+
() => notify(`copied ${lines} lines`, "info"),
|
|
64
|
+
(error: unknown) => notify(`copy failed: ${getErrorMessage(error)}`, "warning"),
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Left click on a frame's header: the arrow folds the block, the trailing `copy`
|
|
70
|
+
* button writes it. Returns true when the click was ours.
|
|
71
|
+
*/
|
|
72
|
+
export function frameClickAt(owner: Owner, event: Click): boolean {
|
|
73
|
+
if (event.type !== "click" || event.button !== "left") return false;
|
|
74
|
+
const frame = frames.get(owner)?.find((candidate) => candidate.row === event.y);
|
|
75
|
+
if (!frame) return false;
|
|
76
|
+
if (event.x < FOLD_ARROW) {
|
|
77
|
+
if (folded.has(frame.text)) folded.delete(frame.text);
|
|
78
|
+
else folded.add(frame.text);
|
|
79
|
+
owner.invalidate?.();
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
if (event.x < event.width - COPY_BUTTON.length) return false;
|
|
83
|
+
copyFrame(frame);
|
|
84
|
+
return true;
|
|
85
|
+
}
|
package/src/fold.ts
CHANGED
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
type Theme,
|
|
14
14
|
} from "@xynogen/pix-pretty/tui";
|
|
15
15
|
import { wrapMethod } from "@xynogen/pix-runtime/patch";
|
|
16
|
+
import { frameClickAt, setCodeFramesExpanded } from "./code-frame.ts";
|
|
16
17
|
|
|
17
18
|
const FOLD = /^::fold(?:\[([^\]\n]*)\])?\s*$/;
|
|
18
19
|
const FENCE = /^\s*(```|~~~)/;
|
|
@@ -143,7 +144,7 @@ export function followFoldInput(): void {
|
|
|
143
144
|
writable: true,
|
|
144
145
|
value(this: AssistantMessageComponent, expanded: boolean): void {
|
|
145
146
|
open = expanded;
|
|
146
|
-
|
|
147
|
+
if (setCodeFramesExpanded(expanded)) this.invalidate();
|
|
147
148
|
if (textsOf(this).some((text) => text.includes("::fold"))) this.invalidate();
|
|
148
149
|
},
|
|
149
150
|
});
|
|
@@ -155,7 +156,7 @@ export function followFoldInput(): void {
|
|
|
155
156
|
const result = native.call(this, event);
|
|
156
157
|
if (result || event.type !== "click" || event.button !== "left") return result;
|
|
157
158
|
const key = foldAt(this, event.width, event.y);
|
|
158
|
-
if (key === undefined) return undefined;
|
|
159
|
+
if (key === undefined) return frameClickAt(this, event) ? { handled: true } : undefined;
|
|
159
160
|
clicked.set(key, !(clicked.get(key) ?? open));
|
|
160
161
|
this.invalidate();
|
|
161
162
|
return { handled: true };
|
package/src/index.ts
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
* @xynogen/pix-pretty/chips Inline chips (tag format, editor, sent messages)
|
|
9
9
|
* thinking.ts Leaked reasoning tag → native thinking content blocks
|
|
10
10
|
* code-blocks.ts Framed, syntax-highlighted code fences in LLM output
|
|
11
|
+
* code-copy.ts The `copy` button on a code frame's header row
|
|
11
12
|
* directives.ts `::card[…]`, charts and inline directives in LLM output
|
|
12
13
|
* fold.ts `::fold[Title]` … `::fold` sections, opened by click or Pi's expand key
|
|
13
14
|
* tables.ts Themed Markdown tables with column alignment
|
|
@@ -18,6 +19,7 @@ import type { ExtensionAPI } from "@xynogen/pix-runtime/host";
|
|
|
18
19
|
import { once } from "@xynogen/pix-runtime/once";
|
|
19
20
|
|
|
20
21
|
import codeBlocksExtension from "./code-blocks.ts";
|
|
22
|
+
import { initCodeFrames } from "./code-frame.ts";
|
|
21
23
|
import directivesExtension from "./directives.ts";
|
|
22
24
|
import { followFoldInput, initFolds } from "./fold.ts";
|
|
23
25
|
import { hasDisplayMath, offerMathInstall, startMath } from "./math.ts";
|
|
@@ -36,7 +38,9 @@ export default async function pixDisplayExtension(pi: ExtensionAPI): Promise<voi
|
|
|
36
38
|
workingExtension(pi);
|
|
37
39
|
followFoldInput();
|
|
38
40
|
pi.on("session_start", (_event, ctx) => {
|
|
39
|
-
if (ctx.mode
|
|
41
|
+
if (ctx.mode !== "tui") return;
|
|
42
|
+
initFolds(ctx.ui);
|
|
43
|
+
initCodeFrames(ctx.ui);
|
|
40
44
|
});
|
|
41
45
|
pi.on("agent_end", (event, ctx) => {
|
|
42
46
|
if (ctx.mode !== "tui") return;
|
package/src/tables.ts
CHANGED
|
@@ -86,12 +86,18 @@ export function columnAt(plain: string, x: number): number | undefined {
|
|
|
86
86
|
return col < 0 ? last - 1 : col;
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
-
/** A copy of the table token with
|
|
90
|
-
function
|
|
91
|
-
const arrow =
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
89
|
+
/** A copy of the table token with sort hints in every header and optional sorted rows. */
|
|
90
|
+
function sortableToken(token: TableToken, sort?: Sort): TableToken {
|
|
91
|
+
const arrow = (i: number) => ({
|
|
92
|
+
type: "text",
|
|
93
|
+
raw: "",
|
|
94
|
+
text: i === sort?.col ? (sort.desc ? "▼ " : "▲ ") : "▾ ",
|
|
95
|
+
});
|
|
96
|
+
const header = token.header.map((cell, i) => ({
|
|
97
|
+
...cell,
|
|
98
|
+
tokens: [arrow(i), ...(cell.tokens ?? [])],
|
|
99
|
+
}));
|
|
100
|
+
if (!sort) return { ...token, header };
|
|
95
101
|
const numeric = isNumericColumn(token.rows, sort.col);
|
|
96
102
|
return { ...token, header, rows: sortRows(token.rows, sort.col, sort.desc, numeric) };
|
|
97
103
|
}
|
|
@@ -204,7 +210,7 @@ function patchTableRenderer(): void {
|
|
|
204
210
|
(native) =>
|
|
205
211
|
function renderStyledTable(this: object, token: TableToken, ...rest: unknown[]) {
|
|
206
212
|
const sort = token.raw ? sorts.get(token.raw) : undefined;
|
|
207
|
-
const lines: string[] = native.call(this,
|
|
213
|
+
const lines: string[] = native.call(this, sortableToken(token, sort), ...rest);
|
|
208
214
|
const styled = activeTheme ? styleTable(lines, token.align ?? [], activeTheme) : lines;
|
|
209
215
|
rememberHeader(this, token, styled);
|
|
210
216
|
return styled;
|