pi-syntax-highlight 0.0.0-stage → 0.1.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.
Files changed (49) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/README.md +68 -2
  3. package/extensions/index.ts +22 -0
  4. package/package.json +35 -4
  5. package/src/active-theme.ts +26 -0
  6. package/src/detect-language.ts +294 -0
  7. package/src/emphasis.ts +160 -0
  8. package/src/grep-parse.ts +42 -0
  9. package/src/highlight.ts +109 -0
  10. package/src/markdown-fence-lang.ts +180 -0
  11. package/src/markdown-style.ts +134 -0
  12. package/src/markdown-theme.ts +53 -0
  13. package/src/render-bash.ts +180 -0
  14. package/src/render-edit.ts +207 -0
  15. package/src/render-find.ts +64 -0
  16. package/src/render-generic.ts +86 -0
  17. package/src/render-grep.ts +142 -0
  18. package/src/render-ls.ts +66 -0
  19. package/src/render-path.ts +54 -0
  20. package/src/render-read.ts +277 -0
  21. package/src/render-utils.ts +219 -0
  22. package/src/render-write.ts +81 -0
  23. package/src/resolve-renderers.ts +406 -0
  24. package/src/style-path.ts +48 -0
  25. package/src/tokens.ts +114 -0
  26. package/test/unit/active-theme.test.ts +38 -0
  27. package/test/unit/bash-preview.test.ts +57 -0
  28. package/test/unit/detect-language.test.ts +124 -0
  29. package/test/unit/emphasis.test.ts +113 -0
  30. package/test/unit/extensions-index.test.ts +116 -0
  31. package/test/unit/grep-parse.test.ts +125 -0
  32. package/test/unit/highlight.test.ts +107 -0
  33. package/test/unit/markdown-fence-lang.test.ts +131 -0
  34. package/test/unit/markdown-pipeline.test.ts +153 -0
  35. package/test/unit/markdown-render-parity.test.ts +127 -0
  36. package/test/unit/markdown-style.test.ts +152 -0
  37. package/test/unit/markdown-theme.test.ts +86 -0
  38. package/test/unit/render-bash.test.ts +194 -0
  39. package/test/unit/render-edit.test.ts +214 -0
  40. package/test/unit/render-find.test.ts +139 -0
  41. package/test/unit/render-generic.test.ts +152 -0
  42. package/test/unit/render-grep.test.ts +338 -0
  43. package/test/unit/render-ls.test.ts +132 -0
  44. package/test/unit/render-read.test.ts +375 -0
  45. package/test/unit/render-utils.test.ts +324 -0
  46. package/test/unit/render-write.test.ts +143 -0
  47. package/test/unit/resolve-renderers.test.ts +474 -0
  48. package/test/unit/style-path.test.ts +74 -0
  49. package/tsconfig.json +15 -0
