dotfiles
bugabingas dorkfiles
owned by bugabinga
pi(ext/footer): semantic zones, flow layout, 4-line stable height, /footer toggle
933bcb65cabda4f3aef47c360b8e85fc575ca9b1
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
Browse filesCompare with HEAD
Changed files (2)
pi/agent/extensions/footer/index.ts
Addednew file mode 100644 14 +import type { ExtensionAPI, ExtensionContext, Theme, ThemeColor }
from "@mariozechner/pi-coding-agent";
15 +import { Key }
from "@mariozechner/pi-coding-agent";
16 +import { truncateToWidth, visibleWidth }
from "@mariozechner/pi-tui";
17 +import { renderContinuousBar, renderBlocksBar, type BarStyle }
from "./progress";
21 +type FooterZone = "workspace" | "llm";
34 +type SegmentUpdate = Segment | { id
: string; text
: undefined };
36 +type FooterConfig = {
37 + separatorColor
: string;
42 +const DEFAULT_CONFIG
: FooterConfig = {
43 + separatorColor:
"borderMuted",
48 +export default function footerExtension(pi
: ExtensionAPI) {
49 + const segments
= new Map<
string,
Segment>();
50 + let config
: FooterConfig = {
...DEFAULT_CONFIG };
51 + let requestRender
: (()
=> void)
| undefined;
52 + let renderScheduled
= false;
57 + function handleSegment(data
: unknown) {
58 + const payload
= data
as SegmentUpdate;
59 + if (
!payload?.
id)
return;
61 + if (
!(
"text" in payload)
|| payload.
text === undefined) {
62 + segments.
delete(payload.
id);
64 + segments.
set(payload.
id, payload
as Segment);
71 + pi.events.
on(
"footer:segment", handleSegment);
75 + function scheduleRender() {
76 + if (renderScheduled)
return;
77 + if (
!requestRender) {
79 + if (segments.
size > 0)
setTimeout(()
=> scheduleRender(),
50);
82 + renderScheduled
= true;
83 + queueMicrotask(()
=> {
84 + renderScheduled
= false;
91 + function getZoneSegments(zone
: FooterZone, ctx
?: ExtensionContext)
: Segment[] {
92 + const segs
= Array.
from(segments.
values()).
filter((s)
=> s.zone
=== zone);
93 + if (zone
=== "workspace" && ctx) {
99 + const name
= getSessionName(ctx);
101 + segs.
push({ id:
"session-name", text: name, icon:
"✎", color:
"accent", zone:
"workspace", order:
0.5 });
104 + return segs.
sort((a, b)
=> (a.order
?? 50)
- (b.order
?? 50));
107 + function separator(theme
: Theme)
: string {
108 + return theme.
fg(config.separatorColor
as ThemeColor,
" | ");
111 + function renderSegment(seg
: Segment, theme
: Theme)
: string {
112 + const color
= (seg.
color ?? "muted")
as ThemeColor;
113 + const parts
: string[]
= [];
115 + if (seg.icon) parts.
push(theme.
fg(color, seg.icon));
116 + if (seg.
text) parts.
push(theme.
fg(color, seg.
text));
118 + if (seg.bar
!== undefined) {
119 + const bar
= config.barStyle
=== "blocks" 120 + ? renderBlocksBar(seg.bar, config.barWidth, theme, color)
121 + : renderContinuousBar(seg.bar, config.barWidth, theme, color);
123 + const suffix
= seg.suffix
? theme.
fg(color, seg.suffix)
: "";
124 + parts.
push(bar
+ suffix);
125 + }
else if (seg.suffix) {
126 + parts.
push(theme.
fg(color, seg.suffix));
129 + return parts.
join(
" ");
132 + function getSessionName(ctx
: ExtensionContext)
: string | undefined {
133 + try {
return ctx.sessionManager.
getSessionName(); }
catch {
return undefined; }
136 + function renderWorkspaceLine(theme
: Theme, width
: number, ctx
: ExtensionContext)
: string {
137 + const segs
= getZoneSegments(
"workspace", ctx);
138 + if (segs.
length === 0)
return "";
139 + const sep
= separator(theme);
140 + return truncateToWidth(segs.
map((s)
=> renderSegment(s, theme)).
join(sep), width);
144 + function renderLlmLines(theme
: Theme, width
: number)
: [
string,
string] {
145 + const segs
= getZoneSegments(
"llm");
146 + if (segs.
length === 0)
return [
"",
""];
148 + const sep
= separator(theme);
149 + const rendered
= segs.
map((s)
=> renderSegment(s, theme));
152 + let line1Width
= 0;
153 + let overflowIndex
= -1;
155 + for (
let i
= 0; i
< rendered.
length; i
++) {
156 + const segWidth
= visibleWidth(rendered[i]
!);
157 + const prefixWidth
= line1Width
> 0 ? visibleWidth(sep)
: 0;
159 + if (line1Width
+ prefixWidth
+ segWidth
<= width) {
160 + line1
+= (line1Width
> 0 ? sep
: "")
+ rendered[i];
161 + line1Width
+= prefixWidth
+ segWidth;
168 + if (overflowIndex
=== -1)
return [line1,
""];
170 + const line2
= truncateToWidth(rendered.
slice(overflowIndex).
join(sep), width);
171 + return [line1, line2];
176 + function toggleVisibility(ctx
: ExtensionContext) {
178 + ctx.ui.
notify(hidden
? "Footer hidden" : "Footer visible",
"info");
182 + pi.
registerCommand(
"footer", {
183 + description:
"Toggle footer visibility",
184 + handler:
async (_args, ctx)
=> toggleVisibility(ctx),
187 + pi.
registerShortcut(Key.
ctrlAlt(
"f"), {
188 + description:
"Toggle footer visibility",
189 + handler:
async (ctx)
=> toggleVisibility(ctx),
194 + pi.
on(
"session_start",
async (_event, ctx)
=> {
195 + if (
!ctx.hasUI)
return;
197 + ctx.ui.
setFooter((tui, theme, footerData)
=> {
198 + requestRender = ()
=> tui.
requestRender();
199 + const unsubBranch
= footerData.
onBranchChange(()
=> scheduleRender());
204 + requestRender
= undefined;
207 + render(width
: number)
: string[] {
208 + if (hidden)
return [];
210 + const contentWidth
= Math.
max(
0, width
- 2);
211 + const padLine = (line
: string)
=> {
212 + if (width
<= 1)
return truncateToWidth(line, width);
213 + return ` ${truncateToWidth(line, contentWidth)} `;
216 + const workspaceLine
= renderWorkspaceLine(theme, contentWidth, ctx);
217 + const [llmLine1, llmLine2]
= renderLlmLines(theme, contentWidth);
220 + const extStatuses
= footerData.
getExtensionStatuses();
221 + let statusLine
= "";
222 + if (extStatuses.
size > 0) {
223 + const parts
= Array.
from(extStatuses.
entries())
224 + .
sort(([a], [b])
=> a.
localeCompare(b))
225 + .
map(([, t])
=> t.
replace(
/[\r\n\t]/g,
" ").
replace(
/ +/g,
" ").
trim())
227 + statusLine
= theme.
fg(
"dim" as ThemeColor, parts);
232 + padLine(workspaceLine),
235 + padLine(statusLine),
243 + setTimeout(()
=> scheduleRender(),
0);
246 + pi.
on(
"session_shutdown",
async ()
=> {
248 + requestRender
= undefined;
pi/agent/extensions/footer/progress.ts
Addednew file mode 100644 9 +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);
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";
41 + const clamped
= Math.
max(
0,
Math.
min(
100, pct));
42 + const filledFloat
= (clamped
/ 100)
* segments;
44 + const result
: string[]
= [];
45 + for (
let i
= 0; i
< segments; i
++) {
46 + 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}`);
52 + return result.
join(
"");
55 +function fgToBgAnsi(fgAnsi
: string)
: string {
56 + return fgAnsi.
replace(
"\x1b[38;",
"\x1b[48;");