@giladbarnea/pi-pretty-bash 1.0.0 → 1.0.2
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/README.md +5 -15
- package/index.ts +71 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
[](LICENSE)
|
|
10
10
|
[](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,20 +25,18 @@ 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
|
-
|
|
28
|
+
## Bash commands and JSON output get syntax colors
|
|
37
29
|
|
|
38
|
-
|
|
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
|
-
|
|
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
|
|
34
|
+
Pi still owns command execution, streaming output, timing, truncation, expansion, and error display.
|
|
43
35
|
|
|
44
36
|
## Compatibility
|
|
45
37
|
|
|
46
38
|
`pi-pretty-bash` requires Pi `0.83.0` or newer.
|
|
47
39
|
|
|
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.
|
|
49
|
-
|
|
50
40
|
## License
|
|
51
41
|
|
|
52
42
|
[MIT](LICENSE)
|
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,45 @@ 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
|
+
if (!hasChildren(component) || !component.children[0]) {
|
|
81
|
+
throw new Error("Pi's Bash result renderer no longer exposes its output component");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const outputComponent = component.children[0];
|
|
85
|
+
const renderOutput = outputComponent.render.bind(outputComponent);
|
|
86
|
+
const highlightedOutput = highlightCode(output, "json").join("\n");
|
|
87
|
+
const maxVisualLines = options.expanded ? Number.POSITIVE_INFINITY : BASH_PREVIEW_LINES;
|
|
88
|
+
|
|
89
|
+
outputComponent.render = (width) => {
|
|
90
|
+
const originalLines = renderOutput(width);
|
|
91
|
+
const highlightedLines = truncateToVisualLines(
|
|
92
|
+
highlightedOutput,
|
|
93
|
+
maxVisualLines,
|
|
94
|
+
width,
|
|
95
|
+
).visualLines;
|
|
96
|
+
const preservedLineCount = originalLines.length - highlightedLines.length;
|
|
97
|
+
|
|
98
|
+
if (preservedLineCount < 0) {
|
|
99
|
+
throw new Error("Pi's Bash result layout changed while highlighting JSON output");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return [...originalLines.slice(0, preservedLineCount), ...highlightedLines];
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
return component;
|
|
106
|
+
},
|
|
36
107
|
});
|
|
37
108
|
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@giladbarnea/pi-pretty-bash",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
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",
|