@xynogen/pix-pretty 3.3.0 → 3.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xynogen/pix-pretty",
3
- "version": "3.3.0",
3
+ "version": "3.4.0",
4
4
  "description": "Enhanced tool output rendering with syntax highlighting, file icons, tree views, diff rendering, and FFF search",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -12,7 +12,7 @@ import * as Diff from "diff";
12
12
  import { BG_BASE, BOLD, FG_DIM, FG_GREEN, FG_LNUM, FG_RED, FG_RULE, RST } from "./ansi.ts";
13
13
  import { MAX_HL_CHARS, MAX_RENDER_LINES, WORD_DIFF_MIN_SIM } from "./config.ts";
14
14
  import type { DiffLine, ParsedDiff } from "./diff.ts";
15
- import { BODY_PAD } from "./frame.ts";
15
+ import { bodyLine } from "./frame.ts";
16
16
  import { hlBlock } from "./highlight.ts";
17
17
  import type { BundledLanguage, FgTheme } from "./types.ts";
18
18
  import { terminalWidth } from "./utils.ts";
@@ -669,7 +669,9 @@ export async function renderUnified(
669
669
 
670
670
  out.push(rule(tw));
671
671
  if (diff.lines.length > vis.length) {
672
- out.push(`${BG_BASE}${FG_DIM}${BODY_PAD}… ${diff.lines.length - vis.length} more lines${RST}`);
672
+ out.push(
673
+ `${BG_BASE}${bodyLine(`${FG_DIM}… ${diff.lines.length - vis.length} more lines${RST}`)}`,
674
+ );
673
675
  }
674
676
  return out.join("\n");
675
677
  }
@@ -851,7 +853,7 @@ export async function renderSplit(
851
853
 
852
854
  out.push(`${rule(half)}${FG_RULE}┊${RST}${rule(half)}`);
853
855
  if (rows.length > vis.length) {
854
- out.push(`${BG_BASE}${FG_DIM}${BODY_PAD}… ${rows.length - vis.length} more lines${RST}`);
856
+ out.push(`${BG_BASE}${bodyLine(`${FG_DIM}… ${rows.length - vis.length} more lines${RST}`)}`);
855
857
  }
856
858
  return out.join("\n");
857
859
  }
package/src/frame.ts CHANGED
@@ -1,4 +1,11 @@
1
- import { type Color, type Component, mixColors, Text, visibleWidth } from "@earendil-works/pi-tui";
1
+ import {
2
+ type Color,
3
+ type Component,
4
+ mixColors,
5
+ Text,
6
+ visibleWidth,
7
+ wrapTextWithAnsi,
8
+ } from "@earendil-works/pi-tui";
2
9
  import { FG_RULE, hasAnsi, RST } from "./ansi.ts";
3
10
  import { MAX_PREVIEW_LINES } from "./config.ts";
4
11
  import { fadeRowsOf, pluralize } from "./text.ts";
@@ -96,22 +103,139 @@ export function sectionRule(line: string, theme: MutedTheme, width: number): str
96
103
  }
97
104
 
98
105
  /**
99
- * Body indent: one tab. pi-tui renders `\t` as 3 cells, the title column after
100
- * the 2-cell status icon + space.
106
+ * The tool-result body column: 3 cells, the title column after the 2-cell status icon and a
107
+ * space. Spaces, not a tab, so `Text` rows and raw component rows land in the same column.
108
+ * Plugins never interpolate it: use `bodyLine`, `bodyText`, `bodyBlock`, `bodyRows`,
109
+ * `glyphBody` or `bodyColumn` (`scripts/deps.test.ts` enforces this).
101
110
  */
102
- export const BODY_PAD = "\t";
111
+ export const BODY_PAD = " ";
112
+
113
+ /** Room for content in the body column of a terminal-wide card. */
114
+ export const bodyWidth = (): number => Math.max(1, terminalWidth() - BODY_PAD.length);
103
115
 
104
116
  /**
105
- * One tool-result body line, aligned under the tool title. An `=== label ===` line
106
- * becomes a full-width section rule. Other lines go through `paint`.
117
+ * One tool-result body line, aligned under the tool title. With a theme, an `=== label ===`
118
+ * line becomes a full-width section rule; without one the line is kept as is. Other lines go
119
+ * through `paint`. Does not wrap: use it for rows that map 1:1 to source lines (fading
120
+ * previews, pre-styled renderer rows); otherwise use `bodyText`.
107
121
  */
