@xynogen/pix-pretty 1.34.1 → 1.36.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": "1.34.1",
3
+ "version": "1.36.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",
@@ -22,6 +22,7 @@
22
22
  "./fff": "./src/fff.ts",
23
23
  "./types": "./src/types.ts",
24
24
  "./utils": "./src/utils.ts",
25
+ "./text": "./src/text.ts",
25
26
  "./widget-format": "./src/widget-format.ts",
26
27
  "./resize": "./src/resize.ts",
27
28
  "./context": "./src/tools/context.ts",
@@ -69,7 +70,7 @@
69
70
  "access": "public"
70
71
  },
71
72
  "dependencies": {
72
- "@xynogen/pix-runtime": "^0.15.0",
73
+ "@xynogen/pix-runtime": "^0.16.0",
73
74
  "chalk": "^4.1.2",
74
75
  "cli-highlight": "^2.1.11",
75
76
  "@ff-labs/fff-node": "^0.10.6",
@@ -1,6 +1,8 @@
1
1
  import { Text, truncateToWidth, visibleWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
2
2
  import { hlBlock } from "./highlight.ts";
3
+ import { fadePaint, fadeText } from "./text.ts";
3
4
  import type { ThemeLike } from "./types.ts";
5
+ import type { formatCollapsedToolRow } from "./utils.ts";
4
6
 
5
7
  export function commandPreview(
6
8
  header: string,
@@ -60,8 +62,15 @@ export function commandPreview(
60
62
  };
61
63
  }
62
64
 
63
- export function collapsedCommandRow(header: string, command: string, theme: Pick<ThemeLike, "fg">) {
64
- let text = `${header}${theme.fg("muted", " · ")}${theme.fg("dim", command.replace(/\s+/g, " ").trim())}`;
65
+ export function collapsedCommandRow(
66
+ header: string,
67
+ command: string,
68
+ theme: Pick<ThemeLike, "fg"> &
69
+ Pick<Parameters<typeof formatCollapsedToolRow>[0], "colors" | "style">,
70
+ ) {
71
+ const plain = command.replace(/\s+/g, " ").trim();
72
+ const prefix = `${header}${theme.fg("muted", " · ")}`;
73
+ let text = `${prefix}${theme.fg("dim", plain)}`;
65
74
  let replaced = false;
66
75
  return {
67
76
  setText(value: string) {
@@ -72,7 +81,12 @@ export function collapsedCommandRow(header: string, command: string, theme: Pick
72
81
  invalidate() {},
73
82
  render(width: number): string[] {
74
83
  if (replaced) return text ? new Text(text, 0, 0).render(width) : [];
75
- return [truncateToWidth(text, width, "…")];
84
+ const room = width - visibleWidth(prefix);
85
+ return [
86
+ room > 0
87
+ ? `${prefix}${fadeText(plain, room, fadePaint(theme))}`
88
+ : truncateToWidth(prefix, width, "…"),
89
+ ];
76
90
  },
77
91
  };
78
92
  }
package/src/shell-tool.ts CHANGED
@@ -14,6 +14,7 @@ import { resolveBaseBackground } from "./ansi.ts";
14
14
  import { collapsedCommandRow, commandPreview } from "./command-preview.ts";
15
15
  import { MAX_PREVIEW_LINES } from "./config.ts";
16
16
  import { renderBashOutput } from "./renderers.ts";
17
+ import { fadeRowsOf } from "./text.ts";
17
18
  import type { ToolContext } from "./tools/context.ts";
18
19
  import type {
19
20
  BashParams,
@@ -226,7 +227,6 @@ export function registerShellTool(
226
227
  }
227
228
 
228
229
  const maxShow = renderCtx.expanded ? lineCount : MAX_PREVIEW_LINES;
229
- const show = lines.slice(0, maxShow);
230
230
  const footer = lineCount > maxShow ? [moreLines(lineCount - maxShow, theme)] : [];
231
231
  // Every result (including single-line) is framed; the rules follow exit
232
232
  // status: green ok, red failure. The `✓ exit N` header is dropped — the
@@ -234,7 +234,12 @@ export function registerShellTool(
234
234
  const exitCode = d.exitCode as number | null;
235
235
  const statusKey = exitCode === null || exitCode === 0 ? "success" : "error";
236
236
  const paint = (s: string) => theme.fg(statusKey, s);
237
- const body = show.map((line) => bodyLine(line, theme));
237
+ // Fade the last preview rows instead of a hard cut before the "… N more" row.
238
+ const body = fadeRowsOf(
239
+ lines.slice(0, maxShow + 1).map((line) => bodyLine(line, theme)),
240
+ theme,
241
+ maxShow,
242
+ );
238
243
  const out = isPartial ? [...body, ...footer] : ruleFrame(body, footer, termW(), paint);
239
244
  text.setText(fillToolBackground(out.join("\n")));
240
245
  return text;
package/src/text.ts ADDED
@@ -0,0 +1,152 @@
1
+ import {
2
+ mixColors,
3
+ sliceByColumn,
4
+ stripTerminalSequences,
5
+ truncateToWidth,
6
+ visibleWidth,
7
+ } from "@earendil-works/pi-tui";
8
+
9
+ const graphemes = new Intl.Segmenter(undefined, { granularity: "grapheme" });
10
+
11
+ /**
12
+ * Horizontal fade for ONE plain line: fit `text` to `width` columns and fade its last
13
+ * `fadeColumns` columns instead of cutting with "…". Use it instead of `truncateToWidth`.
14
+ *
15
+ * fadeText("Write a Conventional Commit message", 20, fadePaint(theme))
16
+ * → "Write a Conventional" (20 columns; "ventional" gets fainter to the right)
17
+ *
18
+ * Input styling and control chars are stripped; `paint(text, fade)` restyles every
19
+ * piece (fade 0 = normal … 1 = background). Use {@link fadePaint} for the pix look.
20
+ * `paint` must not change width. Text that fits is painted with fade 0, uncut.
21
+ */
22
+ export function fadeText(
23
+ text: string,
24
+ width: number,
25
+ paint: (text: string, fade: number) => string,
26
+ fadeColumns = 8,
27
+ ): string {
28
+ if (!Number.isFinite(width) || width < 1) return "";
29
+ const columns = Math.floor(width);
30
+ const plain = stripTerminalSequences(text).replace(/[\x00-\x1f\x7f-\x9f]/g, " ");
31
+ if (visibleWidth(plain) <= columns) return paint(plain, 0);
32
+ const clipped = sliceByColumn(plain, 0, columns, true);
33
+ const end = visibleWidth(clipped);
34
+ const fadeWidth = Number.isFinite(fadeColumns) ? Math.max(0, Math.floor(fadeColumns)) : 8;
35
+ if (!fadeWidth) return paint(clipped, 0);
36
+ const start = Math.max(0, end - fadeWidth);
37
+ let column = 0;
38
+ let head = "";
39
+ let output = "";
40
+ for (const { segment } of graphemes.segment(clipped)) {
41
+ column += visibleWidth(segment);
42
+ if (column <= start) head += segment;
43
+ else {
44
+ if (head) {
45
+ output += paint(head, 0);
46
+ head = "";
47
+ }
48
+ output += paint(segment, (column - start) / (end - start));
49
+ }
50
+ }
51
+ return output;
52
+ }
53
+
54
+ /**
55
+ * Horizontal fade for ONE already-styled line. Like {@link fadeText}, but the head keeps
56
+ * its own colors: only the last `fadeColumns` columns are restyled through `paint`.
57
+ * A line that fits is returned unchanged. Widgets and modal rows use this.
58
+ */
59
+ export function fadeLine(
60
+ line: string,
61
+ width: number,
62
+ paint: (text: string, fade: number) => string,
63
+ fadeColumns = 8,
64
+ ): string {
65
+ if (!Number.isFinite(width) || width < 1) return "";
66
+ const columns = Math.floor(width);
67
+ if (visibleWidth(line) <= columns) return line;
68
+ const head = truncateToWidth(line, Math.max(0, columns - fadeColumns), "");
69
+ const headWidth = visibleWidth(head);
70
+ const rest = sliceByColumn(stripTerminalSequences(line), headWidth, columns - headWidth + 1);
71
+ return head + fadeText(rest, columns - headWidth, paint, fadeColumns);
72
+ }
73
+
74
+ /** Theme slice the fade helpers read; colors/style are optional so plain themes still work. */
75
+ export type FadeTheme = {
76
+ fg: (key: "dim" | "muted", text: string) => string;
77
+ colors?: {
78
+ dim: Parameters<typeof mixColors>[0];
79
+ customMessageBg: Parameters<typeof mixColors>[1];
80
+ };
81
+ style?: (text: string, style: { fg: ReturnType<typeof mixColors> }) => string;
82
+ };
83
+
84
+ /**
85
+ * The standard pix fade color, as a `paint` for {@link fadeText} / {@link fadeLine}.
86
+ * fade 0 is the `dim` role; higher values mix `dim` toward `customMessageBg`, so text
87
+ * sinks into the background. Themes without color data paint every faded cell `muted`.
88
+ */
89
+ export function fadePaint(theme: FadeTheme): (text: string, fade: number) => string {
90
+ return (text, fade) => {
91
+ if (fade === 0) return theme.fg("dim", text);
92
+ if (!theme.colors || !theme.style) return theme.fg("muted", text);
93
+ return theme.style(text, {
94
+ fg: mixColors(theme.colors.dim, theme.colors.customMessageBg, fade),
95
+ });
96
+ };
97
+ }
98
+
99
+ /**
100
+ * Vertical fade for a MULTI-line body: a collapsed tool-result preview. Keeps the first
101
+ * `maxLines` rows, fades the last `fadeRows` of them toward the background, then adds
102
+ * `hint(hidden)` (e.g. "ctrl+o +23 lines"). Use it instead of a hard cut + "… N more".
103
+ *
104
+ * row 1..5 unchanged
105
+ * row 6 faint ↑
106
+ * row 7 fainter │ fadeRows = 3
107
+ * row 8 faintest ↓ (stops at 85%, still readable)
108
+ * hint(22)
109
+ *
110
+ * Faded rows lose their own styling. A body with ≤ `maxLines` rows returns unchanged.
111
+ * Callers show the full body when the result is expanded.
112
+ */
113
+ export function fadeTextRow(
114
+ body: string,
115
+ theme: FadeTheme,
116
+ hint: (hidden: number) => string,
117
+ maxLines = 8,
118
+ fadeRows = 3,
119
+ ): string {
120
+ const lines = body.split("\n");
121
+ if (lines.length <= maxLines) return body;
122
+ return [...fadeRowsOf(lines, theme, maxLines, fadeRows), hint(lines.length - maxLines)].join(
123
+ "\n",
124
+ );
125
+ }
126
+
127
+ /**
128
+ * Array form of {@link fadeTextRow} for renderers that build `string[]` rows: the first
129
+ * `maxLines` rows, the last `fadeRows` of them fading. Returns all rows unchanged when
130
+ * they fit, so `fadeRowsOf(lines, theme, expanded ? Infinity : MAX)` replaces
131
+ * `lines.slice(0, max)`. The caller adds its own "… N more" / hint row.
132
+ */
133
+ export function fadeRowsOf(
134
+ lines: string[],
135
+ theme: FadeTheme,
136
+ maxLines: number,
137
+ fadeRows = 3,
138
+ ): string[] {
139
+ if (lines.length <= maxLines) return lines;
140
+ const paint = fadePaint(theme);
141
+ // At most a third of the preview fades, so short previews stay readable.
142
+ const rows = Math.min(fadeRows, Math.floor(maxLines / 3));
143
+ return lines.slice(0, maxLines).map((line, i) => {
144
+ const step = i - (maxLines - rows) + 1;
145
+ if (step <= 0) return line;
146
+ // Keep leading padding uncolored; stop at 0.85 so the last row stays faintly readable.
147
+ const plain = stripTerminalSequences(line);
148
+ const text = plain.trimStart();
149
+ if (!text) return plain;
150
+ return plain.slice(0, plain.length - text.length) + paint(text, (step / (rows + 1)) * 0.85);
151
+ });
152
+ }
@@ -1,12 +1,27 @@
1
- import { Text, truncateToWidth } from "@earendil-works/pi-tui";
2
- import { rule } from "./utils.ts";
1
+ import type { Theme } from "@earendil-works/pi-coding-agent";
2
+ import { stripTerminalSequences } from "@earendil-works/pi-tui";
3
+ import { icon } from "./icon-catalog.ts";
4
+ import { rule, widgetActivityRow, widgetTitle } from "./utils.ts";
3
5
 
4
6
  export const TRANSIENT_ERROR_TTL_MS = 30_000;
5
7
  const WIDGET_KEY = "pix-transient-error";
6
8
  const timers = new WeakMap<object, ReturnType<typeof setTimeout>>();
7
9
 
8
10
  type TransientMessageTheme = {
9
- fg(color: "error" | "warning" | "accent" | "muted" | "borderMuted", text: string): string;
11
+ fg(
12
+ color:
13
+ | "error"
14
+ | "warning"
15
+ | "accent"
16
+ | "success"
17
+ | "toolTitle"
18
+ | "dim"
19
+ | "muted"
20
+ | "borderMuted",
21
+ text: string,
22
+ ): string;
23
+ colors?: Pick<Theme["colors"], "dim" | "muted">;
24
+ style?: Theme["style"];
10
25
  };
11
26
 
12
27
  export type TransientMessageLevel = "error" | "warning" | "info";
@@ -36,18 +51,33 @@ export function showTransientMessage(
36
51
  ui.setWidget(
37
52
  WIDGET_KEY,
38
53
  (_tui, theme) => {
39
- const text = new Text("", 0, 0);
54
+ const detail = stripTerminalSequences(message)
55
+ .replace(/[\x00-\x1f\x7f-\x9f]/g, " ")
56
+ .replace(/\s+/g, " ")
57
+ .trim();
58
+ const fadeTheme = {
59
+ fg: theme.fg.bind(theme),
60
+ style: theme.style?.bind(theme),
61
+ colors: theme.colors && { dim: theme.colors.dim, customMessageBg: theme.colors.muted },
62
+ };
40
63
  return {
41
64
  render(width: number) {
65
+ if (width < 1) return [];
42
66
  const color = level === "info" ? "accent" : level;
43
- const prefix = theme.fg(color, `${level} `);
44
- text.setText(prefix + theme.fg("muted", message.replace(/\s+/g, " ").trim()));
67
+ const glyph = icon(
68
+ level === "error"
69
+ ? "status.error"
70
+ : level === "warning"
71
+ ? "status.warn"
72
+ : "status.active",
73
+ );
74
+ const prefix = widgetTitle(theme, glyph, "Runtime") + theme.fg(color, level);
45
75
  return [
46
76
  rule(width, (glyphs) => theme.fg("borderMuted", glyphs)),
47
- truncateToWidth(text.render(width)[0] ?? "", width),
77
+ widgetActivityRow(fadeTheme, prefix, detail, width),
48
78
  ];
49
79
  },
50
- invalidate: () => text.invalidate(),
80
+ invalidate() {},
51
81
  };
52
82
  },
53
83
  { placement: "aboveEditor" },
package/src/utils.ts CHANGED
@@ -5,6 +5,7 @@ import {
5
5
  getCapabilities,
6
6
  hyperlink,
7
7
  MouseRegion,
8
+ type mixColors,
8
9
  truncateToWidth,
9
10
  visibleWidth,
10
11
  } from "@earendil-works/pi-tui";
@@ -22,6 +23,7 @@ import {
22
23
  } from "./ansi.ts";
23
24
  import { MAX_PREVIEW_LINES } from "./config.ts";
24
25
  import { icon } from "./icon-catalog.ts";
26
+ import { fadeLine, fadePaint, fadeRowsOf, fadeText } from "./text.ts";
25
27
  import type {
26
28
  FgTheme,
27
29
  TextComponentCtor,
@@ -268,9 +270,15 @@ export type CollapsedToolStatus = "success" | "error" | "warning";
268
270
  * (`⚡` is reserved for strength / model-score badges, not warnings.)
269
271
  */
270
272
  export const COLLAPSED_TOOL_GLYPH: Record<CollapsedToolStatus, string> = {
271
- success: "✓",
272
- warning: "⚠",
273
- error: "✗",
273
+ get success() {
274
+ return icon("status.ok");
275
+ },
276
+ get warning() {
277
+ return icon("status.warn");
278
+ },
279
+ get error() {
280
+ return icon("status.error");
281
+ },
274
282
  };
275
283
 
276
284
  /**
@@ -292,6 +300,15 @@ export function padIcon(glyph: string, width = 2): string {
292
300
  return pad > 0 ? glyph + " ".repeat(pad) : glyph;
293
301
  }
294
302
 
303
+ /** Fixed icon/name columns align the divider across compact activity widgets. */
304
+ export function widgetTitle(
305
+ theme: { fg: (role: "toolTitle" | "muted", text: string) => string },
306
+ glyph: string,
307
+ name: string,
308
+ ): string {
309
+ return theme.fg("toolTitle", `${padIcon(glyph, 3)} ${name.padEnd(8)}`) + theme.fg("muted", " │ ");
310
+ }
311
+
295
312
  /**
296
313
  * Join non-empty parts with a middot separator: `dotJoin(["a", "", "b"])` → `"a · b"`.
297
314
  * Empty/nullish parts are dropped so callers can pass conditional pieces inline.
@@ -311,8 +328,34 @@ type CollapsedToolTheme = {
311
328
  text: string,
312
329
  ) => string;
313
330
  bold: (text: string) => string;
331
+ colors?: {
332
+ dim: Parameters<typeof mixColors>[0];
333
+ customMessageBg: Parameters<typeof mixColors>[1];
334
+ };
335
+ style?: (text: string, style: { fg: ReturnType<typeof mixColors> }) => string;
314
336
  };
315
337
 
338
+ /** Keep identity/stats fixed; only the trailing activity consumes spare columns. */
339
+ export function widgetActivityRow(
340
+ theme: Pick<CollapsedToolTheme, "fg" | "colors" | "style">,
341
+ prefix: string,
342
+ activity: string,
343
+ width: number,
344
+ stats = "",
345
+ ): string {
346
+ const paint = fadePaint(theme);
347
+ if (stats) {
348
+ const room = width - visibleWidth(stats) - 2;
349
+ if (room < visibleWidth(prefix)) return fadeLine(`${prefix} ${stats}`, width, paint);
350
+ const body = widgetActivityRow(theme, prefix, activity, room);
351
+ return body + " ".repeat(width - visibleWidth(body) - visibleWidth(stats)) + stats;
352
+ }
353
+ const separator = theme.fg("muted", " · ");
354
+ const room = width - visibleWidth(prefix) - 3;
355
+ if (!activity || room < 1) return fadeLine(prefix, width, paint);
356
+ return prefix + separator + fadeText(activity, room, paint);
357
+ }
358
+
316
359
  /** Use the host lifecycle fields so every call has the same status marker. */
317
360
  export function formatToolCallTitle(
318
361
  theme: {
@@ -347,16 +390,18 @@ export function formatCollapsedToolRow(
347
390
  target: string,
348
391
  meta = "",
349
392
  status: CollapsedToolStatus = "success",
393
+ width?: number,
350
394
  ): string {
351
395
  const marker = padIcon(
352
396
  icon(status === "success" ? "status.ok" : status === "error" ? "status.error" : "status.warn"),
353
397
  );
354
- const parts = [
355
- `${theme.fg(status, marker)} ${theme.fg("toolTitle", theme.bold(tool))}`,
356
- target ? theme.fg("dim", target) : "",
357
- meta ? `${theme.fg("muted", "·")} ${theme.fg("muted", meta)}` : "",
358
- ].filter(Boolean);
359
- return parts.join(" ");
398
+ const title = `${theme.fg(status, marker)} ${theme.fg("toolTitle", theme.bold(tool))}`;
399
+ const metadata = meta ? ` ${theme.fg("muted", "·")} ${theme.fg("muted", meta)}` : "";
400
+ if (width === undefined)
401
+ return `${title}${target ? ` ${theme.fg("dim", target)}` : ""}${metadata}`;
402
+ const room = width - visibleWidth(title) - visibleWidth(metadata) - 1;
403
+ const fitted = target && room > 0 ? fadeText(target, room, fadePaint(theme)) : "";
404
+ return truncateToWidth(`${title}${fitted ? ` ${fitted}` : ""}${metadata}`, width, "…");
360
405
  }
361
406
 
362
407
  /** Render shared one-row content for tools using the self-rendered shell. */
@@ -369,7 +414,7 @@ export function renderCollapsedToolRow(
369
414
  width?: number,
370
415
  ): string {
371
416
  return fillToolBackground(
372
- formatCollapsedToolRow(theme, tool, target, meta, status),
417
+ formatCollapsedToolRow(theme, tool, target, meta, status, width ?? termW()),
373
418
  BG_BASE,
374
419
  width,
375
420
  );
@@ -462,9 +507,15 @@ export function renderDimPreview(
462
507
  const highlight = opts.highlight;
463
508
  const output = normalizeLineEndings(text).trim() || "done";
464
509
  const lines = output.split("\n");
465
- const body = lines
466
- .slice(0, maxLines)
467
- .map((line) => bodyLine(line, theme, (l) => dimLineWithHighlight(l, theme, highlight)));
510
+ // Highlight only the preview rows (+1 so the fade still knows it was cut), then fade the
511
+ // last of them instead of a hard cut.
512
+ const body = fadeRowsOf(
513
+ lines
514
+ .slice(0, maxLines + 1)
515
+ .map((line) => bodyLine(line, theme, (l) => dimLineWithHighlight(l, theme, highlight))),
516
+ theme,
517
+ maxLines,
518
+ );
468
519
  const header = opts.header ? `${BODY_PAD}${theme.fg("dim", opts.header)}` : undefined;
469
520
  const overflow = lines.length > maxLines ? moreLines(lines.length - maxLines, theme) : undefined;
470
521