repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/loop/src/kitten.ts
Rawimport type { Theme } from "@earendil-works/pi-coding-agent";
import { rgbColor, type TUI } from "@earendil-works/pi-tui";
import {
AFTERGLOW_MS,
type Companion,
type CompanionState,
H,
type Pixels,
type RGB,
} from "./companion.ts";
const INK: RGB = [77, 52, 48];
const COPPER: RGB = [201, 118, 65];
const CREAM: RGB = [255, 228, 183];
const PINK: RGB = [225, 142, 141];
const YARN: RGB = [69, 148, 153];
export type KittenPose =
| "working"
| "watching"
| "puzzled"
| "curled"
| "proud";
/** Original LOOP plush tabby: copper seams, cream bib, endless yarn hoop. */
export function kittenPixels(pose: KittenPose, t: number): Pixels {
const width = 32;
const pixels: Pixels = Array.from({ length: H }, () =>
Array<RGB | null>(width).fill(null),
);
const dot = (x: number, y: number, color: RGB) => {
const row = pixels[Math.round(y)];
x = Math.round(x);
if (row && x >= 0 && x < width) row[x] = color;
};
const oval = (cx: number, cy: number, rx: number, ry: number, color: RGB) => {
for (
let y = Math.max(0, Math.ceil(cy - ry));
y <= Math.min(H - 1, cy + ry);
y++
)
for (
let x = Math.max(0, Math.ceil(cx - rx));
x <= Math.min(width - 1, cx + rx);
x++
)
if (((x - cx) / rx) ** 2 + ((y - cy) / ry) ** 2 <= 1) dot(x, y, color);
};
const plush = (x: number, y: number, rx: number, ry: number) => {
oval(x, y, rx, ry, INK);
oval(x, y - 0.3, rx - 0.8, ry - 0.8, COPPER);
};
const stroke = (
ax: number,
ay: number,
bx: number,
by: number,
color: RGB,
) => {
const steps =
Math.ceil(Math.max(Math.abs(bx - ax), Math.abs(by - ay))) || 1;
for (let i = 0; i <= steps; i++)
dot(ax + ((bx - ax) * i) / steps, ay + ((by - ay) * i) / steps, color);
};
const hoop = (x: number, y: number, radius: number) => {
for (let i = 0; i < 48; i++) {
const a = (i * Math.PI) / 24;
dot(x + Math.cos(a) * radius, y + Math.sin(a) * radius, YARN);
}
};
const nap = pose === "curled";
const proud = pose === "proud";
const play = pose === "working";
const breath = Math.sin(t * 1.8) * 0.45;
const bounce = play ? Math.max(0, Math.sin(t * 3)) * 1.5 : 0;
const hx = nap ? 10 : pose === "puzzled" ? 13 + Math.sin(t) * 0.8 : 14;
const hy = (nap ? 20 : proud ? 10 : 12) + breath - bounce;
// Tail curls independently behind the plush body, with a cream tip.
const ty = 20 + Math.sin(t * (play ? 3 : 1.2)) * 1.5;
plush(25, ty, 4, 6);
oval(26, ty - 2, 1.7, 2.7, CREAM);
plush(15, 23 + breath, nap ? 11 : 7, nap ? 5 : 6);
oval(14, 23 + breath, 4, 4.5, CREAM);
// Triangular ears, stitched dark edging, rose inner fabric.
for (const side of [-1, 1]) {
const ex = hx + side * 5;
for (let row = 0; row < 7; row++) {
const cy = hy - 10 + row + (pose === "puzzled" && side === 1 ? 2 : 0);
stroke(ex - row / 3, cy, ex + row / 3, cy, INK);
if (row > 1) stroke(ex - (row - 2) / 3, cy, ex + (row - 2) / 3, cy, PINK);
}
}
plush(hx, hy, 8, 6.5);
oval(hx, hy + 2, 5.5, 3.5, CREAM);
// Tabby forehead stripes and cheek whiskers, not a borrowed sprite.
for (const dx of [-3, 0, 3])
stroke(hx + dx, hy - 5, hx + dx * 0.7, hy - 3, INK);
const blink = t % 5.6 > 5.35;
for (const side of [-1, 1]) {
const ex = hx + side * 3;
const ey = hy + (pose === "puzzled" && side === 1 ? 1 : 0);
if (nap || blink) stroke(ex - 1, ey, ex + 1, ey, INK);
else if (proud) {
dot(ex, ey - 1, INK);
dot(ex - 1, ey, INK);
dot(ex + 1, ey, INK);
} else {
oval(ex, ey, 1.2, 1.7, INK);
dot(ex, ey - 1, CREAM);
}
stroke(hx + side * 5, hy + 2, hx + side * 8, hy + 1, INK);
stroke(hx + side * 5, hy + 3, hx + side * 8, hy + 4, INK);
}
dot(hx, hy + 2, PINK);
stroke(hx, hy + 3, hx - 1, hy + 4, INK);
dot(hx + 1, hy + 4, INK);
if (!nap) {
stroke(10, hy + 7, 18, hy + 7, YARN);
hoop(14, hy + 9, 1.5);
}
// One paw watches the gate; working bats the yarn; victory raises both.
plush(10, proud ? 19 : 27, 2.5, 2);
const pawY = nap
? 26
: proud
? 17
: pose === "puzzled"
? hy + 2
: pose === "watching"
? 22 + Math.sin(t * 2)
: 24 - Math.sin(t * 3) * 2;
plush(nap ? 17 : 20, pawY, 2.5, 2);
oval(nap ? 17 : 20, pawY, 1.5, 1, CREAM);
if (nap) {
plush(21, 25, 6, 3);
oval(21, 24.5, 4, 1, CREAM);
}
const yarnX = 27 + (play ? Math.sin(t * 3) : 0);
hoop(yarnX, 27, 2.5);
stroke(24, 28, 21, 28, YARN);
return pixels;
}
export function createKitten(
tui: TUI,
theme: Theme,
state: () => CompanionState,
now: () => number = Date.now,
displayScale = 1,
): Companion {
const started = now();
let disposed = false;
let timer: ReturnType<typeof setTimeout> | undefined;
const cancel = () => {
clearTimeout(timer);
timer = undefined;
};
return {
render(width) {
if (disposed) return [];
const current = state();
const time = now();
if (
width < 1 ||
current.status === "stopped" ||
(current.status === "succeeded" &&
time - current.updatedAt > AFTERGLOW_MS)
) {
cancel();
return [];
}
if (!timer) {
timer = setTimeout(() => {
timer = undefined;
if (!disposed) tui.requestRender();
}, 80);
timer.unref?.();
}
const pose: KittenPose =
current.status === "succeeded"
? "proud"
: current.status !== "active"
? "curled"
: current.pending
? "watching"
: current.failed && time - current.failed.at < 7_000
? "puzzled"
: "working";
const pixels = kittenPixels(pose, (time - started) / 1000);
const height = Math.ceil(H * displayScale);
const offset = Math.floor((width - 32 * displayScale) / 2);
const pixel = (x: number, y: number) =>
y < height && x >= offset
? pixels[Math.floor(y / displayScale)]?.[
Math.floor((x - offset) / displayScale)
]
: undefined;
const lines: string[] = [];
for (let y = 0; y < height; y += 2) {
let end = width;
while (end > 0 && !pixel(end - 1, y) && !pixel(end - 1, y + 1)) end--;
let line = "";
for (let x = 0; x < end; x++) {
const top = pixel(x, y),
bottom = pixel(x, y + 1);
if (top && bottom)
line += theme.style("▀", {
fg: rgbColor(...top),
bg: rgbColor(...bottom),
});
else if (top) line += theme.style("▀", { fg: rgbColor(...top) });
else if (bottom)
line += theme.style("▄", { fg: rgbColor(...bottom) });
else line += " ";
}
lines.push(line);
}
return lines;
},
invalidate() {
if (!disposed) {
cancel();
tui.requestRender();
}
},
dispose() {
disposed = true;
cancel();
},
};
}