repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

fix(pi/footer): force render after swap to immediately clear old lines

0ea30fdeee6796e552d96b887ac3cbeaa315e24c

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (2)

pi/agent/extensions/footer/index.ts

Modified
184 184 if (hidden) {
185 185 requestRender = undefined;
186 - currentCtx.ui.setFooter(() => HIDDEN_FOOTER);
186 + currentCtx.ui.setFooter((tui) => {
187 + // Force immediate render to clear old footer lines
188 + queueMicrotask(() => tui.requestRender());
189 + return HIDDEN_FOOTER;
190 + });
187 191 return;

pi/agent/extensions/neko.ts

Modified
13 13 const DEBOUNCE_MS = 420;
14 14 const TICK_MS = 90;
15 -const NOSE_INDEX_LEFT = 4;
16 -const NOSE_INDEX_RIGHT = 7;
17 15 const MOVE_STEP = 1;
18 16 const IDLE_ADVANCE_TICKS = 3;
20 -// Walking left — ~ leans left, ∿ trails
21 -const WALK_LEFT = [
22 - "(~^・ω・^∿)~~ ",
23 - "(~^・ω・^∿)~ ",
24 - "(~^・ω・^∿)< ",
25 - "(~^・ω・^∿)~ ",
26 -];
28 -// Walking right — ∿ trails, ~ leans right
29 -const WALK_RIGHT = [
30 - " ~~(∿^・ω・^~)",
31 - " ~(∿^・ω・^~)",
32 - " >(∿^・ω・^~)",
33 - " ~(∿^・ω・^~)",
34 -];
36 -// Idle left — gentle blink + tail swish, ~2.7s full cycle
37 -// ˘ω˘ is same width as ・ω・ — no padding tricks needed
38 -const IDLE_LEFT = [
39 - "(=^・ω・^=) ", // 0: still
40 - "(=^・ω・^=) ", // 1: still
41 - "(=^˘ω˘^=) ", // 2: blink!
42 - "(=^・ω・^=) ", // 3: eyes open
43 - "(=^・ω・^=) ", // 4: still
44 - "(=^・ω・^=)~ ", // 5: tail out
45 - "(=^・ω・^=)~~ ", // 6: tail curl
46 - "(=^・ω・^=)~ ", // 7: tail back
47 - "(=^・ω・^=) ", // 8: still
48 - "(=^・ω・^=) ", // 9: still
49 -];
51 -// Idle right — mirror
52 -const IDLE_RIGHT = [
53 - " (=^・ω・^=)", // 0
54 - " (=^・ω・^=)", // 1
55 - " (=^˘ω˘^=)", // 2: blink!
56 - " (=^・ω・^=)", // 3
57 - " (=^・ω・^=)", // 4
58 - " ~(=^・ω・^=)", // 5: tail out
59 - " ~~(=^・ω・^=)", // 6: tail curl
60 - " ~(=^・ω・^=)", // 7: tail back
61 - " (=^・ω・^=)", // 8
62 - " (=^・ω・^=)", // 9
63 -];
17 +const SLEEP_AFTER_MS = 30_000;
18 +const ERROR_FLASH_MS = 2_500;
65 20 type Facing = "left" | "right";
21 +type Motion = "idle" | "walking";
22 +type Activity = "normal" | "thinking" | "working" | "compacting" | "error" | "sleeping";
23 +type RawSpriteSet = Record<Motion, Record<Facing, string[]>>;
24 +type SpriteSet = Record<Motion, Record<Facing, SpriteFrame[]>>;
26 +type SpriteFrame = {
27 + text: string;
28 + width: number;
29 + noseAnchor: number;
30 +};
32 +const A = {
33 + reset: "\x1b[0m",
34 + gray: "\x1b[90m",
35 + red: "\x1b[31m",
36 + yellow: "\x1b[33m",
37 + blue: "\x1b[34m",
38 + magenta: "\x1b[35m",
39 + cyan: "\x1b[36m",
40 +} as const;
42 +const ACTIVITY_COLOR: Record<Activity, string> = {
43 + normal: A.cyan,
44 + thinking: A.blue,
45 + working: A.yellow,
46 + compacting: A.magenta,
47 + error: A.red,
48 + sleeping: A.gray,
49 +};
51 +const RAW_SPRITES: Record<Activity, RawSpriteSet> = {
52 + normal: {
53 + idle: {
54 + left: [
55 + "(=^・ω・^=) ",
56 + "(=^・ω・^=) ",
57 + "(=^˘ω˘^=) ",
58 + "(=^・ω・^=) ",
59 + "(=^・ω・^=) ",
60 + "(=^・ω・^=)~ ",
61 + "(=^・ω・^=)~~ ",
62 + "(=^・ω・^=)~ ",
63 + "(=^・ω・^=) ",
64 + "(=^・ω・^=) ",
65 + ],
66 + right: [
67 + " (=^・ω・^=)",
68 + " (=^・ω・^=)",
69 + " (=^˘ω˘^=)",
70 + " (=^・ω・^=)",
71 + " (=^・ω・^=)",
72 + " ~(=^・ω・^=)",
73 + " ~~(=^・ω・^=)",
74 + " ~(=^・ω・^=)",
75 + " (=^・ω・^=)",
76 + " (=^・ω・^=)",
77 + ],
78 + },
79 + walking: {
80 + left: [
81 + "(~^・ω・^∿)~~ ",
82 + "(~^・ω・^∿)~ ",
83 + "(~^・ω・^∿)< ",
84 + "(~^・ω・^∿)~ ",
85 + ],
86 + right: [
87 + " ~~(∿^・ω・^~)",
88 + " ~(∿^・ω・^~)",
89 + " >(∿^・ω・^~)",
90 + " ~(∿^・ω・^~)",
91 + ],
92 + },
93 + },
95 + thinking: {
96 + idle: {
97 + left: [
98 + "(?^・ω・^?) ",
99 + "(?^・ω・^?) . ",
100 + "(?^・ω・^?) ..",
101 + "(?^˘ω˘^?) ..",
102 + "(?^・ω・^?) ..",
103 + "(?^・ω・^?) . ",
104 + ],
105 + right: [
106 + " (?^・ω・^?)",
107 + " . (?^・ω・^?)",
108 + ".. (?^・ω・^?)",
109 + ".. (?^˘ω˘^?)",
110 + ".. (?^・ω・^?)",
111 + " . (?^・ω・^?)",
112 + ],
113 + },
114 + walking: {
115 + left: [
116 + "(?^・ω・^∿)?? ",
117 + "(?^・ω・^∿)? ",
118 + "(?^・ω・^∿)< ",
119 + "(?^˘ω˘^∿)? ",
120 + ],
121 + right: [
122 + " ??(∿^・ω・^?)",
123 + " ?(∿^・ω・^?)",
124 + " >(∿^・ω・^?)",
125 + " ?(∿^˘ω˘^?)",
126 + ],
127 + },
128 + },
130 + working: {
131 + idle: {
132 + left: [
133 + "(=^・ω・^=)  ",
134 + "(=^>ω<^=)  ",
135 + "(=^・ω・^=)  ",
136 + "(=^>ω<^=)  ",
137 + ],
138 + right: [
139 + "  (=^・ω・^=)",
140 + "  (=^>ω<^=)",
141 + "  (=^・ω・^=)",
142 + "  (=^>ω<^=)",
143 + ],
144 + },
145 + walking: {
146 + left: [
147 + "(^・ω・^∿)~~ ",
148 + "(^・ω・^∿)~ ",
149 + "(^>ω<^∿)< ",
150 + "(^・ω・^∿)~ ",
151 + ],
152 + right: [
153 + " ~~(∿^・ω・^)",
154 + " ~(∿^・ω・^)",
155 + " >(∿^>ω<^)",
156 + " ~(∿^・ω・^)",
157 + ],
158 + },
159 + },
161 + compacting: {
162 + idle: {
163 + left: [
164 + "[=^・ω・^=] ",
165 + "[=^˘ω˘^=] ",
166 + "[=^・ω・^=]▣ ",
167 + "[=^・ω・^=]▥ ",
168 + ],
169 + right: [
170 + " [=^・ω・^=]",
171 + " [=^˘ω˘^=]",
172 + " ▣[=^・ω・^=]",
173 + " ▥[=^・ω・^=]",
174 + ],
175 + },
176 + walking: {
177 + left: [
178 + "[~^・ω・^∿]▣ ",
179 + "[~^・ω・^∿]▥ ",
180 + "[~^・ω・^∿]< ",
181 + "[~^˘ω˘^∿]▥ ",
182 + ],
183 + right: [
184 + " ▣[∿^・ω・^~]",
185 + " ▥[∿^・ω・^~]",
186 + " >[∿^・ω・^~]",
187 + " ▥[∿^˘ω˘^~]",
188 + ],
189 + },
190 + },
192 + error: {
193 + idle: {
194 + left: [
195 + "(!^ಠωಠ^)!! ",
196 + "(!!ಠωಠ^)! ",
197 + "(!^ಠωಠ!!) ",
198 + "(!!ಠωಠ!!) ",
199 + ],
200 + right: [
201 + " !!(^ಠωಠ^!)",
202 + " !(^ಠωಠ!!)",
203 + " (!!ಠωಠ^!)",
204 + " (!!ಠωಠ!!)",
205 + ],
206 + },
207 + walking: {
208 + left: [
209 + "(!^ಠωಠ^∿)!!",
210 + "(!^ಠωಠ^∿)! ",
211 + "(!^ಠωಠ^∿)< ",
212 + "(!^ಠωಠ^∿)! ",
213 + ],
214 + right: [
215 + "!!(∿^ಠωಠ^!)",
216 + " !(∿^ಠωಠ^!)",
217 + " >(∿^ಠωಠ^!)",
218 + " !(∿^ಠωಠ^!)",
219 + ],
220 + },
221 + },
223 + sleeping: {
224 + idle: {
225 + left: [
226 + "(-˘ω˘-) z ",
227 + "(-˘ω˘-) z ",
228 + "(-˘ω˘-) Z ",
229 + "(-˘ω˘-) ",
230 + ],
231 + right: [
232 + " z (-˘ω˘-)",
233 + " z (-˘ω˘-)",
234 + " Z (-˘ω˘-)",
235 + " (-˘ω˘-)",
236 + ],
237 + },
238 + walking: {
239 + left: [
240 + "(-˘ω˘-∿)z ",
241 + "(-˘ω˘-∿)z ",
242 + "(-˘ω˘-∿)< ",
243 + "(-˘ω˘-∿) ",
244 + ],
245 + right: [
246 + " z(∿-˘ω˘-)",
247 + " z(∿-˘ω˘-)",
248 + " >(∿-˘ω˘-)",
249 + " (∿-˘ω˘-)",
250 + ],
251 + },
252 + },
255 +function color(activity: Activity, text: string): string {
256 + return `${ACTIVITY_COLOR[activity]}${text}${A.reset}`;
259 +function makeFrame(activity: Activity, raw: string): SpriteFrame {
260 + const noseIndex = raw.indexOf("ω");
261 + const noseAnchor = noseIndex >= 0
262 + ? visibleWidth(raw.slice(0, noseIndex))
263 + : Math.floor(visibleWidth(raw) / 2);
265 + return {
266 + text: color(activity, raw),
267 + width: visibleWidth(raw),
268 + noseAnchor,
269 + };
272 +function makeSpriteSet(activity: Activity, raw: RawSpriteSet): SpriteSet {
273 + return {
274 + idle: {
275 + left: raw.idle.left.map((frame) => makeFrame(activity, frame)),
276 + right: raw.idle.right.map((frame) => makeFrame(activity, frame)),
277 + },
278 + walking: {
279 + left: raw.walking.left.map((frame) => makeFrame(activity, frame)),
280 + right: raw.walking.right.map((frame) => makeFrame(activity, frame)),
281 + },
282 + };
285 +const SPRITES = Object.fromEntries(
286 + (Object.entries(RAW_SPRITES) as [Activity, RawSpriteSet][]).map(
287 + ([activity, sprites]) => [activity, makeSpriteSet(activity, sprites)],
288 + ),
289 +) as Record<Activity, SpriteSet>;
67 291 function clamp(value: number, min: number, max: number): number {
68 292 return Math.max(min, Math.min(value, max));
71 -const ALL_SPRITES = [...WALK_LEFT, ...WALK_RIGHT, ...IDLE_LEFT, ...IDLE_RIGHT];
72 -const SPRITE_WIDTH = Math.max(...ALL_SPRITES.map(visibleWidth));
74 -function overlayLine(width: number, sprite: string, x: number): string {
295 +function overlayLine(width: number, frame: SpriteFrame, x: number): string {
75 296 if (width <= 0) return "";
297 + if (frame.width >= width) return truncateToWidth(frame.text, width, "", true);
77 - const spriteWidth = visibleWidth(sprite);
78 - if (spriteWidth >= width) return truncateToWidth(sprite, width, "", true);
80 - const left = clamp(x, 0, width - spriteWidth);
81 - const right = Math.max(0, width - left - spriteWidth);
82 - return " ".repeat(left) + sprite + " ".repeat(right);
299 + const left = clamp(x, 0, width - frame.width);
300 + const right = Math.max(0, width - left - frame.width);
301 + return " ".repeat(left) + frame.text + " ".repeat(right);
85 304 class NekoEditor extends CustomEditor {
87 306 private targetNoseX = 2;
88 307 private facing: Facing = "left";
89 308 private paddingX: number;
309 + private activity: Activity;
91 311 private frame = 0;
92 312 private idleTick = 0;
93 313 private lastRenderWidth = 80;
314 + private lastInputAt = Date.now();
95 316 private debounceTimer: ReturnType<typeof setTimeout> | null = null;
96 317 private animTimer: ReturnType<typeof setInterval> | null = null;
98 - constructor(paddingX: number, ...args: ConstructorParameters<typeof CustomEditor>) {
319 + constructor(paddingX: number, activity: Activity, ...args: ConstructorParameters<typeof CustomEditor>) {
99 320 super(...args);
100 321 this.paddingX = paddingX;
322 + this.activity = activity;
102 324 this.animTimer = setInterval(() => {
103 325 let changed = false;
104 326 const delta = this.targetNoseX - this.noseX;
327 + const motion: Motion = Math.abs(delta) > MOVE_STEP ? "walking" : "idle";
328 + const frames = this.framesFor(motion);
106 - if (Math.abs(delta) > MOVE_STEP) {
107 - // Walking
330 + if (motion === "walking") {
108 331 this.facing = delta > 0 ? "right" : "left";
109 332 const step = Math.min(MOVE_STEP, Math.abs(delta));
110 333 this.noseX += this.facing === "right" ? step : -step;
111 - this.frame = (this.frame + 1) % WALK_LEFT.length;
334 + this.frame = (this.frame + 1) % frames.length;
112 335 this.idleTick = 0;
113 336 changed = true;
114 337 } else if (delta !== 0) {
115 - // Snap to target
116 338 this.noseX = this.targetNoseX;
117 339 this.idleTick = 0;
118 340 changed = true;
119 341 } else {
120 - // Idle — advance frame every N ticks
121 342 this.idleTick++;
122 343 if (this.idleTick % IDLE_ADVANCE_TICKS === 0) {
123 - this.frame = (this.frame + 1) % IDLE_LEFT.length;
344 + this.frame = (this.frame + 1) % frames.length;
124 345 changed = true;
129 350 }, TICK_MS);
132 - private currentSprite(): string {
133 - const delta = this.targetNoseX - this.noseX;
134 - const walking = Math.abs(delta) > MOVE_STEP;
135 - if (walking) {
136 - return this.facing === "right"
137 - ? WALK_RIGHT[this.frame % WALK_RIGHT.length]!
138 - : WALK_LEFT[this.frame % WALK_LEFT.length]!;
353 + public setActivity(activity: Activity): void {
354 + if (this.activity === activity) return;
355 + this.activity = activity;
356 + this.frame = 0;
357 + this.idleTick = 0;
358 + this.tui.requestRender();
361 + private displayActivity(): Activity {
362 + const moving = Math.abs(this.targetNoseX - this.noseX) > MOVE_STEP;
363 + if (this.activity === "normal" && !moving && Date.now() - this.lastInputAt >= SLEEP_AFTER_MS) {
364 + return "sleeping";
140 - return this.facing === "right"
141 - ? IDLE_RIGHT[this.frame % IDLE_RIGHT.length]!
142 - : IDLE_LEFT[this.frame % IDLE_LEFT.length]!;
366 + return this.activity;
369 + private framesFor(motion: Motion): SpriteFrame[] {
370 + return SPRITES[this.displayActivity()][motion][this.facing];
373 + private currentFrame(): SpriteFrame {
374 + const motion: Motion = Math.abs(this.targetNoseX - this.noseX) > MOVE_STEP ? "walking" : "idle";
375 + const frames = this.framesFor(motion);
376 + return frames[this.frame % frames.length]!;
145 379 override setPaddingX(padding: number): void {
156 390 return Math.max(1, this.lastRenderWidth - paddingX);
159 - private noseBounds(): { min: number; max: number } {
160 - if (this.lastRenderWidth <= SPRITE_WIDTH) {
393 + private noseBounds(frame = this.currentFrame()): { min: number; max: number } {
394 + if (this.lastRenderWidth <= frame.width) {
161 395 return { min: 0, max: Math.max(0, this.lastRenderWidth - 1) };
164 - const anchor = this.facing === "left" ? NOSE_INDEX_LEFT : NOSE_INDEX_RIGHT;
165 398 return {
166 - min: anchor,
167 - max: this.lastRenderWidth - SPRITE_WIDTH + anchor,
399 + min: frame.noseAnchor,
400 + max: this.lastRenderWidth - frame.width + frame.noseAnchor,
171 - private clampNoseX(value: number): number {
172 - const { min, max } = this.noseBounds();
404 + private clampNoseX(value: number, frame = this.currentFrame()): number {
405 + const { min, max } = this.noseBounds(frame);
173 406 return clamp(value, min, max);
181 414 const rawTargetNoseX = this.paddingX + visualCol;
183 - // Update facing first, then clamp with the correct edge anchor.
184 416 const delta = rawTargetNoseX - this.noseX;
185 417 if (delta !== 0) this.facing = delta > 0 ? "right" : "left";
200 432 override handleInput(data: string): void {
433 + this.lastInputAt = Date.now();
201 434 super.handleInput(data);
202 435 this.scheduleMoveToCursor();
209 442 this.lastRenderWidth = width;
211 - const sprite = this.currentSprite();
212 - const noseAnchor = this.facing === "left" ? NOSE_INDEX_LEFT : NOSE_INDEX_RIGHT;
444 + const frame = this.currentFrame();
445 + this.noseX = this.clampNoseX(this.noseX, frame);
446 + this.targetNoseX = this.clampNoseX(this.targetNoseX, frame);
214 - this.noseX = this.clampNoseX(this.noseX);
215 - this.targetNoseX = this.clampNoseX(this.targetNoseX);
217 - const left = this.noseX - noseAnchor;
218 - const catLine = overlayLine(width, sprite, left);
448 + const left = this.noseX - frame.noseAnchor;
449 + const catLine = overlayLine(width, frame, left);
220 451 return [catLine, ...editorLines];
249 480 export default function nekoExtension(pi: ExtensionAPI) {
250 481 const activeEditors = new Set<NekoEditor>();
482 + let agentRunning = false;
483 + let toolsRunning = 0;
484 + let compacting = false;
485 + let errorUntil = 0;
486 + let errorTimer: ReturnType<typeof setTimeout> | null = null;
488 + const resolveActivity = (): Activity => {
489 + if (Date.now() < errorUntil) return "error";
490 + if (compacting) return "compacting";
491 + if (toolsRunning > 0) return "working";
492 + if (agentRunning) return "thinking";
493 + return "normal";
494 + };
496 + const updateActivity = () => {
497 + const activity = resolveActivity();
498 + for (const editor of activeEditors) editor.setActivity(activity);
499 + };
252 501 const disposeEditors = () => {
253 502 for (const editor of activeEditors) editor.dispose();
267 516 const paddingX = resolveEditorPaddingX(ctx.cwd);
268 517 ctx.ui.setEditorComponent((tui, theme, keybindings) => {
269 - const editor = new NekoEditor(paddingX, tui, theme, keybindings);
518 + const editor = new NekoEditor(paddingX, resolveActivity(), tui, theme, keybindings);
270 519 activeEditors.add(editor);
271 520 return editor;
278 527 if (ctx.hasUI) ctx.ui.notify(`Neko ${enabled ? "ON" : "OFF"}`, "info");
530 + const flashError = () => {
531 + errorUntil = Date.now() + ERROR_FLASH_MS;
532 + updateActivity();
533 + if (errorTimer) clearTimeout(errorTimer);
534 + errorTimer = setTimeout(() => {
535 + errorTimer = null;
536 + updateActivity();
537 + }, ERROR_FLASH_MS);
538 + };
281 540 pi.registerCommand("neko", {
282 541 description: "Toggle neko editor cat: /neko [on|off|status]",
283 542 getArgumentCompletions(text: string): AutocompleteItem[] {
311 570 pi.on("session_start", async (_event, ctx) => applyEditor(ctx));
571 + pi.on("agent_start", async () => {
572 + agentRunning = true;
573 + updateActivity();
574 + });
575 + pi.on("agent_end", async () => {
576 + agentRunning = false;
577 + toolsRunning = 0;
578 + compacting = false;
579 + updateActivity();
580 + });
581 + pi.on("tool_execution_start", async () => {
582 + toolsRunning++;
583 + updateActivity();
584 + });
585 + pi.on("tool_execution_end", async () => {
586 + toolsRunning = Math.max(0, toolsRunning - 1);
587 + updateActivity();
588 + });
589 + pi.on("tool_result", async (event) => {
590 + if (event.isError) flashError();
591 + });
592 + pi.on("session_before_compact", async () => {
593 + compacting = true;
594 + updateActivity();
595 + });
596 + pi.on("session_compact", async () => {
597 + compacting = false;
598 + updateActivity();
599 + });
312 600 pi.on("session_shutdown", async (_event, ctx) => {
313 601 disposeEditors();
602 + if (errorTimer) clearTimeout(errorTimer);
314 603 if (ctx.hasUI) ctx.ui.setEditorComponent(undefined);