@aeondave/pi-theme-1337 0.0.0-stage → 0.5.1
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/CHANGELOG.md +253 -0
- package/LICENSE +21 -0
- package/README.md +159 -2
- package/assets/banner.png +0 -0
- package/assets/palettes.gif +0 -0
- package/docs/architecture.md +167 -0
- package/docs/configuration.md +87 -0
- package/docs/palette-design.md +51 -0
- package/docs/theme-motion.md +88 -0
- package/package.json +68 -4
- package/src/avatar.ts +174 -0
- package/src/background.ts +82 -0
- package/src/config.ts +357 -0
- package/src/diff-spans.ts +77 -0
- package/src/editor.ts +443 -0
- package/src/format.ts +464 -0
- package/src/frame.ts +380 -0
- package/src/index.ts +1195 -0
- package/src/quota.ts +581 -0
- package/src/redraw-guard.ts +79 -0
- package/src/run-summary.ts +623 -0
- package/src/spinner.ts +72 -0
- package/src/terminal-title.ts +67 -0
- package/src/tool-activity.ts +216 -0
- package/src/tool-adapter.ts +491 -0
- package/src/tool-groups.ts +192 -0
- package/src/tool-lexicon.ts +200 -0
- package/src/tool-view.ts +788 -0
- package/src/width.ts +246 -0
- package/themes/1337-amber.json +94 -0
- package/themes/1337-amber.pi1337 +3 -0
- package/themes/1337-cosmos.json +94 -0
- package/themes/1337-cosmos.pi1337 +3 -0
- package/themes/1337-cyber.json +94 -0
- package/themes/1337-cyber.pi1337 +3 -0
- package/themes/1337-cyberpunk-2077-edgerunner.json +94 -0
- package/themes/1337-cyberpunk-2077-edgerunner.pi1337 +7 -0
- package/themes/1337-cyberpunk-2077.json +94 -0
- package/themes/1337-cyberpunk-2077.pi1337 +7 -0
- package/themes/1337-fallout.json +94 -0
- package/themes/1337-fallout.pi1337 +4 -0
- package/themes/1337-ice.json +94 -0
- package/themes/1337-ice.pi1337 +3 -0
- package/themes/1337-magma.json +94 -0
- package/themes/1337-magma.pi1337 +1 -0
- package/themes/1337-neon.json +94 -0
- package/themes/1337-neon.pi1337 +3 -0
- package/themes/1337-poison.json +94 -0
- package/themes/1337-poison.pi1337 +3 -0
- package/themes/1337-skynet-dark.json +94 -0
- package/themes/1337-skynet-dark.pi1337 +4 -0
- package/themes/1337-skynet.json +94 -0
- package/themes/1337-skynet.pi1337 +4 -0
- package/themes/1337.json +95 -0
- package/themes/1337.pi1337 +3 -0
package/src/frame.ts
ADDED
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure framing for the editor — a rounded, continuous panel.
|
|
3
|
+
*
|
|
4
|
+
* Pi's editor renders open-sided — a top `─` rule, the content lines (cursor
|
|
5
|
+
* inline), a bottom `─` rule, then any autocomplete rows. `frameEditor` takes
|
|
6
|
+
* that raw output, drops the two rules, and rebuilds a centered box around the
|
|
7
|
+
* editor content. Autocomplete stays below the box at Pi's original row indices.
|
|
8
|
+
* The border carries exactly one label, the model identity at the top-right;
|
|
9
|
+
* the rest of the chrome lives inside: the prompt sigil and spinner sit in the
|
|
10
|
+
* first row's side gutters, and a status row (where you are, left; the
|
|
11
|
+
* session's state, right) sits above the bottom edge when autocomplete is closed.
|
|
12
|
+
*
|
|
13
|
+
* ╭──────────────────────────── anthropic/opus-4-8 · high ──╮
|
|
14
|
+
* │ › refactor the auth module_ ⠹ │
|
|
15
|
+
* │ ~/…/pi-1337 · main 12.3k · 6% · ~$0.42 │
|
|
16
|
+
* ╰─────────────────────────────────────────────────────────╯
|
|
17
|
+
*
|
|
18
|
+
* No Pi imports — `string[]` in, `string[]` out — so it unit-tests with `tsx`.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { stripAnsi, truncateToWidth, visibleWidth } from "./width.ts";
|
|
22
|
+
|
|
23
|
+
export interface BorderStyle {
|
|
24
|
+
tl: string;
|
|
25
|
+
tr: string;
|
|
26
|
+
bl: string;
|
|
27
|
+
br: string;
|
|
28
|
+
h: string;
|
|
29
|
+
v: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Rounded single-line box — a soft, continuous outline. */
|
|
33
|
+
export const ROUNDED: BorderStyle = { tl: "╭", tr: "╮", bl: "╰", br: "╯", h: "─", v: "│" };
|
|
34
|
+
|
|
35
|
+
/** Columns reserved on each side of every body row for the chrome gutters. */
|
|
36
|
+
export const GUTTER = 2;
|
|
37
|
+
|
|
38
|
+
/** Rule cells the top label keeps on its left (at least) and its right (exactly). */
|
|
39
|
+
const LABEL_RULE = 2;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Width the editor must be rendered at to fit inside a `boxWidth` panel: the box
|
|
43
|
+
* spends two columns per side on the rails plus the chrome gutters. Single source
|
|
44
|
+
* of that arithmetic — the caller renders at this width and `frameEditor` fits
|
|
45
|
+
* every row to it, so the two can never disagree and pull the right rail off.
|
|
46
|
+
*/
|
|
47
|
+
export function editorWidthFor(boxWidth: number, spinnerColumns = 1): number {
|
|
48
|
+
// Keep one gap plus the active profile width in the right gutter at all times,
|
|
49
|
+
// including idle and waiting renders, so activity never changes cursor geometry.
|
|
50
|
+
return Math.max(0, boxWidth - 2 - GUTTER - 1 - Math.max(1, spinnerColumns));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Width reserved at the right of a two-row spinner's status row; one-row profiles reserve none. */
|
|
54
|
+
export function statusSlotWidth(spinnerColumns = 1, spinnerHeight = 1): number {
|
|
55
|
+
return spinnerHeight === 2 ? Math.max(1, spinnerColumns) + 1 : 0;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface FrameOptions {
|
|
59
|
+
/** Outer box width in columns (side glyphs included). */
|
|
60
|
+
boxWidth: number;
|
|
61
|
+
/** Spaces of left margin to center the box. */
|
|
62
|
+
leftMargin: number;
|
|
63
|
+
style: BorderStyle;
|
|
64
|
+
/** Colorizer for the border glyphs (corners, edges, side rails). */
|
|
65
|
+
border: (text: string) => string;
|
|
66
|
+
/**
|
|
67
|
+
* Pre-colored ladder (widest first) for the one label the top border carries,
|
|
68
|
+
* at its right end: `╭──… label ──╮`. No rung that fits → the plain rule.
|
|
69
|
+
*/
|
|
70
|
+
borderLabel?: RowCandidate[];
|
|
71
|
+
/** Pre-colored prompt sigil (1 col) placed in the first row's left gutter. */
|
|
72
|
+
prompt?: string;
|
|
73
|
+
/** Pre-colored first spinner row; empty when idle/waiting. */
|
|
74
|
+
spinner?: string;
|
|
75
|
+
/** Optional two-row animation; rows are rendered in the existing reserved gutter. */
|
|
76
|
+
spinnerRows?: readonly string[];
|
|
77
|
+
/** Reserve spinnerColumns + one gap at the status row for a two-row spinner, even while inactive. */
|
|
78
|
+
spinnerHeight?: number;
|
|
79
|
+
/** Fixed right-indicator width reserved even when idle (defaults to one cell). */
|
|
80
|
+
spinnerColumns?: number;
|
|
81
|
+
/** Pre-colored left group of the status row: a plain string, or a degradation ladder (widest first). */
|
|
82
|
+
statusLeft?: string | RowCandidate[];
|
|
83
|
+
/** Right group, right-aligned: a plain string, or a degradation ladder (widest candidate first). */
|
|
84
|
+
statusRight?: string | RowCandidate[];
|
|
85
|
+
/** Transient label painted over the left group's cells; never moves the right group (see layoutRow). */
|
|
86
|
+
statusOverlay?: RowCandidate[];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* One rung of a degradation ladder. `pre` and `post` are kept verbatim during
|
|
91
|
+
* normal narrowing; `flex` is the only part that trims before falling to the
|
|
92
|
+
* next rung. If no rung fits at all, the status row's final fallback may
|
|
93
|
+
* hard-truncate the last rung with a visible `…` (a last rung with `minFlex`
|
|
94
|
+
* is absent instead). All parts are pre-colored strings.
|
|
95
|
+
*/
|
|
96
|
+
export interface RowCandidate {
|
|
97
|
+
pre?: string;
|
|
98
|
+
flex?: string;
|
|
99
|
+
post?: string;
|
|
100
|
+
/**
|
|
101
|
+
* The narrowest `flex` may trim to, in columns. When set, the rung is shown
|
|
102
|
+
* whole, trimmed to no fewer than this, or skipped — as the last rung too,
|
|
103
|
+
* which otherwise trims down to one column. Unset: MIN_FLEX.
|
|
104
|
+
*/
|
|
105
|
+
minFlex?: number;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** A trimmed flex part narrower than this reads as noise — fall to the next rung instead. */
|
|
109
|
+
const MIN_FLEX = 5;
|
|
110
|
+
/** Don't preserve the right group by reducing a plain-string workspace label to a lone glyph. */
|
|
111
|
+
const MIN_LEFT_LABEL = 5;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* The widest rung that fits in `avail` columns, with `flex` trimmed down to
|
|
115
|
+
* MIN_FLEX (the last rung below it, to one column) or to the rung's own
|
|
116
|
+
* `minFlex`, which holds for the last rung too; `undefined` when none does.
|
|
117
|
+
*/
|
|
118
|
+
function fitSegment(candidates: RowCandidate[], avail: number): string | undefined {
|
|
119
|
+
if (avail <= 0) return undefined;
|
|
120
|
+
for (let i = 0; i < candidates.length; i++) {
|
|
121
|
+
const { pre = "", flex = "", post = "", minFlex } = candidates[i];
|
|
122
|
+
const full = pre + flex + post;
|
|
123
|
+
if (visibleWidth(full) <= avail) return full;
|
|
124
|
+
if (flex) {
|
|
125
|
+
const flexAvail = avail - visibleWidth(pre) - visibleWidth(post);
|
|
126
|
+
const isLast = i === candidates.length - 1;
|
|
127
|
+
if (minFlex !== undefined ? flexAvail >= minFlex : flexAvail >= MIN_FLEX || (isLast && flexAvail >= 1)) {
|
|
128
|
+
return pre + truncateToWidth(flex, flexAvail) + post;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Pick the widest ladder rung that fits in `avail` columns. A rung with a
|
|
137
|
+
* `flex` part may trim it with `…` down to MIN_FLEX columns (the last rung
|
|
138
|
+
* trims below the floor rather than vanish). If nothing fits, the last rung is
|
|
139
|
+
* hard-truncated so the row can never overflow — unless it declares a
|
|
140
|
+
* `minFlex`: that rung's contract is whole, trimmed to its floor, or absent,
|
|
141
|
+
* so below its floor the segment is empty rather than a stub of the label.
|
|
142
|
+
*/
|
|
143
|
+
export function resolveSegment(candidates: RowCandidate[], avail: number): string {
|
|
144
|
+
if (avail <= 0 || candidates.length === 0) return "";
|
|
145
|
+
const fitted = fitSegment(candidates, avail);
|
|
146
|
+
if (fitted !== undefined) return fitted;
|
|
147
|
+
const last = candidates[candidates.length - 1];
|
|
148
|
+
if (last.minFlex !== undefined) return "";
|
|
149
|
+
return truncateToWidth(`${last.pre ?? ""}${last.flex ?? ""}${last.post ?? ""}`, avail);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** True when a line is one of Pi's horizontal rules (so we can drop/locate it). */
|
|
153
|
+
export function isRuleLine(line: string): boolean {
|
|
154
|
+
const plain = stripAnsi(line);
|
|
155
|
+
// Pi's border starts at column zero. Editor content and autocomplete are
|
|
156
|
+
// indented (including unselected suggestions whose labels are all dashes).
|
|
157
|
+
if (!/^[─═]/.test(plain)) return false;
|
|
158
|
+
// At editor widths 4–5, Pi clips "─── ↓ N more" to one/two rule
|
|
159
|
+
// glyphs plus a three-dot ellipsis. Recognize that exact terminal shape
|
|
160
|
+
// before applying the ordinary minimum-rule-length heuristic.
|
|
161
|
+
if (/^[─═]+\.{3}$/.test(plain.trim())) return true;
|
|
162
|
+
const dashes = (plain.match(/[─═]/g) ?? []).length;
|
|
163
|
+
if (dashes < 3) return false;
|
|
164
|
+
const residue = plain.replace(/[─═↑↓\s\d]|more/g, "");
|
|
165
|
+
if (residue.length !== 0) return false;
|
|
166
|
+
// The editor's "↑/↓ N more" scroll indicator is a degraded rule with few dashes;
|
|
167
|
+
// recognize it explicitly so lastRuleIndex finds it instead of an autocomplete row.
|
|
168
|
+
if (/[↑↓]/.test(plain) && /more/.test(plain)) return true;
|
|
169
|
+
return dashes >= Math.floor(visibleWidth(line) * 0.4);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Index of the last rule line (the editor's bottom border); anything after is autocomplete. */
|
|
173
|
+
export function lastRuleIndex(lines: string[]): number {
|
|
174
|
+
for (let i = lines.length - 1; i >= 1; i--) {
|
|
175
|
+
if (isRuleLine(lines[i])) return i;
|
|
176
|
+
}
|
|
177
|
+
return lines.length - 1;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* `↑`/`↓` when one of Pi's rules is really its "N more" scroll indicator. Both
|
|
182
|
+
* rules are dropped when the box is rebuilt, so without this the fact that the
|
|
183
|
+
* buffer continues above or below the visible rows would be lost — the glyph is
|
|
184
|
+
* re-hung in the matching row's side gutter instead.
|
|
185
|
+
*/
|
|
186
|
+
export function scrollMarker(rule: string | undefined, glyph: "↑" | "↓"): string {
|
|
187
|
+
if (!rule) return "";
|
|
188
|
+
const plain = stripAnsi(rule);
|
|
189
|
+
return plain.includes(glyph) && plain.includes("more") ? glyph : "";
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* The top edge, `boxWidth` columns: `╭─…─╮`, or `╭─… label ──╮` when a rung of
|
|
194
|
+
* `label` fits with at least two rule cells on its left. The label is measured
|
|
195
|
+
* with the shared ruler and sits between single spaces; the rule pieces are
|
|
196
|
+
* colored on their own so the label keeps its colors. A rung that only trims
|
|
197
|
+
* to a lone `…`, or shows nothing visible, is noise, not a label: the rule
|
|
198
|
+
* stays plain then.
|
|
199
|
+
*/
|
|
200
|
+
function topEdge(style: BorderStyle, border: (text: string) => string, boxWidth: number, label: RowCandidate[] | undefined): string {
|
|
201
|
+
const interior = Math.max(0, boxWidth - 2);
|
|
202
|
+
const plain = border(style.tl + style.h.repeat(interior) + style.tr);
|
|
203
|
+
if (!label || label.length === 0) return plain;
|
|
204
|
+
const room = interior - LABEL_RULE - 2 - LABEL_RULE;
|
|
205
|
+
const shown = fitSegment(label, room);
|
|
206
|
+
// No visible content (escape-only, blank, zero-width) is no label either: never `╭── ──╮`.
|
|
207
|
+
if (shown === undefined) return plain;
|
|
208
|
+
const visible = stripAnsi(shown).trim();
|
|
209
|
+
if (!visible || visible === "…" || visibleWidth(visible) === 0) return plain;
|
|
210
|
+
const lead = interior - 2 - LABEL_RULE - visibleWidth(shown);
|
|
211
|
+
if (lead < LABEL_RULE) return plain;
|
|
212
|
+
const row = `${border(style.tl + style.h.repeat(lead))} ${shown} ${border(style.h.repeat(LABEL_RULE) + style.tr)}`;
|
|
213
|
+
// Defense-in-depth: a residual mismeasure must never change the box width.
|
|
214
|
+
return visibleWidth(row) === boxWidth ? row : plain;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Lay out `left` and `right` across exactly `width` columns using space fill:
|
|
219
|
+
* left-aligned left, right-aligned right. When the row is crowded, a long left
|
|
220
|
+
* segment yields first so a useful right rung can survive. A left ladder
|
|
221
|
+
* reserves its narrowest semantic label before the right is allocated, so the
|
|
222
|
+
* right keeps its widest rung that fits beside that floor and the left then
|
|
223
|
+
* spends what remains. Always returns `width` cols.
|
|
224
|
+
*
|
|
225
|
+
* `overlay` is a transient label (the running tool) that borrows the left
|
|
226
|
+
* segment's cells without moving anything else. The row is first laid out from
|
|
227
|
+
* left/right alone; then the cells before the right segment are repainted with
|
|
228
|
+
* the overlay, left-aligned and space-padded, resolved against those cells − 1
|
|
229
|
+
* so one space always separates it from what follows. Rungs are tried whole,
|
|
230
|
+
* widest first, and only the last rung may trim. An overlay that resolves to
|
|
231
|
+
* nothing — including one whose cells cannot hold its narrowest useful rung —
|
|
232
|
+
* leaves the row as it was. Guarantee: for any width the right segment occupies
|
|
233
|
+
* byte-identical columns with and without the overlay, and the row is exactly
|
|
234
|
+
* `width` columns — a label that comes and goes never reflows the row.
|
|
235
|
+
*/
|
|
236
|
+
export function layoutRow(
|
|
237
|
+
left: string | RowCandidate[],
|
|
238
|
+
right: string | RowCandidate[],
|
|
239
|
+
width: number,
|
|
240
|
+
overlay?: RowCandidate[],
|
|
241
|
+
): string {
|
|
242
|
+
if (width <= 0) return "";
|
|
243
|
+
const { l, r } = placeSegments(left, right, width);
|
|
244
|
+
let lead = `${l}${" ".repeat(Math.max(0, width - visibleWidth(l) - visibleWidth(r)))}`;
|
|
245
|
+
if (overlay && overlay.length > 0) {
|
|
246
|
+
const whole = ({ pre = "", flex = "", post = "" }: RowCandidate): string => pre + flex + post;
|
|
247
|
+
const region = visibleWidth(lead);
|
|
248
|
+
const rung = overlay.find(candidate => visibleWidth(whole(candidate)) <= region - 1);
|
|
249
|
+
// Below its narrowest useful rung the overlay would hard-truncate to a lone
|
|
250
|
+
// "…" in place of the workspace; keep the row as it was instead.
|
|
251
|
+
const avail = region - 1;
|
|
252
|
+
const shown = rung ? whole(rung)
|
|
253
|
+
: avail >= minimumUsefulWidth(overlay) ? resolveSegment([overlay[overlay.length - 1]], avail) : "";
|
|
254
|
+
const shownWidth = visibleWidth(shown);
|
|
255
|
+
if (shownWidth > 0) lead = `${shown}${" ".repeat(Math.max(0, region - shownWidth))}`;
|
|
256
|
+
}
|
|
257
|
+
const line = lead + r;
|
|
258
|
+
// Defense-in-depth: a residual mismeasure must never exceed `width` and wrap the terminal.
|
|
259
|
+
return visibleWidth(line) > width ? truncateToWidth(line, width) : line;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** Resolve the left and right segments for a `width`-column row with at least one column between them. */
|
|
263
|
+
function placeSegments(left: string | RowCandidate[], right: string | RowCandidate[], width: number): { l: string; r: string } {
|
|
264
|
+
const minGap = 1;
|
|
265
|
+
let l: string;
|
|
266
|
+
let r: string;
|
|
267
|
+
if (Array.isArray(left)) {
|
|
268
|
+
// A semantic left ladder (workspace path → repository basename) keeps its
|
|
269
|
+
// narrowest useful rung; the right takes its widest rung beside that floor,
|
|
270
|
+
// and the left then spends what remains on richer labels.
|
|
271
|
+
const rightRoom = Math.max(0, width - minimumUsefulWidth(left) - minGap);
|
|
272
|
+
r = Array.isArray(right) ? resolveSegment(right, rightRoom) : truncateToWidth(right, rightRoom);
|
|
273
|
+
l = resolveSegment(left, Math.max(0, width - visibleWidth(r) - minGap));
|
|
274
|
+
} else {
|
|
275
|
+
l = left;
|
|
276
|
+
const rightFloor = minimumUsefulWidth(right);
|
|
277
|
+
const leftWidth = visibleWidth(l);
|
|
278
|
+
const yieldLeft =
|
|
279
|
+
leftWidth > rightFloor &&
|
|
280
|
+
leftWidth + rightFloor + minGap > width &&
|
|
281
|
+
width - rightFloor - minGap >= MIN_LEFT_LABEL;
|
|
282
|
+
if (yieldLeft) {
|
|
283
|
+
// First keep the widest semantic right rung that fits; then abbreviate only
|
|
284
|
+
// the overlong left metadata to make room for it.
|
|
285
|
+
const rightRoom = width - MIN_LEFT_LABEL - minGap;
|
|
286
|
+
r = Array.isArray(right) ? resolveSegment(right, rightRoom) : truncateToWidth(right, rightRoom);
|
|
287
|
+
l = truncateToWidth(left, width - minGap - visibleWidth(r));
|
|
288
|
+
} else {
|
|
289
|
+
const rightWidth = Math.max(0, width - leftWidth - minGap);
|
|
290
|
+
r = Array.isArray(right) ? resolveSegment(right, rightWidth) : right;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
// Extreme widths only: clip the right segment to what the left leaves, then the
|
|
294
|
+
// left to what remains. The final clamp in layoutRow guarantees no overflow.
|
|
295
|
+
if (visibleWidth(l) + visibleWidth(r) + minGap > width) {
|
|
296
|
+
r = truncateToWidth(r, Math.max(0, width - visibleWidth(l) - minGap));
|
|
297
|
+
}
|
|
298
|
+
if (visibleWidth(l) + visibleWidth(r) + minGap > width) {
|
|
299
|
+
l = truncateToWidth(l, Math.max(0, width - visibleWidth(r) - minGap));
|
|
300
|
+
}
|
|
301
|
+
return { l, r };
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** Width needed to preserve the narrowest meaningful label of a segment. */
|
|
305
|
+
function minimumUsefulWidth(segment: string | RowCandidate[]): number {
|
|
306
|
+
if (!Array.isArray(segment)) return Math.min(MIN_FLEX, visibleWidth(segment));
|
|
307
|
+
const last = segment[segment.length - 1];
|
|
308
|
+
if (!last) return 0;
|
|
309
|
+
const { pre = "", flex = "", post = "", minFlex = MIN_FLEX } = last;
|
|
310
|
+
return visibleWidth(pre) + visibleWidth(post) + Math.min(minFlex, visibleWidth(flex));
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** Pad or truncate a line to exactly `width` columns so the right rail always aligns. */
|
|
314
|
+
function fit(line: string, width: number): string {
|
|
315
|
+
const w = visibleWidth(line);
|
|
316
|
+
if (w === width) return line;
|
|
317
|
+
return w < width ? line + " ".repeat(width - w) : truncateToWidth(line, width, "");
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
export function frameEditor(raw: string[], options: FrameOptions): string[] {
|
|
321
|
+
if (raw.length < 2) return raw;
|
|
322
|
+
// A two-rail frame cannot exist in fewer than two columns; the editor factory
|
|
323
|
+
// normally falls back before this point, but keep the pure seam non-overflowing.
|
|
324
|
+
if (options.boxWidth < 2 + GUTTER + 1 + Math.max(1, options.spinnerColumns ?? 1)) return [];
|
|
325
|
+
|
|
326
|
+
const bottomIdx = lastRuleIndex(raw);
|
|
327
|
+
const content = raw.slice(1, Math.max(1, bottomIdx)); // editor lines (rules dropped)
|
|
328
|
+
const below = raw.slice(bottomIdx + 1); // autocomplete rows stay outside the box
|
|
329
|
+
|
|
330
|
+
const { style, border, boxWidth, leftMargin } = options;
|
|
331
|
+
const pad = " ".repeat(Math.max(0, leftMargin));
|
|
332
|
+
const v = border(style.v);
|
|
333
|
+
const INT = Math.max(0, boxWidth - 2); // interior columns between the rails
|
|
334
|
+
const spinnerColumns = Math.max(1, options.spinnerColumns ?? 1);
|
|
335
|
+
const editorWidth = editorWidthFor(boxWidth, spinnerColumns); // width the editor was rendered at
|
|
336
|
+
|
|
337
|
+
const top = `${pad}${topEdge(style, border, boxWidth, options.borderLabel)}`;
|
|
338
|
+
const bottom = `${pad}${border(style.bl + style.h.repeat(INT) + style.br)}`;
|
|
339
|
+
|
|
340
|
+
const prompt = options.prompt ?? "";
|
|
341
|
+
const spinner = options.spinner ?? "";
|
|
342
|
+
const spinnerRows = options.spinnerRows;
|
|
343
|
+
const pyramid = (options.spinnerHeight ?? spinnerRows?.length ?? 1) === 2;
|
|
344
|
+
// Pi's rules double as scroll indicators; re-hang them on the rows they describe.
|
|
345
|
+
const up = scrollMarker(raw[0], "↑");
|
|
346
|
+
const down = scrollMarker(raw[bottomIdx], "↓");
|
|
347
|
+
const lastContent = content.length - 1;
|
|
348
|
+
|
|
349
|
+
const body = content.map((line, i) => {
|
|
350
|
+
// First row carries the prompt (left) and the spinner (right) in its gutters;
|
|
351
|
+
// the rest get blank gutters so every content column lines up beneath the
|
|
352
|
+
// first. The otherwise-dead right gutters carry the scroll markers.
|
|
353
|
+
const lg = i === 0 && prompt ? ` ${prompt}` : " ";
|
|
354
|
+
const logoRow = pyramid && spinnerRows && (i === 0 || i === 1) ? spinnerRows[i] : "";
|
|
355
|
+
const marker = i === 0 && spinner ? spinner : i === 0 && up ? border(up) : i === lastContent && down ? border(down) : "";
|
|
356
|
+
const gutterText = i === 0 && pyramid && spinner ? `${spinnerRows?.[0] ?? spinner} ` : i === 1 && logoRow ? `${logoRow}${down && i === lastContent ? border(down) : " "}` : marker ? `${marker}${" ".repeat(Math.max(0, spinnerColumns + 1 - visibleWidth(marker)))}` : " ".repeat(spinnerColumns + 1);
|
|
357
|
+
const rg = visibleWidth(gutterText) > spinnerColumns + 1 ? truncateToWidth(gutterText, spinnerColumns + 1, "") : gutterText + " ".repeat(Math.max(0, spinnerColumns + 1 - visibleWidth(gutterText)));
|
|
358
|
+
return `${pad}${v}${lg}${fit(line, editorWidth)}${rg}${v}`;
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
const rows = [top, ...body];
|
|
362
|
+
// The base editor places autocomplete immediately after its bottom rule.
|
|
363
|
+
// Hide status while the menu is open so that row indices match without
|
|
364
|
+
// reaching into the base editor's private mouse/layout state.
|
|
365
|
+
const hasLeft = Array.isArray(options.statusLeft) ? options.statusLeft.length > 0 : !!options.statusLeft;
|
|
366
|
+
const hasRight = Array.isArray(options.statusRight) ? options.statusRight.length > 0 : !!options.statusRight;
|
|
367
|
+
if (below.length === 0 && (hasLeft || hasRight)) {
|
|
368
|
+
const statusLogo = pyramid && content.length === 1 && spinnerRows && spinner ? spinnerRows[1] : "";
|
|
369
|
+
const slotWidth = statusSlotWidth(spinnerColumns, pyramid ? 2 : 1);
|
|
370
|
+
const statusWidth = Math.max(0, INT - slotWidth);
|
|
371
|
+
const interior = layoutRow(options.statusLeft ?? "", options.statusRight ?? "", statusWidth, options.statusOverlay);
|
|
372
|
+
const statusGutter = slotWidth ? fit(`${statusLogo}${down && content.length === 1 ? border(down) : " "}`, slotWidth) : "";
|
|
373
|
+
rows.push(`${pad}${v}${interior}${statusGutter}${v}`);
|
|
374
|
+
}
|
|
375
|
+
rows.push(bottom);
|
|
376
|
+
for (const line of below) {
|
|
377
|
+
rows.push(`${pad}${" ".repeat(1 + GUTTER)}${fit(line, editorWidth)}${" ".repeat(1 + GUTTER)}`);
|
|
378
|
+
}
|
|
379
|
+
return rows;
|
|
380
|
+
}
|