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