repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

refactor(pi/footer): clean sweep — kill dead code, extract helpers, name constants, type properly

b18f0e23bc26ef9b12ba56f8e33b498bb4fe8f00

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

- Delete renderContinuousBar (unused), BarStyle type, FooterConfig/DEFAULT_CONFIG/config indirection
- Type Segment.color as ThemeColor, remove 5 unsafe casts
- Merge duplicate pi-tui imports
- Extract padLine, sanitizeStatus, joinSegments, renderStatusLine, renderFooterLines as named helpers
- Inline session-name injection into renderWorkspaceLine (no try/catch)
- Name magic values: SEPARATOR_COLOR, BAR_WIDTH, RETRY_MOUNT_MS
- Name magic glyph math in progress.ts: MAX_LEVEL, RESET

Changed files (2)

pi/agent/extensions/footer/index.ts

Modified
4 4 * Footer renderer extension.
6 6 * Listens for "footer:segment" events from producer extensions and renders
7 7 * a 4-line footer with stable height:
8 - * line 0 = workspace zone (cwd, branch, PR, runtime)
8 + * line 0 = workspace zone (cwd, branch, PR, runtime, session name)
9 9 * line 1-2 = llm zone (model, thinking, context, tokens, cost, timing) — flow layout
10 10 * line 3 = extension statuses via ctx.ui.setStatus()
17 17 import type { ExtensionAPI, ExtensionContext, Theme, ThemeColor } from "@mariozechner/pi-coding-agent";
18 -import { Key } from "@mariozechner/pi-tui";
19 -import { truncateToWidth, visibleWidth } from "@mariozechner/pi-tui";
20 -import { renderContinuousBar, renderBlocksBar, type BarStyle } from "./progress";
18 +import { Key, truncateToWidth, visibleWidth } from "@mariozechner/pi-tui";
19 +import { renderBlocksBar } from "./progress";
22 -// ── Footer segment types ──────────────────────────────────────────
21 +// ── Constants ─────────────────────────────────────────────────────
23 +const SEPARATOR_COLOR = "borderMuted" as ThemeColor;
24 +const DEFAULT_SEGMENT_COLOR = "muted" as ThemeColor;
25 +const STATUS_COLOR = "dim" as ThemeColor;
26 +const BAR_WIDTH = 10;
27 +const RETRY_MOUNT_MS = 50;
29 +// ── Types ─────────────────────────────────────────────────────────
24 31 type FooterZone = "workspace" | "llm";
27 34 id: string;
28 35 text: string;
29 36 icon?: string;
30 - color?: string;
37 + color?: ThemeColor;
31 38 bar?: number;
32 39 suffix?: string;
33 40 zone: FooterZone;
37 44 type SegmentUpdate = Segment | { id: string; text: undefined };
39 -type FooterConfig = {
40 - separatorColor: string;
41 - barStyle: BarStyle;
42 - barWidth: number;
43 -};
46 +// ── Pure helpers ──────────────────────────────────────────────────
45 -const DEFAULT_CONFIG: FooterConfig = {
46 - separatorColor: "borderMuted",
47 - barStyle: "blocks",
48 - barWidth: 10,
49 -};
48 +/** Pad line with side margins, truncating to fit terminal width. */
49 +function padLine(line: string, width: number): string {
50 + if (width <= 1) return truncateToWidth(line, width);
51 + const contentWidth = width - 2;
52 + return ` ${truncateToWidth(line, contentWidth)} `;
55 +/** Collapse whitespace and strip control characters for single-line display. */
56 +function sanitizeStatus(text: string): string {
57 + return text.replace(/[\r\n\t]/g, " ").replace(/ +/g, " ").trim();
51 60 /** Zero-height footer stub for hidden mode. */
52 61 const HIDDEN_FOOTER = {
55 64 render(): string[] { return []; },
67 +// ── Extension ─────────────────────────────────────────────────────
58 69 export default function footerExtension(pi: ExtensionAPI) {
59 70 const segments = new Map<string, Segment>();
60 - let config: FooterConfig = { ...DEFAULT_CONFIG };
61 71 let requestRender: (() => void) | undefined;
62 72 let renderScheduled = false;
63 73 let hidden = false;
64 74 let currentCtx: ExtensionContext | undefined;
66 - // ── Event listener ────────────────────────────────────────────────
76 + // ── Segment handling ──────────────────────────────────────────
68 78 function handleSegment(data: unknown) {
69 79 const payload = data as SegmentUpdate;
77 87 scheduleRender();
80 - // Subscribe at load time so producers can emit during their session_start
81 - // before footer's session_start runs (load order may vary).
82 90 pi.events.on("footer:segment", handleSegment);
84 - // ── Coalesced update ──────────────────────────────────────────────
92 + // ── Coalesced render scheduling ───────────────────────────────
86 94 function scheduleRender() {
87 95 if (renderScheduled) return;
88 96 if (!requestRender) {
89 - // Footer not mounted yet — retry after a tick
90 - if (segments.size > 0) setTimeout(() => scheduleRender(), 50);
97 + if (segments.size > 0) setTimeout(() => scheduleRender(), RETRY_MOUNT_MS);
91 98 return;
93 100 renderScheduled = true;
100 - // ── Render helpers ────────────────────────────────────────────────
107 + // ── Segment queries ───────────────────────────────────────────
102 - function getZoneSegments(zone: FooterZone, ctx?: ExtensionContext): Segment[] {
103 - const segs = Array.from(segments.values()).filter((s) => s.zone === zone);
104 - if (zone === "workspace" && ctx) {
105 - const name = getSessionName(ctx);
106 - if (name) {
107 - segs.push({ id: "session-name", text: name, icon: "✎", color: "accent", zone: "workspace", order: 0.5 });
108 - }
109 - }
110 - return segs.sort((a, b) => (a.order ?? 50) - (b.order ?? 50));
109 + function getZoneSegments(zone: FooterZone): Segment[] {
110 + return Array.from(segments.values())
111 + .filter((s) => s.zone === zone)
112 + .sort((a, b) => (a.order ?? 50) - (b.order ?? 50));
113 - function separator(theme: Theme): string {
114 - return theme.fg(config.separatorColor as ThemeColor, " | ");
115 - }
115 + // ── Segment rendering ─────────────────────────────────────────
117 117 function renderSegment(seg: Segment, theme: Theme): string {
118 - const color = (seg.color ?? "muted") as ThemeColor;
118 + const color = seg.color ?? DEFAULT_SEGMENT_COLOR;
119 119 const parts: string[] = [];
121 121 if (seg.icon) parts.push(theme.fg(color, seg.icon));
122 122 if (seg.text) parts.push(theme.fg(color, seg.text));
124 124 if (seg.bar !== undefined) {
125 - const bar = config.barStyle === "blocks"
126 - ? renderBlocksBar(seg.bar, config.barWidth, theme, color)
127 - : renderContinuousBar(seg.bar, config.barWidth, theme, color);
125 + const bar = renderBlocksBar(seg.bar, BAR_WIDTH, theme, color);
128 126 const suffix = seg.suffix ? theme.fg(color, seg.suffix) : "";
129 127 parts.push(bar + suffix);
130 128 } else if (seg.suffix) {
134 132 return parts.join(" ");
137 - function getSessionName(ctx: ExtensionContext): string | undefined {
138 - try { return ctx.sessionManager.getSessionName(); } catch { return undefined; }
135 + function joinSegments(segs: Segment[], sep: string, theme: Theme): string {
136 + return segs.map((s) => renderSegment(s, theme)).join(sep);
139 + // ── Zone rendering ────────────────────────────────────────────
141 141 function renderWorkspaceLine(theme: Theme, width: number, ctx: ExtensionContext): string {
142 - const segs = getZoneSegments("workspace", ctx);
142 + const segs = getZoneSegments("workspace");
143 + const name = ctx.sessionManager.getSessionName();
144 + if (name) {
145 + segs.push({ id: "session-name", text: name, icon: "✎", color: "accent" as ThemeColor, zone: "workspace", order: 0.5 });
146 + segs.sort((a, b) => (a.order ?? 50) - (b.order ?? 50));
143 148 if (segs.length === 0) return "";
144 - const sep = separator(theme);
145 - return truncateToWidth(segs.map((s) => renderSegment(s, theme)).join(sep), width);
149 + const sep = theme.fg(SEPARATOR_COLOR, " | ");
150 + return truncateToWidth(joinSegments(segs, sep, theme), width);
148 - /** Flow layout: fill line 1, overflow to line 2, truncate line 2 if needed. */
153 + /** Flow layout: fill line 1, overflow to line 2, truncate line 2. */
149 154 function renderLlmLines(theme: Theme, width: number): [string, string] {
150 155 const segs = getZoneSegments("llm");
151 156 if (segs.length === 0) return ["", ""];
153 - const sep = separator(theme);
158 + const sep = theme.fg(SEPARATOR_COLOR, " | ");
154 159 const rendered = segs.map((s) => renderSegment(s, theme));
156 161 let line1 = "";
176 181 return [line1, line2];
179 - // ── Footer mounting ───────────────────────────────────────────────
184 + function renderStatusLine(theme: Theme, statuses: ReadonlyMap<string, string>): string {
185 + if (statuses.size === 0) return "";
186 + const parts = Array.from(statuses.entries())
187 + .sort(([a], [b]) => a.localeCompare(b))
188 + .map(([, t]) => sanitizeStatus(t))
189 + .join(" · ");
190 + return theme.fg(STATUS_COLOR, parts);
193 + function renderFooterLines(
194 + theme: Theme,
195 + width: number,
196 + ctx: ExtensionContext,
197 + statuses: ReadonlyMap<string, string>,
198 + ): string[] {
199 + const contentWidth = Math.max(0, width - 2);
200 + const workspaceLine = renderWorkspaceLine(theme, contentWidth, ctx);
201 + const [llmLine1, llmLine2] = renderLlmLines(theme, contentWidth);
202 + const statusLine = renderStatusLine(theme, statuses);
204 + return [
205 + padLine(workspaceLine, width),
206 + padLine(llmLine1, width),
207 + padLine(llmLine2, width),
208 + padLine(statusLine, width),
209 + ];
212 + // ── Mounting ──────────────────────────────────────────────────
181 214 function mountFooter() {
182 215 if (!currentCtx) return;
184 217 if (hidden) {
185 218 requestRender = undefined;
186 219 currentCtx.ui.setFooter((tui) => {
187 - // Force full redraw to clear old footer lines
188 220 tui.requestRender(true);
189 221 return HIDDEN_FOOTER;
203 235 invalidate() {},
204 236 render(width: number): string[] {
205 - const contentWidth = Math.max(0, width - 2);
206 - const padLine = (line: string) => {
207 - if (width <= 1) return truncateToWidth(line, width);
208 - return ` ${truncateToWidth(line, contentWidth)} `;
209 - };
211 - const workspaceLine = renderWorkspaceLine(theme, contentWidth, currentCtx!);
212 - const [llmLine1, llmLine2] = renderLlmLines(theme, contentWidth);
214 - // Extension statuses (3rd party, via setStatus)
215 - const extStatuses = footerData.getExtensionStatuses();
216 - let statusLine = "";
217 - if (extStatuses.size > 0) {
218 - const parts = Array.from(extStatuses.entries())
219 - .sort(([a], [b]) => a.localeCompare(b))
220 - .map(([, t]) => t.replace(/[\r\n\t]/g, " ").replace(/ +/g, " ").trim())
221 - .join(" · ");
222 - statusLine = theme.fg("dim" as ThemeColor, parts);
223 - }
225 - // Always return exactly 4 lines for stable height
226 - return [
227 - padLine(workspaceLine),
228 - padLine(llmLine1),
229 - padLine(llmLine2),
230 - padLine(statusLine),
231 - ];
237 + return renderFooterLines(theme, width, currentCtx!, footerData.getExtensionStatuses());
236 - // Force render of segments emitted before mount
237 242 setTimeout(() => scheduleRender(), 0);
240 - // ── Toggle ────────────────────────────────────────────────────────
245 + // ── Toggle ────────────────────────────────────────────────────
242 247 function toggleVisibility(ctx: ExtensionContext) {
243 248 hidden = !hidden;
256 261 handler: async (ctx) => toggleVisibility(ctx),
259 - // ── Lifecycle ─────────────────────────────────────────────────────
264 + // ── Lifecycle ─────────────────────────────────────────────────
261 266 pi.on("session_start", async (_event, ctx) => {
262 267 if (!ctx.hasUI) return;

pi/agent/extensions/footer/progress.ts

Modified
2 - * Progress bar rendering with sub-character precision.
2 + * Progress bar rendering with discrete block characters.
4 - * Two styles:
5 - * - continuous: █ + partial-width glyphs ▏▎▍▌▋▊▉
6 - * - blocks: discrete partial-height glyphs ▁▂▃▄▅▆▇█ on dim background
4 + * Renders ▁▂▃▄▅▆▇█ glyphs on a dim background track.
9 7 import type { Theme, ThemeColor } from "@mariozechner/pi-coding-agent";
11 -export type BarStyle = "continuous" | "blocks";
13 -/** Render a continuous progress bar with sub-character precision. */
14 -export function renderContinuousBar(pct: number, width: number, theme: Theme, color: string): string {
15 - const clamped = Math.max(0, Math.min(100, pct));
16 - const filledFloat = (clamped / 100) * width;
17 - const filledFull = Math.floor(filledFloat);
18 - const remainder = filledFloat - filledFull;
20 - const levels = ["▏", "▎", "▍", "▌", "▋", "▊", "▉"];
21 - const filledStr = theme.fg(color as ThemeColor, "█".repeat(filledFull));
23 - let partial = "";
24 - let emptyCount = width - filledFull;
26 - if (remainder >= 0.0625 && filledFull < width) {
27 - const levelIndex = Math.max(0, Math.min(levels.length - 1, Math.round(remainder * 8) - 1));
28 - partial = theme.fg(color as ThemeColor, levels[levelIndex]!);
29 - emptyCount = Math.max(0, emptyCount - 1);
30 - }
32 - return filledStr + partial + " ".repeat(emptyCount);
33 -}
9 +const GLYPHS = [" ", "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"];
10 +const MAX_LEVEL = GLYPHS.length - 1;
11 +const RESET = "\x1b[39m\x1b[49m";
35 13 /** Render discrete block characters with dim background track. */
36 -export function renderBlocksBar(pct: number, segments: number, theme: Theme, color: string): string {
37 - const glyphs = [" ", "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"];
38 - const dimBg = fgToBgAnsi(theme.getFgAnsi("dim"));
39 - const fgColor = theme.getFgAnsi(color as ThemeColor);
40 - const reset = "\x1b[39m\x1b[49m";
14 +export function renderBlocksBar(pct: number, width: number, theme: Theme, color: ThemeColor): string {
41 15 const clamped = Math.max(0, Math.min(100, pct));
42 - const filledFloat = (clamped / 100) * segments;
16 + const filledFloat = (clamped / 100) * width;
17 + const dimBg = fgToBgAnsi(theme.getFgAnsi("dim"));
18 + const fgColor = theme.getFgAnsi(color);
44 20 const result: string[] = [];
45 - for (let i = 0; i < segments; i++) {
21 + for (let i = 0; i < width; i++) {
46 22 const blockFill = Math.max(0, Math.min(1, filledFloat - i));
47 - const level = Math.round(blockFill * 8);
48 - const glyph = glyphs[level];
49 - result.push(level > 0 ? `${dimBg}${fgColor}${glyph}${reset}` : `${dimBg}${glyph}${reset}`);
23 + const level = Math.round(blockFill * MAX_LEVEL);
24 + const glyph = GLYPHS[level];
25 + result.push(level > 0 ? `${dimBg}${fgColor}${glyph}${RESET}` : `${dimBg}${glyph}${RESET}`);
52 28 return result.join("");