titlebar-spinner.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * Titlebar Spinner Extension
  3. *
  4. * Shows a braille spinner animation in the terminal title while the agent is working.
  5. * Uses `ctx.ui.setTitle()` to update the terminal title via the extension API.
  6. *
  7. * Usage:
  8. * pi --extension examples/extensions/titlebar-spinner.ts
  9. */
  10. import path from "node:path";
  11. import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
  12. const BRAILLE_FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
  13. function getBaseTitle(pi: ExtensionAPI): string {
  14. const cwd = path.basename(process.cwd());
  15. const session = pi.getSessionName();
  16. return session ? `π - ${session} - ${cwd}` : `π - ${cwd}`;
  17. }
  18. export default function (pi: ExtensionAPI) {
  19. let timer: ReturnType<typeof setInterval> | null = null;
  20. let frameIndex = 0;
  21. function stopAnimation(ctx: ExtensionContext) {
  22. if (timer) {
  23. clearInterval(timer);
  24. timer = null;
  25. }
  26. frameIndex = 0;
  27. ctx.ui.setTitle(getBaseTitle(pi));
  28. }
  29. function startAnimation(ctx: ExtensionContext) {
  30. stopAnimation(ctx);
  31. timer = setInterval(() => {
  32. const frame = BRAILLE_FRAMES[frameIndex % BRAILLE_FRAMES.length];
  33. const cwd = path.basename(process.cwd());
  34. const session = pi.getSessionName();
  35. const title = session ? `${frame} π - ${session} - ${cwd}` : `${frame} π - ${cwd}`;
  36. ctx.ui.setTitle(title);
  37. frameIndex++;
  38. }, 80);
  39. }
  40. pi.on("agent_start", async (_event, ctx) => {
  41. startAnimation(ctx);
  42. });
  43. pi.on("agent_end", async (_event, ctx) => {
  44. stopAnimation(ctx);
  45. });
  46. pi.on("session_shutdown", async (_event, ctx) => {
  47. stopAnimation(ctx);
  48. });
  49. }