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
Modified185 185 requestRender
= undefined;
186 - currentCtx.ui.
setFooter(()
=> HIDDEN_FOOTER);
186 + currentCtx.ui.
setFooter((tui)
=> {
188 + queueMicrotask(()
=> tui.
requestRender());
189 + return HIDDEN_FOOTER;
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;
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[]>>;
38 + magenta:
"\x1b[35m",
42 +const ACTIVITY_COLOR
: Record<
Activity,
string>
= {
46 + compacting: A.magenta,
51 +const RAW_SPRITES
: Record<
Activity,
RawSpriteSet>
= {
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);
266 + text:
color(activity, raw),
267 + width:
visibleWidth(raw),
272 +function makeSpriteSet(activity
: Activity, raw
: RawSpriteSet)
: SpriteSet {
275 + left: raw.idle.
left.
map((frame)
=> makeFrame(activity, frame)),
276 + right: raw.idle.
right.
map((frame)
=> makeFrame(activity, frame)),
279 + left: raw.walking.
left.
map((frame)
=> makeFrame(activity, frame)),
280 + right: raw.walking.
right.
map((frame)
=> makeFrame(activity, frame)),
285 +const SPRITES
= Object.
fromEntries(
286 + (
Object.
entries(RAW_SPRITES)
as [
Activity,
RawSpriteSet][]).
map(
287 + ([activity, sprites])
=> [activity,
makeSpriteSet(activity, sprites)],
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
>) {
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) {
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;
114 337 }
else if (delta
!== 0) {
116 338 this.noseX
= this.targetNoseX;
117 339 this.idleTick
= 0;
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;
132 - private
currentSprite(): string {
133 - const delta
= this.targetNoseX
- this.noseX;
134 - const walking
= Math.
abs(delta)
> MOVE_STEP;
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;
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) {
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;
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;
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";
496 + const updateActivity = ()
=> {
497 + const activity
= resolveActivity();
498 + for (
const editor
of activeEditors) editor.
setActivity(activity);
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);
278 527 if (ctx.hasUI) ctx.ui.
notify(
`Neko ${enabled ? "ON" : "OFF"}`,
"info");
530 + const flashError = ()
=> {
531 + errorUntil
= Date.
now()
+ ERROR_FLASH_MS;
533 + if (errorTimer)
clearTimeout(errorTimer);
534 + errorTimer
= setTimeout(()
=> {
537 + }, ERROR_FLASH_MS);
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;
575 + pi.
on(
"agent_end",
async ()
=> {
576 + agentRunning
= false;
578 + compacting
= false;
581 + pi.
on(
"tool_execution_start",
async ()
=> {
585 + pi.
on(
"tool_execution_end",
async ()
=> {
586 + toolsRunning
= Math.
max(
0, toolsRunning
- 1);
589 + pi.
on(
"tool_result",
async (event)
=> {
590 + if (
event.isError)
flashError();
592 + pi.
on(
"session_before_compact",
async ()
=> {
596 + pi.
on(
"session_compact",
async ()
=> {
597 + compacting
= false;
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);