@@ -0,0 +1,152 @@
1
+ import { test } from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { buildGenericResultText, type GenericInput } from "../../src/render-generic.ts";
4
+ import type { HighlightFn } from "../../src/highlight.ts";
5
+ import { TOKEN } from "../../src/tokens.ts";
6
+
7
+ const fakeTheme = {
8
+ fg: (c: string, t: string) => `<${c}>${t}</${c}>`,
9
+ bg: (c: string, t: string) => `<bg:${c}>${t}</bg:${c}>`,
10
+ bold: (t: string) => `<b>${t}</b>`,
11
+ };
12
+
13
+ const EXPAND_HINT = "<hint>";
14
+ const FOOTER_1 = "<muted>\n... (1 more lines,</muted> <hint><muted>)</muted>";
15
+
16
+ function styled(color: string, text: string): string {
17
+ return `<${color}>${text}</${color}>`;
18
+ }
19
+
20
+ function styledDiffChange(background: string, foreground: string, line: string): string {
21
+ return `<bg:${background}>${styled(foreground, line)}</bg:${background}>`;
22
+ }
23
+
24
+ interface HighlightCall {
25
+ code: string;
26
+ lang: string;
27
+ }
28
+
29
+ function makeHighlight(calls: HighlightCall[]): HighlightFn {
30
+ return (code, lang) => {
31
+ calls.push({ code, lang });
32
+ return code.split("\n").map((line) => `<${lang}>${line}</${lang}>`);
33
+ };
34
+ }
35
+
36
+ function genericInput(overrides: Partial<GenericInput> = {}): GenericInput {
37
+ return {
38
+ output: "",
39
+ expanded: false,
40
+ isError: false,
41
+ theme: fakeTheme,
42
+ expandHint: EXPAND_HINT,
43
+ ...overrides,
44
+ };
45
+ }
46
+
47
+ const PLAIN_LINES = Array.from({ length: 11 }, (_, index) => `line ${index}`);
48
+
49
+ test("buildGenericResultText: a JSON object is highlighted as one block", () => {
50
+ const calls: HighlightCall[] = [];
51
+ const json = '{"a":1,"b":[2,3]}';
52
+ const text = buildGenericResultText(genericInput({ output: json }), makeHighlight(calls));
53
+ assert.deepEqual(calls, [{ code: json, lang: "json" }]);
54
+ assert.equal(text, ["", styled("json", json)].join("\n"));
55
+ });
56
+
57
+ test("buildGenericResultText: a multi-line JSON array keeps the surrounding context", () => {
58
+ const calls: HighlightCall[] = [];
59
+ const json = "[\n 1,\n 2\n]";
60
+ const text = buildGenericResultText(genericInput({ output: json }), makeHighlight(calls));
61
+ assert.deepEqual(calls, [{ code: json, lang: "json" }]);
62
+ assert.equal(
63
+ text,
64
+ ["", styled("json", "["), styled("json", " 1,"), styled("json", " 2"), styled("json", "]")].join("\n"),
65
+ );
66
+ });
67
+
68
+ test("buildGenericResultText: output that only looks like JSON keeps the flat content color", () => {
69
+ const calls: HighlightCall[] = [];
70
+ const output = '{"a": }';
71
+ const text = buildGenericResultText(genericInput({ output }), makeHighlight(calls));
72
+ assert.deepEqual(calls, []);
73
+ assert.equal(text, ["", styled("text", output)].join("\n"));
74
+ });
75
+
76
+ test("buildGenericResultText: a diff is painted per line, changes over their own background", () => {
77
+ const calls: HighlightCall[] = [];
78
+ const output = [
79
+ "diff --git a/x.ts b/x.ts",
80
+ "--- a/x.ts",
81
+ "+++ b/x.ts",
82
+ "@@ -1,2 +1,3 @@",
83
+ " const a = 1;",
84
+ "+const b = 2;",
85
+ "-const c = 3;",
86
+ ].join("\n");
87
+ const text = buildGenericResultText(genericInput({ output }), makeHighlight(calls));
88
+ assert.deepEqual(calls, []);
89
+ assert.equal(
90
+ text,
91
+ [
92
+ "",
93
+ styled("text", "diff --git a/x.ts b/x.ts"),
94
+ styled("syntaxFunction", "--- a/x.ts"),
95
+ styled("syntaxFunction", "+++ b/x.ts"),
96
+ styled("syntaxFunction", "@@ -1,2 +1,3 @@"),
97
+ styled("text", " const a = 1;"),
98
+ styledDiffChange("toolSuccessBg", "toolDiffAdded", "+const b = 2;"),
99
+ styledDiffChange("toolErrorBg", "toolDiffRemoved", "-const c = 3;"),
100
+ ].join("\n"),
101
+ );
102
+ });
103
+
104
+ test("buildGenericResultText: plain text is painted line by line in the content token", () => {
105
+ const calls: HighlightCall[] = [];
106
+ const text = buildGenericResultText(genericInput({ output: "alpha\nbeta" }), makeHighlight(calls));
107
+ assert.deepEqual(calls, []);
108
+ assert.equal(text, ["", styled("text", "alpha"), styled("text", "beta")].join("\n"));
109
+ });
110
+
111
+ test("buildGenericResultText: eleven lines collapse to ten plus the footer", () => {
112
+ const text = buildGenericResultText(genericInput({ output: PLAIN_LINES.join("\n") }), makeHighlight([]));
113
+ assert.equal(text, ["", ...PLAIN_LINES.slice(0, 10).map((line) => styled("text", line))].join("\n") + FOOTER_1);
114
+ });
115
+
116
+ test("buildGenericResultText: expanded shows every line and no footer", () => {
117
+ const text = buildGenericResultText(
118
+ genericInput({ output: PLAIN_LINES.join("\n"), expanded: true }),
119
+ makeHighlight([]),
120
+ );
121
+ assert.equal(text, ["", ...PLAIN_LINES.map((line) => styled("text", line))].join("\n"));
122
+ });
123
+
124
+ test("buildGenericResultText: a trailing notice is painted in the warning token and is not a line", () => {
125
+ const text = buildGenericResultText(
126
+ genericInput({ output: `${PLAIN_LINES.join("\n")}\n\n[100 matches limit]` }),
127
+ makeHighlight([]),
128
+ );
129
+ assert.equal(
130
+ text,
131
+ ["", ...PLAIN_LINES.slice(0, 10).map((line) => styled("text", line))].join("\n") +
132
+ FOOTER_1 +
133
+ "\n" +
134
+ styled("warning", "[100 matches limit]"),
135
+ );
136
+ });
137
+
138
+ test("buildGenericResultText: an error result is painted flat in the content color", () => {
139
+ assert.equal(TOKEN.content, "text");
140
+ // A JSON body that parses is the case the flat rule exists for: painted by
141
+ // kind it would look like a normal result instead of a failure.
142
+ const calls: HighlightCall[] = [];
143
+ const json = '{"error":"boom"}';
144
+ const text = buildGenericResultText(genericInput({ output: json, isError: true }), makeHighlight(calls));
145
+ assert.deepEqual(calls, []);
146
+ assert.equal(text, ["", styled("text", json)].join("\n"));
147
+ });
148
+
149
+ test("buildGenericResultText: empty and whitespace-only output render nothing", () => {
150
+ assert.equal(buildGenericResultText(genericInput({ output: "" }), makeHighlight([])), "");
151
+ assert.equal(buildGenericResultText(genericInput({ output: " \n\n \t " }), makeHighlight([])), "");
152
+ });
@@ -0,0 +1,338 @@
1
+ import { test } from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { formatSize } from "@earendil-works/pi-coding-agent";
4
+ import { buildGrepResultText, type GrepInput } from "../../src/render-grep.ts";
5
+ import { UNREADABLE_TEXT } from "../../src/grep-parse.ts";
6
+ import type { HighlightFn } from "../../src/highlight.ts";
7
+ import { BOLD_OFF, BOLD_ON, TOKEN } from "../../src/tokens.ts";
8
+
9
+ const fakeTheme = {
10
+ fg: (c: string, t: string) => `<${c}>${t}</${c}>`,
11
+ bg: (c: string, t: string) => `<bg:${c}>${t}</bg:${c}>`,
12
+ bold: (t: string) => `<b>${t}</b>`,
13
+ };
14
+
15
+ const EXPAND_HINT = "<hint>";
16
+ const FOOTER_5 = "<muted>\n... (5 more lines,</muted> <hint><muted>)</muted>";
17
+
18
+ const NOTICE = "20 matches limit reached. Use limit=40 for more, or refine pattern";
19
+ // Eighteen matches, the blank separator grep prints, and the notice: twenty
20
+ // body lines in total.
21
+ const BODY_WITH_NOTICE = [
22
+ ...Array.from({ length: 18 }, (_, index) => `a.ts:${index + 1}: line${index + 1}`),
23
+ "",
24
+ `[${NOTICE}]`,
25
+ ].join("\n");
26
+
27
+ interface HighlightCall {
28
+ code: string;
29
+ lang: string;
30
+ }
31
+
32
+ function makeHighlight(calls: HighlightCall[]): HighlightFn {
33
+ return (code, lang) => {
34
+ calls.push({ code, lang });
35
+ return code.split("\n").map((line) => `<${lang}>${line}</${lang}>`);
36
+ };
37
+ }
38
+
39
+ // Emphasis is inserted by visible column, so the fakes used with a pattern must
40
+ // not invent markup: `<typescript>` would count as characters on screen.
41
+ const plainHighlight: HighlightFn = (code) => code.split("\n");
42
+ const ansiHighlight: HighlightFn = (code) => code.split("\n").map((line) => `\x1b[36m${line}\x1b[39m`);
43
+
44
+ function plainPrefix(path: string, line: number): string {
45
+ return `<accent>${path}</accent><dim>:</dim><syntaxNumber>${line}</syntaxNumber><dim>:</dim>`;
46
+ }
47
+
48
+ // A match line bolds the path; the unreadable line keeps the same prefix with
49
+ // the accent color alone, which is why both forms are spelled out here.
50
+ function matchLine(path: string, line: number, styledText: string): string {
51
+ return `<b><accent>${path}</accent></b><dim>:</dim><syntaxNumber>${line}</syntaxNumber><dim>:</dim> ${styledText}`;
52
+ }
53
+
54
+ function contextLine(path: string, line: number, styledText: string): string {
55
+ return `<dim>${path}</dim>-<dim>${line}</dim>- ${styledText}`;
56
+ }
57
+
58
+ function grepInput(overrides: Partial<GrepInput> = {}): GrepInput {
59
+ return {
60
+ output: "",
61
+ expanded: false,
62
+ pattern: {},
63
+ details: undefined,
64
+ theme: fakeTheme,
65
+ expandHint: EXPAND_HINT,
66
+ highlight: makeHighlight([]),
67
+ ...overrides,
68
+ };
69
+ }
70
+
71
+ test("buildGrepResultText: three matches render in full with no footer", () => {
72
+ const calls: HighlightCall[] = [];
73
+ const text = buildGrepResultText(
74
+ grepInput({
75
+ output: "a.ts:1: one\na.ts:2: two\na.ts:3: three",
76
+ highlight: makeHighlight(calls),
77
+ }),
78
+ );
79
+ assert.equal(
80
+ text,
81
+ [
82
+ "",
83
+ matchLine("a.ts", 1, "<typescript>one</typescript>"),
84
+ matchLine("a.ts", 2, "<typescript>two</typescript>"),
85
+ matchLine("a.ts", 3, "<typescript>three</typescript>"),
86
+ ].join("\n"),
87
+ );
88
+ assert.deepEqual(calls, [{ code: "one\ntwo\nthree", lang: "typescript" }]);
89
+ });
90
+
91
+ test("buildGrepResultText: twenty lines collapse to fifteen plus a footer", () => {
92
+ const output = Array.from({ length: 20 }, (_, index) => `a.ts:${index + 1}: line${index + 1}`).join("\n");
93
+ const visible = Array.from({ length: 15 }, (_, index) =>
94
+ matchLine("a.ts", index + 1, `<typescript>line${index + 1}</typescript>`),
95
+ );
96
+ assert.equal(buildGrepResultText(grepInput({ output })), ["", ...visible].join("\n") + FOOTER_5);
97
+ });
98
+
99
+ test("buildGrepResultText: expanded shows every line and no footer", () => {
100
+ const output = Array.from({ length: 20 }, (_, index) => `a.ts:${index + 1}: line${index + 1}`).join("\n");
101
+ const all = Array.from({ length: 20 }, (_, index) =>
102
+ matchLine("a.ts", index + 1, `<typescript>line${index + 1}</typescript>`),
103
+ );
104
+ assert.equal(buildGrepResultText(grepInput({ output, expanded: true })), ["", ...all].join("\n"));
105
+ });
106
+
107
+ test("buildGrepResultText: the unreadable marker is painted in the error color and its path stays unbolded", () => {
108
+ assert.equal(TOKEN.broken, "error");
109
+ const text = buildGrepResultText(grepInput({ output: "a.ts:9: (unable to read file)" }));
110
+ assert.equal(text, ["", `${plainPrefix("a.ts", 9)} <error>(unable to read file)</error>`].join("\n"));
111
+ });
112
+
113
+ test("buildGrepResultText: the tool's own notice is a body line, so the footer counts it", () => {
114
+ assert.equal(TOKEN.notice, "warning");
115
+ // grep appends its limit block after a blank line, so the notice is the last of
116
+ // the body's twenty lines and the blank line before it is one more.
117
+ const visible = Array.from({ length: 15 }, (_, index) =>
118
+ matchLine("a.ts", index + 1, `<typescript>line${index + 1}</typescript>`),
119
+ );
120
+ assert.equal(buildGrepResultText(grepInput({ output: BODY_WITH_NOTICE })), ["", ...visible].join("\n") + FOOTER_5);
121
+ // The five that stay hidden include the notice, which is why the count matches
122
+ // stock pi: it never separates the block from the lines above it.
123
+ assert.equal(BODY_WITH_NOTICE.split("\n").length, 20);
124
+ });
125
+
126
+ test("buildGrepResultText: expanded shows the notice painted in the warning color", () => {
127
+ const text = buildGrepResultText(grepInput({ output: BODY_WITH_NOTICE, expanded: true }));
128
+ assert.equal(text.includes("more lines,"), false);
129
+ assert.ok(
130
+ text.endsWith(`\n<text></text>\n<warning>[${NOTICE}]</warning>`),
131
+ `expected the blank line and the notice at the end of: ${text}`,
132
+ );
133
+ });
134
+
135
+ test("buildGrepResultText: empty and blank outputs render nothing", () => {
136
+ assert.equal(buildGrepResultText(grepInput({ output: "" })), "");
137
+ assert.equal(buildGrepResultText(grepInput({ output: " \n " })), "");
138
+ });
139
+
140
+ test("buildGrepResultText: a path with no known language falls back to the content color", () => {
141
+ assert.equal(TOKEN.content, "text");
142
+ const calls: HighlightCall[] = [];
143
+ const text = buildGrepResultText(grepInput({ output: "notes:12: plain text", highlight: makeHighlight(calls) }));
144
+ assert.equal(text, ["", matchLine("notes", 12, "<text>plain text</text>")].join("\n"));
145
+ assert.deepEqual(calls, []);
146
+ });
147
+
148
+ test("buildGrepResultText: contiguous lines of the same language are highlighted in one call per run", () => {
149
+ const calls: HighlightCall[] = [];
150
+ const output = "a.ts:1: one\na.ts:2: two\nREADME.md:3: doc\na.ts:4: four";
151
+ const text = buildGrepResultText(grepInput({ output, highlight: makeHighlight(calls) }));
152
+ assert.deepEqual(calls, [
153
+ { code: "one\ntwo", lang: "typescript" },
154
+ { code: "doc", lang: "markdown" },
155
+ { code: "four", lang: "typescript" },
156
+ ]);
157
+ assert.equal(
158
+ text,
159
+ [
160
+ "",
161
+ matchLine("a.ts", 1, "<typescript>one</typescript>"),
162
+ matchLine("a.ts", 2, "<typescript>two</typescript>"),
163
+ matchLine("README.md", 3, "<markdown>doc</markdown>"),
164
+ matchLine("a.ts", 4, "<typescript>four</typescript>"),
165
+ ].join("\n"),
166
+ );
167
+ });
168
+
169
+ test("buildGrepResultText: two files of the same language are highlighted in separate calls", () => {
170
+ const calls: HighlightCall[] = [];
171
+ const output = "a.ts:1: one\nb.ts:2: two";
172
+ const text = buildGrepResultText(grepInput({ output, highlight: makeHighlight(calls) }));
173
+ assert.deepEqual(calls, [
174
+ { code: "one", lang: "typescript" },
175
+ { code: "two", lang: "typescript" },
176
+ ]);
177
+ assert.equal(
178
+ text,
179
+ [
180
+ "",
181
+ matchLine("a.ts", 1, "<typescript>one</typescript>"),
182
+ matchLine("b.ts", 2, "<typescript>two</typescript>"),
183
+ ].join("\n"),
184
+ );
185
+ });
186
+
187
+ test("buildGrepResultText: the lines of one file are highlighted in a single call", () => {
188
+ const calls: HighlightCall[] = [];
189
+ const output = "a.ts:1: one\na.ts:2: two\na.ts:3: three";
190
+ buildGrepResultText(grepInput({ output, highlight: makeHighlight(calls) }));
191
+ assert.deepEqual(calls, [{ code: "one\ntwo\nthree", lang: "typescript" }]);
192
+ });
193
+
194
+ test("buildGrepResultText: the unreadable marker is the shared constant, painted in the error color", () => {
195
+ assert.equal(UNREADABLE_TEXT, "(unable to read file)");
196
+ assert.equal(TOKEN.broken, "error");
197
+ const text = buildGrepResultText(grepInput({ output: `a.ts:9: ${UNREADABLE_TEXT}` }));
198
+ assert.equal(text, ["", `${plainPrefix("a.ts", 9)} <error>${UNREADABLE_TEXT}</error>`].join("\n"));
199
+ });
200
+
201
+ test("buildGrepResultText: context lines keep their dash separators, dim colors and unbolded path", () => {
202
+ const calls: HighlightCall[] = [];
203
+ const output = "a.ts:1: one\na.ts-2- two";
204
+ const text = buildGrepResultText(grepInput({ output, highlight: makeHighlight(calls) }));
205
+ assert.equal(
206
+ text,
207
+ ["", matchLine("a.ts", 1, "<typescript>one</typescript>"), contextLine("a.ts", 2, "<typescript>two</typescript>")].join(
208
+ "\n",
209
+ ),
210
+ );
211
+ assert.deepEqual(calls, [{ code: "one\ntwo", lang: "typescript" }]);
212
+ });
213
+
214
+ test("buildGrepResultText: the truncation warning lists the limits that were hit, in order", () => {
215
+ const text = buildGrepResultText(
216
+ grepInput({
217
+ output: "notes:1: x",
218
+ details: { matchLimitReached: 100, truncation: { truncated: true, maxBytes: 1048576 }, linesTruncated: true },
219
+ }),
220
+ );
221
+ assert.equal(
222
+ text,
223
+ [
224
+ "",
225
+ matchLine("notes", 1, "<text>x</text>"),
226
+ `<warning>[Truncated: 100 matches limit, ${formatSize(1048576)} limit, some lines truncated]</warning>`,
227
+ ].join("\n"),
228
+ );
229
+ });
230
+
231
+ test("buildGrepResultText: the matched span of a match line is bolded and inverted", () => {
232
+ const text = buildGrepResultText(
233
+ grepInput({
234
+ output: "a.ts:1: export function foo",
235
+ pattern: { pattern: "function", literal: true },
236
+ highlight: plainHighlight,
237
+ }),
238
+ );
239
+ assert.equal(text, ["", matchLine("a.ts", 1, `export ${BOLD_ON}function${BOLD_OFF} foo`)].join("\n"));
240
+ });
241
+
242
+ test("buildGrepResultText: ignoreCase emphasizes a differently cased occurrence", () => {
243
+ const output = "a.ts:1: Function run";
244
+ assert.equal(
245
+ buildGrepResultText(
246
+ grepInput({ output, pattern: { pattern: "function", ignoreCase: true }, highlight: plainHighlight }),
247
+ ),
248
+ ["", matchLine("a.ts", 1, `${BOLD_ON}Function${BOLD_OFF} run`)].join("\n"),
249
+ );
250
+ // Without the flag the same line stays plain.
251
+ assert.equal(
252
+ buildGrepResultText(grepInput({ output, pattern: { pattern: "function" }, highlight: plainHighlight })),
253
+ ["", matchLine("a.ts", 1, "Function run")].join("\n"),
254
+ );
255
+ });
256
+
257
+ test("buildGrepResultText: a literal pattern does not emphasize a regex lookalike", () => {
258
+ const lookalike = "a.ts:1: aXb";
259
+ assert.equal(
260
+ buildGrepResultText(grepInput({ output: lookalike, pattern: { pattern: "a.b" }, highlight: plainHighlight })),
261
+ ["", matchLine("a.ts", 1, `${BOLD_ON}aXb${BOLD_OFF}`)].join("\n"),
262
+ );
263
+ assert.equal(
264
+ buildGrepResultText(
265
+ grepInput({ output: lookalike, pattern: { pattern: "a.b", literal: true }, highlight: plainHighlight }),
266
+ ),
267
+ ["", matchLine("a.ts", 1, "aXb")].join("\n"),
268
+ );
269
+ // The literal pattern still matches itself.
270
+ assert.equal(
271
+ buildGrepResultText(
272
+ grepInput({ output: "a.ts:2: x a.b y", pattern: { pattern: "a.b", literal: true }, highlight: plainHighlight }),
273
+ ),
274
+ ["", matchLine("a.ts", 2, `x ${BOLD_ON}a.b${BOLD_OFF} y`)].join("\n"),
275
+ );
276
+ });
277
+
278
+ test("buildGrepResultText: every occurrence on a match line gets its own pair of markers", () => {
279
+ assert.equal(
280
+ buildGrepResultText(
281
+ grepInput({
282
+ output: "a.ts:1: function add, function sub",
283
+ pattern: { pattern: "function", literal: true },
284
+ highlight: plainHighlight,
285
+ }),
286
+ ),
287
+ ["", matchLine("a.ts", 1, `${BOLD_ON}function${BOLD_OFF} add, ${BOLD_ON}function${BOLD_OFF} sub`)].join("\n"),
288
+ );
289
+ });
290
+
291
+ test("buildGrepResultText: the emphasis lands on the visible column of an already colored line", () => {
292
+ const text = buildGrepResultText(
293
+ grepInput({
294
+ output: "a.ts:1: const functionName = 1",
295
+ pattern: { pattern: "function", literal: true },
296
+ highlight: ansiHighlight,
297
+ }),
298
+ );
299
+ assert.equal(
300
+ text,
301
+ ["", matchLine("a.ts", 1, `\x1b[36mconst ${BOLD_ON}function${BOLD_OFF}Name = 1\x1b[39m`)].join("\n"),
302
+ );
303
+ });
304
+
305
+ test("buildGrepResultText: the path and the line number are never emphasized", () => {
306
+ const text = buildGrepResultText(
307
+ grepInput({ output: "a.ts:1: foo", pattern: { pattern: "ts" }, highlight: plainHighlight }),
308
+ );
309
+ // `ts` is in the path only, so no emphasis marker is inserted and the prefix
310
+ // is exactly what it is without a pattern; the path's weight is the bold every
311
+ // match line carries, not an emphasis span.
312
+ assert.equal(text, ["", matchLine("a.ts", 1, "foo")].join("\n"));
313
+ assert.equal(text.includes(BOLD_ON), false);
314
+ });
315
+
316
+ test("buildGrepResultText: context lines are not emphasized", () => {
317
+ const text = buildGrepResultText(
318
+ grepInput({
319
+ output: "a.ts:1: function one\na.ts-2- function two",
320
+ pattern: { pattern: "function", literal: true },
321
+ highlight: plainHighlight,
322
+ }),
323
+ );
324
+ assert.equal(
325
+ text,
326
+ ["", matchLine("a.ts", 1, `${BOLD_ON}function${BOLD_OFF} one`), contextLine("a.ts", 2, "function two")].join("\n"),
327
+ );
328
+ });
329
+
330
+ test("buildGrepResultText: without a usable pattern the output is byte-identical to the previous render", () => {
331
+ const calls: HighlightCall[] = [];
332
+ const output = "a.ts:1: one";
333
+ const expected = ["", matchLine("a.ts", 1, "<typescript>one</typescript>")].join("\n");
334
+ for (const pattern of [{}, { pattern: "" }, { pattern: 42 }, { pattern: null }]) {
335
+ assert.equal(buildGrepResultText(grepInput({ output, pattern, highlight: makeHighlight(calls) })), expected);
336
+ }
337
+ assert.equal(expected.includes(BOLD_ON), false);
338
+ });
@@ -0,0 +1,132 @@
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 { buildLsResultText, type LsInput } from "../../src/render-ls.ts";
5
+ import { TOKEN } from "../../src/tokens.ts";
6
+
7
+ const fakeTheme = {
8
+ fg: (c: string, t: string) => `<${c}>${t}</${c}>`,
9
+ bg: (c: string, t: string) => `<bg:${c}>${t}</bg:${c}>`,
10
+ bold: (t: string) => `<b>${t}</b>`,
11
+ };
12
+
13
+ const EXPAND_HINT = "<hint>";
14
+ const FOOTER_1 = "<muted>\n... (1 more lines,</muted> <hint><muted>)</muted>";
15
+ const FOOTER_3 = "<muted>\n... (3 more lines,</muted> <hint><muted>)</muted>";
16
+
17
+ const ENTRIES = Array.from({ length: 21 }, (_, index) => `f${index}.ts`);
18
+ const NOTICE = "50 entries limit reached. Use limit=100 for more";
19
+ // Twenty-one entries, the blank separator ls prints, and the notice: twenty-three
20
+ // body lines in total.
21
+ const ENTRIES_WITH_NOTICE = `${ENTRIES.join("\n")}\n\n[${NOTICE}]`;
22
+
23
+ function styledEntry(entry: string): string {
24
+ return `<b><syntaxType>${entry}</syntaxType></b>`;
25
+ }
26
+
27
+ function lsInput(overrides: Partial<LsInput> = {}): LsInput {
28
+ return {
29
+ output: "",
30
+ expanded: false,
31
+ isError: false,
32
+ details: undefined,
33
+ theme: fakeTheme,
34
+ expandHint: EXPAND_HINT,
35
+ ...overrides,
36
+ };
37
+ }
38
+
39
+ test("buildLsResultText: three entries render in full with no footer", () => {
40
+ const text = buildLsResultText(lsInput({ output: "a.ts\nb.ts\nc.ts" }));
41
+ assert.equal(text, ["", styledEntry("a.ts"), styledEntry("b.ts"), styledEntry("c.ts")].join("\n"));
42
+ });
43
+
44
+ test("buildLsResultText: twenty-one entries collapse to twenty plus a footer", () => {
45
+ const text = buildLsResultText(lsInput({ output: ENTRIES.join("\n") }));
46
+ assert.equal(text, ["", ...ENTRIES.slice(0, 20).map(styledEntry)].join("\n") + FOOTER_1);
47
+ });
48
+
49
+ test("buildLsResultText: expanded shows every entry and no footer", () => {
50
+ const text = buildLsResultText(lsInput({ output: ENTRIES.join("\n"), expanded: true }));
51
+ assert.equal(text, ["", ...ENTRIES.map(styledEntry)].join("\n"));
52
+ });
53
+
54
+ test("buildLsResultText: entry names are not split into path segments", () => {
55
+ const text = buildLsResultText(lsInput({ output: "src" }));
56
+ assert.equal(text, ["", "<b><toolOutput>src</toolOutput></b>"].join("\n"));
57
+ });
58
+
59
+ test("buildLsResultText: a directory keeps its trailing slash and is dimmed", () => {
60
+ const text = buildLsResultText(lsInput({ output: "src/" }));
61
+ assert.equal(text, ["", "<b><dim>src</dim></b>/"].join("\n"));
62
+ });
63
+
64
+ test("buildLsResultText: the entries limit warning sits after the footer", () => {
65
+ const text = buildLsResultText(lsInput({ output: ENTRIES.join("\n"), details: { entryLimitReached: 50 } }));
66
+ const footerAt = text.indexOf("more lines,");
67
+ const warningAt = text.indexOf("[Truncated:");
68
+ assert.ok(footerAt !== -1, `expected the footer in: ${text}`);
69
+ assert.ok(warningAt > footerAt, `expected the warning after the footer in: ${text}`);
70
+ assert.ok(
71
+ text.endsWith("\n<warning>[Truncated: 50 entries limit]</warning>"),
72
+ `expected the entries limit warning at the end of: ${text}`,
73
+ );
74
+ });
75
+
76
+ test("buildLsResultText: the size limit is formatted through formatSize, with the default when absent", () => {
77
+ const explicit = buildLsResultText(
78
+ lsInput({ output: "a.ts", details: { truncation: { truncated: true, maxBytes: 1048576 } } }),
79
+ );
80
+ assert.equal(
81
+ explicit,
82
+ ["", styledEntry("a.ts"), `<warning>[Truncated: ${formatSize(1048576)} limit]</warning>`].join("\n"),
83
+ );
84
+
85
+ const defaulted = buildLsResultText(lsInput({ output: "a.ts", details: { truncation: { truncated: true } } }));
86
+ assert.equal(
87
+ defaulted,
88
+ ["", styledEntry("a.ts"), `<warning>[Truncated: ${formatSize(DEFAULT_MAX_BYTES)} limit]</warning>`].join("\n"),
89
+ );
90
+ });
91
+
92
+ test("buildLsResultText: the size warning is absent when the output was not truncated", () => {
93
+ const text = buildLsResultText(lsInput({ output: "a.ts", details: { truncation: { truncated: false } } }));
94
+ assert.equal(text, ["", styledEntry("a.ts")].join("\n"));
95
+ });
96
+
97
+ test("buildLsResultText: the tool's own entries notice is a body line, so the footer counts it", () => {
98
+ assert.equal(TOKEN.notice, "warning");
99
+ assert.equal(
100
+ buildLsResultText(lsInput({ output: ENTRIES_WITH_NOTICE })),
101
+ ["", ...ENTRIES.slice(0, 20).map(styledEntry)].join("\n") + FOOTER_3,
102
+ );
103
+
104
+ const expanded = buildLsResultText(lsInput({ output: ENTRIES_WITH_NOTICE, expanded: true }));
105
+ assert.equal(expanded.includes("more lines,"), false);
106
+ assert.ok(
107
+ expanded.endsWith(`\n<warning>[${NOTICE}]</warning>`),
108
+ `expected the notice at the end of: ${expanded}`,
109
+ );
110
+ });
111
+
112
+ test("buildLsResultText: an error result is painted flat in the content color", () => {
113
+ assert.equal(TOKEN.content, "text");
114
+ const text = buildLsResultText(lsInput({ output: "src/a.ts\nsrc/\n(empty directory)", isError: true }));
115
+ // No bold, no file-family color, and no sentinel dimming: the failure is the
116
+ // whole message, so every line keeps the one weight.
117
+ assert.equal(
118
+ text,
119
+ ["", "<text>src/a.ts</text>", "<text>src/</text>", "<text>(empty directory)</text>"].join("\n"),
120
+ );
121
+ });
122
+
123
+ test("buildLsResultText: an empty directory is dimmed as a sentinel, not styled as an entry", () => {
124
+ assert.equal(TOKEN.empty, "dim");
125
+ const text = buildLsResultText(lsInput({ output: "(empty directory)" }));
126
+ assert.equal(text, ["", "<dim>(empty directory)</dim>"].join("\n"));
127
+ });
128
+
129
+ test("buildLsResultText: empty and blank outputs render nothing", () => {
130
+ assert.equal(buildLsResultText(lsInput({ output: "" })), "");
131
+ assert.equal(buildLsResultText(lsInput({ output: " \n " })), "");
132
+ });