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 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";
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;
24 31 type FooterZone = "workspace" | "llm";
37 44 type SegmentUpdate = Segment | { id
: string; text
: undefined };
39 -type FooterConfig = {
40 - separatorColor
: string;
45 -const DEFAULT_CONFIG
: FooterConfig = {
46 - separatorColor:
"borderMuted",
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)} `;
56 +function sanitizeStatus(text
: string)
: string {
57 + return text.
replace(
/[\r\n\t]/g,
" ").
replace(
/ +/g,
" ").
trim();
52 61 const HIDDEN_FOOTER
= {
55 64 render(): string[] {
return []; },
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;
68 78 function handleSegment(data
: unknown) {
69 79 const payload
= data
as SegmentUpdate;
82 90 pi.events.
on(
"footer:segment", handleSegment);
86 94 function scheduleRender() {
87 95 if (renderScheduled)
return;
88 96 if (
!requestRender) {
90 - if (segments.
size > 0)
setTimeout(()
=> scheduleRender(),
50);
97 + if (segments.
size > 0)
setTimeout(()
=> scheduleRender(), RETRY_MOUNT_MS);
93 100 renderScheduled
= true;
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);
107 - segs.
push({ id:
"session-name", text: name, icon:
"✎", color:
"accent", zone:
"workspace", order:
0.5 });
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,
" | ");
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);
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();
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);
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));
176 181 return [line1, line2];
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))
190 + return theme.
fg(STATUS_COLOR, parts);
193 + function renderFooterLines(
196 + ctx
: ExtensionContext,
197 + statuses
: ReadonlyMap<
string,
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);
205 + padLine(workspaceLine, width),
206 + padLine(llmLine1, width),
207 + padLine(llmLine2, width),
208 + padLine(statusLine, width),
181 214 function mountFooter() {
182 215 if (
!currentCtx)
return;
185 218 requestRender
= undefined;
186 219 currentCtx.ui.
setFooter((tui)
=> {
188 220 tui.
requestRender(
true);
189 221 return HIDDEN_FOOTER;
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)} `;
211 - const workspaceLine
= renderWorkspaceLine(theme, contentWidth, currentCtx
!);
212 - const [llmLine1, llmLine2]
= renderLlmLines(theme, contentWidth);
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())
222 - statusLine
= theme.
fg(
"dim" as ThemeColor, parts);
227 - padLine(workspaceLine),
230 - padLine(statusLine),
237 + return renderFooterLines(theme, width, currentCtx
!, footerData.
getExtensionStatuses());
237 242 setTimeout(()
=> scheduleRender(),
0);
242 247 function toggleVisibility(ctx
: ExtensionContext) {
243 248 hidden
= !hidden;
256 261 handler:
async (ctx)
=> toggleVisibility(ctx),
261 266 pi.
on(
"session_start",
async (_event, ctx)
=> {
262 267 if (
!ctx.hasUI)
return;
pi/agent/extensions/footer/progress.ts
Modified 9 7 import type { Theme, ThemeColor }
from "@mariozechner/pi-coding-agent";
11 -export type BarStyle = "continuous" | "blocks";
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));
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);
32 - return filledStr
+ partial
+ " ".
repeat(emptyCount);
9 +const GLYPHS
= [
" ",
"▁",
"▂",
"▃",
"▄",
"▅",
"▆",
"▇",
"█"];
10 +const MAX_LEVEL
= GLYPHS.
length - 1;
11 +const RESET
= "\x1b[39m\x1b[49m";
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(
"");