@xynogen/pix-pretty 1.35.0 → 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 +1 -1
- package/src/command-preview.ts +3 -17
- package/src/shell-tool.ts +7 -2
- package/src/text.ts +116 -3
- package/src/utils.ts +16 -31
package/package.json
CHANGED
package/src/command-preview.ts
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
mixColors,
|
|
3
|
-
Text,
|
|
4
|
-
truncateToWidth,
|
|
5
|
-
visibleWidth,
|
|
6
|
-
wrapTextWithAnsi,
|
|
7
|
-
} from "@earendil-works/pi-tui";
|
|
1
|
+
import { Text, truncateToWidth, visibleWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
|
|
8
2
|
import { hlBlock } from "./highlight.ts";
|
|
9
|
-
import { fadeText } from "./text.ts";
|
|
3
|
+
import { fadePaint, fadeText } from "./text.ts";
|
|
10
4
|
import type { ThemeLike } from "./types.ts";
|
|
11
5
|
import type { formatCollapsedToolRow } from "./utils.ts";
|
|
12
6
|
|
|
@@ -90,15 +84,7 @@ export function collapsedCommandRow(
|
|
|
90
84
|
const room = width - visibleWidth(prefix);
|
|
91
85
|
return [
|
|
92
86
|
room > 0
|
|
93
|
-
? `${prefix}${fadeText(plain, room, (
|
|
94
|
-
fade === 0
|
|
95
|
-
? theme.fg("dim", text)
|
|
96
|
-
: theme.colors && theme.style
|
|
97
|
-
? theme.style(text, {
|
|
98
|
-
fg: mixColors(theme.colors.dim, theme.colors.customMessageBg, fade),
|
|
99
|
-
})
|
|
100
|
-
: theme.fg("muted", text),
|
|
101
|
-
)}`
|
|
87
|
+
? `${prefix}${fadeText(plain, room, fadePaint(theme))}`
|
|
102
88
|
: truncateToWidth(prefix, width, "…"),
|
|
103
89
|
];
|
|
104
90
|
},
|
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
|
-
|
|
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
CHANGED
|
@@ -1,10 +1,23 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
mixColors,
|
|
3
|
+
sliceByColumn,
|
|
4
|
+
stripTerminalSequences,
|
|
5
|
+
truncateToWidth,
|
|
6
|
+
visibleWidth,
|
|
7
|
+
} from "@earendil-works/pi-tui";
|
|
2
8
|
|
|
3
9
|
const graphemes = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
4
10
|
|
|
5
11
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
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.
|
|
8
21
|
*/
|
|
9
22
|
export function fadeText(
|
|
10
23
|
text: string,
|
|
@@ -37,3 +50,103 @@ export function fadeText(
|
|
|
37
50
|
}
|
|
38
51
|
return output;
|
|
39
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
|
+
}
|
package/src/utils.ts
CHANGED
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
getCapabilities,
|
|
6
6
|
hyperlink,
|
|
7
7
|
MouseRegion,
|
|
8
|
-
mixColors,
|
|
8
|
+
type mixColors,
|
|
9
9
|
truncateToWidth,
|
|
10
10
|
visibleWidth,
|
|
11
11
|
} from "@earendil-works/pi-tui";
|
|
@@ -23,7 +23,7 @@ import {
|
|
|
23
23
|
} from "./ansi.ts";
|
|
24
24
|
import { MAX_PREVIEW_LINES } from "./config.ts";
|
|
25
25
|
import { icon } from "./icon-catalog.ts";
|
|
26
|
-
import { fadeText } from "./text.ts";
|
|
26
|
+
import { fadeLine, fadePaint, fadeRowsOf, fadeText } from "./text.ts";
|
|
27
27
|
import type {
|
|
28
28
|
FgTheme,
|
|
29
29
|
TextComponentCtor,
|
|
@@ -343,28 +343,17 @@ export function widgetActivityRow(
|
|
|
343
343
|
width: number,
|
|
344
344
|
stats = "",
|
|
345
345
|
): string {
|
|
346
|
+
const paint = fadePaint(theme);
|
|
346
347
|
if (stats) {
|
|
347
348
|
const room = width - visibleWidth(stats) - 2;
|
|
348
|
-
if (room < visibleWidth(prefix)) return
|
|
349
|
+
if (room < visibleWidth(prefix)) return fadeLine(`${prefix} ${stats}`, width, paint);
|
|
349
350
|
const body = widgetActivityRow(theme, prefix, activity, room);
|
|
350
351
|
return body + " ".repeat(width - visibleWidth(body) - visibleWidth(stats)) + stats;
|
|
351
352
|
}
|
|
352
353
|
const separator = theme.fg("muted", " · ");
|
|
353
354
|
const room = width - visibleWidth(prefix) - 3;
|
|
354
|
-
if (!activity || room < 1) return
|
|
355
|
-
return (
|
|
356
|
-
prefix +
|
|
357
|
-
separator +
|
|
358
|
-
fadeText(activity, room, (text, fade) =>
|
|
359
|
-
fade === 0
|
|
360
|
-
? theme.fg("dim", text)
|
|
361
|
-
: theme.colors && theme.style
|
|
362
|
-
? theme.style(text, {
|
|
363
|
-
fg: mixColors(theme.colors.dim, theme.colors.customMessageBg, fade),
|
|
364
|
-
})
|
|
365
|
-
: theme.fg("muted", text),
|
|
366
|
-
)
|
|
367
|
-
);
|
|
355
|
+
if (!activity || room < 1) return fadeLine(prefix, width, paint);
|
|
356
|
+
return prefix + separator + fadeText(activity, room, paint);
|
|
368
357
|
}
|
|
369
358
|
|
|
370
359
|
/** Use the host lifecycle fields so every call has the same status marker. */
|
|
@@ -411,17 +400,7 @@ export function formatCollapsedToolRow(
|
|
|
411
400
|
if (width === undefined)
|
|
412
401
|
return `${title}${target ? ` ${theme.fg("dim", target)}` : ""}${metadata}`;
|
|
413
402
|
const room = width - visibleWidth(title) - visibleWidth(metadata) - 1;
|
|
414
|
-
const fitted =
|
|
415
|
-
target && room > 0
|
|
416
|
-
? fadeText(target, room, (text, fade) => {
|
|
417
|
-
if (fade === 0) return theme.fg("dim", text);
|
|
418
|
-
return theme.colors && theme.style
|
|
419
|
-
? theme.style(text, {
|
|
420
|
-
fg: mixColors(theme.colors.dim, theme.colors.customMessageBg, fade),
|
|
421
|
-
})
|
|
422
|
-
: theme.fg("muted", text);
|
|
423
|
-
})
|
|
424
|
-
: "";
|
|
403
|
+
const fitted = target && room > 0 ? fadeText(target, room, fadePaint(theme)) : "";
|
|
425
404
|
return truncateToWidth(`${title}${fitted ? ` ${fitted}` : ""}${metadata}`, width, "…");
|
|
426
405
|
}
|
|
427
406
|
|
|
@@ -528,9 +507,15 @@ export function renderDimPreview(
|
|
|
528
507
|
const highlight = opts.highlight;
|
|
529
508
|
const output = normalizeLineEndings(text).trim() || "done";
|
|
530
509
|
const lines = output.split("\n");
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
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
|
+
);
|
|
534
519
|
const header = opts.header ? `${BODY_PAD}${theme.fg("dim", opts.header)}` : undefined;
|
|
535
520
|
const overflow = lines.length > maxLines ? moreLines(lines.length - maxLines, theme) : undefined;
|
|
536
521
|
|