pi-syntax-highlight 0.1.0 → 0.2.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/CHANGELOG.md +81 -40
- package/DEVELOPER.md +100 -0
- package/LICENSE +21 -0
- package/README.md +42 -44
- package/extensions/index.ts +5 -10
- package/package.json +20 -5
- package/src/detect-language.ts +205 -8
- package/src/emphasis.ts +82 -1
- package/src/highlight.ts +25 -9
- package/src/lru-cache.ts +30 -0
- package/src/markdown-fence-lang.ts +20 -87
- package/src/render-bash.ts +29 -2
- package/src/render-edit.ts +0 -6
- package/src/render-generic.ts +10 -21
- package/src/render-read.ts +8 -149
- package/src/render-utils.ts +8 -17
- package/src/resolve-renderers.ts +181 -240
- package/src/tokens.ts +1 -34
- package/src/active-theme.ts +0 -26
- package/src/markdown-style.ts +0 -134
- package/src/markdown-theme.ts +0 -53
- package/src/render-path.ts +0 -54
- package/src/render-write.ts +0 -81
- package/test/unit/active-theme.test.ts +0 -38
- package/test/unit/bash-preview.test.ts +0 -57
- package/test/unit/detect-language.test.ts +0 -124
- package/test/unit/emphasis.test.ts +0 -113
- package/test/unit/extensions-index.test.ts +0 -116
- package/test/unit/grep-parse.test.ts +0 -125
- package/test/unit/highlight.test.ts +0 -107
- package/test/unit/markdown-fence-lang.test.ts +0 -131
- package/test/unit/markdown-pipeline.test.ts +0 -153
- package/test/unit/markdown-render-parity.test.ts +0 -127
- package/test/unit/markdown-style.test.ts +0 -152
- package/test/unit/markdown-theme.test.ts +0 -86
- package/test/unit/render-bash.test.ts +0 -194
- package/test/unit/render-edit.test.ts +0 -214
- package/test/unit/render-find.test.ts +0 -139
- package/test/unit/render-generic.test.ts +0 -152
- package/test/unit/render-grep.test.ts +0 -338
- package/test/unit/render-ls.test.ts +0 -132
- package/test/unit/render-read.test.ts +0 -375
- package/test/unit/render-utils.test.ts +0 -324
- package/test/unit/render-write.test.ts +0 -143
- package/test/unit/resolve-renderers.test.ts +0 -474
- package/test/unit/style-path.test.ts +0 -74
- package/tsconfig.json +0 -15
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { getMarkdownTheme, initTheme } from "@earendil-works/pi-coding-agent";
|
|
4
|
-
import { Markdown, stripTerminalSequences, visibleWidth } from "@earendil-works/pi-tui";
|
|
5
|
-
import { createMarkdownTransformer } from "../../src/markdown-fence-lang.ts";
|
|
6
|
-
import { markdownPalette } from "../../src/markdown-theme.ts";
|
|
7
|
-
import { ITALIC_OFF, ITALIC_ON, type AnsiTheme } from "../../src/tokens.ts";
|
|
8
|
-
|
|
9
|
-
// The theme the host renders with lives on `globalThis` under this symbol and is the
|
|
10
|
-
// same object an extension receives as `ctx.ui.theme`; reading it is the only way to
|
|
11
|
-
// hand the transformer the colors the renderer below actually uses.
|
|
12
|
-
const HOST_THEME_KEY = Symbol.for("@earendil-works/pi-coding-agent:theme");
|
|
13
|
-
|
|
14
|
-
initTheme("system");
|
|
15
|
-
const hostTheme = (globalThis as unknown as Record<symbol, AnsiTheme>)[HOST_THEME_KEY];
|
|
16
|
-
|
|
17
|
-
const WIDTH = 100;
|
|
18
|
-
|
|
19
|
-
// One transformer for the whole file, like the extension registers: the memo is the
|
|
20
|
-
// only state it has and every render below goes through it.
|
|
21
|
-
const transformer = createMarkdownTransformer({ resolveTheme: () => hostTheme });
|
|
22
|
-
|
|
23
|
-
function transform(markdown: string): string {
|
|
24
|
-
return transformer(markdown, { messageType: "assistant", isStreaming: false, availableWidth: WIDTH });
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// The markdown component the assistant message builds: no padding, no default text
|
|
28
|
-
// style, and the transformer only on the styled side.
|
|
29
|
-
function renderLines(markdown: string, styled: boolean): string[] {
|
|
30
|
-
const options = styled ? { transform } : undefined;
|
|
31
|
-
return new Markdown(markdown, 0, 0, getMarkdownTheme(), undefined, options).render(WIDTH);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function withoutBackticks(line: string): string {
|
|
35
|
-
return line.replace(/`/g, "");
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function trimEnd(line: string): string {
|
|
39
|
-
return line.trimEnd();
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function occurrences(text: string, part: string): number {
|
|
43
|
-
return text.split(part).length - 1;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// The corpus covers every construct the pass touches plus the ones it must leave
|
|
47
|
-
// alone. Every fence is labeled on purpose: an unlabeled one is relabeled by the
|
|
48
|
-
// fence stage, which is a visible change that stage owns and this contract does not.
|
|
49
|
-
const CORPUS: ReadonlyArray<{ name: string; markdown: string }> = [
|
|
50
|
-
{
|
|
51
|
-
name: "headings",
|
|
52
|
-
markdown: "# One\n\n## Two\n\n### Three\n\n#### Four\n\n##### Five\n\n###### Six\n",
|
|
53
|
-
},
|
|
54
|
-
{ name: "quotes", markdown: "> quoted text\n>\n> > nested quote\n> second line\n" },
|
|
55
|
-
{ name: "lists", markdown: "- one\n- two with `code`\n - nested\n\n1. first\n2. second\n" },
|
|
56
|
-
{ name: "table", markdown: "| key | value |\n| --- | --- |\n| a | b |\n" },
|
|
57
|
-
{ name: "inline styles", markdown: "Some **bold**, *italic*, ~~struck~~ and `code` in one line.\n" },
|
|
58
|
-
{ name: "links", markdown: "See [the docs](https://example.com) and the [other `one`](https://example.org).\n" },
|
|
59
|
-
{ name: "js fence", markdown: "```js\nconst x = 1;\n```\n" },
|
|
60
|
-
{ name: "mermaid fence", markdown: "```mermaid\ngraph TD;\n A-->B;\n```\n" },
|
|
61
|
-
{ name: "math fence", markdown: "```math\nx^2 + y^2\n```\n" },
|
|
62
|
-
{ name: "inline math", markdown: "The value $x^2$ stays inline.\n" },
|
|
63
|
-
{
|
|
64
|
-
name: "code spans in every context",
|
|
65
|
-
markdown: "# Heading with `code`\n\n> quote with `code`\n\n- item with `code`\n\nText with `code` and `--flag`.\n",
|
|
66
|
-
},
|
|
67
|
-
];
|
|
68
|
-
|
|
69
|
-
test("the rendered transcript keeps every visible character of the untransformed one", () => {
|
|
70
|
-
for (const item of CORPUS) {
|
|
71
|
-
const plain = renderLines(item.markdown, false);
|
|
72
|
-
const transformed = renderLines(item.markdown, true);
|
|
73
|
-
assert.equal(transformed.length, plain.length, item.name);
|
|
74
|
-
const stripped = transformed.map(stripTerminalSequences);
|
|
75
|
-
const expected = plain.map(stripTerminalSequences);
|
|
76
|
-
// The restored delimiters of the inline code spans are the only visible
|
|
77
|
-
// difference the pass is allowed to make. Right padding is layout, not text:
|
|
78
|
-
// a line carrying two extra visible characters keeps the same total width and
|
|
79
|
-
// therefore two spaces less, so the comparison is over trimmed lines.
|
|
80
|
-
assert.deepEqual(stripped.map(withoutBackticks).map(trimEnd), expected.map(withoutBackticks).map(trimEnd), item.name);
|
|
81
|
-
stripped.forEach((line, index) => {
|
|
82
|
-
const plainLine = expected[index] ?? "";
|
|
83
|
-
// Sequences take no column: the width the renderer measures for the styled
|
|
84
|
-
// line is the width of its text, which is the untouched line plus the ticks.
|
|
85
|
-
assert.equal(visibleWidth(transformed[index] ?? ""), visibleWidth(line), item.name);
|
|
86
|
-
const added = occurrences(line, "`") - occurrences(plainLine, "`");
|
|
87
|
-
// Padding equalizes both widths, so the column difference the delimiters add
|
|
88
|
-
// is only observable on the trimmed lines.
|
|
89
|
-
assert.equal(visibleWidth(line.trimEnd()), visibleWidth(plainLine.trimEnd()) + added, item.name);
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
test("every code span of the corpus comes back with its delimiters", () => {
|
|
95
|
-
const stripped = renderLines(CORPUS[CORPUS.length - 1]?.markdown ?? "", true)
|
|
96
|
-
.map(stripTerminalSequences)
|
|
97
|
-
.join("\n");
|
|
98
|
-
assert.ok(stripped.includes("`code`"));
|
|
99
|
-
assert.ok(stripped.includes("`--flag`"));
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
test("the transformer injects the palette the live theme yields", () => {
|
|
103
|
-
const palette = markdownPalette(hostTheme);
|
|
104
|
-
const transformed = transform("# Title\n\n> quoted\n");
|
|
105
|
-
// The attributes travel whatever the theme resolves the colors to, so these hold
|
|
106
|
-
// even in a monochrome terminal, where the token maps to the default color.
|
|
107
|
-
assert.ok(transformed.includes(`${ITALIC_ON}Title`));
|
|
108
|
-
assert.ok(transformed.includes(ITALIC_OFF));
|
|
109
|
-
assert.ok(!transformed.includes("\u001b[0m"));
|
|
110
|
-
if (palette.accent !== "") assert.ok(transformed.includes(palette.accent));
|
|
111
|
-
if (palette.quoteOpen !== "") assert.ok(transformed.includes(palette.quoteOpen));
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
test("a table cell with a code span keeps its text and only moves its columns", () => {
|
|
115
|
-
const markdown = "| key | value |\n| --- | --- |\n| a | `b` |\n";
|
|
116
|
-
const plain = renderLines(markdown, false).map(stripTerminalSequences);
|
|
117
|
-
const styled = renderLines(markdown, true).map(stripTerminalSequences);
|
|
118
|
-
// The restored ticks make one cell two columns wider and the renderer measures the
|
|
119
|
-
// cell to lay the table out, so the padding moves. The content does not: taking the
|
|
120
|
-
// padding and the ticks out of both renders leaves the same characters, and this is
|
|
121
|
-
// the only construct where the two renders differ by more than the delimiters.
|
|
122
|
-
assert.equal(
|
|
123
|
-
styled.join("\n").replace(/[` ]/g, ""),
|
|
124
|
-
plain.join("\n").replace(/[` ]/g, ""),
|
|
125
|
-
);
|
|
126
|
-
assert.ok(styled.join("\n").includes("`b`"));
|
|
127
|
-
});
|
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { styleMarkdown } from "../../src/markdown-style.ts";
|
|
4
|
-
import type { MarkdownPalette } from "../../src/markdown-theme.ts";
|
|
5
|
-
import {
|
|
6
|
-
DIM_OFF,
|
|
7
|
-
DIM_ON,
|
|
8
|
-
FG_DEFAULT,
|
|
9
|
-
ITALIC_OFF,
|
|
10
|
-
ITALIC_ON,
|
|
11
|
-
UNDERLINE_OFF,
|
|
12
|
-
UNDERLINE_ON,
|
|
13
|
-
} from "../../src/tokens.ts";
|
|
14
|
-
|
|
15
|
-
const ACCENT = "\x1b[38;5;5m";
|
|
16
|
-
const QUOTE_OPEN = DIM_OFF + ACCENT;
|
|
17
|
-
const QUOTE_CLOSE = FG_DEFAULT + DIM_ON;
|
|
18
|
-
|
|
19
|
-
// The pass receives the palette as a value, so every case reads as arithmetic on
|
|
20
|
-
// the sequences instead of depending on a theme.
|
|
21
|
-
const palette: MarkdownPalette = {
|
|
22
|
-
accent: ACCENT,
|
|
23
|
-
quoteOpen: QUOTE_OPEN,
|
|
24
|
-
quoteClose: QUOTE_CLOSE,
|
|
25
|
-
signature: [ACCENT, QUOTE_OPEN, QUOTE_CLOSE].join("\u0000"),
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
// A monochrome theme: no color, so only the attributes are left to inject.
|
|
29
|
-
const COLORLESS: MarkdownPalette = { accent: "", quoteOpen: "", quoteClose: "", signature: "" };
|
|
30
|
-
|
|
31
|
-
function style(markdown: string, selected: MarkdownPalette = palette): string {
|
|
32
|
-
return styleMarkdown(markdown, selected);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
test("styleMarkdown: an inline code span gets its delimiters back as escaped text", () => {
|
|
36
|
-
assert.equal(style("Run `npm test` first"), "Run \\`npm test\\` first");
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test("styleMarkdown: every code span of a line is restored", () => {
|
|
40
|
-
assert.equal(style("`a` and `b`"), "\\`a\\` and \\`b\\`");
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
test("styleMarkdown: a code span is restored inside a list item, a link and a table cell", () => {
|
|
44
|
-
assert.equal(style("- use `tsc` here"), "- use \\`tsc\\` here");
|
|
45
|
-
assert.equal(style("[the `--watch` flag](https://example.com)"), "[the \\`--watch\\` flag](https://example.com)");
|
|
46
|
-
assert.equal(style("| `a` | b |"), "| \\`a\\` | b |");
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
test("styleMarkdown: a code span inside a quote keeps the quote pair around it", () => {
|
|
50
|
-
assert.equal(style("> run `x` now"), `> ${QUOTE_OPEN}run \\\`x\\\` now${QUOTE_CLOSE}`);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
test("styleMarkdown: a code span inside a heading keeps the heading style around it", () => {
|
|
54
|
-
assert.equal(
|
|
55
|
-
style("## Run `x`"),
|
|
56
|
-
`## ${ACCENT}${UNDERLINE_ON}Run \\\`x\\\`${UNDERLINE_OFF}${FG_DEFAULT}`,
|
|
57
|
-
);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
test("styleMarkdown: an already escaped backtick leaves the line alone", () => {
|
|
61
|
-
// The model wrote the delimiters itself: escaping them again would print the
|
|
62
|
-
// backslashes and lose the span.
|
|
63
|
-
assert.equal(style("Run \\`npm test\\` first"), "Run \\`npm test\\` first");
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
test("styleMarkdown: a span whose text ends in a backslash leaves the line alone", () => {
|
|
67
|
-
assert.equal(style("Run `a\\` now"), "Run `a\\` now");
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
test("styleMarkdown: a lone backtick leaves the line alone until its pair arrives", () => {
|
|
71
|
-
assert.equal(style("Run `npm test first"), "Run `npm test first");
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
test("styleMarkdown: a multi-backtick span is not touched", () => {
|
|
75
|
-
assert.equal(style("Use ``a`b`` here"), "Use ``a`b`` here");
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
test("styleMarkdown: a code span inside a fence body is not touched", () => {
|
|
79
|
-
const markdown = "```md\nRun `npm test` first\n```\n";
|
|
80
|
-
assert.equal(style(markdown), markdown);
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
test("styleMarkdown: every heading level takes the accent and the level decides the attribute", () => {
|
|
84
|
-
assert.equal(style("# Title"), `# ${ACCENT}${ITALIC_ON}Title${ITALIC_OFF}${FG_DEFAULT}`);
|
|
85
|
-
assert.equal(style("## Title"), `## ${ACCENT}${UNDERLINE_ON}Title${UNDERLINE_OFF}${FG_DEFAULT}`);
|
|
86
|
-
assert.equal(style("### Title"), `### ${ACCENT}${ITALIC_ON}Title${ITALIC_OFF}${FG_DEFAULT}`);
|
|
87
|
-
// h4-h6 add no attribute: the host keeps them bold and the renderer prints their
|
|
88
|
-
// `#` prefix, which is what tells them apart.
|
|
89
|
-
assert.equal(style("#### Title"), `#### ${ACCENT}Title${FG_DEFAULT}`);
|
|
90
|
-
assert.equal(style("##### Title"), `##### ${ACCENT}Title${FG_DEFAULT}`);
|
|
91
|
-
assert.equal(style("###### Title"), `###### ${ACCENT}Title${FG_DEFAULT}`);
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
test("styleMarkdown: a heading takes the accent without re-injecting it after an inline span", () => {
|
|
95
|
-
const styled = style("# **bold** tail");
|
|
96
|
-
assert.equal(styled, `# ${ACCENT}${ITALIC_ON}**bold** tail${ITALIC_OFF}${FG_DEFAULT}`);
|
|
97
|
-
// Accepted behaviour of the injected route: the host re-opens its own heading
|
|
98
|
-
// color after the bold run, so the accent appears once, never after the inline.
|
|
99
|
-
assert.equal(styled.split(ACCENT).length - 1, 1);
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
test("styleMarkdown: an empty heading is not touched", () => {
|
|
103
|
-
assert.equal(style("## "), "## ");
|
|
104
|
-
assert.equal(style("##"), "##");
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
test("styleMarkdown: a heading inside a fence body is not touched", () => {
|
|
108
|
-
const markdown = "```md\n# Title\n```\n";
|
|
109
|
-
assert.equal(style(markdown), markdown);
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
test("styleMarkdown: setext headings are not touched", () => {
|
|
113
|
-
const markdown = "Title\n=====\n";
|
|
114
|
-
assert.equal(style(markdown), markdown);
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
test("styleMarkdown: up to three spaces of indentation still open a heading", () => {
|
|
118
|
-
assert.equal(style(" # Title"), ` # ${ACCENT}${ITALIC_ON}Title${ITALIC_OFF}${FG_DEFAULT}`);
|
|
119
|
-
// Four spaces are an indented code block, so the line is body text.
|
|
120
|
-
assert.equal(style(" # Title"), " # Title");
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
test("styleMarkdown: a quote line wraps its text in the quote pair after every marker", () => {
|
|
124
|
-
assert.equal(style("> quoted"), `> ${QUOTE_OPEN}quoted${QUOTE_CLOSE}`);
|
|
125
|
-
assert.equal(style("> > nested"), `> > ${QUOTE_OPEN}nested${QUOTE_CLOSE}`);
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
test("styleMarkdown: each line of a multi-line quote gets its own pair", () => {
|
|
129
|
-
assert.equal(style("> one\n> two"), `> ${QUOTE_OPEN}one${QUOTE_CLOSE}\n> ${QUOTE_OPEN}two${QUOTE_CLOSE}`);
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
test("styleMarkdown: a quote line with no text is not touched", () => {
|
|
133
|
-
assert.equal(style(">"), ">");
|
|
134
|
-
assert.equal(style("> "), "> ");
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
test("styleMarkdown: a monochrome theme keeps the heading attributes and drops the colors", () => {
|
|
138
|
-
assert.equal(style("# Title", COLORLESS), `# ${ITALIC_ON}Title${ITALIC_OFF}`);
|
|
139
|
-
assert.equal(style("## Title", COLORLESS), `## ${UNDERLINE_ON}Title${UNDERLINE_OFF}`);
|
|
140
|
-
// Nothing left to inject at all, so the line is returned exactly as it came.
|
|
141
|
-
assert.equal(style("#### Title", COLORLESS), "#### Title");
|
|
142
|
-
assert.equal(style("> quoted", COLORLESS), "> quoted");
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
test("styleMarkdown: a second pass over the same text changes nothing", () => {
|
|
146
|
-
const markdown = "# Title\n\n> quote with `code`\n\n- item `x`\n\n```js\nconst x = 1;\n```\n";
|
|
147
|
-
const once = style(markdown);
|
|
148
|
-
assert.notEqual(once, markdown);
|
|
149
|
-
// The guard for lines that already carry a sequence is what makes this hold: a
|
|
150
|
-
// restyle would nest the previous pair inside the new one.
|
|
151
|
-
assert.equal(style(once), once);
|
|
152
|
-
});
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { markdownPalette } from "../../src/markdown-theme.ts";
|
|
4
|
-
import { DIM_OFF, DIM_ON, FG_DEFAULT, type AnsiTheme } from "../../src/tokens.ts";
|
|
5
|
-
|
|
6
|
-
const ACCENT = "\x1b[38;5;5m";
|
|
7
|
-
const QUOTE_COLOR = "\x1b[38;5;8m";
|
|
8
|
-
const OTHER_QUOTE_COLOR = "\x1b[38;5;1m";
|
|
9
|
-
|
|
10
|
-
// A theme whose sequences live in a mutable map, like the host's: a theme switch
|
|
11
|
-
// changes the values under the same object, so a fake that returned fixed literals
|
|
12
|
-
// could not show the signature moving.
|
|
13
|
-
function fakeTheme(overrides: Record<string, string> = {}): { theme: AnsiTheme; ansi: Record<string, string> } {
|
|
14
|
-
const ansi: Record<string, string> = { accent: ACCENT, mdQuote: FG_DEFAULT + DIM_ON, ...overrides };
|
|
15
|
-
return {
|
|
16
|
-
ansi,
|
|
17
|
-
theme: {
|
|
18
|
-
getFgAnsi: (color) => ansi[color] ?? "",
|
|
19
|
-
getBgAnsi: () => "",
|
|
20
|
-
},
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
test("markdownPalette: the heading accent is the theme's foreground for the accent token", () => {
|
|
25
|
-
assert.equal(markdownPalette(fakeTheme().theme).accent, ACCENT);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
test("markdownPalette: a faint quote turns dim off while it opens and restores it when it closes", () => {
|
|
29
|
-
const palette = markdownPalette(fakeTheme().theme);
|
|
30
|
-
assert.equal(palette.quoteOpen, DIM_OFF + ACCENT);
|
|
31
|
-
// `\x1b[22m` closes bold along with dim, which is why the close puts the theme's
|
|
32
|
-
// own faint back instead of leaving the line brighter than the host painted it.
|
|
33
|
-
assert.equal(palette.quoteClose, FG_DEFAULT + DIM_ON);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
test("markdownPalette: a quote that is not faint opens with the color alone", () => {
|
|
37
|
-
const palette = markdownPalette(fakeTheme({ mdQuote: QUOTE_COLOR }).theme);
|
|
38
|
-
assert.equal(palette.quoteOpen, ACCENT);
|
|
39
|
-
assert.equal(palette.quoteClose, FG_DEFAULT);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
test("markdownPalette: a token that resolves to the terminal default adds no color", () => {
|
|
43
|
-
// The host maps a token with no color of its own to 39, and wrapping text in that
|
|
44
|
-
// sequence only cancels the style the surrounding line already has.
|
|
45
|
-
assert.equal(markdownPalette(fakeTheme({ accent: "" }).theme).accent, "");
|
|
46
|
-
assert.equal(markdownPalette(fakeTheme({ accent: FG_DEFAULT }).theme).accent, "");
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
test("markdownPalette: a colorless quote injects no pair at all", () => {
|
|
50
|
-
const palette = markdownPalette(fakeTheme({ accent: FG_DEFAULT }).theme);
|
|
51
|
-
assert.equal(palette.quoteOpen, "");
|
|
52
|
-
assert.equal(palette.quoteClose, "");
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
test("markdownPalette: the signature is stable while the sequences are", () => {
|
|
56
|
-
const { theme } = fakeTheme();
|
|
57
|
-
const first = markdownPalette(theme);
|
|
58
|
-
assert.equal(markdownPalette(theme).signature, first.signature);
|
|
59
|
-
// The signature is the concatenation of what the pass injects, so two palettes
|
|
60
|
-
// with the same one cannot produce different bytes.
|
|
61
|
-
assert.equal(first.signature, [first.accent, first.quoteOpen, first.quoteClose].join("\u0000"));
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
test("markdownPalette: the signature follows the accent when the theme changes", () => {
|
|
65
|
-
const { theme, ansi } = fakeTheme();
|
|
66
|
-
const before = markdownPalette(theme).signature;
|
|
67
|
-
ansi.accent = "\x1b[38;5;6m";
|
|
68
|
-
assert.notEqual(markdownPalette(theme).signature, before);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
test("markdownPalette: the signature follows the faint flag of the quote", () => {
|
|
72
|
-
const { theme, ansi } = fakeTheme();
|
|
73
|
-
const before = markdownPalette(theme).signature;
|
|
74
|
-
ansi.mdQuote = QUOTE_COLOR;
|
|
75
|
-
assert.notEqual(markdownPalette(theme).signature, before);
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
test("markdownPalette: a change the pass cannot see leaves the signature alone", () => {
|
|
79
|
-
const { theme, ansi } = fakeTheme({ mdQuote: QUOTE_COLOR });
|
|
80
|
-
const before = markdownPalette(theme).signature;
|
|
81
|
-
// Another color, same faint flag: the injected sequences are byte identical, so
|
|
82
|
-
// invalidating a memoized render over it would be wasted work.
|
|
83
|
-
ansi.mdQuote = OTHER_QUOTE_COLOR;
|
|
84
|
-
ansi.warning = "\x1b[38;5;3m";
|
|
85
|
-
assert.equal(markdownPalette(theme).signature, before);
|
|
86
|
-
});
|
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { DEFAULT_MAX_BYTES, formatSize } from "@earendil-works/pi-coding-agent";
|
|
4
|
-
import { buildBashBody, formatDuration, type BashInput } from "../../src/render-bash.ts";
|
|
5
|
-
import type { HighlightFn } from "../../src/highlight.ts";
|
|
6
|
-
import { TOKEN } from "../../src/tokens.ts";
|
|
7
|
-
|
|
8
|
-
const fakeTheme = {
|
|
9
|
-
fg: (c: string, t: string) => `<${c}>${t}</${c}>`,
|
|
10
|
-
bg: (c: string, t: string) => `<bg:${c}>${t}</bg:${c}>`,
|
|
11
|
-
bold: (t: string) => `<b>${t}</b>`,
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
interface HighlightCall {
|
|
15
|
-
code: string;
|
|
16
|
-
lang: string;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function makeHighlight(calls: HighlightCall[]): HighlightFn {
|
|
20
|
-
return (code, lang) => {
|
|
21
|
-
calls.push({ code, lang });
|
|
22
|
-
return code.split("\n").map((line) => `<${lang}>${line}</${lang}>`);
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function styled(color: string, text: string): string {
|
|
27
|
-
return `<${color}>${text}</${color}>`;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function styledDiffChange(background: string, foreground: string, line: string): string {
|
|
31
|
-
return `<bg:${background}>${styled(foreground, line)}</bg:${background}>`;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function bashInput(overrides: Partial<BashInput> = {}): BashInput {
|
|
35
|
-
return {
|
|
36
|
-
output: "",
|
|
37
|
-
details: undefined,
|
|
38
|
-
durationMs: undefined,
|
|
39
|
-
theme: fakeTheme,
|
|
40
|
-
...overrides,
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
test("buildBashBody: a diff is painted per line, headers in the function color and changes over their own background", () => {
|
|
45
|
-
const output = [
|
|
46
|
-
"diff --git a/x.ts b/x.ts",
|
|
47
|
-
"--- a/x.ts",
|
|
48
|
-
"+++ b/x.ts",
|
|
49
|
-
"@@ -1,2 +1,3 @@",
|
|
50
|
-
" const a = 1;",
|
|
51
|
-
"+const b = 2;",
|
|
52
|
-
"-const c = 3;",
|
|
53
|
-
].join("\n");
|
|
54
|
-
const { body, warnings, timer } = buildBashBody(bashInput({ output }));
|
|
55
|
-
assert.equal(
|
|
56
|
-
body,
|
|
57
|
-
[
|
|
58
|
-
styled("text", "diff --git a/x.ts b/x.ts"),
|
|
59
|
-
styled("syntaxFunction", "--- a/x.ts"),
|
|
60
|
-
styled("syntaxFunction", "+++ b/x.ts"),
|
|
61
|
-
styled("syntaxFunction", "@@ -1,2 +1,3 @@"),
|
|
62
|
-
styled("text", " const a = 1;"),
|
|
63
|
-
styledDiffChange("toolSuccessBg", "toolDiffAdded", "+const b = 2;"),
|
|
64
|
-
styledDiffChange("toolErrorBg", "toolDiffRemoved", "-const c = 3;"),
|
|
65
|
-
].join("\n"),
|
|
66
|
-
);
|
|
67
|
-
assert.equal(warnings, undefined);
|
|
68
|
-
assert.equal(timer, undefined);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
test("buildBashBody: JSON output is highlighted as one block", () => {
|
|
72
|
-
const calls: HighlightCall[] = [];
|
|
73
|
-
const json = '{"a":1,"b":[2,3]}';
|
|
74
|
-
const { body } = buildBashBody(bashInput({ output: json, highlight: makeHighlight(calls) }));
|
|
75
|
-
assert.deepEqual(calls, [{ code: json, lang: "json" }]);
|
|
76
|
-
assert.equal(body, "<json>{\"a\":1,\"b\":[2,3]}</json>");
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
test("buildBashBody: output that starts like JSON but does not parse is painted per line", () => {
|
|
80
|
-
const { body } = buildBashBody(bashInput({ output: '{"a": }', highlight: makeHighlight([]) }));
|
|
81
|
-
assert.equal(body, styled("text", '{"a": }'));
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
test("buildBashBody: error and warning lines get their own colors", () => {
|
|
85
|
-
assert.equal(TOKEN.broken, "error");
|
|
86
|
-
assert.equal(TOKEN.notice, "warning");
|
|
87
|
-
const { body } = buildBashBody(bashInput({ output: "error: boom\nwarning: careful\nplain text" }));
|
|
88
|
-
assert.equal(
|
|
89
|
-
body,
|
|
90
|
-
[
|
|
91
|
-
styled("error", "error: boom"),
|
|
92
|
-
styled("warning", "warning: careful"),
|
|
93
|
-
styled("text", "plain text"),
|
|
94
|
-
].join("\n"),
|
|
95
|
-
);
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
test("buildBashBody: a path, line and text line is split into its three colors", () => {
|
|
99
|
-
const { body } = buildBashBody(bashInput({ output: "src/app.ts:12: some text" }));
|
|
100
|
-
assert.equal(
|
|
101
|
-
body,
|
|
102
|
-
[
|
|
103
|
-
styled("accent", "src/app.ts"),
|
|
104
|
-
styled("dim", ":"),
|
|
105
|
-
styled("syntaxNumber", "12"),
|
|
106
|
-
styled("dim", ":"),
|
|
107
|
-
" ",
|
|
108
|
-
styled("text", "some text"),
|
|
109
|
-
].join(""),
|
|
110
|
-
);
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
test("buildBashBody: line truncation is reported with the shown and total line counts", () => {
|
|
114
|
-
const { body, warnings } = buildBashBody(
|
|
115
|
-
bashInput({
|
|
116
|
-
output: "line1\nline2",
|
|
117
|
-
details: { truncation: { truncated: true, truncatedBy: "lines", outputLines: 10, totalLines: 500 } },
|
|
118
|
-
}),
|
|
119
|
-
);
|
|
120
|
-
assert.equal(warnings, "Truncated: showing 10 of 500 lines");
|
|
121
|
-
assert.equal(body, [styled("text", "line1"), styled("text", "line2")].join("\n"));
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
test("buildBashBody: byte truncation reports the limit through formatSize", () => {
|
|
125
|
-
const recorded = buildBashBody(
|
|
126
|
-
bashInput({
|
|
127
|
-
output: "x",
|
|
128
|
-
details: { truncation: { truncated: true, truncatedBy: "bytes", outputLines: 7, maxBytes: 1024 } },
|
|
129
|
-
}),
|
|
130
|
-
);
|
|
131
|
-
assert.equal(recorded.warnings, `Truncated: 7 lines shown (${formatSize(1024)} limit)`);
|
|
132
|
-
|
|
133
|
-
const implicit = buildBashBody(
|
|
134
|
-
bashInput({ output: "x", details: { truncation: { truncated: true, truncatedBy: "bytes", outputLines: 7 } } }),
|
|
135
|
-
);
|
|
136
|
-
assert.equal(implicit.warnings, `Truncated: 7 lines shown (${formatSize(DEFAULT_MAX_BYTES)} limit)`);
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
test("buildBashBody: the full output block moves from the body to the warnings", () => {
|
|
140
|
-
const output = "line1\nline2\n\n[Full output: /tmp/pi-bash-1.log]";
|
|
141
|
-
const { body, warnings } = buildBashBody(
|
|
142
|
-
bashInput({
|
|
143
|
-
output,
|
|
144
|
-
details: {
|
|
145
|
-
truncation: { truncated: true, truncatedBy: "lines", outputLines: 2, totalLines: 900 },
|
|
146
|
-
fullOutputPath: "/tmp/pi-bash-1.log",
|
|
147
|
-
},
|
|
148
|
-
}),
|
|
149
|
-
);
|
|
150
|
-
assert.equal(body, [styled("text", "line1"), styled("text", "line2")].join("\n"));
|
|
151
|
-
assert.equal(warnings, "Full output: /tmp/pi-bash-1.log. Truncated: showing 2 of 900 lines");
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
test("buildBashBody: a full output path without truncation is warned and stays in the body", () => {
|
|
155
|
-
const output = "line1\n\n[Full output: /tmp/x.log]";
|
|
156
|
-
const { body, warnings } = buildBashBody(bashInput({ output, details: { fullOutputPath: "/tmp/x.log" } }));
|
|
157
|
-
assert.equal(warnings, "Full output: /tmp/x.log");
|
|
158
|
-
assert.equal(
|
|
159
|
-
body,
|
|
160
|
-
[
|
|
161
|
-
styled("text", "line1"),
|
|
162
|
-
styled("text", ""),
|
|
163
|
-
styled("text", "[Full output: /tmp/x.log]"),
|
|
164
|
-
].join("\n"),
|
|
165
|
-
);
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
test("buildBashBody: an untruncated result carries no warning", () => {
|
|
169
|
-
const { warnings } = buildBashBody(bashInput({ output: "line1" }));
|
|
170
|
-
assert.equal(warnings, undefined);
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
test("buildBashBody: durationMs becomes the timer text without the prefix", () => {
|
|
174
|
-
assert.equal(buildBashBody(bashInput({ output: "x", durationMs: 4200 })).timer, "4.2s");
|
|
175
|
-
assert.equal(buildBashBody(bashInput({ output: "x" })).timer, undefined);
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
test("buildBashBody: empty and blank outputs have no body, warnings or timer", () => {
|
|
179
|
-
for (const output of ["", " \n "]) {
|
|
180
|
-
assert.deepEqual(buildBashBody(bashInput({ output })), {
|
|
181
|
-
body: "",
|
|
182
|
-
warnings: undefined,
|
|
183
|
-
timer: undefined,
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
test("formatDuration: identical to the built-in formatter", () => {
|
|
189
|
-
assert.equal(formatDuration(4200), "4.2s");
|
|
190
|
-
assert.equal(formatDuration(65000), "1m 5s");
|
|
191
|
-
assert.equal(formatDuration(7324000), "2h 2m 4s");
|
|
192
|
-
// toFixed rounds 59.999 up to 60, which is what stock pi prints.
|
|
193
|
-
assert.equal(formatDuration(59999), "60.0s");
|
|
194
|
-
});
|