108
122
  export function bodyLine(
109
123
  line: string,
110
- theme: MutedTheme,
124
+ theme?: MutedTheme,
111
125
  paint: (line: string) => string = (s) => s,
112
126
  ): string {
113
- // ponytail: 3 = pi-tui tab width. Use visibleWidth if BODY_PAD stops being one tab.
114
- return `${BODY_PAD}${sectionRule(line, theme, terminalWidth() - 3) ?? paint(line)}`;
127
+ const rule = theme && sectionRule(line, theme, bodyWidth());
128
+ return `${BODY_PAD}${rule ?? paint(line)}`;
129
+ }
130
+
131
+ /** Leading escape codes, then the row's own indent. */
132
+ const LEAD = /^((?:\x1b\[[0-9;]*m)*)([ \t]*)/;
133
+
134
+ /**
135
+ * How far continuation rows hang past a line's own indent: a fixed count, or a function of
136
+ * that indent's width (e.g. `(indent) => (indent ? 2 : 0)` hangs only nested lines).
137
+ */
138
+ export type Hang = number | ((indent: number) => number);
139
+
140
+ /**
141
+ * Wrap `text` to `width`. Each row keeps its own leading indent, so a nested row never
142
+ * wraps back to column 0; continuation rows hang `hang` cells deeper. A color opened on one
143
+ * line carries to the next (`theme.fg("error", "a\nb")` paints both rows).
144
+ */
145
+ export function wrapUnderIndent(text: string, width: number, hang: Hang = 0): string[] {
146
+ // An unbounded width splits on `\n` only; pi-tui re-opens the active codes on each line.
147
+ return wrapTextWithAnsi(text, Number.MAX_SAFE_INTEGER).flatMap((line) => {
148
+ const [, codes = "", pad = ""] = LEAD.exec(line) ?? [];
149
+ const rest = codes + line.slice(codes.length + pad.length);
150
+ const indent = visibleWidth(pad);
151
+ const deeper = pad + " ".repeat(typeof hang === "number" ? hang : hang(indent));
152
+ const room = Math.max(10, width - visibleWidth(deeper));
153
+ return wrapTextWithAnsi(rest, room).map((row, i) => (i === 0 ? pad : deeper) + row);
154
+ });
155
+ }
156
+
157
+ /**
158
+ * A whole tool-result body as a string, for `Text` components: every row in the body column,
159
+ * wrapped at terminal width under its own indent. A narrower render width re-wraps without
160
+ * the indent; for a body whose card can be narrower than the terminal use `bodyBlock`.
161
+ */
162
+ export function bodyText(text: string, theme: MutedTheme, hang: Hang = 0): string {
163
+ return wrapUnderIndent(text, bodyWidth(), hang)
164
+ .map((row) => (visibleWidth(row) ? bodyLine(row, theme) : row))
165
+ .join("\n");
166
+ }
167
+
168
+ type BodyComponent = { render(width: number): string[]; invalidate(): void };
169
+
170
+ /**
171
+ * Indent a component into the body column: it renders at the width minus the indent, and
172
+ * every row it returns is indented. Its own wrapping or clipping stays in the column.
173
+ */
174
+ export function bodyColumn(inner: {
175
+ render(width: number): readonly string[];
176
+ invalidate(): void;
177
+ }): BodyComponent {
178
+ return {
179
+ render: (width) =>
180
+ inner.render(Math.max(1, width - BODY_PAD.length)).map((row) => BODY_PAD + row),
181
+ invalidate: () => inner.invalidate(),
182
+ };
183
+ }
184
+
185
+ /**
186
+ * A whole tool-result body as a component: like `bodyText`, but it wraps at the render width,
187
+ * so it holds at any card width. `hang` indents continuation rows.
188
+ */
189
+ export function bodyBlock(text: string, hang: Hang = 0): BodyComponent {
190
+ return bodyRows(() => [text], hang);
191
+ }
192
+
193
+ /**
194
+ * A body built at render time from parts: plain content (put in the body column) or an
195
+ * already-indented `Body` (a `renderDimPreview`, a nested block). Nothing is indented twice
196
+ * and nothing wraps back to column 0. `build` gets the body width (the card width minus the
197
+ * body indent), the room a plain part has.
198
+ */
199
+ export function bodyRows(
200
+ build: (bodyWidth: number) => readonly (string | Body | false | null | undefined)[],
201
+ hang: Hang = 0,
202
+ ): BodyComponent {
203
+ return {
204
+ render: (width) =>
205
+ build(Math.max(1, width - BODY_PAD.length))
206
+ .flatMap((part) => {
207
+ if (!part && part !== "") return [];
208
+ // An indented part already sits in the column: re-wrap it under its own indent.
209
+ if (typeof part !== "string") return wrapUnderIndent(part.text, width, hang);
210
+ return wrapUnderIndent(part, width - BODY_PAD.length, hang).map((row) =>
211
+ visibleWidth(row) ? BODY_PAD + row : row,
212
+ );
213
+ })
214
+ // Full-width rows, like `Text`, so the card background fills the line.
215
+ .map((row) => row + " ".repeat(Math.max(0, width - visibleWidth(row)))),
216
+ invalidate() {},
217
+ };
218
+ }
219
+
220
+ /** Content that already sits in the body column, so `bodyRows` keeps it as is. */
221
+ export type Body = { readonly text: string };
222
+
223
+ /** Mark text that already carries the body indent (e.g. a `renderDimPreview`). */
224
+ export const indented = (text: string): Body => ({ text });
225
+
226
+ /**
227
+ * A body led by a status glyph: the glyph (padded to 2 cells) and a space take the first
228
+ * row's indent; every other row sits in the body column under it. `glyph` is painted.
229
+ */
230
+ export function glyphBody(glyph: string, text: string): BodyComponent {
231
+ const lead = `${glyph}${" ".repeat(Math.max(1, BODY_PAD.length - visibleWidth(glyph)))}`;
232
+ return {
233
+ render: (width) =>
234
+ wrapUnderIndent(text, width - BODY_PAD.length).map(
235
+ (row, i) => (i === 0 ? lead : BODY_PAD) + row,
236
+ ),
237
+ invalidate() {},
238
+ };
115
239
  }
116
240
 
117
241
  /** Renderer context slice that knows the user's expand key (Pi's `ToolRenderContext`). */
@@ -132,11 +256,24 @@ export function expandHint(theme: MutedTheme, ctx?: ExpandHintCtx): string {
132
256
  }
133
257
 
134
258
  /**
135
- * Muted `… N more lines (ctrl+o to expand)` footer for a truncated tool-result body.
136
- * Pass the renderer context so the hint shows the user's own expand key.
259
+ * Muted `… N more <noun>s (ctrl+o to expand)` footer text for a truncated body. Unindented:
260
+ * put it in the body with the rest of the content. `noun` carries its own qualifier
261
+ * (`"more line"`, `"earlier line"`). Pass the renderer context so the hint shows the user's
262
+ * own expand key.
137
263
  */
264
+ export function moreText(
265
+ count: number,
266
+ noun: string,
267
+ theme: MutedTheme,
268
+ ctx?: ExpandHintCtx,
269
+ plural?: string,
270
+ ): string {
271
+ return `${theme.fg("muted", `… ${pluralize(count, noun, plural)} `)}${expandHint(theme, ctx)}`;
272
+ }
273
+
274
+ /** `moreText` for lines, as a finished body row (for row-by-row previews). */
138
275
  export function moreLines(count: number, theme: MutedTheme, ctx?: ExpandHintCtx): string {
139
- return `${BODY_PAD}${theme.fg("muted", `… ${pluralize(count, "more line")} `)}${expandHint(theme, ctx)}`;
276
+ return `${BODY_PAD}${moreText(count, "more line", theme, ctx)}`;
140
277
  }
141
278
 
142
279
  /** Decorate completed tool output with status-colored default chrome. */
package/src/render-row.ts CHANGED
@@ -9,8 +9,8 @@ import {
9
9
  import { BG_BASE, BG_ERROR, BOLD, BOLD_OFF, hasAnsi } from "./ansi.ts";
10
10
  import { MAX_PREVIEW_LINES } from "./config.ts";
11
11
  import {
12
- BODY_PAD,
13
12
  bodyLine,
13
+ bodyText,
14
14
  type ExpandHintCtx,
15
15
  moreLines,
16
16
  type RulePaint,
@@ -21,8 +21,9 @@ import { icon } from "./icon-catalog.ts";
21
21
  import { fadeLine, fadePaint, fadeRowsOf, fadeText, normalizeLineEndings } from "./text.ts";
22
22
  import type { FgTheme } from "./types.ts";
23
23
  import { fillToolBackground, terminalWidth } from "./utils.ts";
24
+ /** A tool's error text as a result body: indented under the title, every row painted red. */
24
25
  export function renderToolError(error: string, theme: FgTheme): string {
25
- return fillToolBackground(theme.fg("error", error), BG_ERROR);
26
+ return fillToolBackground(bodyText(theme.fg("error", error), theme), BG_ERROR);
26
27
  }
27
28
 
28
29
  export type CollapsedToolStatus = "success" | "error" | "warning";
@@ -314,7 +315,7 @@ export function renderDimPreview(
314
315
  theme,
315
316
  maxLines,
316
317
  );
317
- const header = opts.header ? `${BODY_PAD}${theme.fg("dim", opts.header)}` : undefined;
318
+ const header = opts.header ? bodyLine(theme.fg("dim", opts.header), theme) : undefined;
318
319
  const overflow =
319
320
  lines.length > maxLines ? moreLines(lines.length - maxLines, theme, opts.ctx) : undefined;
320
321
 
package/src/renderers.ts CHANGED
@@ -4,7 +4,7 @@ import { prettySection } from "@xynogen/pix-runtime/sections";
4
4
 
5
5
  import { FG_DIM, FG_RULE, RST } from "./ansi.ts";
6
6
  import { MAX_PREVIEW_LINES } from "./config.ts";
7
- import { BODY_PAD, type ExpandHintCtx, expandHint, rule } from "./frame.ts";
7
+ import { bodyLine, bodyWidth, type ExpandHintCtx, expandHint, rule } from "./frame.ts";
8
8
  import { hlBlock } from "./highlight.ts";
9
9
  import { dirIcon, fileColor, fileIcon } from "./icons.ts";
10
10
  import { lang } from "./lang.ts";
@@ -73,11 +73,13 @@ export async function renderFileContent(
73
73
 
74
74
  out.push(rule(tw));
75
75
  if (total > maxLines) {
76
- const more = `${BODY_PAD}… ${pluralize(total - maxLines, "more line")} (${total} total) `;
76
+ const more = `… ${pluralize(total - maxLines, "more line")} (${total} total) `;
77
77
  out.push(
78
- theme
79
- ? `${theme.fg("muted", more)}${expandHint(theme, options.ctx)}`
80
- : `${FG_DIM}${more}${RST}`,
78
+ bodyLine(
79
+ theme
80
+ ? `${theme.fg("muted", more)}${expandHint(theme, options.ctx)}`
81
+ : `${FG_DIM}${more}${RST}`,
82
+ ),
81
83
  );
82
84
  }
83
85
  return out.join("\n");
@@ -108,16 +110,21 @@ export function renderBashOutput(
108
110
  return { summary: codeStr, body };
109
111
  }
110
112
 
111
- /** Render ls output using the configured style (grid or tree). */
113
+ /** Render ls output using the configured style (grid or tree), in the body column. */
112
114
  export function renderTree(
113
115
  text: string,
114
116
  basePath: string,
115
117
  theme?: FgTheme,
116
118
  ctx?: ExpandHintCtx,
117
119
  ): string {
118
- return config(prettySection).lsStyle === "tree"
119
- ? renderLsTree(text, basePath, theme, ctx)
120
- : renderLsGrid(text, basePath, theme, ctx);
120
+ const out =
121
+ config(prettySection).lsStyle === "tree"
122
+ ? renderLsTree(text, basePath, theme, ctx)
123
+ : renderLsGrid(text, basePath, theme, ctx);
124
+ return out
125
+ .split("\n")
126
+ .map((row) => bodyLine(row))
127
+ .join("\n");
121
128
  }
122
129
 
123
130
  /** Vertical tree view with connectors and icons. */
@@ -199,8 +206,8 @@ function renderLsGrid(
199
206
  cellWidths.push(visibleWidth(cell));
200
207
  }
201
208
 
202
- // Layout into columns that fit the terminal width
203
- const tw = terminalWidth();
209
+ // Layout into columns that fit the terminal width, minus the body indent renderTree adds.
210
+ const tw = bodyWidth();
204
211
  const GAP = 3; // spaces between columns
205
212
  const rows = layoutGrid(cells, cellWidths, tw, GAP);
206
213
 
package/src/shell-tool.ts CHANGED
@@ -13,7 +13,7 @@ import { type CollapseState, tickCollapse } from "@xynogen/pix-runtime/collapse"
13
13
  import { getErrorMessage } from "@xynogen/pix-runtime/errors";
14
14
  import { resolveBaseBackground } from "./ansi.ts";
15
15
  import { collapsedCommandRow, commandPreview } from "./command-preview.ts";
16
- import { BODY_PAD, bodyLine, frameToolResult, outputBodyRows, reuseText } from "./frame.ts";
16
+ import { bodyLine, bodyText, frameToolResult, outputBodyRows, reuseText } from "./frame.ts";
17
17
  import {
18
18
  dotJoin,
19
19
  hideCollapsedToolCall,
@@ -196,7 +196,7 @@ export function registerShellTool(
196
196
  theme,
197
197
  );
198
198
  } else {
199
- text.setText(fillToolBackground(`${BODY_PAD}${theme.fg("muted", "done")}`));
199
+ text.setText(fillToolBackground(bodyLine(theme.fg("muted", "done"), theme)));
200
200
  }
201
201
  return text;
202
202
  }
@@ -238,7 +238,7 @@ export function registerShellTool(
238
238
  return text;
239
239
  }
240
240
  text.setText(
241
- fillToolBackground(`${BODY_PAD}${theme.fg("dim", String(fallbackText).slice(0, 120))}`),
241
+ fillToolBackground(bodyText(theme.fg("dim", String(fallbackText).slice(0, 120)), theme)),
242
242
  );
243
243
  return completed();
244
244
  },
@@ -355,7 +355,7 @@ export function renderGatedCommandResult(
355
355
 
356
356
  if (running) {
357
357
  text.setText(
358
- fillToolBackground(`${BODY_PAD}${theme.fg("muted", getTextContent(result) || "working")}`),
358
+ fillToolBackground(bodyText(theme.fg("muted", getTextContent(result) || "working"), theme)),
359
359
  );
360
360
  return text;
361
361
  }
@@ -365,7 +365,7 @@ export function renderGatedCommandResult(
365
365
  text.setText(
366
366
  details.outcome === "error"
367
367
  ? renderToolError(diagnostic, theme)
368
- : fillToolBackground(`${BODY_PAD}${theme.fg("warning", diagnostic)}`),
368
+ : fillToolBackground(bodyText(theme.fg("warning", diagnostic), theme)),
369
369
  );
370
370
  return isPartial ? text : completed(true);
371
371
  }
@@ -374,7 +374,7 @@ export function renderGatedCommandResult(
374
374
  const rendered = typeof details._render === "string" ? details._render : "";
375
375
  if (!rendered) {
376
376
  const { summary } = renderBashOutput(rendered, code, theme);
377
- text.setText(fillToolBackground(`${BODY_PAD}${summary}`));
377
+ text.setText(fillToolBackground(bodyLine(summary, theme)));
378
378
  return isPartial ? text : completed(details.outcome !== "success");
379
379
  }
380
380
 
package/src/tool-card.ts CHANGED
@@ -10,7 +10,7 @@
10
10
  import { Box, Markdown } from "@earendil-works/pi-tui";
11
11
  import { type CollapseState, tickCollapse } from "@xynogen/pix-runtime/collapse";
12
12
  import { resolveBaseBackground } from "./ansi.ts";
13
- import { BODY_PAD, bodyLine, frameToolResult, moreLines, reuseText } from "./frame.ts";
13
+ import { bodyLine, bodyText, frameToolResult, moreLines, reuseText } from "./frame.ts";
14
14
  import {
15
15
  type CollapsedToolStatus,
16
16
  hideCollapsedToolCall,
@@ -195,7 +195,10 @@ export function bodyCardRenderer<D = Record<string, unknown>, S extends RenderSt
195
195
 
196
196
  if (ctx.isError || !body.trim()) {
197
197
  text.setText(
198
- `${BODY_PAD}${ctx.isError ? theme.fg("error", body || "Error") : theme.fg("muted", "(empty)")}`,
198
+ bodyText(
199
+ ctx.isError ? theme.fg("error", body || "Error") : theme.fg("muted", "(empty)"),
200
+ theme,
201
+ ),
199
202
  );
200
203
  return isPartial ? text : frameToolResult(text, theme, ctx.isError);
201
204
  }