@xynogen/pix-display 0.1.12 → 0.1.13
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 +1 -1
- package/src/code-blocks.ts +8 -26
package/package.json
CHANGED
package/src/code-blocks.ts
CHANGED
|
@@ -9,10 +9,11 @@ const OPENING_FENCE_RE = /^```([^`]*)$/;
|
|
|
9
9
|
const CLOSING_FENCE_RE = /^```\s*$/;
|
|
10
10
|
const DEFAULT_LABEL = "code";
|
|
11
11
|
const ANSI_RE = /\x1b\[[0-?]*[ -/]*[@-~]/g;
|
|
12
|
+
const BACKGROUND_ANSI_RE = /\x1b\[(?:4[0-9]|48)(?:;[^m]*)?m/g;
|
|
12
13
|
const OSC_RE = /\x1b\][^\x07]*(?:\x07|\x1b\\)/g;
|
|
13
14
|
const PATCHED = Symbol.for("@xynogen/pix-display:code-block-renderer");
|
|
14
15
|
|
|
15
|
-
type CodeFrameTheme = Pick<Theme, "
|
|
16
|
+
type CodeFrameTheme = Pick<Theme, "bold" | "fg">;
|
|
16
17
|
type RenderMode = "tui" | "rpc" | "json" | "print";
|
|
17
18
|
|
|
18
19
|
type PatchablePrototype = {
|
|
@@ -52,6 +53,7 @@ function stripLayoutWhitespace(line: string, count: number): string {
|
|
|
52
53
|
let remaining = count;
|
|
53
54
|
return line
|
|
54
55
|
.replace(OSC_RE, "")
|
|
56
|
+
.replace(BACKGROUND_ANSI_RE, "")
|
|
55
57
|
.replace(/^(?:\x1b\[[0-?]*[ -/]*[@-~]| )+/, (prefix) =>
|
|
56
58
|
prefix.replace(/ /g, (space) => {
|
|
57
59
|
if (remaining <= 0) return space;
|
|
@@ -62,18 +64,6 @@ function stripLayoutWhitespace(line: string, count: number): string {
|
|
|
62
64
|
.replace(/ +$/, "");
|
|
63
65
|
}
|
|
64
66
|
|
|
65
|
-
function paintFrame(theme: CodeFrameTheme, text: string): string {
|
|
66
|
-
try {
|
|
67
|
-
const bg = theme.getBgAnsi("toolSuccessBg");
|
|
68
|
-
const preserved = text.replace(/\x1b\[([0-9;]*)m/g, (sequence, params: string) =>
|
|
69
|
-
params === "0" || params.split(";").includes("49") ? `${sequence}${bg}` : sequence,
|
|
70
|
-
);
|
|
71
|
-
return `${bg}${preserved}\x1b[49m`;
|
|
72
|
-
} catch {
|
|
73
|
-
return theme.bg("toolSuccessBg", text);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
67
|
function fenceLabel(line: string): string | undefined {
|
|
78
68
|
const match = plainText(line).trim().match(OPENING_FENCE_RE);
|
|
79
69
|
if (!match) return undefined;
|
|
@@ -86,26 +76,18 @@ function topRule(width: number, language: string, theme: CodeFrameTheme): string
|
|
|
86
76
|
const displayLanguage = truncateToWidth(language, available, "…");
|
|
87
77
|
const label = theme.bold(theme.fg("accent", ` ${displayLanguage} `));
|
|
88
78
|
const ruleWidth = Math.max(0, width - visibleWidth(label) - 2);
|
|
89
|
-
return
|
|
90
|
-
theme,
|
|
91
|
-
`${theme.fg("borderMuted", "──")}${label}${theme.fg("borderMuted", "─".repeat(ruleWidth))}`,
|
|
92
|
-
);
|
|
79
|
+
return `${theme.fg("borderMuted", "──")}${label}${theme.fg("borderMuted", "─".repeat(ruleWidth))}`;
|
|
93
80
|
}
|
|
94
81
|
|
|
95
82
|
function bottomRule(width: number, theme: CodeFrameTheme): string {
|
|
96
|
-
return
|
|
83
|
+
return theme.fg("borderMuted", "─".repeat(width));
|
|
97
84
|
}
|
|
98
85
|
|
|
99
|
-
function bodyLine(
|
|
100
|
-
line: string,
|
|
101
|
-
width: number,
|
|
102
|
-
layoutIndent: number,
|
|
103
|
-
theme: CodeFrameTheme,
|
|
104
|
-
): string {
|
|
86
|
+
function bodyLine(line: string, width: number, layoutIndent: number): string {
|
|
105
87
|
const content = truncateToWidth(stripLayoutWhitespace(line, layoutIndent), width, "…");
|
|
106
88
|
// Keep every code row free of frame glyphs and layout padding. Selection must
|
|
107
89
|
// copy only source text, including meaningful relative indentation in Python.
|
|
108
|
-
return
|
|
90
|
+
return content;
|
|
109
91
|
}
|
|
110
92
|
|
|
111
93
|
/**
|
|
@@ -138,7 +120,7 @@ export function renderCodeFences(lines: string[], width: number, theme: CodeFram
|
|
|
138
120
|
framed.push(`${oscSequences(out[start] ?? "")}${topRule(frameWidth, language, theme)}`);
|
|
139
121
|
for (let index = start + 1; index < end; index++) {
|
|
140
122
|
framed.push(
|
|
141
|
-
`${oscSequences(out[index] ?? "")}${bodyLine(out[index] ?? "", width, bodyIndent
|
|
123
|
+
`${oscSequences(out[index] ?? "")}${bodyLine(out[index] ?? "", width, bodyIndent)}`,
|
|
142
124
|
);
|
|
143
125
|
}
|
|
144
126
|
framed.push(`${oscSequences(out[end] ?? "")}${bottomRule(frameWidth, theme)}`);
|