@giladbarnea/pi-pretty-bash 1.0.7 → 1.0.9
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 +4 -2
- package/index.ts +32 -5
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
---
|
|
2
|
-
last_updated: 2026-11-
|
|
2
|
+
last_updated: 2026-11-13T22-20
|
|
3
3
|
---
|
|
4
4
|
|
|
5
5
|
<div align="center">
|
|
@@ -27,7 +27,9 @@ Alternatively, install from Git with `pi install git:github.com/giladbarnea/pi-p
|
|
|
27
27
|
|
|
28
28
|
## Rich Bash layout without replacing the Bash tool
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
Dense Bash gets readable spacing and indentation from [shfmt](https://github.com/mvdan/sh), bundled through `sh-syntax` with no system binary needed. Only the display is formatted. Execution and model context keep the original command. Unparseable input stays as written.
|
|
31
|
+
|
|
32
|
+
Short commands get syntax colors in the header. Multiline formatted commands get numbered boxes, and long commands wrap without losing their end. Inputs have a black background; output keeps the terminal background so the two stay distinct. Result boxes show status, elapsed time and line counts. Pi's tool expansion toggle governs both boxes. Collapsed, a command box over ten rows shows its first and last five, and results show ten visual tail rows; expansion shows the complete command and returned text.
|
|
31
33
|
|
|
32
34
|
The Bash palette preserves the former personal `bash-rich-highlight` choices, including function colors for built-ins. It uses `cli-highlight` with Pi theme colors, not the former HTML parser or hardcoded installation paths.
|
|
33
35
|
|
package/index.ts
CHANGED
|
@@ -10,10 +10,13 @@ import {
|
|
|
10
10
|
} from "@earendil-works/pi-coding-agent";
|
|
11
11
|
import { truncateToWidth, visibleWidth, wrapTextWithAnsi, type Component } from "@earendil-works/pi-tui";
|
|
12
12
|
import { highlight } from "cli-highlight";
|
|
13
|
+
import { LangVariant, ParseError, print } from "sh-syntax/sync";
|
|
13
14
|
|
|
14
15
|
const JSON_HIGHLIGHT_MAX_BYTES = 200 * 1024;
|
|
15
16
|
const PREVIEW_ROWS = 10;
|
|
16
17
|
const RESET = "\x1b[0m";
|
|
18
|
+
const BLACK_BACKGROUND = "\x1b[48;5;16m";
|
|
19
|
+
const SURFACE_TEXT = "\x1b[38;5;252m";
|
|
17
20
|
|
|
18
21
|
type BashArguments = { command?: string; timeout?: number };
|
|
19
22
|
type Result = Parameters<NonNullable<ToolRenderers["renderResult"]>>[0];
|
|
@@ -25,9 +28,11 @@ class BashDisplay implements Component {
|
|
|
25
28
|
private width?: number;
|
|
26
29
|
private lines?: string[];
|
|
27
30
|
private draw: (width: number) => string[];
|
|
31
|
+
private background: boolean;
|
|
28
32
|
|
|
29
|
-
constructor(draw: (width: number) => string[]) {
|
|
33
|
+
constructor(draw: (width: number) => string[], background = false) {
|
|
30
34
|
this.draw = draw;
|
|
35
|
+
this.background = background;
|
|
31
36
|
}
|
|
32
37
|
|
|
33
38
|
invalidate(): void {
|
|
@@ -37,11 +42,21 @@ class BashDisplay implements Component {
|
|
|
37
42
|
render(width: number): string[] {
|
|
38
43
|
if (this.lines && this.width === width) return this.lines;
|
|
39
44
|
this.width = width;
|
|
40
|
-
this.lines = this.draw(Math.max(1, width - 1)).map(line => truncateToWidth(line, width));
|
|
45
|
+
this.lines = this.draw(Math.max(1, width - 1)).map(line => this.background ? blackBackground(line, width) : truncateToWidth(line, width));
|
|
41
46
|
return this.lines;
|
|
42
47
|
}
|
|
43
48
|
}
|
|
44
49
|
|
|
50
|
+
/** Fill a terminal row with black, restoring its surface colors after nested styling resets.
|
|
51
|
+
* @example stripVTControlCharacters(blackBackground("git status", 12)) // "git status "
|
|
52
|
+
*/
|
|
53
|
+
function blackBackground(line: string, width: number): string {
|
|
54
|
+
const padded = truncateToWidth(line, width, "...", true);
|
|
55
|
+
const styled = padded.replace(/\x1b\[(?:0)?m/g, `$&${SURFACE_TEXT}${BLACK_BACKGROUND}`)
|
|
56
|
+
.replace(/\x1b\[(?:22;)?39m/g, `$&${SURFACE_TEXT}`).replaceAll("\x1b[49m", BLACK_BACKGROUND);
|
|
57
|
+
return `${SURFACE_TEXT}${BLACK_BACKGROUND}${styled}${RESET}`;
|
|
58
|
+
}
|
|
59
|
+
|
|
45
60
|
/** Apply the old Bash token palette without its hand-written HTML parser.
|
|
46
61
|
* @example bashColors('echo "$HOME"', theme) // Built-ins use syntaxFunction; variables use syntaxVariable.
|
|
47
62
|
*/
|
|
@@ -96,11 +111,23 @@ function box(rows: string[], width: number, theme: Theme, caption = ""): string[
|
|
|
96
111
|
];
|
|
97
112
|
}
|
|
98
113
|
|
|
99
|
-
/**
|
|
114
|
+
/** Format display-only Bash with shfmt, leaving incomplete or unsupported syntax as written.
|
|
115
|
+
* @example formatCommand("echo one;echo two") // "echo one\necho two"
|
|
116
|
+
*/
|
|
117
|
+
function formatCommand(command: string): string {
|
|
118
|
+
try {
|
|
119
|
+
return print(command, { variant: LangVariant.LangBash }).replace(/\n$/, "");
|
|
120
|
+
} catch (error) {
|
|
121
|
+
if (error instanceof ParseError) return command;
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Draw the command, using a numbered box for multiline formatted input; collapsed boxes hide their middle rows.
|
|
100
127
|
* @example commandRows({command: "echo hi"}, theme, false, false).render(80) // A syntax-colored Bash header.
|
|
101
128
|
*/
|
|
102
129
|
function commandRows(args: BashArguments, theme: Theme, pending: boolean, expanded: boolean): Component {
|
|
103
|
-
const command = displayText(args.command ?? "");
|
|
130
|
+
const command = displayText(formatCommand(args.command ?? ""));
|
|
104
131
|
const sourceLines = command.split("\n");
|
|
105
132
|
const highlighted = bashColors(command, theme).split("\n");
|
|
106
133
|
const firstLine = sourceLines[0].trim() || "…";
|
|
@@ -127,7 +154,7 @@ function commandRows(args: BashArguments, theme: Theme, pending: boolean, expand
|
|
|
127
154
|
if (expanded || rows.length <= PREVIEW_ROWS) return [header, ...box(rows, width, theme)];
|
|
128
155
|
const hint = wrapTextWithAnsi(`${theme.fg("muted", `… ${rows.length - PREVIEW_ROWS} more lines · `)}${keyHint("app.tools.expand", "to expand")}`, Math.max(1, width - 4));
|
|
129
156
|
return [header, ...box([...rows.slice(0, PREVIEW_ROWS / 2), ...hint, ...rows.slice(-PREVIEW_ROWS / 2)], width, theme)];
|
|
130
|
-
});
|
|
157
|
+
}, true);
|
|
131
158
|
}
|
|
132
159
|
|
|
133
160
|
/** Remove untrusted terminal instructions before applying renderer-owned styling.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@giladbarnea/pi-pretty-bash",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Rich Bash command and result displays with syntax-colored commands and JSON output.",
|
|
6
6
|
"keywords": [
|
|
@@ -46,6 +46,7 @@
|
|
|
46
46
|
"test": "node --test test/*.test.mjs"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"cli-highlight": "2.1.11"
|
|
49
|
+
"cli-highlight": "2.1.11",
|
|
50
|
+
"sh-syntax": "0.8.4"
|
|
50
51
|
}
|
|
51
52
|
}
|