import type { Theme } from "@earendil-works/pi-coding-agent"; import type { Credits, CreditsLane } from "./types"; const WINDOWS_WARNING = 70; const WINDOWS_ERROR = 90; const BALANCE_WARNING = 10; const BALANCE_ERROR = 5; export function renderCredits(theme: Theme, label: string, credits: Credits): string { const styledLabel = theme.fg("dim", label ? `${label} ` : ""); const value = credits.type === "windows" ? renderWindows(theme, credits) : renderBalance(theme, credits); const suffix = credits.suffix ? ` ${theme.fg("dim", credits.suffix)}` : ""; return `${styledLabel}${value}${suffix}`; } export function renderError(theme: Theme, label: string, message: string): string { return theme.fg("error", `${label} ${message}`); } function renderWindows(theme: Theme, credits: Extract): string { const unlimited = credits.unlimited || credits.lanes.every((lane) => lane.percent === undefined); if (unlimited) return theme.fg("success", "unlimited"); return credits.lanes.map((lane) => renderLane(theme, lane)).join(" "); } function renderLane(theme: Theme, lane: CreditsLane): string { const text = `${lane.label} ${lane.percent === undefined ? "?" : lane.percent.toFixed(0)}%`; if (lane.percent && lane.percent > WINDOWS_ERROR) return theme.fg("error", text); if (lane.percent && lane.percent > WINDOWS_WARNING) return theme.fg("warning", text); return theme.fg("success", text); } function renderBalance(theme: Theme, credits: Extract): string { if (credits.remaining === undefined) return theme.fg("dim", "$?"); const text = `$${credits.remaining.toFixed(2)}`; if (credits.remaining < BALANCE_ERROR) return theme.fg("error", text); if (credits.remaining < BALANCE_WARNING) return theme.fg("warning", text); return theme.fg("success", text); }