pi-pretty-tui 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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Kainan Yang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # pi-pretty-tui
2
+
3
+ A visual refinement extension for the [Pi coding agent](https://pi.dev/). It provides compact tool-call rendering and cleaner list presentation while preserving Pi's built-in tool behavior.
4
+
5
+ ## Features
6
+
7
+ - Compact `Read`, `Bash`, `Edit`, `Write`, `Grep`, `Find`, and `List` calls
8
+ - Gray running, green success, and red failure indicators
9
+ - Concise result summaries with expandable output
10
+ - Colored `+added` and `-removed` edit statistics and diffs
11
+ - Correct hanging indentation for long paths and wrapped tool output
12
+ - Bullet (`•`) markers for unordered Markdown lists
13
+
14
+ ## Install
15
+
16
+ ```sh
17
+ pi install npm:pi-pretty-tui
18
+ ```
19
+
20
+ Restart Pi or run `/reload` after installation.
21
+
22
+ Pi may warn that built-in tools are being overridden. This is expected: pi-pretty-tui re-registers Pi's built-in tools and delegates execution to their original implementations, changing only their TUI renderers.
23
+
24
+ ## Expand tool output
25
+
26
+ Use Pi's configured `app.tools.expand` keybinding (usually `Ctrl+E`) to show or hide detailed output and edit diffs.
27
+
28
+ ## Compatibility notice
29
+
30
+ Tool rendering uses Pi's documented extension APIs. Changing unordered-list markers currently requires runtime patching of an internal `@earendil-works/pi-tui` Markdown renderer method because Pi does not expose a public hook for that presentation detail. Code blocks use Pi's original renderer without modification.
31
+
32
+ No Pi source files are modified. The list-marker patch is removed during session shutdown, but a future Pi release may require this extension to be updated.
33
+
34
+ ## Uninstall
35
+
36
+ ```sh
37
+ pi remove npm:pi-pretty-tui
38
+ ```
39
+
40
+ Then restart Pi.
41
+
42
+ ## License
43
+
44
+ MIT
@@ -0,0 +1,330 @@
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
+ import {
3
+ createBashTool,
4
+ createEditTool,
5
+ createFindTool,
6
+ createGrepTool,
7
+ createLsTool,
8
+ createReadTool,
9
+ createWriteTool,
10
+ } from "@earendil-works/pi-coding-agent";
11
+ import {
12
+ type Component,
13
+ Markdown,
14
+ visibleWidth,
15
+ wrapTextWithAnsi,
16
+ } from "@earendil-works/pi-tui";
17
+
18
+ /** Polished tool calls and list rendering for Pi's TUI. */
19
+ export default function prettyTui(pi: ExtensionAPI) {
20
+ const cwd = process.cwd();
21
+
22
+ // Pi normalizes unordered-list markers to "-". Replace only that marker;
23
+ // leave code-block rendering entirely to Pi's built-in Markdown renderer.
24
+ const markdownPrototype = Markdown.prototype as any;
25
+ const listPatchKey = Symbol.for("pretty-tui.list-bullets");
26
+ if (!markdownPrototype[listPatchKey]) {
27
+ const originalRenderList = markdownPrototype.renderList;
28
+ const patchedRenderList = function (
29
+ this: any,
30
+ token: any,
31
+ depth: number,
32
+ width: number,
33
+ styleContext?: any,
34
+ ): string[] {
35
+ const originalListBullet = this.theme.listBullet;
36
+ this.theme.listBullet = (marker: string) =>
37
+ originalListBullet(marker.replace(/^- /, "• "));
38
+ try {
39
+ return originalRenderList.call(this, token, depth, width, styleContext);
40
+ } finally {
41
+ this.theme.listBullet = originalListBullet;
42
+ }
43
+ };
44
+
45
+ markdownPrototype[listPatchKey] = { originalRenderList, patchedRenderList };
46
+ markdownPrototype.renderList = patchedRenderList;
47
+
48
+ pi.on("session_shutdown", () => {
49
+ const patch = markdownPrototype[listPatchKey];
50
+ if (patch?.patchedRenderList === markdownPrototype.renderList) {
51
+ markdownPrototype.renderList = patch.originalRenderList;
52
+ }
53
+ if (patch) delete markdownPrototype[listPatchKey];
54
+ });
55
+ }
56
+
57
+ const textContent = (result: any): string =>
58
+ result.content
59
+ ?.filter((item: any) => item.type === "text")
60
+ .map((item: any) => item.text)
61
+ .join("\n") ?? "";
62
+
63
+ const nonEmptyLines = (value: string): number =>
64
+ value ? value.split("\n").filter((line) => line.length > 0).length : 0;
65
+
66
+ type DisplayValue = string | (() => string);
67
+ type DisplayRow = {
68
+ prefix: DisplayValue;
69
+ continuation?: DisplayValue;
70
+ content: DisplayValue;
71
+ };
72
+
73
+ /**
74
+ * Wrap content separately from its prefix. Text's normal word wrapping can
75
+ * leave a bullet by itself and drops tree guides on continuation lines.
76
+ */
77
+ const block = (rows: DisplayRow[]): Component => ({
78
+ render(width: number): string[] {
79
+ const rendered: string[] = [];
80
+
81
+ for (const row of rows) {
82
+ const prefix = typeof row.prefix === "function" ? row.prefix() : row.prefix;
83
+ const content = typeof row.content === "function" ? row.content() : row.content;
84
+ const rawContinuation = row.continuation ?? " ".repeat(visibleWidth(prefix));
85
+ const continuation = typeof rawContinuation === "function" ? rawContinuation() : rawContinuation;
86
+ const prefixWidth = Math.max(visibleWidth(prefix), visibleWidth(continuation));
87
+ const contentWidth = Math.max(1, width - prefixWidth);
88
+ const wrapped = wrapTextWithAnsi(content || " ", contentWidth);
89
+
90
+ rendered.push(prefix + (wrapped[0] ?? ""));
91
+ for (const line of wrapped.slice(1)) rendered.push(continuation + line);
92
+ }
93
+
94
+ return rendered;
95
+ },
96
+ invalidate() {},
97
+ });
98
+
99
+ type ToolStatus = "running" | "success" | "error";
100
+ const setStatus = (context: any, status: ToolStatus) => {
101
+ context.state.compactToolStatus = status;
102
+ };
103
+
104
+ const call = (theme: any, name: string, detail: string, state: any) =>
105
+ block([{
106
+ prefix: () => {
107
+ const status = (state.compactToolStatus ?? "running") as ToolStatus;
108
+ const color = status === "success" ? "success" : status === "error" ? "error" : "dim";
109
+ return theme.fg(color, "● ");
110
+ },
111
+ continuation: " ",
112
+ content:
113
+ theme.fg("accent", theme.bold(name)) +
114
+ theme.fg("dim", "(") +
115
+ theme.fg("text", detail) +
116
+ theme.fg("dim", ")"),
117
+ }]);
118
+
119
+ const result = (
120
+ theme: any,
121
+ summary: string,
122
+ output = "",
123
+ expanded = false,
124
+ error = false,
125
+ summaryIsStyled = false,
126
+ outputStyle: (line: string) => string = (line) => theme.fg("dim", line),
127
+ ) => {
128
+ const rows: DisplayRow[] = [{
129
+ prefix: theme.fg("dim", "└ "),
130
+ continuation: " ",
131
+ content: summaryIsStyled ? summary : theme.fg(error ? "error" : "toolOutput", summary),
132
+ }];
133
+
134
+ if (expanded && output) {
135
+ const lines = output.split("\n");
136
+ const shown = lines.slice(0, 40);
137
+ for (const line of shown) {
138
+ rows.push({
139
+ prefix: theme.fg("dim", " │ "),
140
+ continuation: theme.fg("dim", " │ "),
141
+ content: outputStyle(line || " "),
142
+ });
143
+ }
144
+ if (lines.length > shown.length) {
145
+ rows.push({
146
+ prefix: theme.fg("muted", " └ "),
147
+ continuation: " ",
148
+ content: theme.fg("muted", `… ${lines.length - shown.length} more lines`),
149
+ });
150
+ }
151
+ }
152
+
153
+ return block(rows);
154
+ };
155
+
156
+ const isError = (renderContext: any, output: string): boolean =>
157
+ Boolean(renderContext?.isError) || /^(error|failed|access denied)\b/i.test(output.trim());
158
+
159
+ const partialResult = (context: any, theme: any, label: string): Component => {
160
+ setStatus(context, "running");
161
+ return result(theme, label);
162
+ };
163
+
164
+ const completeStatus = (context: any, output: string): boolean => {
165
+ const failed = isError(context, output);
166
+ setStatus(context, failed ? "error" : "success");
167
+ return failed;
168
+ };
169
+
170
+ const read = createReadTool(cwd);
171
+ pi.registerTool({
172
+ ...read,
173
+ renderShell: "self",
174
+ renderCall(args: any, theme: any, context: any) {
175
+ const range = args.offset || args.limit
176
+ ? ` · lines ${args.offset ?? 1}${args.limit ? `–${(args.offset ?? 1) + args.limit - 1}` : "+"}`
177
+ : "";
178
+ return call(theme, "Read", `${args.path}${range}`, context.state);
179
+ },
180
+ renderResult(toolResult: any, options: any, theme: any, context: any) {
181
+ if (options.isPartial) return partialResult(context, theme, "Reading…");
182
+ const output = textContent(toolResult);
183
+ const image = toolResult.content?.find((item: any) => item.type === "image");
184
+ const failed = completeStatus(context, output);
185
+ if (image) return result(theme, "Read image", "", false, failed);
186
+ const lines = nonEmptyLines(output);
187
+ const truncated = toolResult.details?.truncation?.truncated ? " · truncated" : "";
188
+ return result(
189
+ theme,
190
+ failed ? (output.split("\n")[0] || "Read failed") : `Read ${lines} ${lines === 1 ? "line" : "lines"}${truncated}`,
191
+ output,
192
+ options.expanded,
193
+ failed,
194
+ );
195
+ },
196
+ } as any);
197
+
198
+ const bash = createBashTool(cwd);
199
+ pi.registerTool({
200
+ ...bash,
201
+ renderShell: "self",
202
+ renderCall(args: any, theme: any, context: any) {
203
+ const command = String(args.command).replace(/\s*\n\s*/g, " ↵ ");
204
+ return call(theme, "Bash", command, context.state);
205
+ },
206
+ renderResult(toolResult: any, options: any, theme: any, context: any) {
207
+ if (options.isPartial) return partialResult(context, theme, "Running…");
208
+ const output = textContent(toolResult);
209
+ const failed = completeStatus(context, output);
210
+ const lines = nonEmptyLines(output);
211
+ const summary = failed
212
+ ? output.split("\n")[0] || "Command failed"
213
+ : output
214
+ ? `Done · ${lines} output ${lines === 1 ? "line" : "lines"}`
215
+ : "Done";
216
+ return result(theme, summary, output, options.expanded, failed);
217
+ },
218
+ } as any);
219
+
220
+ const edit = createEditTool(cwd);
221
+ pi.registerTool({
222
+ ...edit,
223
+ renderShell: "self",
224
+ renderCall(args: any, theme: any, context: any) {
225
+ const count = Array.isArray(args.edits) ? ` · ${args.edits.length} changes` : "";
226
+ return call(theme, "Edit", `${args.path}${count}`, context.state);
227
+ },
228
+ renderResult(toolResult: any, options: any, theme: any, context: any) {
229
+ if (options.isPartial) return partialResult(context, theme, "Editing…");
230
+ const output = textContent(toolResult);
231
+ const failed = completeStatus(context, output);
232
+ const diff = toolResult.details?.diff ?? "";
233
+ const additions = diff.split("\n").filter((line: string) => line.startsWith("+") && !line.startsWith("+++")).length;
234
+ const removals = diff.split("\n").filter((line: string) => line.startsWith("-") && !line.startsWith("---")).length;
235
+ const summary = failed
236
+ ? output.split("\n")[0] || "Edit failed"
237
+ : diff
238
+ ? theme.fg("toolOutput", "Updated · ") +
239
+ theme.fg("success", `+${additions}`) +
240
+ theme.fg("toolOutput", " ") +
241
+ theme.fg("error", `-${removals}`)
242
+ : "Updated";
243
+ return result(
244
+ theme,
245
+ summary,
246
+ diff || output,
247
+ options.expanded,
248
+ failed,
249
+ !failed && Boolean(diff),
250
+ (line) => {
251
+ if (line.startsWith("+") && !line.startsWith("+++")) return theme.fg("toolDiffAdded", line);
252
+ if (line.startsWith("-") && !line.startsWith("---")) return theme.fg("toolDiffRemoved", line);
253
+ return theme.fg("toolDiffContext", line);
254
+ },
255
+ );
256
+ },
257
+ } as any);
258
+
259
+ const write = createWriteTool(cwd);
260
+ pi.registerTool({
261
+ ...write,
262
+ renderShell: "self",
263
+ renderCall(args: any, theme: any, context: any) {
264
+ const content = String(args.content ?? "");
265
+ return call(theme, "Write", `${args.path} · ${nonEmptyLines(content)} lines`, context.state);
266
+ },
267
+ renderResult(toolResult: any, options: any, theme: any, context: any) {
268
+ if (options.isPartial) return partialResult(context, theme, "Writing…");
269
+ const output = textContent(toolResult);
270
+ const failed = completeStatus(context, output);
271
+ return result(
272
+ theme,
273
+ failed ? output.split("\n")[0] || "Write failed" : "Written",
274
+ output,
275
+ options.expanded,
276
+ failed,
277
+ );
278
+ },
279
+ } as any);
280
+
281
+ const grep = createGrepTool(cwd);
282
+ pi.registerTool({
283
+ ...grep,
284
+ renderShell: "self",
285
+ renderCall(args: any, theme: any, context: any) {
286
+ const where = args.path ? ` · ${args.path}` : "";
287
+ const glob = args.glob ? ` · ${args.glob}` : "";
288
+ return call(theme, "Grep", `${args.pattern}${where}${glob}`, context.state);
289
+ },
290
+ renderResult(toolResult: any, options: any, theme: any, context: any) {
291
+ if (options.isPartial) return partialResult(context, theme, "Searching…");
292
+ const output = textContent(toolResult);
293
+ const failed = completeStatus(context, output);
294
+ const matches = nonEmptyLines(output);
295
+ return result(theme, failed ? output.split("\n")[0] : `Found ${matches} matches`, output, options.expanded, failed);
296
+ },
297
+ } as any);
298
+
299
+ const find = createFindTool(cwd);
300
+ pi.registerTool({
301
+ ...find,
302
+ renderShell: "self",
303
+ renderCall(args: any, theme: any, context: any) {
304
+ return call(theme, "Find", `${args.pattern}${args.path ? ` · ${args.path}` : ""}`, context.state);
305
+ },
306
+ renderResult(toolResult: any, options: any, theme: any, context: any) {
307
+ if (options.isPartial) return partialResult(context, theme, "Searching…");
308
+ const output = textContent(toolResult);
309
+ const failed = completeStatus(context, output);
310
+ const matches = nonEmptyLines(output);
311
+ return result(theme, failed ? output.split("\n")[0] : `Found ${matches} paths`, output, options.expanded, failed);
312
+ },
313
+ } as any);
314
+
315
+ const ls = createLsTool(cwd);
316
+ pi.registerTool({
317
+ ...ls,
318
+ renderShell: "self",
319
+ renderCall(args: any, theme: any, context: any) {
320
+ return call(theme, "List", args.path ?? ".", context.state);
321
+ },
322
+ renderResult(toolResult: any, options: any, theme: any, context: any) {
323
+ if (options.isPartial) return partialResult(context, theme, "Listing…");
324
+ const output = textContent(toolResult);
325
+ const failed = completeStatus(context, output);
326
+ const entries = nonEmptyLines(output);
327
+ return result(theme, failed ? output.split("\n")[0] : `Listed ${entries} entries`, output, options.expanded, failed);
328
+ },
329
+ } as any);
330
+ }
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "pi-pretty-tui",
3
+ "version": "0.1.0",
4
+ "description": "Polished tool calls and Markdown rendering for the Pi coding agent TUI",
5
+ "license": "MIT",
6
+ "author": "Kainan Yang <ykn0309@whu.edu.cn>",
7
+ "type": "module",
8
+ "keywords": [
9
+ "pi-package",
10
+ "pi-extension",
11
+ "pi-coding-agent",
12
+ "tui",
13
+ "terminal-ui"
14
+ ],
15
+ "files": [
16
+ "extensions",
17
+ "README.md",
18
+ "LICENSE"
19
+ ],
20
+ "engines": {
21
+ "node": ">=20"
22
+ },
23
+ "peerDependencies": {
24
+ "@earendil-works/pi-coding-agent": "*",
25
+ "@earendil-works/pi-tui": "*"
26
+ },
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "pi": {
31
+ "extensions": [
32
+ "./extensions/pretty-tui.ts"
33
+ ]
34
+ }
35
+ }