@xynogen/pix-pretty 2.1.0 → 3.0.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/src/text.ts CHANGED
@@ -154,3 +154,27 @@ export function fadeRowsOf(
154
154
  }
155
155
  return shown;
156
156
  }
157
+
158
+ export function normalizeLineEndings(text: string): string {
159
+ return text.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
160
+ }
161
+
162
+ export function pluralize(count: number, noun: string, plural?: string): string {
163
+ return `${count} ${count === 1 ? noun : (plural ?? `${noun}s`)}`;
164
+ }
165
+
166
+ export function humanSize(bytes: number): string {
167
+ if (bytes < 1024) return `${bytes} B`;
168
+ if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KiB`;
169
+ if (bytes < 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MiB`;
170
+ return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)} GiB`;
171
+ }
172
+
173
+ export function appendNotices(text: string, notices: string[]): string {
174
+ return notices.length ? `${text}\n\n[${notices.join(". ")}]` : text;
175
+ }
176
+
177
+ export function trimToUndefined(value: string | undefined): string | undefined {
178
+ const trimmed = value?.trim();
179
+ return trimmed ? trimmed : undefined;
180
+ }
package/src/tool-card.ts CHANGED
@@ -7,8 +7,20 @@
7
7
  * text, and the body.
8
8
  */
9
9
 
10
+ import { Box, Markdown } from "@earendil-works/pi-tui";
10
11
  import { type CollapseState, tickCollapse } from "@xynogen/pix-runtime/collapse";
11
12
  import { resolveBaseBackground } from "./ansi.ts";
13
+ import { BODY_PAD, bodyLine, frameToolResult, moreLines, reuseText } from "./frame.ts";
14
+ import {
15
+ type CollapsedToolStatus,
16
+ hideCollapsedToolCall,
17
+ renderCollapsed,
18
+ renderRow,
19
+ renderToolError,
20
+ } from "./render-row.ts";
21
+ import { getTextContent } from "./result.ts";
22
+ import { fadeRowsOf } from "./text.ts";
23
+ import { getMarkdownTheme } from "./tui.ts";
12
24
  import type {
13
25
  RenderContextLike,
14
26
  TextComponentCtor,
@@ -16,14 +28,6 @@ import type {
16
28
  ThemeLike,
17
29
  ToolResultLike,
18
30
  } from "./types.ts";
19
- import {
20
- frameToolResult,
21
- getTextContent,
22
- hideCollapsedToolCall,
23
- renderCollapsed,
24
- renderToolError,
25
- reuseText,
26
- } from "./utils.ts";
27
31
 
28
32
  type RenderState = Record<string, string | undefined>;
29
33
 
@@ -109,3 +113,116 @@ export function toolCallRenderer<A, S extends RenderState = RenderState>(
109
113
  return text;
110
114
  };
111
115
  }
116
+
117
+ /** A tool whose result body is fetched/searched text: a preview while collapsed, all when expanded. */
118
+ export interface BodyCardSpec<D> {
119
+ /** Tool name: the collapse key and the default collapsed-row title. */
120
+ tool: string;
121
+ /** Collapsed-row title when it differs from `tool`. */
122
+ title?: string;
123
+ /** Collapsed-row target and summary; both default to empty. */
124
+ target?(d: D): string;
125
+ meta?(d: D): string;
126
+ status?(d: D): CollapsedToolStatus;
127
+ /** Preview budget in rows while collapsed; expanding shows everything. Default 16. */
128
+ maxPreviewLines?: number;
129
+ /** Render a successful body as Markdown (fades its tail) instead of dim text. */
130
+ markdown?: boolean;
131
+ }
132
+
133
+ /**
134
+ * `renderResult` for a text-body tool: error and empty states first, then the auto-collapsed
135
+ * row, then the body — dim text, or Markdown for `spec.markdown` — inside the result frame.
136
+ */
137
+ export function bodyCardRenderer<D = Record<string, unknown>, S extends RenderState = RenderState>(
138
+ TextComponent: TextComponentCtor,
139
+ spec: BodyCardSpec<D>,
140
+ ) {
141
+ const maxPreview = spec.maxPreviewLines ?? 16;
142
+ const dimBody = (
143
+ body: string,
144
+ theme: ThemeLike,
145
+ ctx: RenderContextLike<S>,
146
+ expanded: boolean,
147
+ ) => {
148
+ const lines = body.split("\n");
149
+ const shown = expanded ? lines.length : maxPreview;
150
+ const out = fadeRowsOf(
151
+ lines.slice(0, shown + 1).map((line) => bodyLine(line, theme, (l) => theme.fg("dim", l))),
152
+ theme,
153
+ shown,
154
+ );
155
+ const hidden = lines.length - shown;
156
+ if (hidden > 0) out.push(moreLines(hidden, theme, ctx));
157
+ return out.join("\n");
158
+ };
159
+ const markdownBody = (
160
+ body: string,
161
+ theme: ThemeLike,
162
+ ctx: RenderContextLike<S>,
163
+ expanded: boolean,
164
+ ) => {
165
+ const view = new Box(0, 0);
166
+ const page = new Markdown(body, 3, 0, getMarkdownTheme(), {
167
+ color: (value) => theme.fg("dim", value),
168
+ });
169
+ if (expanded) view.addChild(page);
170
+ // Fade the last preview rows of the rendered page instead of a hard cut.
171
+ else
172
+ view.addChild({
173
+ invalidate: () => page.invalidate(),
174
+ render: (width: number) => {
175
+ const rows = page.render(width);
176
+ const hidden = rows.length - maxPreview;
177
+ const shownRows = fadeRowsOf(rows, theme, maxPreview);
178
+ return hidden > 0 ? [...shownRows, moreLines(hidden, theme, ctx)] : shownRows;
179
+ },
180
+ });
181
+ return view;
182
+ };
183
+
184
+ return (
185
+ result: ToolResultLike,
186
+ options: { isPartial?: boolean; expanded?: boolean } | undefined,
187
+ theme: ThemeLike,
188
+ ctx: RenderContextLike<S>,
189
+ ) => {
190
+ resolveBaseBackground(theme);
191
+ const text = reuseText(ctx.lastComponent, TextComponent);
192
+ const isPartial = options?.isPartial === true;
193
+ const expanded = options?.expanded ?? ctx.expanded;
194
+ const body = getTextContent(result);
195
+
196
+ if (ctx.isError || !body.trim()) {
197
+ text.setText(
198
+ `${BODY_PAD}${ctx.isError ? theme.fg("error", body || "Error") : theme.fg("muted", "(empty)")}`,
199
+ );
200
+ return isPartial ? text : frameToolResult(text, theme, ctx.isError);
201
+ }
202
+
203
+ // SAFETY: the renderer receives the tool's own details; the spec types them as D.
204
+ const d = (result.details ?? undefined) as D | undefined;
205
+ if (
206
+ !isPartial &&
207
+ d &&
208
+ tickCollapse(spec.tool, ctx.state as CollapseState, ctx.invalidate, expanded)
209
+ ) {
210
+ text.setText(
211
+ renderRow(
212
+ theme,
213
+ spec.title ?? spec.tool,
214
+ spec.target?.(d) ?? "",
215
+ spec.meta?.(d) ?? "",
216
+ spec.status?.(d) ?? "success",
217
+ ),
218
+ );
219
+ return text;
220
+ }
221
+
222
+ const failed = Boolean(d && spec.status?.(d) === "error");
223
+ if (spec.markdown && !failed && !isPartial)
224
+ return frameToolResult(markdownBody(body, theme, ctx, expanded), theme, false);
225
+ text.setText(dimBody(body, theme, ctx, expanded));
226
+ return isPartial ? text : frameToolResult(text, theme, failed);
227
+ };
228
+ }
@@ -1,7 +1,8 @@
1
1
  import type { Theme } from "@earendil-works/pi-coding-agent";
2
2
  import { stripTerminalSequences } from "@earendil-works/pi-tui";
3
+ import { rule } from "./frame.ts";
3
4
  import { icon } from "./icon-catalog.ts";
4
- import { rule, widgetActivityRow, widgetTitle } from "./utils.ts";
5
+ import { widgetActivityRow, widgetTitle } from "./render-row.ts";
5
6
 
6
7
  export const TRANSIENT_ERROR_TTL_MS = 30_000;
7
8
  const WIDGET_KEY = "pix-transient-error";