@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/editor.ts
ADDED
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FramedEditor — Pi's editor, re-housed in a centered rounded panel.
|
|
3
|
+
*
|
|
4
|
+
* It renders the real editor at a reduced inner width, then hands the raw lines
|
|
5
|
+
* to `frameEditor` (pure) which drops Pi's open-sided rules and rebuilds a
|
|
6
|
+
* continuous rounded box with a left margin. The box is capped at `maxWidth`
|
|
7
|
+
* and centered, so on a wide terminal there is breathing room on both sides.
|
|
8
|
+
*
|
|
9
|
+
* The border carries one label, the model identity (provider/model · thinking
|
|
10
|
+
* level) at its top-right; the rest of the chrome lives inside. The first row's
|
|
11
|
+
* gutters carry the `›` prompt sigil (left) and the working spinner (right); a
|
|
12
|
+
* dedicated status row above the bottom edge carries where you are (workspace ·
|
|
13
|
+
* branch · session, left) and the session's state (usage, cost estimate, run
|
|
14
|
+
* delta, subscription quota, persona, right). All editing/keybinding behavior is the base `CustomEditor`'s — the
|
|
15
|
+
* subclass only changes presentation and invalidates its transparent raw-layout
|
|
16
|
+
* cache around state changes.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import {
|
|
20
|
+
CustomEditor,
|
|
21
|
+
type ExtensionContext,
|
|
22
|
+
type KeybindingsManager,
|
|
23
|
+
} from "@earendil-works/pi-coding-agent";
|
|
24
|
+
import type { Color, EditorTheme, TUI, TuiMouseEvent, TuiMouseEventResult } from "@earendil-works/pi-tui";
|
|
25
|
+
import {
|
|
26
|
+
bashInputMode,
|
|
27
|
+
cleanModel,
|
|
28
|
+
formatContextWindow,
|
|
29
|
+
formatElapsed,
|
|
30
|
+
formatTokens,
|
|
31
|
+
type ContextUsageLike,
|
|
32
|
+
sanitizeInlineText,
|
|
33
|
+
shortCwd,
|
|
34
|
+
stableEmojiWidth,
|
|
35
|
+
usageLevel,
|
|
36
|
+
} from "./format.ts";
|
|
37
|
+
import { editorWidthFor, frameEditor, GUTTER, ROUNDED, statusSlotWidth, type RowCandidate } from "./frame.ts";
|
|
38
|
+
import type { QuotaView, QuotaViewWindow } from "./quota.ts";
|
|
39
|
+
import type { Activity } from "./tool-activity.ts";
|
|
40
|
+
import { chooseBox, visibleWidth } from "./width.ts";
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Levels the border label names, each painted with the palette's own thinking
|
|
44
|
+
* token. A whitelist rather than a sanitizer: `off`, a missing level and any
|
|
45
|
+
* text the host did not define show nothing.
|
|
46
|
+
*/
|
|
47
|
+
const THINKING_COLORS = {
|
|
48
|
+
minimal: "thinkingMinimal",
|
|
49
|
+
low: "thinkingLow",
|
|
50
|
+
medium: "thinkingMedium",
|
|
51
|
+
high: "thinkingHigh",
|
|
52
|
+
xhigh: "thinkingXhigh",
|
|
53
|
+
max: "thinkingMax",
|
|
54
|
+
} as const;
|
|
55
|
+
|
|
56
|
+
function thinkingColor(level: string | undefined): (typeof THINKING_COLORS)[keyof typeof THINKING_COLORS] | undefined {
|
|
57
|
+
return level !== undefined && Object.hasOwn(THINKING_COLORS, level) ? THINKING_COLORS[level as keyof typeof THINKING_COLORS] : undefined;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** A named call shows its elapsed time once it has run this long; shorter calls need no counter. */
|
|
61
|
+
export const ACTIVITY_ELAPSED_MS = 3000;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The narrowest a session name may shorten to on the status row: nine
|
|
65
|
+
* characters and the ellipsis. A shorter name is shown whole or not at all.
|
|
66
|
+
*/
|
|
67
|
+
export const SESSION_MIN_COLUMNS = 10;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The narrowest the activity label may shorten to: seven characters and the
|
|
71
|
+
* ellipsis, where most verbs still read whole (`Running…`). With fewer cells
|
|
72
|
+
* the workspace keeps its place rather than a stub of a label taking it.
|
|
73
|
+
*/
|
|
74
|
+
export const ACTIVITY_MIN_COLUMNS = 8;
|
|
75
|
+
|
|
76
|
+
export interface FramedEditorDeps {
|
|
77
|
+
ctx: ExtensionContext;
|
|
78
|
+
getPersona: () => string | undefined;
|
|
79
|
+
/** Current git branch from Pi's footer data (null = not a repo). */
|
|
80
|
+
getGitBranch?: () => string | null | undefined;
|
|
81
|
+
/** Cached outside render; session walks in the 90ms spinner path are forbidden. */
|
|
82
|
+
getSessionName: () => string | undefined;
|
|
83
|
+
getContextUsage: () => ContextUsageLike | undefined;
|
|
84
|
+
/** Formatted session estimate ("~$1.24"), cached outside render; "" hides it. */
|
|
85
|
+
getCost: () => string;
|
|
86
|
+
/** Formatted spend of the current or last run ("+$0.42"), cached outside render; "" hides it. */
|
|
87
|
+
getRunDelta?: () => string;
|
|
88
|
+
/**
|
|
89
|
+
* The current provider's subscription quota, computed on events and cached
|
|
90
|
+
* outside render; `undefined` (or an absent callback) leaves the row as it is
|
|
91
|
+
* without one.
|
|
92
|
+
*/
|
|
93
|
+
getQuota?: () => QuotaView | undefined;
|
|
94
|
+
/** Host thinking level, cached outside render; shown only for a reasoning model. */
|
|
95
|
+
getThinkingLevel?: () => string | undefined;
|
|
96
|
+
isWorking: () => boolean;
|
|
97
|
+
/** Static prompt state; takes precedence over the animated working spinner. */
|
|
98
|
+
isWaitingForUser?: () => boolean;
|
|
99
|
+
/**
|
|
100
|
+
* The running call worth naming, or the run clock when none is (revealed by
|
|
101
|
+
* the spinner repaint, never its own timer). Consulted only while working and
|
|
102
|
+
* not waiting for the user; it borrows the left group's cells and never moves
|
|
103
|
+
* the right group.
|
|
104
|
+
*/
|
|
105
|
+
getActivity?: () => Activity | undefined;
|
|
106
|
+
spinnerFrame: () => string;
|
|
107
|
+
spinnerRows?: () => readonly string[];
|
|
108
|
+
spinnerColor?: () => Color | undefined;
|
|
109
|
+
/** Active theme's text-motion height; absent legacy callers keep one row. */
|
|
110
|
+
spinnerHeight?: () => number;
|
|
111
|
+
/** Active theme's fixed indicator width; absent legacy callers keep one cell. */
|
|
112
|
+
spinnerColumns?: () => number;
|
|
113
|
+
/** Hand the live TUI back so the spinner timer can request renders. */
|
|
114
|
+
bindTui: (tui: TUI) => void;
|
|
115
|
+
/** Hand the live editor back so a shortcut can copy its logical buffer. */
|
|
116
|
+
bindEditor: (editor: CustomEditor) => void;
|
|
117
|
+
maxWidth: number;
|
|
118
|
+
minMargin: number;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
class FramedEditor extends CustomEditor {
|
|
122
|
+
/**
|
|
123
|
+
* Pi asks every component to render again for the 90 ms spinner tick. The
|
|
124
|
+
* base editor has no render cache, so relaying that call would re-wrap and
|
|
125
|
+
* re-measure the complete input on every tick even when no editor state
|
|
126
|
+
* changed. Keep only the base editor's raw lines: the outer frame is still
|
|
127
|
+
* rebuilt each time because its spinner/status chrome is live.
|
|
128
|
+
*/
|
|
129
|
+
private rawRenderCache:
|
|
130
|
+
| {
|
|
131
|
+
width: number;
|
|
132
|
+
terminalRows: number;
|
|
133
|
+
focused: boolean;
|
|
134
|
+
paddingX: number;
|
|
135
|
+
raw: string[];
|
|
136
|
+
}
|
|
137
|
+
| undefined;
|
|
138
|
+
|
|
139
|
+
constructor(tui: TUI, theme: EditorTheme, keybindings: KeybindingsManager, private readonly deps: FramedEditorDeps) {
|
|
140
|
+
super(tui, theme, keybindings, { paddingX: 1 });
|
|
141
|
+
deps.bindTui(tui);
|
|
142
|
+
deps.bindEditor(this);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private invalidateRawRenderCache(): void {
|
|
146
|
+
this.rawRenderCache = undefined;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** State-changing editor APIs must invalidate the raw layout cache. */
|
|
150
|
+
override invalidate(): void {
|
|
151
|
+
this.invalidateRawRenderCache();
|
|
152
|
+
super.invalidate();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
override handleInput(data: string): void {
|
|
156
|
+
this.invalidateRawRenderCache();
|
|
157
|
+
super.handleInput(data);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
override setPaddingX(padding: number): void {
|
|
161
|
+
this.invalidateRawRenderCache();
|
|
162
|
+
super.setPaddingX(padding);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
override setText(text: string): void {
|
|
166
|
+
this.invalidateRawRenderCache();
|
|
167
|
+
super.setText(text);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
override insertTextAtCursor(text: string): void {
|
|
171
|
+
this.invalidateRawRenderCache();
|
|
172
|
+
super.insertTextAtCursor(text);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Forward mouse routing to the host implementation so a click that moves the
|
|
177
|
+
* cursor cannot leave a spinner redraw on the old raw layout. Pi's screen-level text
|
|
178
|
+
* selection owns press/drag/release on editor rows, so unhandled motion events
|
|
179
|
+
* must not evict the unchanged layout cache.
|
|
180
|
+
*/
|
|
181
|
+
handleMouse(event: TuiMouseEvent): TuiMouseEventResult | undefined {
|
|
182
|
+
const base = (CustomEditor.prototype as unknown as {
|
|
183
|
+
handleMouse?: (this: FramedEditor, value: TuiMouseEvent) => TuiMouseEventResult | undefined;
|
|
184
|
+
}).handleMouse;
|
|
185
|
+
if (!base || !event || typeof event !== "object") return undefined;
|
|
186
|
+
const mouse = event as { x?: unknown; width?: unknown };
|
|
187
|
+
if (typeof mouse.x !== "number" || typeof mouse.width !== "number") {
|
|
188
|
+
const result = base.call(this, event);
|
|
189
|
+
if (event.type === "click" || result !== undefined) this.invalidateRawRenderCache();
|
|
190
|
+
return result;
|
|
191
|
+
}
|
|
192
|
+
const { fallback, boxWidth, leftMargin } = chooseBox(mouse.width, this.deps.maxWidth, this.deps.minMargin);
|
|
193
|
+
// render() uses the bare editor below this same inner-width threshold.
|
|
194
|
+
// Mouse must see the identical viewport or a narrow click shifts columns.
|
|
195
|
+
const editorWidth = editorWidthFor(boxWidth, this.deps.spinnerColumns?.() ?? 1);
|
|
196
|
+
if (fallback || editorWidth < 4) {
|
|
197
|
+
const result = base.call(this, event);
|
|
198
|
+
if (event.type === "click" || result !== undefined) this.invalidateRawRenderCache();
|
|
199
|
+
return result;
|
|
200
|
+
}
|
|
201
|
+
// The framed component is laid out at terminal width, while Editor.handleMouse
|
|
202
|
+
// expects coordinates in the reduced raw-editor viewport.
|
|
203
|
+
const frameLeft = leftMargin + 1 + GUTTER; // margin + left rail + left gutter
|
|
204
|
+
// The frame keeps both content and autocomplete at the base editor's row
|
|
205
|
+
// indices; only the horizontal viewport differs.
|
|
206
|
+
const result = base.call(this, { ...event, x: mouse.x - frameLeft, width: editorWidth });
|
|
207
|
+
if (event.type === "click" || result !== undefined) this.invalidateRawRenderCache();
|
|
208
|
+
return result;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
render(width: number): string[] {
|
|
212
|
+
const { ctx, maxWidth, minMargin } = this.deps;
|
|
213
|
+
|
|
214
|
+
const { fallback, boxWidth, leftMargin } = chooseBox(width, maxWidth, minMargin);
|
|
215
|
+
// The panel spends two columns per side on the rails plus the chrome gutters,
|
|
216
|
+
// so the editor renders at the reduced width. Too tiny to frame → fall back.
|
|
217
|
+
const spinnerColumns = this.deps.spinnerColumns?.() ?? 1;
|
|
218
|
+
const editorWidth = editorWidthFor(boxWidth, spinnerColumns);
|
|
219
|
+
if (fallback || editorWidth < 4) return super.render(width);
|
|
220
|
+
|
|
221
|
+
// Autocomplete suggestions can change asynchronously without changing the
|
|
222
|
+
// editor text, so let the base editor render freely while its menu is open.
|
|
223
|
+
// The normal steady state (including spinner-only redraws) reuses its raw
|
|
224
|
+
// layout and therefore avoids a full grapheme/wrap pass every 90 ms.
|
|
225
|
+
const canReuseRaw = !this.isShowingAutocomplete();
|
|
226
|
+
const cached = this.rawRenderCache;
|
|
227
|
+
const raw =
|
|
228
|
+
canReuseRaw &&
|
|
229
|
+
cached?.width === editorWidth &&
|
|
230
|
+
cached.terminalRows === this.tui.terminal.rows &&
|
|
231
|
+
cached.focused === this.focused &&
|
|
232
|
+
cached.paddingX === this.getPaddingX()
|
|
233
|
+
? cached.raw
|
|
234
|
+
: super.render(editorWidth);
|
|
235
|
+
if (canReuseRaw && raw !== cached?.raw) {
|
|
236
|
+
this.rawRenderCache = {
|
|
237
|
+
width: editorWidth,
|
|
238
|
+
terminalRows: this.tui.terminal.rows,
|
|
239
|
+
focused: this.focused,
|
|
240
|
+
paddingX: this.getPaddingX(),
|
|
241
|
+
raw,
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
if (raw.length < 2) return raw;
|
|
245
|
+
|
|
246
|
+
const theme = ctx.ui.theme;
|
|
247
|
+
const border = (s: string) => theme.fg("muted", s);
|
|
248
|
+
const accent = (s: string) => theme.fg("accent", s);
|
|
249
|
+
const dim = (s: string) => theme.fg("dim", s);
|
|
250
|
+
const text = (s: string) => theme.fg("text", s);
|
|
251
|
+
const muted = (s: string) => theme.fg("muted", s);
|
|
252
|
+
const separator = dim(" · ");
|
|
253
|
+
|
|
254
|
+
// pi-persona's label may carry a VS16 emoji (🗺️, ⚖️) whose terminal width is
|
|
255
|
+
// ambiguous; normalize it so the box's right rail can't drift off the corner.
|
|
256
|
+
const personaRaw = this.deps.getPersona();
|
|
257
|
+
const persona = personaRaw ? stableEmojiWidth(personaRaw) : "";
|
|
258
|
+
const cwd = shortCwd(ctx.cwd);
|
|
259
|
+
const sessionName = this.deps.getSessionName();
|
|
260
|
+
const contextUsage = this.deps.getContextUsage();
|
|
261
|
+
// Capacity gets the signal color, not the adjacent cost estimate. Unknown
|
|
262
|
+
// context stays quiet; known usage follows Pi's >70 / >90 thresholds.
|
|
263
|
+
const level = usageLevel(contextUsage?.percent);
|
|
264
|
+
const usageKnown = contextUsage?.tokens != null;
|
|
265
|
+
const usageColor = !usageKnown ? "dim" : level === "critical" ? "error" : level === "warn" ? "warning" : "accent";
|
|
266
|
+
const cost = this.deps.getCost();
|
|
267
|
+
const runDelta = cost ? this.deps.getRunDelta?.() ?? "" : "";
|
|
268
|
+
const waiting = this.deps.isWaitingForUser?.() ?? false;
|
|
269
|
+
const working = this.deps.isWorking();
|
|
270
|
+
const activeSpinnerRows = working ? this.deps.spinnerRows?.() : undefined;
|
|
271
|
+
const spinnerColor = this.deps.spinnerColor?.();
|
|
272
|
+
const colorSpinner = (value: string) => spinnerColor ? theme.style(value, { fg: spinnerColor }) : accent(value);
|
|
273
|
+
const spinner = waiting ? theme.fg("warning", "?") : working ? colorSpinner(activeSpinnerRows?.[0] ?? this.deps.spinnerFrame()) : "";
|
|
274
|
+
const branch = sanitizeInlineText(this.deps.getGitBranch?.() ?? "");
|
|
275
|
+
const session = sanitizeInlineText(sessionName ?? "");
|
|
276
|
+
// Identity is readable text; branch is the navigational accent, while
|
|
277
|
+
// session metadata and separators recede without losing their labels.
|
|
278
|
+
const withBranch = text(` ${cwd}`) + (branch ? separator + accent(branch) : "");
|
|
279
|
+
// If the status row gets tight, keep the repository basename rather than
|
|
280
|
+
// clipping the tail of its path into an unidentifiable ancestor prefix.
|
|
281
|
+
const cwdSegments = cwd.split("/").filter(Boolean);
|
|
282
|
+
const cwdBasename = cwdSegments[cwdSegments.length - 1];
|
|
283
|
+
const compactCwd = cwdSegments.length > 1 && cwdBasename ? `…/${cwdBasename}` : cwd;
|
|
284
|
+
// The left group gives up whole details, never characters of one: the
|
|
285
|
+
// session name goes, then the branch, then the path down to its basename.
|
|
286
|
+
// A branch is an identifier, so it is whole or absent. Only the session
|
|
287
|
+
// name, free text, may shorten, and only down to SESSION_MIN_COLUMNS.
|
|
288
|
+
const statusLeft: RowCandidate[] = [
|
|
289
|
+
...(session ? [{ pre: withBranch + separator, flex: muted(session), minFlex: SESSION_MIN_COLUMNS }] : []),
|
|
290
|
+
...(branch ? [{ pre: withBranch }] : []),
|
|
291
|
+
{ pre: text(` ${cwd}`) },
|
|
292
|
+
];
|
|
293
|
+
if (compactCwd !== cwd) statusLeft.push({ pre: text(` ${compactCwd}`) });
|
|
294
|
+
// Bash mode (leading `!`): the sigil flips to a `$` so the mode is visible
|
|
295
|
+
// at the prompt itself. `!!` keeps the output out of the model's context,
|
|
296
|
+
// and the host paints that block dim instead of bashMode: so does the sigil.
|
|
297
|
+
const bash = bashInputMode(this.getText());
|
|
298
|
+
const sigil = bash === "none" ? accent("›") : theme.fg(bash === "excluded" ? "dim" : "bashMode", "$");
|
|
299
|
+
|
|
300
|
+
// The running call's label is painted over the workspace cells only, so its
|
|
301
|
+
// arrival and departure never reflow usage, cost or persona. Sanitized again
|
|
302
|
+
// here (with the persona label's VS16 discipline): the frame must stay exact
|
|
303
|
+
// even if a caller hands it raw tool text.
|
|
304
|
+
const activity = working && !waiting ? this.deps.getActivity?.() : undefined;
|
|
305
|
+
const activityLabel = activity ? stableEmojiWidth(sanitizeInlineText(activity.label)) : "";
|
|
306
|
+
// A long call says how long it has run, as stock Pi's "Elapsed" does, so a
|
|
307
|
+
// quiet `npm test` does not read as hung. The spinner's repaint advances it.
|
|
308
|
+
const elapsed = activity && activity.elapsedMs >= ACTIVITY_ELAPSED_MS ? formatElapsed(activity.elapsedMs) : "";
|
|
309
|
+
const count = activity && activity.more > 0 ? ` +${activity.more}` : "";
|
|
310
|
+
// Overlay rungs are tried whole and only the last trims, so the count yields
|
|
311
|
+
// first, then the elapsed time, and only then does the label shorten: to
|
|
312
|
+
// no fewer than ACTIVITY_MIN_COLUMNS, below which the workspace stays.
|
|
313
|
+
const named: RowCandidate = { pre: " ", flex: text(activityLabel), minFlex: ACTIVITY_MIN_COLUMNS };
|
|
314
|
+
const timed = elapsed ? [{ ...named, post: dim(` ${elapsed}${count}`) }, ...(count ? [{ ...named, post: dim(` ${elapsed}`) }] : [])]
|
|
315
|
+
: count ? [{ ...named, post: dim(count) }] : [];
|
|
316
|
+
// The run clock ("Working… 45s") names no call, so there is nothing to trim:
|
|
317
|
+
// with its time, without it, or the workspace label as it was. A rung with
|
|
318
|
+
// no flex part is shown whole or not at all.
|
|
319
|
+
const clock: RowCandidate[] = [
|
|
320
|
+
...(elapsed ? [{ pre: ` ${text(activityLabel)}${dim(` ${elapsed}`)}` }] : []),
|
|
321
|
+
{ pre: ` ${text(activityLabel)}` },
|
|
322
|
+
];
|
|
323
|
+
const statusOverlay: RowCandidate[] | undefined = !activity || !activityLabel ? undefined : activity.run ? clock : [...timed, named];
|
|
324
|
+
|
|
325
|
+
// The right group is the session's state: usage, cost, persona. Its core
|
|
326
|
+
// narrows as one ladder; ornaments are decided first, against the full
|
|
327
|
+
// left group, because layoutRow lets any right rung outrank left detail.
|
|
328
|
+
const percent = contextUsage?.percent;
|
|
329
|
+
const percentText = usageKnown && percent != null && Number.isFinite(percent) ? `${Math.round(percent)}%` : "";
|
|
330
|
+
const tokensText = usageKnown ? formatTokens(contextUsage?.tokens ?? 0) : "";
|
|
331
|
+
const windowText = tokensText ? formatContextWindow(contextUsage?.contextWindow) : "";
|
|
332
|
+
// The piece that stands last: the percentage, else the bare token count,
|
|
333
|
+
// else the quiet unknown marker. No usage at all (no model) shows none.
|
|
334
|
+
const unknownUsage = contextUsage && !usageKnown ? dim("ctx —") : "";
|
|
335
|
+
const join = (...parts: string[]) => parts.filter(Boolean).join(separator);
|
|
336
|
+
// The subscription quota the provider reports, read from a view cached on
|
|
337
|
+
// events. Labels and digits are generated in quota.ts, never copied from a
|
|
338
|
+
// response. A calm figure nobody has confirmed for a while recedes to dim
|
|
339
|
+
// (same width); a warning or critical one keeps its color however old.
|
|
340
|
+
const quota = this.deps.getQuota?.();
|
|
341
|
+
const quotaWindow = (window: QuotaViewWindow): string => dim(`${window.label} `) + theme.fg(
|
|
342
|
+
window.level === "critical" ? "error" : window.level === "warn" ? "warning" : window.aged ? "dim" : "muted", `${window.percent}%`);
|
|
343
|
+
// One window — the binding one — or `limit` in its place; and both windows.
|
|
344
|
+
const quotaOne = quota?.limit ? theme.fg("error", quota.limit) : quota?.one && quota.binding ? quotaWindow(quota.binding) : "";
|
|
345
|
+
const quotaTwo = quota?.two ? quota.windows.map(quotaWindow).join(separator) : "";
|
|
346
|
+
// A hot window (or `limit`) is content and narrows with the core; a calm one is an ornament.
|
|
347
|
+
const quotaCore: 0 | 1 = quotaOne && quota?.hot ? 1 : 0;
|
|
348
|
+
const right = (shape: { tokens?: boolean; cost?: boolean; window?: boolean; delta?: boolean; gauge?: boolean; quota?: 0 | 1 | 2 }): string => {
|
|
349
|
+
const tokens = tokensText ? theme.fg(usageColor, tokensText) + (shape.window && windowText ? dim(`/${windowText}`) : "") : "";
|
|
350
|
+
const usage = percentText ? join(shape.tokens ? tokens : "", theme.fg(usageColor, percentText)) : tokens || unknownUsage;
|
|
351
|
+
const money = shape.cost && cost ? muted(cost) + (shape.delta && runDelta ? dim(` ${runDelta}`) : "") : "";
|
|
352
|
+
const limits = shape.quota === 2 && quotaTwo ? quotaTwo : shape.quota ? quotaOne : "";
|
|
353
|
+
const body = join(usage, money, limits, persona ? text(persona) : "");
|
|
354
|
+
if (!body) return "";
|
|
355
|
+
const filled = Math.round(Math.max(0, Math.min(100, percent ?? 0)) / 20);
|
|
356
|
+
const gauge = dim("[") + theme.fg(usageColor, "▰".repeat(filled)) + dim("▱".repeat(5 - filled)) + dim("]");
|
|
357
|
+
return `${shape.gauge ? `${gauge} ` : ""}${body} `;
|
|
358
|
+
};
|
|
359
|
+
const statusWidth = boxWidth - 2 - statusSlotWidth(spinnerColumns, this.deps.spinnerHeight?.() ?? 1);
|
|
360
|
+
const fullLeft = `${statusLeft[0].pre ?? ""}${statusLeft[0].flex ?? ""}`;
|
|
361
|
+
// The full left group, this right rung and the one-column gap: exactly
|
|
362
|
+
// when layoutRow keeps both at their widest.
|
|
363
|
+
const fits = (rung: string) => visibleWidth(fullLeft) + visibleWidth(rung) + 1 <= statusWidth;
|
|
364
|
+
// Ornaments never cost content. They surrender in a fixed order — run
|
|
365
|
+
// delta, then window denominator, then the second quota window, then the
|
|
366
|
+
// gauge, then a calm binding quota window — each shown only while the full
|
|
367
|
+
// left and everything that outranks it still fit.
|
|
368
|
+
const shape: { tokens: boolean; cost: boolean; window: boolean; delta: boolean; gauge: boolean; quota: 0 | 1 | 2 } =
|
|
369
|
+
{ tokens: true, cost: true, window: false, delta: false, gauge: false, quota: quotaCore };
|
|
370
|
+
const admit = (change: Partial<typeof shape>, present: boolean): boolean => {
|
|
371
|
+
if (!present) return true;
|
|
372
|
+
if (!fits(right({ ...shape, ...change }))) return false;
|
|
373
|
+
Object.assign(shape, change);
|
|
374
|
+
return true;
|
|
375
|
+
};
|
|
376
|
+
if (admit({ quota: 1 }, !!quotaOne && !quotaCore) && admit({ gauge: true }, !!percentText)
|
|
377
|
+
&& admit({ quota: 2 }, !!quotaTwo) && admit({ window: true }, !!windowText)) admit({ delta: true }, !!runDelta);
|
|
378
|
+
// Then the core: cost goes, then the token count, then the persona trims
|
|
379
|
+
// and goes, then a hot quota window (or `limit`), and the percentage (in
|
|
380
|
+
// its warning color past 70%) is the last standing: finally without its
|
|
381
|
+
// pad, and below that nothing: the frame hard-truncates a last rung that
|
|
382
|
+
// does not fit, and "71…" or "7…" reads as another number. A quota text
|
|
383
|
+
// is never clipped either: whole or absent.
|
|
384
|
+
const last = percentText ? theme.fg(usageColor, percentText) : tokensText ? theme.fg(usageColor, tokensText) : unknownUsage;
|
|
385
|
+
const hotQuota = quotaCore ? quotaOne : "";
|
|
386
|
+
const stand = join(last, hotQuota);
|
|
387
|
+
const ladder: RowCandidate[] = [
|
|
388
|
+
{ pre: right(shape) },
|
|
389
|
+
{ pre: right({ tokens: true, cost: true, quota: quotaCore }) },
|
|
390
|
+
{ pre: right({ tokens: true, quota: quotaCore }) },
|
|
391
|
+
...(persona ? [{ pre: stand ? stand + separator : "", flex: text(persona), post: " " }] : []),
|
|
392
|
+
...(hotQuota ? [{ pre: `${stand} ` }] : []),
|
|
393
|
+
...(last ? [{ pre: `${last} ` }, { pre: last }] : hotQuota ? [{ pre: hotQuota }] : []),
|
|
394
|
+
];
|
|
395
|
+
const statusRight = ladder.filter((rung, i) => {
|
|
396
|
+
const whole = `${rung.pre ?? ""}${rung.flex ?? ""}${rung.post ?? ""}`;
|
|
397
|
+
const previous = ladder[i - 1];
|
|
398
|
+
return whole.trim() !== "" && (!previous || whole !== `${previous.pre ?? ""}${previous.flex ?? ""}${previous.post ?? ""}`);
|
|
399
|
+
});
|
|
400
|
+
if (last || quotaOne) statusRight.push({}); // the empty rung always fits: whole or not at all
|
|
401
|
+
|
|
402
|
+
// The border's one label is the model identity, top-right. Thinking level is
|
|
403
|
+
// named only for a reasoning model, by Pi's own lowercase names, each in the
|
|
404
|
+
// palette's thinking token: `off`, a missing level, and any text the host
|
|
405
|
+
// did not define show nothing.
|
|
406
|
+
// An id that sanitizes to nothing visible ("claude-", blanks, escapes,
|
|
407
|
+
// zero-width) offers no rung at all: not an empty label, not "provider/ ".
|
|
408
|
+
const modelText = stableEmojiWidth(cleanModel(ctx.model?.id));
|
|
409
|
+
const model = modelText.trim() && visibleWidth(modelText.trim()) > 0 ? accent(modelText) : "";
|
|
410
|
+
const provider = ctx.model?.provider ? stableEmojiWidth(sanitizeInlineText(ctx.model.provider)) : "";
|
|
411
|
+
const thinkingLevel = ctx.model?.reasoning === true ? this.deps.getThinkingLevel?.() : undefined;
|
|
412
|
+
const thinkingToken = thinkingColor(thinkingLevel);
|
|
413
|
+
const levelTail = thinkingToken && thinkingLevel ? separator + theme.fg(thinkingToken, thinkingLevel) : "";
|
|
414
|
+
// provider/model · level → model · level → model alone; only the model trims.
|
|
415
|
+
const borderLabel: RowCandidate[] = !model ? [] : [
|
|
416
|
+
...(provider ? [{ pre: `${dim(`${provider}/`)}${model}${levelTail}` }] : []),
|
|
417
|
+
...(levelTail ? [{ flex: model, post: levelTail }] : []),
|
|
418
|
+
{ flex: model },
|
|
419
|
+
];
|
|
420
|
+
return frameEditor(raw, {
|
|
421
|
+
boxWidth,
|
|
422
|
+
leftMargin,
|
|
423
|
+
style: ROUNDED,
|
|
424
|
+
border,
|
|
425
|
+
borderLabel,
|
|
426
|
+
prompt: sigil, // first-row left gutter
|
|
427
|
+
spinner, // first-row right gutter (empty when idle)
|
|
428
|
+
spinnerRows: waiting ? undefined : activeSpinnerRows?.map(colorSpinner),
|
|
429
|
+
spinnerHeight: this.deps.spinnerHeight?.() ?? 1,
|
|
430
|
+
spinnerColumns,
|
|
431
|
+
statusLeft,
|
|
432
|
+
statusOverlay,
|
|
433
|
+
statusRight,
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
export type EditorFactory = (tui: TUI, theme: EditorTheme, keybindings: KeybindingsManager) => FramedEditor;
|
|
439
|
+
|
|
440
|
+
/** Build the `setEditorComponent` factory bound to live session state. */
|
|
441
|
+
export function makeFramedEditorFactory(deps: FramedEditorDeps): EditorFactory {
|
|
442
|
+
return (tui, theme, keybindings) => new FramedEditor(tui, theme, keybindings, deps);
|
|
443
|
+
}
|