@xynogen/pix-pretty 1.35.0 → 1.37.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/README.md +24 -6
- package/package.json +2 -2
- package/src/command-preview.ts +10 -20
- package/src/renderers.ts +56 -14
- package/src/shell-tool.ts +12 -4
- package/src/text.ts +120 -3
- package/src/types.ts +2 -0
- package/src/utils.ts +86 -44
package/README.md
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/xynogen/pix-mono/main/.github/assets/pix-logo.svg" alt="Pix" width="152" height="80">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
1
5
|
# pix-pretty
|
|
2
6
|
|
|
3
7
|
Rendering and formatting library for Pi Coding Agent with syntax highlighting, file icons, tree views, FFF search integration, and gate-dialog overlay.
|
|
4
8
|
|
|
5
9
|
## What it does
|
|
6
10
|
|
|
7
|
-
This package is a
|
|
11
|
+
This package is a library and a small extension that other pix packages
|
|
8
12
|
consume. It does not register user-facing tools itself — the tool renderers
|
|
9
13
|
(`pix-read`, `pix-bash`, `pix-ls`, `pix-find`, `pix-grep`, `pix-edit`,
|
|
10
14
|
`pix-write`) import from it. The extension entry point (`src/index.ts`) only
|
|
11
|
-
|
|
15
|
+
keeps above-editor widgets in a stable order (TUI sessions), clears the highlight
|
|
12
16
|
cache, seeds the icon mode from `pix.json`, and registers two FFF slash
|
|
13
17
|
commands (`/fff-health`, `/fff-rescan`) once `pix-grep` has brought the FFF
|
|
14
18
|
finder online. The `/pix` settings command lives in `pix-runtime`.
|
|
@@ -21,12 +25,16 @@ finder online. The `/pix` settings command lives in `pix-runtime`.
|
|
|
21
25
|
- **Tree views** — hierarchical directory display for ls
|
|
22
26
|
- **Diff rendering** — side-by-side split diff for edit/write
|
|
23
27
|
- **Bash exit summary** — colored status, line count, truncation notice
|
|
28
|
+
- **Truncated previews** — a cut preview fades out bottom-up (`./text`:
|
|
29
|
+
`fadeRowsOf`) and ends with one muted footer, `… N more lines (ctrl+o to expand)`
|
|
30
|
+
(`./utils`: `moreLines`, `expandHint`). The hint shows the user's own expand
|
|
31
|
+
key through Pi's `keyHint`. Expanded views show every row.
|
|
24
32
|
|
|
25
33
|
### Icon catalog (l10n-style)
|
|
26
34
|
|
|
27
35
|
Icons are treated like a localization catalog: packages never hardcode glyph
|
|
28
|
-
codepoints — they ask for a
|
|
29
|
-
against one global icon mode. Reskinning, or fixing a missing-glyph (
|
|
36
|
+
codepoints — they ask for a semantic role and the catalog resolves it
|
|
37
|
+
against one global icon mode. Reskinning, or fixing a missing-glyph ("tofu")
|
|
30
38
|
problem on terminals without a Nerd Font, becomes a one-file edit here.
|
|
31
39
|
|
|
32
40
|
- **`./icon-catalog`** — `icon(key)` resolves a semantic key (`"cwd"`,
|
|
@@ -61,12 +69,12 @@ The one source of truth for inline chip tags. Producers build tags with `chipTag
|
|
|
61
69
|
The model receives each tag verbatim. `registerChips(pi)` shows them as chips in the prompt
|
|
62
70
|
editor and in sent user messages. `pix-display` calls it. Add a new chip kind here, not in a feature package.
|
|
63
71
|
|
|
64
|
-
UI features that moved to [`pix-display`](
|
|
72
|
+
UI features that moved to [`pix-display`](https://www.npmjs.com/package/@xynogen/pix-display): reasoning-tag (`<think>`/`<thinking>`) → native
|
|
65
73
|
`thinking` content blocks.
|
|
66
74
|
|
|
67
75
|
## Configuration
|
|
68
76
|
|
|
69
|
-
Configuration is read from
|
|
77
|
+
Configuration is read from `~/.pi/agent/pix.json` (the unified config file owned by `@xynogen/pix-runtime/config`). The `pretty` section of that file sets the defaults for theme, icon mode, and preview lines. Environment variables still override `pix.json` values.
|
|
70
78
|
|
|
71
79
|
> **Note:** config and collapse helpers previously shipped with `pix-pretty`; they now live in `pix-runtime` (`@xynogen/pix-runtime/config` and `@xynogen/pix-runtime/collapse`). Update any direct imports.
|
|
72
80
|
|
|
@@ -76,6 +84,7 @@ Configuration is read from **`~/.pi/agent/pix.json`** (the unified config file o
|
|
|
76
84
|
{
|
|
77
85
|
"pretty": {
|
|
78
86
|
"icons": "nerd", // nerd | unicode | ascii
|
|
87
|
+
"lsStyle": "grid", // grid | tree (pix-ls layout)
|
|
79
88
|
"maxPreviewLines": 80,
|
|
80
89
|
"maxRenderLines": 150,
|
|
81
90
|
"maxRenderWidth": "65%", // modal frame width; percentage or columns
|
|
@@ -95,6 +104,7 @@ Syntax highlighting, diffs, and tool surfaces use the active Pi theme. Color ove
|
|
|
95
104
|
### Environment Variables (override `pix.json`)
|
|
96
105
|
|
|
97
106
|
- `PRETTY_MAX_HL_CHARS` — max characters to highlight (default: 80000)
|
|
107
|
+
- `PRETTY_MAX_HL_LINE_CHARS` — max characters per line to highlight (default: 2000; env only)
|
|
98
108
|
- `PRETTY_MAX_PREVIEW_LINES` — max lines in preview output
|
|
99
109
|
- `PRETTY_CACHE_LIMIT` — FFF cache size limit
|
|
100
110
|
- `PRETTY_ICONS` — default icon mode when none is persisted: `nerd` (default),
|
|
@@ -111,6 +121,7 @@ The package exposes its sub-modules via `exports`:
|
|
|
111
121
|
```
|
|
112
122
|
@xynogen/pix-pretty (default — extension entry)
|
|
113
123
|
@xynogen/pix-pretty/ansi
|
|
124
|
+
@xynogen/pix-pretty/batch (multi-target `paths` helpers and section caps)
|
|
114
125
|
@xynogen/pix-pretty/confirm
|
|
115
126
|
@xynogen/pix-pretty/progress
|
|
116
127
|
@xynogen/pix-pretty/config
|
|
@@ -125,12 +136,19 @@ The package exposes its sub-modules via `exports`:
|
|
|
125
136
|
@xynogen/pix-pretty/fff
|
|
126
137
|
@xynogen/pix-pretty/types
|
|
127
138
|
@xynogen/pix-pretty/utils
|
|
139
|
+
@xynogen/pix-pretty/text (text fade helpers for previews and widgets)
|
|
140
|
+
@xynogen/pix-pretty/widget-format
|
|
128
141
|
@xynogen/pix-pretty/resize
|
|
129
142
|
@xynogen/pix-pretty/context
|
|
130
143
|
@xynogen/pix-pretty/gate-overlay
|
|
131
144
|
@xynogen/pix-pretty/modal-frame
|
|
132
145
|
@xynogen/pix-pretty/provider-picker
|
|
133
146
|
@xynogen/pix-pretty/shell-tool (shared bash/powershell tool registrar + renderer)
|
|
147
|
+
@xynogen/pix-pretty/command-preview
|
|
148
|
+
@xynogen/pix-pretty/tool-status (binary download status + missing-binary warnings)
|
|
149
|
+
@xynogen/pix-pretty/test-utils (renderer test helpers)
|
|
150
|
+
@xynogen/pix-pretty/transient-error
|
|
151
|
+
@xynogen/pix-pretty/grammar-consent
|
|
134
152
|
@xynogen/pix-pretty/chips (inline chip tags + editor/history chip renderer)
|
|
135
153
|
```
|
|
136
154
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xynogen/pix-pretty",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.37.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",
|
|
@@ -70,7 +70,7 @@
|
|
|
70
70
|
"access": "public"
|
|
71
71
|
},
|
|
72
72
|
"dependencies": {
|
|
73
|
-
"@xynogen/pix-runtime": "^0.
|
|
73
|
+
"@xynogen/pix-runtime": "^0.17.0",
|
|
74
74
|
"chalk": "^4.1.2",
|
|
75
75
|
"cli-highlight": "^2.1.11",
|
|
76
76
|
"@ff-labs/fff-node": "^0.10.6",
|
package/src/command-preview.ts
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
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, fadeRowsOf, fadeText } from "./text.ts";
|
|
10
4
|
import type { ThemeLike } from "./types.ts";
|
|
11
|
-
import type
|
|
5
|
+
import { type ExpandHintCtx, expandHint, type formatCollapsedToolRow } from "./utils.ts";
|
|
12
6
|
|
|
13
7
|
export function commandPreview(
|
|
14
8
|
header: string,
|
|
@@ -20,6 +14,8 @@ export function commandPreview(
|
|
|
20
14
|
expanded = false,
|
|
21
15
|
/** Body column: under the tool name (2-col status glyph + space). */
|
|
22
16
|
indent = 3,
|
|
17
|
+
/** Renderer context: the cut-off row shows the user's expand key. */
|
|
18
|
+
ctx?: ExpandHintCtx,
|
|
23
19
|
) {
|
|
24
20
|
type Slot = { code: string; language: string | undefined; theme: typeof theme; styled: string };
|
|
25
21
|
let slot = state.commandPreview as Slot | undefined;
|
|
@@ -56,12 +52,14 @@ export function commandPreview(
|
|
|
56
52
|
// ponytail: wrap without rewriting shell syntax or quoted strings. Add a shell parser only for semantic formatting.
|
|
57
53
|
const padding = " ".repeat(indent);
|
|
58
54
|
const lines = wrapTextWithAnsi(current.styled, Math.max(1, width - indent));
|
|
59
|
-
const shown = expanded ? lines : lines.slice(0, 16);
|
|
55
|
+
const shown = expanded ? lines : fadeRowsOf(lines.slice(0, 17), theme, 16);
|
|
60
56
|
return [
|
|
61
57
|
truncateToWidth(`${header}${separator}`, width, "…"),
|
|
62
58
|
...shown.map((line) => `${padding}${line}`),
|
|
63
59
|
...(shown.length < lines.length
|
|
64
|
-
? [
|
|
60
|
+
? [
|
|
61
|
+
`${theme.fg("muted", `${padding}… +${lines.length - shown.length} lines `)}${expandHint(theme, ctx)}`,
|
|
62
|
+
]
|
|
65
63
|
: []),
|
|
66
64
|
];
|
|
67
65
|
},
|
|
@@ -90,15 +88,7 @@ export function collapsedCommandRow(
|
|
|
90
88
|
const room = width - visibleWidth(prefix);
|
|
91
89
|
return [
|
|
92
90
|
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
|
-
)}`
|
|
91
|
+
? `${prefix}${fadeText(plain, room, fadePaint(theme))}`
|
|
102
92
|
: truncateToWidth(prefix, width, "…"),
|
|
103
93
|
];
|
|
104
94
|
},
|
package/src/renderers.ts
CHANGED
|
@@ -7,8 +7,18 @@ import { MAX_PREVIEW_LINES } from "./config.ts";
|
|
|
7
7
|
import { hlBlock } from "./highlight.ts";
|
|
8
8
|
import { dirIcon, fileColor, fileIcon } from "./icons.ts";
|
|
9
9
|
import { lang } from "./lang.ts";
|
|
10
|
+
import { type FadeTheme, fadeRowsOf } from "./text.ts";
|
|
10
11
|
import type { FgTheme } from "./types.ts";
|
|
11
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
BODY_PAD,
|
|
14
|
+
type ExpandHintCtx,
|
|
15
|
+
expandHint,
|
|
16
|
+
lnum,
|
|
17
|
+
normalizeLineEndings,
|
|
18
|
+
pluralize,
|
|
19
|
+
rule,
|
|
20
|
+
termW,
|
|
21
|
+
} from "./utils.ts";
|
|
12
22
|
|
|
13
23
|
/** Layout controls for {@link renderFileContent}. */
|
|
14
24
|
export interface RenderFileContentOptions {
|
|
@@ -20,6 +30,8 @@ export interface RenderFileContentOptions {
|
|
|
20
30
|
* expanded views pass `true` so no source text is hidden.
|
|
21
31
|
*/
|
|
22
32
|
wrapLongLines?: boolean;
|
|
33
|
+
/** Renderer context: the overflow row shows the user's expand key. */
|
|
34
|
+
ctx?: ExpandHintCtx;
|
|
23
35
|
}
|
|
24
36
|
|
|
25
37
|
/** Render syntax-highlighted file content with line numbers. */
|
|
@@ -66,10 +78,15 @@ export async function renderFileContent(
|
|
|
66
78
|
out.push(`${lnum(ln, nw)} ${FG_RULE}│${RST} ${display}${RST}`);
|
|
67
79
|
}
|
|
68
80
|
|
|
81
|
+
// A cut preview fades its last code rows (rule row 0 stays as is).
|
|
82
|
+
if (theme && total > maxLines) out.splice(1, Infinity, ...fadeTail(out.slice(1), theme));
|
|
69
83
|
out.push(rule(tw));
|
|
70
84
|
if (total > maxLines) {
|
|
85
|
+
const more = `${BODY_PAD}… ${pluralize(total - maxLines, "more line")} (${total} total) `;
|
|
71
86
|
out.push(
|
|
72
|
-
|
|
87
|
+
theme
|
|
88
|
+
? `${theme.fg("muted", more)}${expandHint(theme, options.ctx)}`
|
|
89
|
+
: `${FG_DIM}${more}${RST}`,
|
|
73
90
|
);
|
|
74
91
|
}
|
|
75
92
|
return out.join("\n");
|
|
@@ -101,10 +118,15 @@ export function renderBashOutput(
|
|
|
101
118
|
}
|
|
102
119
|
|
|
103
120
|
/** Render ls output using the configured style (grid or tree). */
|
|
104
|
-
export function renderTree(
|
|
121
|
+
export function renderTree(
|
|
122
|
+
text: string,
|
|
123
|
+
basePath: string,
|
|
124
|
+
theme?: FgTheme,
|
|
125
|
+
ctx?: ExpandHintCtx,
|
|
126
|
+
): string {
|
|
105
127
|
return config(prettySection).lsStyle === "tree"
|
|
106
|
-
? renderLsTree(text, basePath, theme)
|
|
107
|
-
: renderLsGrid(text, basePath, theme);
|
|
128
|
+
? renderLsTree(text, basePath, theme, ctx)
|
|
129
|
+
: renderLsGrid(text, basePath, theme, ctx);
|
|
108
130
|
}
|
|
109
131
|
|
|
110
132
|
/** Vertical tree view with connectors and icons. */
|
|
@@ -115,17 +137,23 @@ function entryColor(isDir: boolean, name: string, theme?: FgTheme): string {
|
|
|
115
137
|
return isDir ? theme.fg("accent", name) : fileColor(name, name, theme);
|
|
116
138
|
}
|
|
117
139
|
|
|
118
|
-
function renderLsTree(
|
|
140
|
+
function renderLsTree(
|
|
141
|
+
text: string,
|
|
142
|
+
_basePath: string,
|
|
143
|
+
theme?: FgTheme,
|
|
144
|
+
ctx?: ExpandHintCtx,
|
|
145
|
+
): string {
|
|
119
146
|
const lines = text.trim().split("\n").filter(Boolean);
|
|
120
147
|
if (!lines.length) return `${FG_DIM}(empty directory)${RST}`;
|
|
121
148
|
|
|
122
149
|
const out: string[] = [];
|
|
123
150
|
const total = lines.length;
|
|
124
|
-
const
|
|
151
|
+
const max = ctx?.expanded ? total : MAX_PREVIEW_LINES;
|
|
152
|
+
const show = lines.slice(0, max);
|
|
125
153
|
|
|
126
154
|
for (let i = 0; i < show.length; i++) {
|
|
127
155
|
const entry = (show[i] ?? "").trim();
|
|
128
|
-
const isLast = i === show.length - 1 && total <=
|
|
156
|
+
const isLast = i === show.length - 1 && total <= max;
|
|
129
157
|
const prefix = isLast ? "└── " : "├── ";
|
|
130
158
|
const connector = `${FG_RULE}${prefix}${RST}`;
|
|
131
159
|
|
|
@@ -137,22 +165,35 @@ function renderLsTree(text: string, _basePath: string, theme?: FgTheme): string
|
|
|
137
165
|
out.push(`${connector}${icon}${displayName}`);
|
|
138
166
|
}
|
|
139
167
|
|
|
140
|
-
if (total >
|
|
168
|
+
if (theme && total > max) out.splice(0, Infinity, ...fadeTail(out, theme));
|
|
169
|
+
if (total > max) {
|
|
141
170
|
out.push(
|
|
142
|
-
`${FG_RULE}└── ${RST}${FG_DIM}… ${pluralize(total -
|
|
171
|
+
`${FG_RULE}└── ${RST}${FG_DIM}… ${pluralize(total - max, "more entry", "more entries")} ${RST}${theme ? expandHint(theme, ctx) : ""}`,
|
|
143
172
|
);
|
|
144
173
|
}
|
|
145
174
|
|
|
146
175
|
return out.join("\n");
|
|
147
176
|
}
|
|
148
177
|
|
|
178
|
+
/** Fade the last text rows of an already-cut preview (blank rows take no step). */
|
|
179
|
+
function fadeTail(rows: string[], theme: FgTheme): string[] {
|
|
180
|
+
// One sentinel row past the end tells fadeRowsOf the preview was cut.
|
|
181
|
+
return fadeRowsOf([...rows, ""], theme as FadeTheme, rows.length);
|
|
182
|
+
}
|
|
183
|
+
|
|
149
184
|
/** Horizontal grid with icons (like eza/ls). */
|
|
150
|
-
function renderLsGrid(
|
|
185
|
+
function renderLsGrid(
|
|
186
|
+
text: string,
|
|
187
|
+
_basePath: string,
|
|
188
|
+
theme?: FgTheme,
|
|
189
|
+
ctx?: ExpandHintCtx,
|
|
190
|
+
): string {
|
|
151
191
|
const lines = text.trim().split("\n").filter(Boolean);
|
|
152
192
|
if (!lines.length) return `${FG_DIM}(empty directory)${RST}`;
|
|
153
193
|
|
|
154
194
|
const total = lines.length;
|
|
155
|
-
const
|
|
195
|
+
const max = ctx?.expanded ? total : MAX_PREVIEW_LINES;
|
|
196
|
+
const show = lines.slice(0, max);
|
|
156
197
|
|
|
157
198
|
// Build styled cells + measure their visible widths
|
|
158
199
|
const cells: string[] = [];
|
|
@@ -174,9 +215,10 @@ function renderLsGrid(text: string, _basePath: string, theme?: FgTheme): string
|
|
|
174
215
|
const GAP = 3; // spaces between columns
|
|
175
216
|
const rows = layoutGrid(cells, cellWidths, tw, GAP);
|
|
176
217
|
|
|
177
|
-
if (total >
|
|
218
|
+
if (theme && total > max) rows.splice(0, Infinity, ...fadeTail(rows, theme));
|
|
219
|
+
if (total > max) {
|
|
178
220
|
rows.push(
|
|
179
|
-
`${FG_DIM}… ${pluralize(total -
|
|
221
|
+
`${FG_DIM}… ${pluralize(total - max, "more entry", "more entries")} ${RST}${theme ? expandHint(theme, ctx) : ""}`,
|
|
180
222
|
);
|
|
181
223
|
}
|
|
182
224
|
|
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,
|
|
@@ -27,6 +28,7 @@ import {
|
|
|
27
28
|
BODY_PAD,
|
|
28
29
|
bodyLine,
|
|
29
30
|
dotJoin,
|
|
31
|
+
fadeRulePaint,
|
|
30
32
|
fillToolBackground,
|
|
31
33
|
formatCollapsedToolRow,
|
|
32
34
|
formatToolCallTitle,
|
|
@@ -162,6 +164,7 @@ export function registerShellTool(
|
|
|
162
164
|
renderCtx.invalidate,
|
|
163
165
|
renderCtx.expanded,
|
|
164
166
|
3,
|
|
167
|
+
renderCtx,
|
|
165
168
|
);
|
|
166
169
|
},
|
|
167
170
|
|
|
@@ -226,15 +229,20 @@ export function registerShellTool(
|
|
|
226
229
|
}
|
|
227
230
|
|
|
228
231
|
const maxShow = renderCtx.expanded ? lineCount : MAX_PREVIEW_LINES;
|
|
229
|
-
const
|
|
230
|
-
|
|
232
|
+
const footer =
|
|
233
|
+
lineCount > maxShow ? [moreLines(lineCount - maxShow, theme, renderCtx)] : [];
|
|
231
234
|
// Every result (including single-line) is framed; the rules follow exit
|
|
232
235
|
// status: green ok, red failure. The `✓ exit N` header is dropped — the
|
|
233
236
|
// collapsed row already carries status.
|
|
234
237
|
const exitCode = d.exitCode as number | null;
|
|
235
238
|
const statusKey = exitCode === null || exitCode === 0 ? "success" : "error";
|
|
236
|
-
const paint = (
|
|
237
|
-
|
|
239
|
+
const paint = fadeRulePaint(theme, statusKey);
|
|
240
|
+
// Fade the last preview rows instead of a hard cut before the "… N more" row.
|
|
241
|
+
const body = fadeRowsOf(
|
|
242
|
+
lines.slice(0, maxShow + 1).map((line) => bodyLine(line, theme)),
|
|
243
|
+
theme,
|
|
244
|
+
maxShow,
|
|
245
|
+
);
|
|
238
246
|
const out = isPartial ? [...body, ...footer] : ruleFrame(body, footer, termW(), paint);
|
|
239
247
|
text.setText(fillToolBackground(out.join("\n")));
|
|
240
248
|
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,107 @@ 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
|
+
* Blank rows never take a fade step: the steps go to the last rows with text.
|
|
134
|
+
*/
|
|
135
|
+
export function fadeRowsOf(
|
|
136
|
+
lines: string[],
|
|
137
|
+
theme: FadeTheme,
|
|
138
|
+
maxLines: number,
|
|
139
|
+
fadeRows = 3,
|
|
140
|
+
): string[] {
|
|
141
|
+
if (lines.length <= maxLines) return lines;
|
|
142
|
+
const paint = fadePaint(theme);
|
|
143
|
+
// At most a third of the preview fades, so short previews stay readable.
|
|
144
|
+
const rows = Math.min(fadeRows, Math.floor(maxLines / 3));
|
|
145
|
+
const shown = lines.slice(0, maxLines);
|
|
146
|
+
// Walk up from the bottom: the last text row is faintest, blank rows are skipped.
|
|
147
|
+
for (let i = shown.length - 1, step = rows; i >= 0 && step > 0; i--) {
|
|
148
|
+
const plain = stripTerminalSequences(shown[i] ?? "");
|
|
149
|
+
const text = plain.trimStart();
|
|
150
|
+
if (!text) continue;
|
|
151
|
+
// Keep leading padding uncolored; stop at 0.85 so the last row stays faintly readable.
|
|
152
|
+
shown[i] = plain.slice(0, plain.length - text.length) + paint(text, (step / (rows + 1)) * 0.85);
|
|
153
|
+
step--;
|
|
154
|
+
}
|
|
155
|
+
return shown;
|
|
156
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -74,6 +74,8 @@ export type RenderContextLike<
|
|
|
74
74
|
toolCallId?: string;
|
|
75
75
|
/** Host-recorded execution time of the final result (Pi >= 1.1). Undefined while partial. */
|
|
76
76
|
durationMs?: number;
|
|
77
|
+
/** Host-styled `<keys> <description>` for a keybinding (Pi >= 1.5). Use for the expand hint. */
|
|
78
|
+
keyHint?: (keybinding: "app.tools.expand", description: string) => string;
|
|
77
79
|
};
|
|
78
80
|
|
|
79
81
|
type SessionContextLike = ExtensionContext;
|
package/src/utils.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { isAbsolute, relative, resolve } from "node:path";
|
|
2
2
|
import { pathToFileURL } from "node:url";
|
|
3
3
|
import {
|
|
4
|
+
type Color,
|
|
4
5
|
type Component,
|
|
5
6
|
getCapabilities,
|
|
6
7
|
hyperlink,
|
|
@@ -23,7 +24,7 @@ import {
|
|
|
23
24
|
} from "./ansi.ts";
|
|
24
25
|
import { MAX_PREVIEW_LINES } from "./config.ts";
|
|
25
26
|
import { icon } from "./icon-catalog.ts";
|
|
26
|
-
import { fadeText } from "./text.ts";
|
|
27
|
+
import { fadeLine, fadePaint, fadeRowsOf, fadeText } from "./text.ts";
|
|
27
28
|
import type {
|
|
28
29
|
FgTheme,
|
|
29
30
|
TextComponentCtor,
|
|
@@ -90,9 +91,7 @@ type ResultComponent = ViewportComponent & {
|
|
|
90
91
|
};
|
|
91
92
|
|
|
92
93
|
type FramableComponent = Partial<ResultComponent> & Partial<TextComponentLike>;
|
|
93
|
-
type ResultFrameTheme =
|
|
94
|
-
fg: (key: "success" | "error", text: string) => string;
|
|
95
|
-
};
|
|
94
|
+
type ResultFrameTheme = StatusRuleTheme;
|
|
96
95
|
|
|
97
96
|
const RESULT_FRAME = Symbol("pix.resultFrame");
|
|
98
97
|
type FramedResultComponent = ResultComponent & {
|
|
@@ -343,28 +342,17 @@ export function widgetActivityRow(
|
|
|
343
342
|
width: number,
|
|
344
343
|
stats = "",
|
|
345
344
|
): string {
|
|
345
|
+
const paint = fadePaint(theme);
|
|
346
346
|
if (stats) {
|
|
347
347
|
const room = width - visibleWidth(stats) - 2;
|
|
348
|
-
if (room < visibleWidth(prefix)) return
|
|
348
|
+
if (room < visibleWidth(prefix)) return fadeLine(`${prefix} ${stats}`, width, paint);
|
|
349
349
|
const body = widgetActivityRow(theme, prefix, activity, room);
|
|
350
350
|
return body + " ".repeat(width - visibleWidth(body) - visibleWidth(stats)) + stats;
|
|
351
351
|
}
|
|
352
352
|
const separator = theme.fg("muted", " · ");
|
|
353
353
|
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
|
-
);
|
|
354
|
+
if (!activity || room < 1) return fadeLine(prefix, width, paint);
|
|
355
|
+
return prefix + separator + fadeText(activity, room, paint);
|
|
368
356
|
}
|
|
369
357
|
|
|
370
358
|
/** Use the host lifecycle fields so every call has the same status marker. */
|
|
@@ -411,17 +399,7 @@ export function formatCollapsedToolRow(
|
|
|
411
399
|
if (width === undefined)
|
|
412
400
|
return `${title}${target ? ` ${theme.fg("dim", target)}` : ""}${metadata}`;
|
|
413
401
|
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
|
-
: "";
|
|
402
|
+
const fitted = target && room > 0 ? fadeText(target, room, fadePaint(theme)) : "";
|
|
425
403
|
return truncateToWidth(`${title}${fitted ? ` ${fitted}` : ""}${metadata}`, width, "…");
|
|
426
404
|
}
|
|
427
405
|
|
|
@@ -466,6 +444,8 @@ export type DimPreviewOptions = {
|
|
|
466
444
|
frame?: boolean;
|
|
467
445
|
/** Tint the frame rules (green ok, red error) — same status color as bash/ls. */
|
|
468
446
|
paint?: RulePaint;
|
|
447
|
+
/** Renderer context: the overflow row shows the user's expand key. */
|
|
448
|
+
ctx?: ExpandHintCtx;
|
|
469
449
|
};
|
|
470
450
|
|
|
471
451
|
/** Compile a highlight pattern into a global RegExp, or null when it can't
|
|
@@ -528,11 +508,18 @@ export function renderDimPreview(
|
|
|
528
508
|
const highlight = opts.highlight;
|
|
529
509
|
const output = normalizeLineEndings(text).trim() || "done";
|
|
530
510
|
const lines = output.split("\n");
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
511
|
+
// Highlight only the preview rows (+1 so the fade still knows it was cut), then fade the
|
|
512
|
+
// last of them instead of a hard cut.
|
|
513
|
+
const body = fadeRowsOf(
|
|
514
|
+
lines
|
|
515
|
+
.slice(0, maxLines + 1)
|
|
516
|
+
.map((line) => bodyLine(line, theme, (l) => dimLineWithHighlight(l, theme, highlight))),
|
|
517
|
+
theme,
|
|
518
|
+
maxLines,
|
|
519
|
+
);
|
|
534
520
|
const header = opts.header ? `${BODY_PAD}${theme.fg("dim", opts.header)}` : undefined;
|
|
535
|
-
const overflow =
|
|
521
|
+
const overflow =
|
|
522
|
+
lines.length > maxLines ? moreLines(lines.length - maxLines, theme, opts.ctx) : undefined;
|
|
536
523
|
|
|
537
524
|
if (opts.frame) {
|
|
538
525
|
// One shape regardless of line count: a single framed box, no floating
|
|
@@ -634,14 +621,49 @@ export function linkedShortPath(cwd: string, home: string, p: string): string {
|
|
|
634
621
|
/** Paints a rule line. Callers pass `(s) => theme.fg(status, s)` so the frame
|
|
635
622
|
* tint follows the active theme (success/error/warning). Default = neutral FG_RULE. */
|
|
636
623
|
export type RulePaint = (glyphs: string) => string;
|
|
637
|
-
export type RuleStyle = "solid" | "dashed";
|
|
638
624
|
|
|
639
|
-
/** Render a full-width solid rule
|
|
640
|
-
export function rule(w: number, paint?: RulePaint
|
|
641
|
-
const glyphs =
|
|
625
|
+
/** Render a full-width solid rule. */
|
|
626
|
+
export function rule(w: number, paint?: RulePaint): string {
|
|
627
|
+
const glyphs = "─".repeat(w);
|
|
642
628
|
return paint ? paint(glyphs) : `${FG_RULE}${glyphs}${RST}`;
|
|
643
629
|
}
|
|
644
630
|
|
|
631
|
+
/** Theme slice for {@link fadeRulePaint}. Without `colors`/`style` the rule stays flat. */
|
|
632
|
+
export type StatusRuleTheme = {
|
|
633
|
+
fg: (key: "success" | "error", text: string) => string;
|
|
634
|
+
colors?: Readonly<Record<string, Color | undefined>>;
|
|
635
|
+
style?: (text: string, style: { fg: Color }) => string;
|
|
636
|
+
};
|
|
637
|
+
|
|
638
|
+
/**
|
|
639
|
+
* Status-colored rule paint that fades in on the left and out on the right: the last
|
|
640
|
+
* `fadeColumns` cells at each end mix toward the tool background. Themes without color
|
|
641
|
+
* data get the plain `theme.fg(role, …)` rule.
|
|
642
|
+
*/
|
|
643
|
+
export function fadeRulePaint(
|
|
644
|
+
theme: StatusRuleTheme,
|
|
645
|
+
role: "success" | "error",
|
|
646
|
+
fadeColumns = 8,
|
|
647
|
+
): RulePaint {
|
|
648
|
+
return (glyphs) => {
|
|
649
|
+
const cells = [...glyphs];
|
|
650
|
+
const edge = Math.min(fadeColumns, Math.floor(cells.length / 2));
|
|
651
|
+
const fg = theme.colors?.[role];
|
|
652
|
+
const bg = theme.colors?.[role === "error" ? "toolErrorBg" : "toolSuccessBg"];
|
|
653
|
+
if (!fg || !bg || !theme.style || edge < 1) return theme.fg(role, glyphs);
|
|
654
|
+
// Cell i from the edge (0 = outermost) is faintest at the edge, full color past `edge`.
|
|
655
|
+
// Call as a method: Pi's Theme.style reads `this`.
|
|
656
|
+
const cell = (ch: string, i: number) =>
|
|
657
|
+
theme.style?.(ch, { fg: mixColors(fg, bg, 1 - (i + 1) / (edge + 1)) }) ?? ch;
|
|
658
|
+
const left = cells.slice(0, edge).map(cell).join("");
|
|
659
|
+
const right = cells
|
|
660
|
+
.slice(-edge)
|
|
661
|
+
.map((ch, i) => cell(ch, edge - 1 - i))
|
|
662
|
+
.join("");
|
|
663
|
+
return left + theme.fg(role, cells.slice(edge, -edge).join("")) + right;
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
|
|
645
667
|
/** Narrow theme for helpers that paint only `muted`. Pi's Theme and FgTheme both fit. */
|
|
646
668
|
type MutedTheme = { fg: (key: "muted", text: string) => string };
|
|
647
669
|
|
|
@@ -712,9 +734,29 @@ export function bodyLine(
|
|
|
712
734
|
return `${BODY_PAD}${sectionRule(line, theme, termW() - 3) ?? paint(line)}`;
|
|
713
735
|
}
|
|
714
736
|
|
|
715
|
-
/**
|
|
716
|
-
export
|
|
717
|
-
|
|
737
|
+
/** Renderer context slice that knows the user's expand key (Pi's `ToolRenderContext`). */
|
|
738
|
+
export type ExpandHintCtx = {
|
|
739
|
+
keyHint?: (keybinding: "app.tools.expand", description: string) => string;
|
|
740
|
+
/** Expanded view: renderers that take this context show every row, no hint. */
|
|
741
|
+
expanded?: boolean;
|
|
742
|
+
};
|
|
743
|
+
|
|
744
|
+
/**
|
|
745
|
+
* `(ctrl+o to expand)` with the user's configured key, styled like Pi's own hint.
|
|
746
|
+
* Without a host `keyHint` (tests, old hosts), the key falls back to `ctrl+o`.
|
|
747
|
+
*/
|
|
748
|
+
export function expandHint(theme: MutedTheme, ctx?: ExpandHintCtx): string {
|
|
749
|
+
const hint =
|
|
750
|
+
ctx?.keyHint?.("app.tools.expand", "to expand") ?? theme.fg("muted", "ctrl+o to expand");
|
|
751
|
+
return `${theme.fg("muted", "(")}${hint}${theme.fg("muted", ")")}`;
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* Muted `… N more lines (ctrl+o to expand)` footer for a truncated tool-result body.
|
|
756
|
+
* Pass the renderer context so the hint shows the user's own expand key.
|
|
757
|
+
*/
|
|
758
|
+
export function moreLines(count: number, theme: MutedTheme, ctx?: ExpandHintCtx): string {
|
|
759
|
+
return `${BODY_PAD}${theme.fg("muted", `… ${pluralize(count, "more line")} `)}${expandHint(theme, ctx)}`;
|
|
718
760
|
}
|
|
719
761
|
|
|
720
762
|
/** Decorate completed tool output with status-colored default chrome. */
|
|
@@ -770,7 +812,7 @@ function decorateToolResult(
|
|
|
770
812
|
},
|
|
771
813
|
wantsKeyRelease: component.wantsKeyRelease,
|
|
772
814
|
render(width) {
|
|
773
|
-
const paint = (
|
|
815
|
+
const paint = fadeRulePaint(frameTheme, failed ? "error" : "success");
|
|
774
816
|
const lines = component.render?.(width) ?? [];
|
|
775
817
|
return solid ? sectionFrame(lines, [], width, paint) : ruleFrame(lines, [], width, paint);
|
|
776
818
|
},
|
|
@@ -791,7 +833,7 @@ export function unframeToolResult<T extends FramableComponent>(component: T): T
|
|
|
791
833
|
}
|
|
792
834
|
|
|
793
835
|
/**
|
|
794
|
-
* Default completed-tool chrome: unchanged body, full-width
|
|
836
|
+
* Default completed-tool chrome: unchanged body, full-width solid close, then
|
|
795
837
|
* footer lines. Existing callers inherit this distro-wide presentation.
|
|
796
838
|
*/
|
|
797
839
|
export function ruleFrame(
|
|
@@ -800,7 +842,7 @@ export function ruleFrame(
|
|
|
800
842
|
width?: number,
|
|
801
843
|
paint?: RulePaint,
|
|
802
844
|
): string[] {
|
|
803
|
-
return [...bodyLines, rule(width ?? termW(), paint
|
|
845
|
+
return [...bodyLines, rule(width ?? termW(), paint), ...footerLines];
|
|
804
846
|
}
|
|
805
847
|
|
|
806
848
|
/** Explicit solid top/bottom frame for sections that need stronger separation. */
|