Something went wrong. Try again.
fork of prime agent with changes i want but are not upstreamable probqbly (and some fixes)
Something went wrong. Try again.
1.9 kB · 50 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051import { type Component, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";import { theme } from "../theme/theme.js";
export interface TopBarOptions { getChatName: () => string | undefined; /** Total session spend in USD (branch total, subagents included). */ getCostUsd?: () => number | undefined;}
/** * Pinned top bar for fullscreen chats: the chat name centered in plain text on * the terminal background, with the session's spend beside it. Rendered as the * fullscreen viewport's pinned header, so it stays on screen in every scroll * position. */export class TopBar implements Component { private readonly options: TopBarOptions;
constructor(options: TopBarOptions) { this.options = options; }
invalidate(): void { // Render output is derived from live session state via getters. }
render(width: number): string[] { const safeWidth = Math.max(1, width); // Strip terminal control characters (C0, DEL, C1): a persisted session // name could carry escape sequences that would execute on every bar // repaint. Then collapse all whitespace: an embedded newline in the // name would emit multiple rows and break the fixed fullscreen frame. const name = (this.options.getChatName() ?? "") .replace(/[\u0000-\u001f\u007f-\u009f]/g, " ") .replace(/\s+/g, " ") .trim(); if (!name) { return [""]; } // Center the name; the cost trails it with a small gap. No background, // no rules: the bar should read as plain text on the terminal. const nameWidth = visibleWidth(name); const cost = this.options.getCostUsd?.(); const costText = typeof cost === "number" && Number.isFinite(cost) && cost >= 0 ? theme.fg("dim", `$${cost.toFixed(2)}`) : ""; const start = Math.max(0, Math.floor((safeWidth - nameWidth) / 2)); const line = `${" ".repeat(start)}${theme.fg("text", name)}${costText ? ` ${costText}` : ""}`; return [truncateToWidth(line, safeWidth, "")]; }}