@giladbarnea/pi-pretty-bash 1.0.13 → 1.0.14

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 (2) hide show
  1. package/index.ts +29 -10
  2. package/package.json +1 -1
package/index.ts CHANGED
@@ -19,10 +19,19 @@ formatter.setConfig({}, { dialect: "bash", useTabs: false, indentWidth: 2, binar
19
19
  const JSON_HIGHLIGHT_MAX_BYTES = 200 * 1024;
20
20
  const PREVIEW_ROWS = 10;
21
21
  const RESET = "\x1b[0m";
22
+ // Every theme color a formatted, highlighted command can contain, including Pi's highlightCode for heredoc bodies.
23
+ const COMMAND_COLORS = [
24
+ "syntaxFunction", "syntaxKeyword", "syntaxType", "syntaxString", "syntaxVariable", "syntaxOperator",
25
+ "syntaxComment", "syntaxNumber", "syntaxPunctuation", "text", "muted", "mdCodeBlock", "toolDiffAdded", "toolDiffRemoved",
26
+ ] as const;
27
+ // Pi registers its eager grammars at startup and all the others in one later step (loadAllHighlightLanguages),
28
+ // so whether one deferred grammar, diff, colors this probe tells which grammar set highlighted a heredoc body.
29
+ const GRAMMAR_PROBE = "-a\n+b";
22
30
 
23
31
  type BashArguments = { command?: string; timeout?: number };
24
32
  type Result = Parameters<NonNullable<ToolRenderers["renderResult"]>>[0];
25
- type RenderState = { startedAt?: number; endedAt?: number; timer?: ReturnType<typeof setInterval> };
33
+ type PreparedCommand = { input: string; colors: string; highlighter: BashHighlighter; command: string; sourceLines: string[]; highlighted: string[]; firstLineColored: string };
34
+ type RenderState = { startedAt?: number; endedAt?: number; timer?: ReturnType<typeof setInterval>; command?: PreparedCommand };
26
35
  type Context = Omit<Parameters<NonNullable<ToolRenderers["renderCall"]>>[2], "state"> & { state: RenderState };
27
36
  type ParsedOutput = { body: string; status: string };
28
37
 
@@ -97,20 +106,29 @@ function formatCommand(command: string): string {
97
106
  }
98
107
  }
99
108
 
100
- /** Draw the command, using a numbered box for multiline formatted input; collapsed boxes hide their middle rows.
101
- * @example commandRows({command: "echo hi"}, theme, false, false, highlighter.highlight).render(80) // A syntax-colored Bash header.
109
+ /** Format and highlight a command once per input, theme colors, grammar set and highlighter; Pi redraws past calls on every transcript-wide invalidation.
110
+ * @example preparedCommand(state, "echo hi", theme, highlighter).firstLineColored // A syntax-colored "echo hi".
102
111
  */
103
- function commandRows(args: BashArguments, theme: Theme, pending: boolean, expanded: boolean, highlightBash: BashHighlighter["highlight"]): Component {
112
+ function preparedCommand(state: RenderState, input: string, theme: Theme, highlighter: BashHighlighter): PreparedCommand {
113
+ const colors = COMMAND_COLORS.map(color => theme.fg(color, "")).join("") + highlightCode(GRAMMAR_PROBE, "diff").join("\n");
114
+ if (state.command?.input === input && state.command.colors === colors && state.command.highlighter === highlighter) return state.command;
104
115
  // The highlighter sees tabs, because only leading tabs may precede a <<- terminator;
105
116
  // the size cutoff still measures the displayed text.
106
- const source = sanitizedText(formatCommand(args.command ?? ""));
117
+ const source = sanitizedText(formatCommand(input));
107
118
  const command = expandTabs(source);
108
119
  const sourceLines = command.split("\n");
109
- const highlighted = (command.length > MAX_SOURCE_LENGTH ? command : expandTabs(highlightBash(source, theme))).split("\n");
110
- const firstLine = sourceLines[0].trim() || "…";
111
- const firstLineColored = highlightBash(firstLine, theme);
120
+ const highlighted = (command.length > MAX_SOURCE_LENGTH ? command : expandTabs(highlighter.highlight(source, theme))).split("\n");
121
+ const firstLineColored = highlighter.highlight(sourceLines[0].trim() || "…", theme);
122
+ state.command = { input, colors, highlighter, command, sourceLines, highlighted, firstLineColored };
123
+ return state.command;
124
+ }
125
+
126
+ /** Draw the command, using a numbered box for multiline formatted input; collapsed boxes hide their middle rows.
127
+ * @example commandRows(preparedCommand(state, "echo hi", theme, highlighter), undefined, theme, false, false).render(80) // A syntax-colored Bash header.
128
+ */
129
+ function commandRows({ command, sourceLines, highlighted, firstLineColored }: PreparedCommand, timeout: number | undefined, theme: Theme, pending: boolean, expanded: boolean): Component {
112
130
  const prefix = `${theme.fg(pending ? "warning" : "accent", "● ")}${theme.bold("Bash ")}`;
113
- const facts = commandFacts(command, args.timeout);
131
+ const facts = commandFacts(command, timeout);
114
132
  const suffix = facts.length ? theme.fg("dim", ` · ${facts.join(" · ")}`) : "";
115
133
  const candidates = [
116
134
  `${prefix}${firstLineColored}${suffix}`,
@@ -210,7 +228,8 @@ export default async function prettyBash(pi: ExtensionAPI): Promise<void> {
210
228
  active.add(state);
211
229
  }
212
230
  if (!context.isPartial) finish(state);
213
- return commandRows((args ?? {}) as BashArguments, theme, context.executionStarted && context.isPartial && state.endedAt === undefined, context.expanded, highlighter.highlight);
231
+ const { command, timeout } = (args ?? {}) as BashArguments;
232
+ return commandRows(preparedCommand(state, command ?? "", theme, highlighter), timeout, theme, context.executionStarted && context.isPartial && state.endedAt === undefined, context.expanded);
214
233
  },
215
234
  renderResult(result: Result, options, theme: Theme, context: Context) {
216
235
  const output = result.content.filter(part => part.type === "text").map(part => part.text).join("\n");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@giladbarnea/pi-pretty-bash",
3
- "version": "1.0.13",
3
+ "version": "1.0.14",
4
4
  "type": "module",
5
5
  "description": "Rich Bash command and result displays with syntax-colored commands and JSON output.",
6
6
  "keywords": [