chg: [opencode-greenpt-usage] tighten the sidebar balance layout

Put the balance on the section header line and keep the folded header to
one row with today's spend. Remove the gap between the title and the
metrics line, then indent model tokens below the full model name so they
read as subordinate detail instead of wrapping mid-name.

The stacked model rows avoid the narrow sidebar splitting a model id;
costs stay right-aligned. Typecheck and the 49 unit tests pass.
This commit is contained in:
Stéphan Sainléger
2026-11-18 23:13:52 +01:00
parent dadc93a22d
commit d123b9b3be

View File

@@ -5,6 +5,23 @@ import { BALANCE_OPEN_KV, formatBalanceError, formatCredits, formatTokens, DEFAU
import { dayTotals, monthTotals, type ModelTotal } from "./usage" import { dayTotals, monthTotals, type ModelTotal } from "./usage"
import type { BalanceStore } from "./store" import type { BalanceStore } from "./store"
/** Maximum characters before a model name is ellipsized in the table. */
const MODEL_NAME_MAX = 28
/** Number of model rows shown before the "other models" summary. */
const MODEL_ROWS_VISIBLE = 3
/**
* Shorten a model id for a narrow sidebar column.
*
* @param model Model id reported by OpenCode.
* @returns The id unchanged when short enough, otherwise an ellipsized form.
*/
function shortModel(model: string): string {
if (model.length <= MODEL_NAME_MAX) return model
return `${model.slice(0, MODEL_NAME_MAX - 1)}…`
}
/** /**
* Sidebar section showing the remaining GreenPT credit balance and the * Sidebar section showing the remaining GreenPT credit balance and the
* consumption of the API key: real credit spend per day/month plus a * consumption of the API key: real credit spend per day/month plus a
@@ -61,7 +78,7 @@ export function GreenptWidget(props: {
const d = new Date(s.fetchedAt) const d = new Date(s.fetchedAt)
const hh = String(d.getHours()).padStart(2, "0") const hh = String(d.getHours()).padStart(2, "0")
const mm = String(d.getMinutes()).padStart(2, "0") const mm = String(d.getMinutes()).padStart(2, "0")
return `updated ${hh}:${mm}` return `${hh}:${mm}`
} }
const usage = () => props.store.usageDays() const usage = () => props.store.usageDays()
@@ -70,69 +87,80 @@ export function GreenptWidget(props: {
const usageVisible = () => todayUsage().creditsSpent > 0 || monthUsage().cost > 0 const usageVisible = () => todayUsage().creditsSpent > 0 || monthUsage().cost > 0
const todayText = () => `${formatCredits(todayUsage().creditsSpent)} ${unit()}` const todayText = () => `${formatCredits(todayUsage().creditsSpent)} ${unit()}`
const monthText = () => `${formatCredits(monthUsage().cost)} ${unit()}` const monthText = () => `${formatCredits(monthUsage().cost)} ${unit()}`
const foldedSuffix = () => (usageVisible() ? ` · today ${todayText()}` : "") const modelRows = (): ModelTotal[] => monthUsage().byModel.slice(0, MODEL_ROWS_VISIBLE)
const modelRows = (): ModelTotal[] => monthUsage().byModel.slice(0, 5) const otherModels = () => Math.max(0, monthUsage().byModel.length - MODEL_ROWS_VISIBLE)
return ( return (
<Show when={!hidden()} fallback={<box />}> <Show when={!hidden()} fallback={<box />}>
<box flexDirection="column" gap={1}> <box flexDirection="column">
<text fg={theme().text.base} onMouseUp={toggle}> <box flexDirection="row" justifyContent="space-between" gap={1}>
<span style={{ fg: theme().text.muted }}>{open() ? "▼ " : "▶ "}</span> <text fg={theme().text.base} onMouseUp={toggle}>
<b>GreenPT</b> <span style={{ fg: theme().text.muted }}>{open() ? "▼ " : "▶ "}</span>
<Show when={!open() && balanceText()}> <b>GreenPT</b>
{(text) => ( </text>
<span style={{ fg: theme().text.feedback[balanceColor()].base }}> <box flexDirection="row" gap={1} justifyContent="flex-end">
{" "} <Show when={balanceText()}>
{text()} {(text) => (
{foldedSuffix()} <text fg={theme().text.feedback[balanceColor()].base}>
</span> <b>{text()}</b>
)} </text>
</Show> )}
</text> </Show>
<Show when={!open() && usageVisible()}>
<text fg={theme().text.muted}>· today {todayText()}</text>
</Show>
</box>
</box>
<Show when={open()}> <Show when={open()}>
<Switch> <Switch>
<Match when={status() === "ready"}> <Match when={status() === "ready"}>
<box flexDirection="row" gap={1} justifyContent="space-between"> <box paddingLeft={2}>
<text fg={theme().text.muted}>Balance</text> <text fg={theme().text.muted}>
<box flexDirection="row" gap={1}> today {todayText()} · month {monthText()} · {updatedAt()}
<text fg={theme().text.feedback[balanceColor()].base}>{balanceText()}</text> </text>
<text fg={theme().text.muted}>{updatedAt()}</text>
</box>
</box> </box>
</Match> </Match>
<Match when={status() === "loading"}> <Match when={status() === "loading"}>
<text fg={theme().text.muted}>loading…</text> <box paddingLeft={2}>
<text fg={theme().text.muted}>loading…</text>
</box>
</Match> </Match>
<Match when={credentialReason() !== undefined && props.showWhenUnavailable}> <Match when={credentialReason() !== undefined && props.showWhenUnavailable}>
<CredentialHint reason={credentialReason()!} theme={theme()} /> <box paddingLeft={2}>
<CredentialHint reason={credentialReason()!} theme={theme()} />
</box>
</Match> </Match>
<Match when={status() === "error"}> <Match when={status() === "error"}>
<text fg={theme().text.muted}>{formatBalanceError(error()!)}</text> <box paddingLeft={2}>
<text fg={theme().text.muted}>{formatBalanceError(error()!)}</text>
</box>
</Match> </Match>
</Switch> </Switch>
<Show when={usageVisible()}> <Show when={usageVisible()}>
<box flexDirection="row" gap={1} justifyContent="space-between"> <box flexDirection="column" marginTop={1}>
<text fg={theme().text.muted}>Spent</text> <For each={modelRows()}>
<box flexDirection="row" gap={1}> {(row) => (
<text fg={theme().text.base}>today {todayText()}</text> <box flexDirection="column" paddingLeft={2}>
<text fg={theme().text.muted}>month {monthText()}</text> <text fg={theme().text.muted}>{shortModel(row.model)}</text>
</box> <box flexDirection="row" justifyContent="space-between" paddingLeft={2}>
</box> <text fg={theme().text.muted}>
<For each={modelRows()}> {formatTokens(row.usage.tokens.input + row.usage.tokens.output)} tok
{(row) => ( </text>
<box flexDirection="row" gap={1} justifyContent="space-between"> <text fg={theme().text.base}>
<text fg={theme().text.muted}>{row.model}</text> ~{formatCredits(row.usage.cost)} {unit()}
<box flexDirection="row" gap={1}> </text>
<text fg={theme().text.muted}> </box>
{formatTokens(row.usage.tokens.input + row.usage.tokens.output)} tok
</text>
<text fg={theme().text.base}>
~{formatCredits(row.usage.cost)} {unit()}
</text>
</box> </box>
)}
</For>
<Show when={otherModels() > 0}>
<box paddingLeft={2}>
<text fg={theme().text.muted}>+ {otherModels()} more models</text>
</box> </box>
)} </Show>
</For> </box>
</Show> </Show>
</Show> </Show>
</box> </box>