@giladbarnea/pi-custom-footer 0.1.1 → 0.1.3
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 +3 -1
- package/index.ts +370 -163
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -25,7 +25,9 @@ Below the editor, see:
|
|
|
25
25
|
- **Cache behavior:** session hit rate, latest hit or estimated miss, and reused/new token totals.
|
|
26
26
|
- **Where you are:** Git branch, dirty marker, directory, and other extensions’ status messages.
|
|
27
27
|
|
|
28
|
-
The layout follows your Pi theme
|
|
28
|
+
The layout follows your Pi theme. The header and the footer each take the fewest lines they can. To fit, they first drop labels, round readings, remove whitespace, and drop the branch icon. Only then does the footer hide facts: the reused/new split, the session hit rate, the token counts, and last the cost per turn.
|
|
29
|
+
|
|
30
|
+
On terminals with 40 rows or fewer, skills get at most four rows. To fit, the row first drops token ages, then separators, then shortens long names in the middle (`caut…ctor`). Only then does it show three rows plus a hidden-skill count.
|
|
29
31
|
|
|
30
32
|
**Resize without rescanning the conversation.** The header and footer share cached values. Session changes refresh them, including forks and compaction.
|
|
31
33
|
|
package/index.ts
CHANGED
|
@@ -1,22 +1,85 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Show session context, cache usage, cost, and loaded skills around the editor.
|
|
3
|
+
*
|
|
4
|
+
* Skill row. Each loaded skill shows its name and its token age: how far the
|
|
5
|
+
* context has grown since the first response that saw the skill. The age tells
|
|
6
|
+
* how deep in the context the skill now sits. On a terminal of 40 rows or
|
|
7
|
+
* fewer, the skills get at most four rows. When they do not fit, the row gives
|
|
8
|
+
* up the least important thing left, one step at a time:
|
|
9
|
+
* 1. The token ages.
|
|
10
|
+
* 2. The separators between skills.
|
|
11
|
+
* 3. The middle of the longest names, one character per step, down to four
|
|
12
|
+
* characters on each side of the ellipsis. Names shorter than ten
|
|
13
|
+
* characters never change.
|
|
14
|
+
* 4. Last, three rows in the most compact layout plus a "+N skills" count.
|
|
15
|
+
* Knowing which skills are loaded matters more than any detail about them, so
|
|
16
|
+
* hiding a skill is the last resort. The Claude Code status line in
|
|
17
|
+
* ~/.claude/statusline.sh uses the same order.
|
|
18
|
+
*
|
|
19
|
+
* Known gaps reviewed September 30, 2026, against Pi 0.99.0:
|
|
20
|
+
* - Costs omit codemode and nested-model usage recorded on tool results.
|
|
21
|
+
* - Loaded skills omit successful reads recorded in tool-result nestedCalls.
|
|
22
|
+
* - The header omits the routed physical model and thinking level.
|
|
23
|
+
* - After compaction, unknown usage falls back to the selected model's window
|
|
24
|
+
* or 200k, ignoring the known physical window from getContextUsage().
|
|
25
|
+
*/
|
|
2
26
|
import type { AssistantMessage } from "@earendil-works/pi-ai";
|
|
3
|
-
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
4
|
-
import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
27
|
+
import type { ExtensionAPI, ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
|
|
28
|
+
import { truncateToWidth, visibleWidth, type TUI } from "@earendil-works/pi-tui";
|
|
5
29
|
|
|
6
30
|
import { getLoadedSkills, type SkillLocation } from "./skill-tracker.ts";
|
|
7
31
|
|
|
8
32
|
// ── Layout constants ────────────────────────────────────────────────────────
|
|
9
33
|
|
|
10
|
-
const
|
|
34
|
+
const SHORT_TERMINAL_HEIGHT = 40;
|
|
35
|
+
const SKILL_COLLAPSE_ROW_THRESHOLD = 4;
|
|
36
|
+
const COLLAPSED_SKILL_ROWS = 3;
|
|
37
|
+
// A truncated name keeps four characters on each side of the ellipsis.
|
|
38
|
+
const MIN_TRUNCATED_NAME_LENGTH = 9;
|
|
11
39
|
const NARROW_SEPARATOR = " ";
|
|
12
40
|
const WIDE_SEPARATOR = " · ";
|
|
13
|
-
const
|
|
41
|
+
const BRANCH_GLYPH = "";
|
|
42
|
+
const CONTEXT_GAUGE_SYMBOLS = ["▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"];
|
|
14
43
|
const CONTEXT_GAUGE_STEP_COUNT = CONTEXT_GAUGE_SYMBOLS.length;
|
|
15
|
-
const CONTEXT_MUTED_THRESHOLD = 40;
|
|
16
|
-
const CONTEXT_WARNING_THRESHOLD = 65;
|
|
17
|
-
const CONTEXT_ERROR_THRESHOLD = 85;
|
|
18
44
|
const CACHE_MISS_NOISE_FLOOR_TOKENS = 1_024;
|
|
19
|
-
|
|
45
|
+
// Match Theme.fg, which resets the foreground only. A full reset would punch a
|
|
46
|
+
// hole in any background the caller has already opened, such as the cwd pill.
|
|
47
|
+
const ANSI_FG_RESET = "\x1b[39m";
|
|
48
|
+
const QUIET_BLEND = 0.38;
|
|
49
|
+
|
|
50
|
+
// ── Colour rules ────────────────────────────────────────────────────────────
|
|
51
|
+
// Hue means one of two things here and nothing else.
|
|
52
|
+
//
|
|
53
|
+
// 1. A RAMP is a measurement. Its colour moves with the value, and it is loud
|
|
54
|
+
// only where there is something to act on. CONTEXT_STOPS runs quiet to loud
|
|
55
|
+
// as the window fills. CACHE_STOPS is that same scale mirrored, because a
|
|
56
|
+
// full cache is the good end and an empty one is the problem.
|
|
57
|
+
// 2. A FAMILY is a category. One token per kind of fact, held everywhere it
|
|
58
|
+
// appears, so the shape of the footer becomes learnable:
|
|
59
|
+
// accent place cwd, branch, dirty marker
|
|
60
|
+
// warning spend total cost, cost per turn
|
|
61
|
+
// syntaxKeyword machine model name; thinking has its own theme ramp
|
|
62
|
+
// mdCode skills loaded skill names
|
|
63
|
+
// dim / muted labels, units, reference figures, session id
|
|
64
|
+
//
|
|
65
|
+
// Every colour resolves through the theme. No literal colours below this line.
|
|
66
|
+
|
|
67
|
+
type Stop = { at: number; token: string; quiet?: boolean };
|
|
68
|
+
|
|
69
|
+
const CONTEXT_STOPS: readonly Stop[] = [
|
|
70
|
+
{ at: 0, token: "success", quiet: true },
|
|
71
|
+
{ at: 40, token: "success" },
|
|
72
|
+
{ at: 70, token: "warning" },
|
|
73
|
+
{ at: 100, token: "error" },
|
|
74
|
+
];
|
|
75
|
+
|
|
76
|
+
/** The context ramp mirrored, so a full cache is quiet and an empty one is loud. */
|
|
77
|
+
const CACHE_STOPS: readonly Stop[] = CONTEXT_STOPS.map((stop) => ({ ...stop, at: 100 - stop.at })).reverse();
|
|
78
|
+
|
|
79
|
+
const PLACE_TOKEN = "accent";
|
|
80
|
+
const SPEND_TOKEN = "warning";
|
|
81
|
+
const MACHINE_TOKEN = "syntaxKeyword";
|
|
82
|
+
const SKILL_TOKEN = "mdCode";
|
|
20
83
|
|
|
21
84
|
const THINKING_LEVELS: Record<string, { full: string; short: string; token: string }> = {
|
|
22
85
|
off: { full: "off", short: "o", token: "dim" },
|
|
@@ -28,19 +91,76 @@ const THINKING_LEVELS: Record<string, { full: string; short: string; token: stri
|
|
|
28
91
|
max: { full: "max", short: "mx", token: "thinkingMax" },
|
|
29
92
|
};
|
|
30
93
|
|
|
31
|
-
// ──
|
|
94
|
+
// ── Shrink ladders ──────────────────────────────────────────────────────────
|
|
95
|
+
// The same approach as the Claude Code status line in ~/.claude/statusline.sh.
|
|
96
|
+
// Each region packs its rows onto the fewest lines it can, at the fullest state
|
|
97
|
+
// that reaches that count. State N applies the first N steps. Each phase gives
|
|
98
|
+
// up a kind of detail that matters more than the phase before it, and within a
|
|
99
|
+
// phase the least needed fact goes first.
|
|
100
|
+
|
|
101
|
+
type Form = "full" | "short" | "bare" | "hidden";
|
|
102
|
+
/** A fact's text in each form it has. A step names only forms its fact has. */
|
|
103
|
+
type FactForms = { full: string } & Partial<Record<"short" | "bare", string>>;
|
|
104
|
+
/** One row of facts. Each inner list is one group, joined by the narrow joiner. */
|
|
105
|
+
type Row<Key extends string> = readonly (readonly Key[])[];
|
|
106
|
+
type Ladder<Key extends string> = {
|
|
107
|
+
rows: readonly Row<Key>[];
|
|
108
|
+
steps: readonly (readonly [Key | "spacing", Form])[];
|
|
109
|
+
};
|
|
110
|
+
type HeaderFact = "model" | "thinking" | "sessionName" | "sessionId";
|
|
111
|
+
type FooterFact = "branch" | "cwd" | "context" | "tokens" | "cost" | "perTurn" | "cacheSession" | "latest" | "reuse";
|
|
32
112
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
113
|
+
const SPACING = {
|
|
114
|
+
full: { separator: WIDE_SEPARATOR, joiner: " " },
|
|
115
|
+
bare: { separator: NARROW_SEPARATOR, joiner: " " },
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const HEADER_LADDER: Ladder<HeaderFact> = {
|
|
119
|
+
rows: [[["model"], ["thinking"], ["sessionName"], ["sessionId"]]],
|
|
120
|
+
steps: [
|
|
121
|
+
// 1. Narrow the separators to one space.
|
|
122
|
+
["spacing", "bare"],
|
|
123
|
+
// 2. Shorten the thinking level.
|
|
124
|
+
["thinking", "short"],
|
|
125
|
+
// 3. Trim the session id to its first two segments. Last, because the
|
|
126
|
+
// full id is what finds the session again.
|
|
127
|
+
["sessionId", "short"],
|
|
128
|
+
],
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const FOOTER_LADDER: Ladder<FooterFact> = {
|
|
132
|
+
rows: [
|
|
133
|
+
[["branch"], ["cwd"]],
|
|
134
|
+
[["context", "tokens"], ["cost", "perTurn"]],
|
|
135
|
+
[["cacheSession"], ["latest"], ["reuse"]],
|
|
136
|
+
],
|
|
137
|
+
steps: [
|
|
138
|
+
// 1. Drop the labels the values already imply.
|
|
139
|
+
["tokens", "short"], ["context", "bare"], ["cacheSession", "bare"],
|
|
140
|
+
// 2. Shorten the readings: the latest cache figure, then the cost, rounded twice.
|
|
141
|
+
["latest", "short"], ["cost", "short"], ["cost", "bare"],
|
|
142
|
+
// 3. Remove whitespace: the padding inside the cwd pill, then the separators.
|
|
143
|
+
["cwd", "short"], ["spacing", "bare"],
|
|
144
|
+
// 4. Drop the branch icon.
|
|
145
|
+
["branch", "bare"],
|
|
146
|
+
// 5. Hide whole facts. The cost per turn goes last, because seeing what
|
|
147
|
+
// each turn costs is half of what the footer is for.
|
|
148
|
+
["reuse", "hidden"], ["cacheSession", "hidden"], ["tokens", "hidden"], ["perTurn", "hidden"],
|
|
149
|
+
],
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
// ── Generic helpers ─────────────────────────────────────────────────────────
|
|
36
153
|
|
|
37
154
|
function joinedLine(parts: string[], separator: string): string {
|
|
38
155
|
return ` ${parts.join(separator)}`;
|
|
39
156
|
}
|
|
40
157
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
158
|
+
/** Wrap whole parts into lines narrower than width. A part wider than width takes a line alone.
|
|
159
|
+
* @example
|
|
160
|
+
* wrapParts(["one", "two"], 6, " · ") // [["one"], ["two"]]
|
|
161
|
+
*/
|
|
162
|
+
function wrapParts(parts: string[], width: number, separator: string): string[][] {
|
|
163
|
+
const lines: string[][] = [];
|
|
44
164
|
const remainingParts = [...parts];
|
|
45
165
|
while (remainingParts.length > 0) {
|
|
46
166
|
const lineParts: string[] = [];
|
|
@@ -50,11 +170,89 @@ function wrapJoinedLines(parts: string[], width: number, separator: string): str
|
|
|
50
170
|
lineParts.push(remainingParts.shift()!);
|
|
51
171
|
if (visibleWidth(joinedLine(lineParts, separator)) >= width) break;
|
|
52
172
|
}
|
|
53
|
-
lines.push(
|
|
173
|
+
lines.push(lineParts);
|
|
54
174
|
}
|
|
55
175
|
return lines;
|
|
56
176
|
}
|
|
57
177
|
|
|
178
|
+
type WrappedLine = { text: string; partCount: number };
|
|
179
|
+
|
|
180
|
+
/** Wrap whole parts and retain the number of parts in each row.
|
|
181
|
+
* @example
|
|
182
|
+
* wrapJoinedLines(["one", "two"], 6, " · ") // [{ text: " one", partCount: 1 }, { text: " two", partCount: 1 }]
|
|
183
|
+
*/
|
|
184
|
+
function wrapJoinedLines(parts: string[], width: number, separator: string): WrappedLine[] {
|
|
185
|
+
return wrapParts(parts, width, separator).map((lineParts) => ({
|
|
186
|
+
text: truncateToWidth(joinedLine(lineParts, separator), width),
|
|
187
|
+
partCount: lineParts.length,
|
|
188
|
+
}));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** Pack rows onto lines. Rows share a line when they fit, and a row breaks only
|
|
192
|
+
* when it is wider than the width, so a fact moves between lines only with its whole row.
|
|
193
|
+
* @example
|
|
194
|
+
* packRows([["a", "b"], ["c"]], 20, " · ") // [" a · b · c"]
|
|
195
|
+
*/
|
|
196
|
+
function packRows(rows: string[][], width: number, separator: string): string[] {
|
|
197
|
+
const rowLines = rows.flatMap((row) => wrapParts(row, width, separator).map((lineParts) => lineParts.join(separator)));
|
|
198
|
+
return wrapJoinedLines(rowLines, width, separator).map((line) => line.text);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** The ladder's fullest state among those that pack the facts onto the fewest lines.
|
|
202
|
+
* A fact missing from `facts` is left out.
|
|
203
|
+
*/
|
|
204
|
+
function fitFacts<Key extends string>(
|
|
205
|
+
facts: Partial<Record<Key, FactForms>>,
|
|
206
|
+
ladder: Ladder<Key>,
|
|
207
|
+
width: number,
|
|
208
|
+
theme: Theme,
|
|
209
|
+
): string[] {
|
|
210
|
+
const linesAt = (state: number): string[] => {
|
|
211
|
+
const forms = new Map<Key | "spacing", Form>(ladder.steps.slice(0, state));
|
|
212
|
+
const spacing = forms.get("spacing") === "bare" ? SPACING.bare : SPACING.full;
|
|
213
|
+
const text = (key: Key): string => {
|
|
214
|
+
const form = forms.get(key) ?? "full";
|
|
215
|
+
const fact = facts[key];
|
|
216
|
+
return form === "hidden" || !fact ? "" : fact[form]!;
|
|
217
|
+
};
|
|
218
|
+
const rows = ladder.rows.map((row) => row.map((group) => group.map(text).filter(Boolean).join(spacing.joiner)).filter(Boolean));
|
|
219
|
+
return packRows(rows, width, theme.fg("dim", spacing.separator));
|
|
220
|
+
};
|
|
221
|
+
const states = Array.from({ length: ladder.steps.length + 1 }, (_, state) => linesAt(state));
|
|
222
|
+
return states.find((lines) => lines.length === states.at(-1)!.length)!;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** Cut the middle of a name down to maxLength, marking the cut with an ellipsis.
|
|
226
|
+
* @example
|
|
227
|
+
* middleTruncate("cautious-refactor", 9) // "caut…ctor"
|
|
228
|
+
*/
|
|
229
|
+
function middleTruncate(name: string, maxLength: number): string {
|
|
230
|
+
if (name.length <= maxLength) return name;
|
|
231
|
+
const kept = maxLength - 1;
|
|
232
|
+
return `${name.slice(0, Math.ceil(kept / 2))}…${name.slice(name.length - Math.floor(kept / 2))}`;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
type SkillLayout = { ages: boolean; separator: string; maxNameLength: number };
|
|
236
|
+
|
|
237
|
+
/** Skill row layouts, fullest first. Each step gives up the least important
|
|
238
|
+
* thing left: first the token ages, then the separators, then one more
|
|
239
|
+
* character of the longest names.
|
|
240
|
+
* @example
|
|
241
|
+
* skillLayouts(3).length // 3
|
|
242
|
+
*/
|
|
243
|
+
function skillLayouts(longestName: number): SkillLayout[] {
|
|
244
|
+
const truncations = Array.from(
|
|
245
|
+
{ length: Math.max(0, longestName - MIN_TRUNCATED_NAME_LENGTH) },
|
|
246
|
+
(_, index) => ({ ages: false, separator: NARROW_SEPARATOR, maxNameLength: longestName - 1 - index }),
|
|
247
|
+
);
|
|
248
|
+
return [
|
|
249
|
+
{ ages: true, separator: WIDE_SEPARATOR, maxNameLength: longestName },
|
|
250
|
+
{ ages: false, separator: WIDE_SEPARATOR, maxNameLength: longestName },
|
|
251
|
+
{ ages: false, separator: NARROW_SEPARATOR, maxNameLength: longestName },
|
|
252
|
+
...truncations,
|
|
253
|
+
];
|
|
254
|
+
}
|
|
255
|
+
|
|
58
256
|
function renderExtensionStatuses(
|
|
59
257
|
extensionStatuses: ReadonlyMap<string, string>,
|
|
60
258
|
width: number,
|
|
@@ -99,16 +297,14 @@ function trimSessionPrefix(session: string, cwd: string): string {
|
|
|
99
297
|
: session;
|
|
100
298
|
}
|
|
101
299
|
|
|
102
|
-
type
|
|
300
|
+
type DisplaySession = { name: string | null; id: string | null };
|
|
103
301
|
|
|
104
|
-
function
|
|
105
|
-
const customName = pi.getSessionName?.() || null;
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
const idPart = sessionId ? [{ text: sessionId, token: "dim" } as const] : [];
|
|
111
|
-
return [namePart, ...idPart];
|
|
302
|
+
function getDisplaySession(ctx: any, cwd: string, pi: ExtensionAPI): DisplaySession {
|
|
303
|
+
const customName: string | null = pi.getSessionName?.() || null;
|
|
304
|
+
return {
|
|
305
|
+
name: customName && trimSessionPrefix(customName, cwd),
|
|
306
|
+
id: ctx.sessionManager?.getSessionId?.() || null,
|
|
307
|
+
};
|
|
112
308
|
}
|
|
113
309
|
|
|
114
310
|
function getSkillLocations(pi: ExtensionAPI): SkillLocation[] {
|
|
@@ -122,7 +318,6 @@ function getSkillLocations(pi: ExtensionAPI): SkillLocation[] {
|
|
|
122
318
|
// ── ANSI color helpers ──────────────────────────────────────────────────────
|
|
123
319
|
|
|
124
320
|
type RgbColor = { red: number; green: number; blue: number };
|
|
125
|
-
type RgbTokens = { dim: RgbColor | null; muted: RgbColor | null; warning: RgbColor | null; error: RgbColor | null };
|
|
126
321
|
|
|
127
322
|
function ansi256ToRgb(index: number): RgbColor {
|
|
128
323
|
const basicPalette: RgbColor[] = [
|
|
@@ -167,7 +362,12 @@ function ansiCodeToRgb(code: number): RgbColor | null {
|
|
|
167
362
|
}
|
|
168
363
|
|
|
169
364
|
function getThemeTokenRgb(theme: any, token: string): RgbColor | null {
|
|
170
|
-
|
|
365
|
+
let themed: string;
|
|
366
|
+
try {
|
|
367
|
+
themed = typeof theme.getFgAnsi === "function" ? theme.getFgAnsi(token) : theme.fg(token, "x");
|
|
368
|
+
} catch {
|
|
369
|
+
return null;
|
|
370
|
+
}
|
|
171
371
|
const matches = [...themed.matchAll(/\x1b\[([0-9;]+)m/g)];
|
|
172
372
|
for (let i = matches.length - 1; i >= 0; i -= 1) {
|
|
173
373
|
const codes = matches[i]?.[1]?.split(";").map(Number) ?? [];
|
|
@@ -199,38 +399,65 @@ function blendRgb(start: RgbColor, end: RgbColor, ratio: number): RgbColor {
|
|
|
199
399
|
}
|
|
200
400
|
|
|
201
401
|
function colorizeRgb(text: string, rgb: RgbColor): string {
|
|
202
|
-
return `\x1b[38;2;${rgb.red};${rgb.green};${rgb.blue}m${text}${
|
|
402
|
+
return `\x1b[38;2;${rgb.red};${rgb.green};${rgb.blue}m${text}${ANSI_FG_RESET}`;
|
|
203
403
|
}
|
|
204
404
|
|
|
205
|
-
function
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
);
|
|
232
|
-
|
|
233
|
-
|
|
405
|
+
function nearestStop(stops: readonly Stop[], value: number): Stop {
|
|
406
|
+
return stops.reduce((best, stop) =>
|
|
407
|
+
Math.abs(stop.at - value) < Math.abs(best.at - value) ? stop : best,
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
export interface Painter {
|
|
412
|
+
/** A family colour, held wherever that kind of fact appears. */
|
|
413
|
+
token(name: string, text: string): string;
|
|
414
|
+
/** The same family colour, receded, for a figure subordinate to another. */
|
|
415
|
+
quiet(name: string, text: string): string;
|
|
416
|
+
/** A measurement, coloured by where it sits on its own scale. */
|
|
417
|
+
ramp(stops: readonly Stop[], value: number, text: string): string;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Resolve every colour the footer needs from the live theme.
|
|
422
|
+
*
|
|
423
|
+
* A 256-colour terminal cannot render a blend, so both the ramp and the quiet
|
|
424
|
+
* variant fall back to the nearest whole theme token rather than emitting
|
|
425
|
+
* truecolor the terminal would drop.
|
|
426
|
+
*/
|
|
427
|
+
function makePainter(theme: any): Painter {
|
|
428
|
+
const blendable = theme.getColorMode?.() !== "256color";
|
|
429
|
+
const resolved = new Map<string, RgbColor | null>();
|
|
430
|
+
const rgbOf = (name: string): RgbColor | null => {
|
|
431
|
+
if (!resolved.has(name)) resolved.set(name, getThemeTokenRgb(theme, name));
|
|
432
|
+
return resolved.get(name) ?? null;
|
|
433
|
+
};
|
|
434
|
+
const quietRgb = (name: string): RgbColor | null => {
|
|
435
|
+
const base = rgbOf(name);
|
|
436
|
+
const dim = rgbOf("dim");
|
|
437
|
+
return base && dim ? blendRgb(base, dim, QUIET_BLEND) : null;
|
|
438
|
+
};
|
|
439
|
+
const stopRgb = (stop: Stop): RgbColor | null => (stop.quiet ? quietRgb(stop.token) : rgbOf(stop.token));
|
|
440
|
+
|
|
441
|
+
return {
|
|
442
|
+
token: (name, text) => theme.fg(name, text),
|
|
443
|
+
quiet(name, text) {
|
|
444
|
+
const rgb = blendable ? quietRgb(name) : null;
|
|
445
|
+
return rgb ? colorizeRgb(text, rgb) : theme.fg("dim", text);
|
|
446
|
+
},
|
|
447
|
+
ramp(stops, value, text) {
|
|
448
|
+
const clamped = Math.min(Math.max(value, stops[0]!.at), stops[stops.length - 1]!.at);
|
|
449
|
+
for (let i = 0; i < stops.length - 1; i += 1) {
|
|
450
|
+
const low = stops[i]!;
|
|
451
|
+
const high = stops[i + 1]!;
|
|
452
|
+
if (clamped > high.at) continue;
|
|
453
|
+
const lowRgb = stopRgb(low);
|
|
454
|
+
const highRgb = stopRgb(high);
|
|
455
|
+
if (!blendable || !lowRgb || !highRgb) return theme.fg(nearestStop(stops, clamped).token, text);
|
|
456
|
+
return colorizeRgb(text, blendRgb(lowRgb, highRgb, (clamped - low.at) / (high.at - low.at)));
|
|
457
|
+
}
|
|
458
|
+
return theme.fg(stops[stops.length - 1]!.token, text);
|
|
459
|
+
},
|
|
460
|
+
};
|
|
234
461
|
}
|
|
235
462
|
|
|
236
463
|
function ctxGauge(pct: number): string {
|
|
@@ -241,8 +468,7 @@ function ctxGauge(pct: number): string {
|
|
|
241
468
|
return CONTEXT_GAUGE_SYMBOLS[stepIndex]!;
|
|
242
469
|
}
|
|
243
470
|
|
|
244
|
-
function renderThinkingLabel(theme: any, level: string, full: boolean): string
|
|
245
|
-
if (!level) return null;
|
|
471
|
+
function renderThinkingLabel(theme: any, level: string, full: boolean): string {
|
|
246
472
|
const info = THINKING_LEVELS[level];
|
|
247
473
|
if (!info) return level;
|
|
248
474
|
const label = full ? info.full : info.short;
|
|
@@ -348,7 +574,7 @@ interface FooterSnapshot {
|
|
|
348
574
|
model: ExtensionContext["model"];
|
|
349
575
|
stats: SessionStats;
|
|
350
576
|
context: ReturnType<typeof getContextPercent>;
|
|
351
|
-
|
|
577
|
+
session: DisplaySession;
|
|
352
578
|
}
|
|
353
579
|
|
|
354
580
|
// ── Extension entry point ───────────────────────────────────────────────────
|
|
@@ -392,123 +618,104 @@ export default function (pi: ExtensionAPI) {
|
|
|
392
618
|
snapshot = {
|
|
393
619
|
manager, sessionId, leafId, model, stats,
|
|
394
620
|
context: getContextPercent(currentCtx, stats.lastContextTokens, usage),
|
|
395
|
-
|
|
621
|
+
session: getDisplaySession(currentCtx, currentCtx.cwd || ".", pi),
|
|
396
622
|
};
|
|
397
623
|
return snapshot;
|
|
398
624
|
}
|
|
399
625
|
|
|
400
|
-
function renderAbove(width: number, theme:
|
|
626
|
+
function renderAbove(width: number, terminalHeight: number, theme: Theme): string[] {
|
|
401
627
|
if (!currentCtx) return [];
|
|
402
|
-
const
|
|
403
|
-
const
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
628
|
+
const paint = makePainter(theme);
|
|
629
|
+
const { session } = getSnapshot();
|
|
630
|
+
// Weight is reserved for the pill and for thinking at high and above, so
|
|
631
|
+
// the model carries its family hue and nothing more.
|
|
632
|
+
const headerFacts: Partial<Record<HeaderFact, FactForms>> = {
|
|
633
|
+
model: { full: paint.token(MACHINE_TOKEN, formatModel(currentCtx.model)) },
|
|
634
|
+
thinking: { full: renderThinkingLabel(theme, thinkingLevel, true), short: renderThinkingLabel(theme, thinkingLevel, false) },
|
|
635
|
+
sessionName: session.name ? { full: theme.fg("text", session.name) } : undefined,
|
|
636
|
+
sessionId: session.id
|
|
637
|
+
? { full: theme.fg("dim", session.id), short: theme.fg("dim", session.id.split("-").slice(0, 2).join("-")) }
|
|
638
|
+
: undefined,
|
|
639
|
+
};
|
|
640
|
+
// No background here: the cwd pill is the one filled element in the whole
|
|
641
|
+
// footer, and a second pill style would leave neither of them the anchor.
|
|
642
|
+
const renderSkillLines = (layout: SkillLayout) => wrapJoinedLines(
|
|
643
|
+
loadedSkills.map(({ name, tokenAge }) => {
|
|
644
|
+
const nameText = paint.token(SKILL_TOKEN, middleTruncate(name, layout.maxNameLength));
|
|
645
|
+
const ageText = layout.ages && tokenAge !== null ? theme.fg("dim", ` -${formatTokens(tokenAge)}`) : "";
|
|
646
|
+
return `${nameText}${ageText}`;
|
|
647
|
+
}),
|
|
648
|
+
width,
|
|
649
|
+
theme.fg("dim", layout.separator),
|
|
650
|
+
);
|
|
651
|
+
// A short terminal takes the fullest layout that fits the row limit. Hiding
|
|
652
|
+
// skills behind a count is the last resort, after the most compact layout.
|
|
653
|
+
const shortTerminal = terminalHeight <= SHORT_TERMINAL_HEIGHT;
|
|
654
|
+
const layouts = skillLayouts(Math.max(0, ...loadedSkills.map(({ name }) => name.length)));
|
|
655
|
+
const candidates = shortTerminal ? layouts : layouts.slice(0, 1);
|
|
656
|
+
const layout = candidates.find((candidate) => renderSkillLines(candidate).length <= SKILL_COLLAPSE_ROW_THRESHOLD) ?? candidates.at(-1)!;
|
|
657
|
+
const skillLines = renderSkillLines(layout);
|
|
658
|
+
const collapseSkills = shortTerminal && skillLines.length > SKILL_COLLAPSE_ROW_THRESHOLD;
|
|
659
|
+
const visibleSkillLines = collapseSkills ? skillLines.slice(0, COLLAPSED_SKILL_ROWS) : skillLines;
|
|
660
|
+
const hiddenSkillCount = skillLines.slice(visibleSkillLines.length).reduce((count, line) => count + line.partCount, 0);
|
|
661
|
+
const overflowLines = hiddenSkillCount === 0 ? [] : [truncateToWidth(theme.fg("dim", ` +${hiddenSkillCount} skills`), width)];
|
|
414
662
|
return [
|
|
415
|
-
...
|
|
416
|
-
...
|
|
663
|
+
...fitFacts(headerFacts, HEADER_LADDER, width, theme),
|
|
664
|
+
...visibleSkillLines.map((line) => line.text),
|
|
665
|
+
...overflowLines,
|
|
417
666
|
];
|
|
418
667
|
}
|
|
419
668
|
|
|
420
669
|
function renderBelow(width: number, theme: any): string[] {
|
|
421
670
|
if (!currentCtx) return [];
|
|
422
671
|
const { stats, context: { pct, window, tokens } } = getSnapshot();
|
|
423
|
-
const
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
function renderFooterWide(
|
|
435
|
-
width: number,
|
|
436
|
-
theme: any,
|
|
437
|
-
stats: SessionStats,
|
|
438
|
-
pct: number,
|
|
439
|
-
window: number,
|
|
440
|
-
tokens: number,
|
|
441
|
-
rgbTokens: RgbTokens,
|
|
442
|
-
): string[] {
|
|
443
|
-
const separator = theme.fg("dim", WIDE_SEPARATOR);
|
|
444
|
-
const label = (text: string) => theme.fg("dim", text.padEnd(8));
|
|
445
|
-
const cwd = currentCtx.cwd || ".";
|
|
446
|
-
|
|
447
|
-
const locationParts: string[] = [];
|
|
448
|
-
if (gitBranch) {
|
|
449
|
-
const dirty = gitDirty ? theme.fg("warning", "*") : "";
|
|
450
|
-
locationParts.push(`${theme.fg("accent", ` ${gitBranch}`)}${dirty}`);
|
|
451
|
-
}
|
|
452
|
-
locationParts.push(theme.bg("selectedBg", theme.fg("text", ` ${currentDirectoryName(cwd)}/ `)));
|
|
453
|
-
const locationLine = truncateToWidth(` ${locationParts.join(separator)}`, width);
|
|
454
|
-
|
|
455
|
-
const gauge = renderContextUsage(theme, pct, `${ctxGauge(pct)} ${Math.round(pct)}%`, rgbTokens);
|
|
456
|
-
const contextValue = theme.fg("text", `${formatTokens(tokens)} / ${formatTokens(window)} tokens`);
|
|
672
|
+
const paint = makePainter(theme);
|
|
673
|
+
const label = (text: string) => theme.fg("dim", `${text} `);
|
|
674
|
+
const pill = (text: string) => theme.bg("selectedBg", paint.token(PLACE_TOKEN, theme.bold(text)));
|
|
675
|
+
const directory = currentDirectoryName(currentCtx.cwd || ".");
|
|
676
|
+
// A dirty worktree is a property of the place, not a cost, so it stays in
|
|
677
|
+
// the place family and leaves `warning` meaning spend alone.
|
|
678
|
+
const dirty = gitDirty ? paint.token(PLACE_TOKEN, "*") : "";
|
|
679
|
+
const gauge = paint.ramp(CONTEXT_STOPS, pct, `${ctxGauge(pct)} ${Math.round(pct)}%`);
|
|
680
|
+
// Colour the figure that moves and leave its reference quiet, so the eye
|
|
681
|
+
// lands on the reading rather than on the window size beside it.
|
|
682
|
+
const usedTokens = paint.ramp(CONTEXT_STOPS, pct, formatTokens(tokens));
|
|
457
683
|
const usingSubscription = currentCtx.model
|
|
458
684
|
? currentCtx.modelRegistry?.isUsingOAuth?.(currentCtx.model)
|
|
459
685
|
: false;
|
|
460
|
-
const
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
const contextLine = truncateToWidth(
|
|
464
|
-
` ${label("context")}${gauge} ${contextValue}${separator}${costText}${subLabel} ${perTurn}`,
|
|
465
|
-
width,
|
|
466
|
-
);
|
|
467
|
-
|
|
686
|
+
const cost = (amount: string) => paint.token(SPEND_TOKEN, `$${amount}`) + (usingSubscription ? theme.fg("muted", " sub") : "");
|
|
687
|
+
// A hit rate is a measurement, so it rides the ramp. The old cliff at 80%
|
|
688
|
+
// made 79% and 20% look identical and threw the reading away.
|
|
468
689
|
const sessionHit = stats.sessionCacheHitRate;
|
|
469
|
-
const
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
const latestText = stats.latestCacheMissTokens != null
|
|
473
|
-
? theme.fg("warning", `latest miss ${formatTokens(stats.latestCacheMissTokens)} re-billed`)
|
|
474
|
-
: latestHit != null
|
|
475
|
-
? theme.fg(latestHit >= 80 ? "success" : "muted", `latest ${latestHit.toFixed(1)}%`)
|
|
476
|
-
: null;
|
|
477
|
-
const reuseSplit = theme.fg("dim", `${formatTokens(stats.totalReused)} reused / ${formatTokens(stats.totalFresh)} new`);
|
|
478
|
-
const cacheParts = [theme.fg(sessionHitToken, sessionHitText), latestText, reuseSplit].filter(Boolean);
|
|
479
|
-
const cacheLine = truncateToWidth(` ${label("cache")}${cacheParts.join(separator)}`, width);
|
|
480
|
-
|
|
481
|
-
return [locationLine, contextLine, cacheLine];
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
function renderFooterNarrow(
|
|
485
|
-
width: number,
|
|
486
|
-
theme: any,
|
|
487
|
-
stats: SessionStats,
|
|
488
|
-
pct: number,
|
|
489
|
-
window: number,
|
|
490
|
-
rgbTokens: RgbTokens,
|
|
491
|
-
): string[] {
|
|
492
|
-
const cwd = currentCtx.cwd || ".";
|
|
493
|
-
const locationParts: string[] = [];
|
|
494
|
-
if (gitBranch) {
|
|
495
|
-
const dirty = gitDirty ? theme.fg("warning", "*") : "";
|
|
496
|
-
locationParts.push(`${theme.fg("accent", gitBranch)}${dirty}`);
|
|
497
|
-
}
|
|
498
|
-
locationParts.push(theme.fg("text", `${currentDirectoryName(cwd)}/`));
|
|
499
|
-
const locationLine = truncateToWidth(` ${locationParts.join(NARROW_SEPARATOR)}`, width);
|
|
500
|
-
|
|
501
|
-
const gauge = renderContextUsage(theme, pct, `${Math.round(pct)}%/${formatTokens(window)}`, rgbTokens);
|
|
690
|
+
const sessionHitText = sessionHit != null
|
|
691
|
+
? paint.ramp(CACHE_STOPS, sessionHit, `${sessionHit.toFixed(1)}% session`)
|
|
692
|
+
: theme.fg("dim", "no cache yet");
|
|
502
693
|
const latestHit = stats.latestCacheHitRate;
|
|
503
|
-
const
|
|
504
|
-
|
|
505
|
-
:
|
|
506
|
-
?
|
|
507
|
-
:
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
694
|
+
const missedTokens = stats.latestCacheMissTokens;
|
|
695
|
+
const footerFacts: Partial<Record<FooterFact, FactForms>> = {
|
|
696
|
+
branch: gitBranch
|
|
697
|
+
? { full: `${paint.token(PLACE_TOKEN, `${BRANCH_GLYPH} ${gitBranch}`)}${dirty}`, bare: `${paint.token(PLACE_TOKEN, gitBranch)}${dirty}` }
|
|
698
|
+
: undefined,
|
|
699
|
+
cwd: { full: pill(` ${directory}/ `), short: pill(`${directory}/`) },
|
|
700
|
+
context: { full: `${label("context")}${gauge}`, bare: gauge },
|
|
701
|
+
tokens: {
|
|
702
|
+
full: `${usedTokens}${theme.fg("dim", ` / ${formatTokens(window)} tokens`)}`,
|
|
703
|
+
short: `${usedTokens}${theme.fg("dim", ` / ${formatTokens(window)}`)}`,
|
|
704
|
+
},
|
|
705
|
+
cost: { full: cost(formatCost(stats.totalCost)), short: cost(stats.totalCost.toFixed(1)), bare: cost(stats.totalCost.toFixed(0)) },
|
|
706
|
+
perTurn: { full: paint.quiet(SPEND_TOKEN, `$${formatCost(stats.averageCostPerTurn)}/turn`) },
|
|
707
|
+
cacheSession: { full: `${label("cache")}${sessionHitText}`, bare: sessionHitText },
|
|
708
|
+
latest: missedTokens != null
|
|
709
|
+
? { full: theme.fg("error", `latest miss ${formatTokens(missedTokens)} re-billed`), short: theme.fg("error", `miss ${formatTokens(missedTokens)}`) }
|
|
710
|
+
: latestHit != null
|
|
711
|
+
? { full: paint.ramp(CACHE_STOPS, latestHit, `latest ${latestHit.toFixed(1)}%`), short: paint.ramp(CACHE_STOPS, latestHit, `latest ${Math.round(latestHit)}%`) }
|
|
712
|
+
: undefined,
|
|
713
|
+
reuse: {
|
|
714
|
+
full: paint.ramp(CACHE_STOPS, sessionHit ?? 0, formatTokens(stats.totalReused)) +
|
|
715
|
+
theme.fg("dim", ` reused / ${formatTokens(stats.totalFresh)} new`),
|
|
716
|
+
},
|
|
717
|
+
};
|
|
718
|
+
return fitFacts(footerFacts, FOOTER_LADDER, width, theme);
|
|
512
719
|
}
|
|
513
720
|
|
|
514
721
|
// ── Git ───────────────────────────────────────────────────────────────
|
|
@@ -577,11 +784,11 @@ export default function (pi: ExtensionAPI) {
|
|
|
577
784
|
|
|
578
785
|
ctx.ui.setWidget(
|
|
579
786
|
"custom-footer-above",
|
|
580
|
-
(
|
|
787
|
+
(tui: TUI, theme: Theme) => ({
|
|
581
788
|
dispose() {},
|
|
582
789
|
invalidate() {},
|
|
583
790
|
render(width: number): string[] {
|
|
584
|
-
return renderAbove(width, theme);
|
|
791
|
+
return renderAbove(width, tui.terminal.rows, theme);
|
|
585
792
|
},
|
|
586
793
|
}),
|
|
587
794
|
{ placement: "aboveEditor" },
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@giladbarnea/pi-custom-footer",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Context, cache, cost, and loaded skills at a glance in Pi.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"LICENSE"
|
|
29
29
|
],
|
|
30
30
|
"scripts": {
|
|
31
|
-
"test": "node tests/skills.mjs && node tests/cache.mjs && node tests/load.mjs",
|
|
31
|
+
"test": "node tests/skills.mjs && node tests/cache.mjs && node tests/layout.mjs && node tests/load.mjs",
|
|
32
32
|
"check": "tsc --noEmit"
|
|
33
33
|
},
|
|
34
34
|
"engines": {
|