@giladbarnea/pi-pretty-bash 1.0.1 → 1.0.3

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 (3) hide show
  1. package/README.md +6 -16
  2. package/index.ts +73 -0
  3. package/package.json +9 -3
package/README.md CHANGED
@@ -9,7 +9,7 @@
9
9
  [![license](https://img.shields.io/github/license/giladbarnea/pi-pretty-bash)](LICENSE)
10
10
  [![TypeScript](https://img.shields.io/badge/TypeScript-3178C6?logo=typescript&logoColor=white)](https://www.typescriptlang.org/)
11
11
 
12
- `pi-pretty-bash` adds theme-aware syntax colors to Bash tool calls in Pi's TUI.
12
+ `pi-pretty-bash` adds theme-aware syntax colors to Bash tool calls and JSON output in Pi's TUI.
13
13
 
14
14
  </div>
15
15
 
@@ -17,14 +17,6 @@
17
17
  <img src="screenshots/1.png" alt="A syntax-highlighted Bash tool call in Pi" width="760">
18
18
  </p>
19
19
 
20
- ## Pi already had the right highlighter
21
-
22
- Pi highlights fenced Bash blocks in Markdown well. Its SDK exports that same highlighter as `highlightCode()`.
23
-
24
- This extension sends each Bash command through `highlightCode(command, "bash")`. The colors therefore match Pi's active theme and Markdown output.
25
-
26
- The extension does not ship another Bash grammar or import `highlight.js` directly.
27
-
28
20
  ## Install
29
21
 
30
22
  ```sh
@@ -33,19 +25,17 @@ pi install npm:@giladbarnea/pi-pretty-bash
33
25
 
34
26
  Start a new Pi session, or run `/reload` in the current session. Every later Bash tool call is highlighted automatically.
35
27
 
36
- Pi may report that the extension overrides the built-in `bash` tool. This warning is expected because tool renderers are attached to tool definitions.
28
+ ## Bash commands and JSON output get syntax colors
37
29
 
38
- ## Only the command display changes
30
+ The extension starts from Pi's complete built-in Bash definition. It highlights the visible command with Pi's Bash colors.
39
31
 
40
- The extension starts from Pi's complete built-in Bash definition. It calls Pi's original renderer, then replaces only the visible command text with highlighted text.
32
+ After a command finishes, the extension checks its complete output with `JSON.parse()`. Valid JSON gets Pi's JSON colors without reformatting. All other output keeps Pi's built-in rendering.
41
33
 
42
- Pi still owns command execution, streaming output, timing, truncation, expansion, error display, and result rendering.
34
+ Pi still owns command execution, streaming output, timing, truncation, expansion, and error display.
43
35
 
44
36
  ## Compatibility
45
37
 
46
- `pi-pretty-bash` requires Pi `0.83.0` or newer.
47
-
48
- Another extension that also replaces the built-in `bash` tool can conflict with this extension. In that case, the last registered Bash tool wins.
38
+ `pi-pretty-bash` requires Pi `0.99.2` or newer.
49
39
 
50
40
  ## License
51
41
 
package/index.ts CHANGED
@@ -1,16 +1,47 @@
1
1
  import {
2
2
  createBashToolDefinition,
3
3
  highlightCode,
4
+ truncateToVisualLines,
4
5
  type ExtensionAPI,
5
6
  } from "@earendil-works/pi-coding-agent";
6
7
 
8
+ const BASH_PREVIEW_LINES = 5;
9
+
10
+ interface RenderComponent {
11
+ render(width: number): string[];
12
+ }
13
+
14
+ interface ComponentContainer extends RenderComponent {
15
+ children: RenderComponent[];
16
+ }
17
+
18
+ function hasChildren(component: RenderComponent): component is ComponentContainer {
19
+ return "children" in component && Array.isArray(component.children);
20
+ }
21
+
22
+ /**
23
+ * @example isJson('{"enabled":true}') // true
24
+ */
25
+ function isJson(output: string): boolean {
26
+ try {
27
+ const parsed: unknown = JSON.parse(output);
28
+ return JSON.stringify(parsed) !== undefined;
29
+ } catch {
30
+ return false;
31
+ }
32
+ }
33
+
7
34
  export default function prettyBash(pi: ExtensionAPI): void {
8
35
  const bashTool = createBashToolDefinition(process.cwd());
9
36
  const renderBashCall = bashTool.renderCall;
37
+ const renderBashResult = bashTool.renderResult;
10
38
 
11
39
  if (!renderBashCall) {
12
40
  throw new Error("Pi's Bash tool has no call renderer");
13
41
  }
42
+ if (!renderBashResult) {
43
+ throw new Error("Pi's Bash tool has no result renderer");
44
+ }
14
45
 
15
46
  pi.registerTool({
16
47
  ...bashTool,
@@ -33,5 +64,47 @@ export default function prettyBash(pi: ExtensionAPI): void {
33
64
  component.setText(`${theme.fg("toolTitle", theme.bold("$"))} ${renderedCommand}${timeout}`);
34
65
  return component;
35
66
  },
67
+
68
+ renderResult(result, options, theme, context) {
69
+ const component = renderBashResult(result, options, theme, context);
70
+ const output = result.content
71
+ .map((content) => (content.type === "text" ? content.text : ""))
72
+ .join("\n")
73
+ .trim();
74
+ const shouldHighlight =
75
+ !options.isPartial && !result.details?.truncation?.truncated && isJson(output);
76
+
77
+ if (!shouldHighlight) {
78
+ return component;
79
+ }
80
+ // Pi renders expanded output as one component, and collapsed output as a spacer followed by its preview.
81
+ const outputIndex = options.expanded ? 0 : 1;
82
+ if (!hasChildren(component) || !component.children[outputIndex]) {
83
+ throw new Error("Pi's Bash result renderer no longer exposes its output component");
84
+ }
85
+
86
+ const outputComponent = component.children[outputIndex];
87
+ const renderOutput = outputComponent.render.bind(outputComponent);
88
+ const highlightedOutput = highlightCode(output, "json").join("\n");
89
+ const maxVisualLines = options.expanded ? Number.POSITIVE_INFINITY : BASH_PREVIEW_LINES;
90
+
91
+ outputComponent.render = (width) => {
92
+ const originalLines = renderOutput(width);
93
+ const highlightedLines = truncateToVisualLines(
94
+ highlightedOutput,
95
+ maxVisualLines,
96
+ width,
97
+ ).visualLines;
98
+ const preservedLineCount = originalLines.length - highlightedLines.length;
99
+
100
+ if (preservedLineCount < 0) {
101
+ throw new Error("Pi's Bash result layout changed while highlighting JSON output");
102
+ }
103
+
104
+ return [...originalLines.slice(0, preservedLineCount), ...highlightedLines];
105
+ };
106
+
107
+ return component;
108
+ },
36
109
  });
37
110
  }
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@giladbarnea/pi-pretty-bash",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "type": "module",
5
- "description": "Syntax-highlight Bash tool calls in Pi's TUI.",
5
+ "description": "Syntax-highlight Bash tool calls and JSON output in Pi's TUI.",
6
6
  "keywords": [
7
7
  "pi-package",
8
8
  "pi",
@@ -28,7 +28,7 @@
28
28
  "screenshots/1.png"
29
29
  ],
30
30
  "peerDependencies": {
31
- "@earendil-works/pi-coding-agent": ">=0.83.0"
31
+ "@earendil-works/pi-coding-agent": ">=0.99.2"
32
32
  },
33
33
  "publishConfig": {
34
34
  "access": "public"
@@ -37,5 +37,11 @@
37
37
  "extensions": [
38
38
  "./index.ts"
39
39
  ]
40
+ },
41
+ "devDependencies": {
42
+ "@earendil-works/pi-coding-agent": "1.1.0"
43
+ },
44
+ "scripts": {
45
+ "test": "node --test test/*.test.mjs"
40
46
  }
41
47
  }