@xynogen/pix-display 0.3.10 → 0.4.1
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 +9 -3
- package/src/code-blocks.ts +13 -9
- package/src/directives.ts +489 -0
- package/src/image.ts +98 -0
- package/src/index.ts +24 -1
- package/src/math.ts +283 -0
- package/src/mermaid.ts +339 -0
- package/src/tables.ts +86 -0
package/src/tables.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Markdown, sliceByColumn, type Theme, visibleWidth } from "@xynogen/pix-pretty/tui";
|
|
2
|
+
import type { ExtensionAPI } from "@xynogen/pix-runtime/host";
|
|
3
|
+
import { themeAfterSessionStart } from "./code-blocks.ts";
|
|
4
|
+
|
|
5
|
+
type TableTheme = Pick<Theme, "bold" | "fg">;
|
|
6
|
+
type Align = "left" | "center" | "right" | null;
|
|
7
|
+
type RenderTable = (
|
|
8
|
+
this: unknown,
|
|
9
|
+
token: { align?: readonly Align[] },
|
|
10
|
+
...rest: unknown[]
|
|
11
|
+
) => string[];
|
|
12
|
+
|
|
13
|
+
const NATIVE = Symbol.for("@xynogen/pix-display:native-render-table");
|
|
14
|
+
const CORNERS: Record<string, string> = { "┌": "╭", "┐": "╮", "└": "╰", "┘": "╯" };
|
|
15
|
+
// Pi pads each cell with plain spaces, sometimes inside its closing style codes.
|
|
16
|
+
const TRAILING_PAD_RE = /(?: |\x1b\[[0-9;]*m)+$/;
|
|
17
|
+
// Same text-style reset Pi uses between wrapped cell fragments; the slice drops a
|
|
18
|
+
// style close that sits exactly on the column edge.
|
|
19
|
+
const STYLE_RESET = "\x1b[22;23;24;25;27;28;29;39m";
|
|
20
|
+
|
|
21
|
+
let activeTheme: TableTheme | undefined;
|
|
22
|
+
|
|
23
|
+
function alignCell(cell: string, width: number, align: Align | undefined): string {
|
|
24
|
+
const tail = cell.match(TRAILING_PAD_RE)?.[0] ?? "";
|
|
25
|
+
const text = cell.slice(0, cell.length - tail.length) + tail.replaceAll(" ", "");
|
|
26
|
+
const gap = Math.max(0, width - visibleWidth(text));
|
|
27
|
+
const left = align === "right" ? gap : align === "center" ? Math.floor(gap / 2) : 0;
|
|
28
|
+
return `${" ".repeat(left)}${text}${STYLE_RESET}${" ".repeat(gap - left)}`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Restyle Pi's box table: dim rounded borders, column alignment and an accent header.
|
|
33
|
+
* No background fill: a cell-wide fill overhangs the half-cell `│` borders, and some
|
|
34
|
+
* themes and terminals show it barely or not at all, so the table looks the same everywhere.
|
|
35
|
+
*/
|
|
36
|
+
export function styleTable(
|
|
37
|
+
lines: readonly string[],
|
|
38
|
+
align: readonly Align[],
|
|
39
|
+
theme: TableTheme,
|
|
40
|
+
): string[] {
|
|
41
|
+
const top = lines[0];
|
|
42
|
+
// Pi falls back to raw Markdown when the table cannot fit.
|
|
43
|
+
if (!top?.startsWith("┌")) return [...lines];
|
|
44
|
+
const widths = top
|
|
45
|
+
.slice(2, -2)
|
|
46
|
+
.split("─┬─")
|
|
47
|
+
.map((cell) => cell.length);
|
|
48
|
+
const border = (text: string) => theme.fg("borderMuted", text);
|
|
49
|
+
const bar = border("│");
|
|
50
|
+
let header = true;
|
|
51
|
+
return lines.map((line) => {
|
|
52
|
+
if (!line.startsWith("│")) {
|
|
53
|
+
if (line.startsWith("├")) header = false;
|
|
54
|
+
return line && border(line.replace(/^[┌└]|[┐┘]$/g, (c) => CORNERS[c] ?? c));
|
|
55
|
+
}
|
|
56
|
+
let col = 2;
|
|
57
|
+
const cells = widths.map((width, i) => {
|
|
58
|
+
const cell = alignCell(sliceByColumn(line, col, width, true), width, align[i]);
|
|
59
|
+
col += width + 3;
|
|
60
|
+
return header ? theme.fg("accent", cell) : cell;
|
|
61
|
+
});
|
|
62
|
+
return `${bar} ${cells.join(` ${bar} `)} ${bar}`;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function patchTableRenderer(): void {
|
|
67
|
+
const prototype = Markdown.prototype;
|
|
68
|
+
// Pi's own renderTable, kept on the prototype: /reload loads a new module that must
|
|
69
|
+
// replace the previous wrapper, not wrap it or skip patching.
|
|
70
|
+
const native: unknown = Reflect.get(prototype, NATIVE) ?? Reflect.get(prototype, "renderTable");
|
|
71
|
+
// A Pi release without this private method keeps its own table rendering.
|
|
72
|
+
if (typeof native !== "function") return;
|
|
73
|
+
const renderStyledTable: RenderTable = function renderStyledTable(token, ...rest) {
|
|
74
|
+
const lines: string[] = native.call(this, token, ...rest);
|
|
75
|
+
return activeTheme ? styleTable(lines, token.align ?? [], activeTheme) : lines;
|
|
76
|
+
};
|
|
77
|
+
Reflect.set(prototype, NATIVE, native);
|
|
78
|
+
Reflect.set(prototype, "renderTable", renderStyledTable);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export default function tablesExtension(pi: ExtensionAPI): void {
|
|
82
|
+
patchTableRenderer();
|
|
83
|
+
pi.on("session_start", (_event, ctx) => {
|
|
84
|
+
activeTheme = themeAfterSessionStart(activeTheme, ctx.mode, ctx.ui.theme);
|
|
85
|
+
});
|
|
86
|
+
}
|