@xynogen/pix-pretty 1.37.0 → 1.37.2
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 +75 -36
package/package.json
CHANGED
package/src/command-preview.ts
CHANGED
|
@@ -14,54 +14,79 @@ export function commandPreview(
|
|
|
14
14
|
expanded = false,
|
|
15
15
|
/** Body column: under the tool name (2-col status glyph + space). */
|
|
16
16
|
indent = 3,
|
|
17
|
-
/** Renderer context:
|
|
18
|
-
ctx?: ExpandHintCtx,
|
|
17
|
+
/** Renderer context: expand key and whether streamed arguments are ready to highlight. */
|
|
18
|
+
ctx?: ExpandHintCtx & { executionStarted?: boolean },
|
|
19
19
|
) {
|
|
20
|
+
if (ctx?.executionStarted === false) language = undefined;
|
|
20
21
|
type Slot = { code: string; language: string | undefined; theme: typeof theme; styled: string };
|
|
21
22
|
let slot = state.commandPreview as Slot | undefined;
|
|
22
23
|
if (!slot || slot.code !== code || slot.language !== language || slot.theme !== theme) {
|
|
23
|
-
slot = { code, language, theme, styled: theme.fg("dim", code) };
|
|
24
|
+
slot = { code, language, theme, styled: language ? theme.fg("dim", code) : code };
|
|
24
25
|
state.commandPreview = slot;
|
|
25
26
|
const pending = slot;
|
|
26
|
-
|
|
27
|
-
(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
27
|
+
if (language)
|
|
28
|
+
void hlBlock(code, language, theme).then(
|
|
29
|
+
(lines) => {
|
|
30
|
+
if (state.commandPreview !== pending) return;
|
|
31
|
+
const styled = lines.join("\n");
|
|
32
|
+
if (pending.styled === styled) return;
|
|
33
|
+
pending.styled = styled;
|
|
34
|
+
invalidate();
|
|
35
|
+
},
|
|
36
|
+
() => {
|
|
37
|
+
/* Plain text remains available if highlighting fails. */
|
|
38
|
+
},
|
|
39
|
+
);
|
|
36
40
|
}
|
|
37
41
|
const current = slot;
|
|
38
42
|
let replacement: string | undefined;
|
|
43
|
+
const text = new Text("", 0, 0);
|
|
44
|
+
let memo:
|
|
45
|
+
| { width: number; styled: string; replacement: string | undefined; lines: string[] }
|
|
46
|
+
| undefined;
|
|
47
|
+
const layout = (width: number): string[] => {
|
|
48
|
+
if (replacement !== undefined) {
|
|
49
|
+
text.setText(replacement);
|
|
50
|
+
return replacement ? text.render(width) : [];
|
|
51
|
+
}
|
|
52
|
+
const separator = theme.fg("muted", " · ");
|
|
53
|
+
if (!code.includes("\n") && visibleWidth(header) + 3 + visibleWidth(code) <= width) {
|
|
54
|
+
text.setText(`${header}${separator}${current.styled}`);
|
|
55
|
+
return text.render(width);
|
|
56
|
+
}
|
|
57
|
+
// ponytail: preserve shell syntax with native wrapping; use a parser only for semantic formatting.
|
|
58
|
+
const padding = " ".repeat(indent);
|
|
59
|
+
const lines = wrapTextWithAnsi(current.styled, Math.max(1, width - indent));
|
|
60
|
+
const shown = expanded ? lines : fadeRowsOf(lines.slice(0, 17), theme, 16);
|
|
61
|
+
return [
|
|
62
|
+
truncateToWidth(`${header}${separator}`, width, "…"),
|
|
63
|
+
...shown.map((line) => `${padding}${line}`),
|
|
64
|
+
...(shown.length < lines.length
|
|
65
|
+
? [
|
|
66
|
+
`${theme.fg("muted", `${padding}… +${lines.length - shown.length} lines `)}${expandHint(theme, ctx)}`,
|
|
67
|
+
]
|
|
68
|
+
: []),
|
|
69
|
+
];
|
|
70
|
+
};
|
|
39
71
|
return {
|
|
40
72
|
setText(value: string) {
|
|
41
73
|
replacement = value;
|
|
42
74
|
},
|
|
43
75
|
getText: () => replacement ?? `${header}${theme.fg("muted", " · ")}${current.styled}`,
|
|
44
|
-
invalidate() {
|
|
76
|
+
invalidate() {
|
|
77
|
+
memo = undefined;
|
|
78
|
+
text.invalidate();
|
|
79
|
+
},
|
|
45
80
|
render(width: number): string[] {
|
|
46
|
-
if (
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const shown = expanded ? lines : fadeRowsOf(lines.slice(0, 17), theme, 16);
|
|
56
|
-
return [
|
|
57
|
-
truncateToWidth(`${header}${separator}`, width, "…"),
|
|
58
|
-
...shown.map((line) => `${padding}${line}`),
|
|
59
|
-
...(shown.length < lines.length
|
|
60
|
-
? [
|
|
61
|
-
`${theme.fg("muted", `${padding}… +${lines.length - shown.length} lines `)}${expandHint(theme, ctx)}`,
|
|
62
|
-
]
|
|
63
|
-
: []),
|
|
64
|
-
];
|
|
81
|
+
if (
|
|
82
|
+
memo?.width === width &&
|
|
83
|
+
memo.styled === current.styled &&
|
|
84
|
+
memo.replacement === replacement
|
|
85
|
+
)
|
|
86
|
+
return memo.lines;
|
|
87
|
+
const lines = layout(width);
|
|
88
|
+
memo = { width, styled: current.styled, replacement, lines };
|
|
89
|
+
return lines;
|
|
65
90
|
},
|
|
66
91
|
};
|
|
67
92
|
}
|
|
@@ -76,21 +101,35 @@ export function collapsedCommandRow(
|
|
|
76
101
|
const prefix = `${header}${theme.fg("muted", " · ")}`;
|
|
77
102
|
let text = `${prefix}${theme.fg("dim", plain)}`;
|
|
78
103
|
let replaced = false;
|
|
104
|
+
const replacementText = new Text("", 0, 0);
|
|
105
|
+
let memo: { width: number; text: string; replaced: boolean; lines: string[] } | undefined;
|
|
79
106
|
return {
|
|
80
107
|
setText(value: string) {
|
|
81
108
|
text = value;
|
|
82
109
|
replaced = true;
|
|
83
110
|
},
|
|
84
111
|
getText: () => text,
|
|
85
|
-
invalidate() {
|
|
112
|
+
invalidate() {
|
|
113
|
+
memo = undefined;
|
|
114
|
+
replacementText.invalidate();
|
|
115
|
+
},
|
|
86
116
|
render(width: number): string[] {
|
|
87
|
-
if (
|
|
117
|
+
if (memo?.width === width && memo.text === text && memo.replaced === replaced)
|
|
118
|
+
return memo.lines;
|
|
119
|
+
if (replaced) {
|
|
120
|
+
replacementText.setText(text);
|
|
121
|
+
const lines = text ? replacementText.render(width) : [];
|
|
122
|
+
memo = { width, text, replaced, lines };
|
|
123
|
+
return lines;
|
|
124
|
+
}
|
|
88
125
|
const room = width - visibleWidth(prefix);
|
|
89
|
-
|
|
126
|
+
const lines = [
|
|
90
127
|
room > 0
|
|
91
128
|
? `${prefix}${fadeText(plain, room, fadePaint(theme))}`
|
|
92
129
|
: truncateToWidth(prefix, width, "…"),
|
|
93
130
|
];
|
|
131
|
+
memo = { width, text, replaced, lines };
|
|
132
|
+
return lines;
|
|
94
133
|
},
|
|
95
134
|
};
|
|
96
135
|
}
|