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 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
* 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 hh = String(d.getHours()).padStart(2, "0")
const mm = String(d.getMinutes()).padStart(2, "0")
return `updated ${hh}:${mm}`
return `${hh}:${mm}`
}
const usage = () => props.store.usageDays()
@@ -70,69 +87,80 @@ export function GreenptWidget(props: {
const usageVisible = () => todayUsage().creditsSpent > 0 || monthUsage().cost > 0
const todayText = () => `${formatCredits(todayUsage().creditsSpent)} ${unit()}`
const monthText = () => `${formatCredits(monthUsage().cost)} ${unit()}`
const foldedSuffix = () => (usageVisible() ? ` · today ${todayText()}` : "")
const modelRows = (): ModelTotal[] => monthUsage().byModel.slice(0, 5)
const modelRows = (): ModelTotal[] => monthUsage().byModel.slice(0, MODEL_ROWS_VISIBLE)
const otherModels = () => Math.max(0, monthUsage().byModel.length - MODEL_ROWS_VISIBLE)
return (
<Show when={!hidden()} fallback={<box />}>
<box flexDirection="column" gap={1}>
<text fg={theme().text.base} onMouseUp={toggle}>
<span style={{ fg: theme().text.muted }}>{open() ? "▼ " : "▶ "}</span>
<b>GreenPT</b>
<Show when={!open() && balanceText()}>
{(text) => (
<span style={{ fg: theme().text.feedback[balanceColor()].base }}>
{" "}
{text()}
{foldedSuffix()}
</span>
)}
</Show>
</text>
<box flexDirection="column">
<box flexDirection="row" justifyContent="space-between" gap={1}>
<text fg={theme().text.base} onMouseUp={toggle}>
<span style={{ fg: theme().text.muted }}>{open() ? "▼ " : "▶ "}</span>
<b>GreenPT</b>
</text>
<box flexDirection="row" gap={1} justifyContent="flex-end">
<Show when={balanceText()}>
{(text) => (
<text fg={theme().text.feedback[balanceColor()].base}>
<b>{text()}</b>
</text>
)}
</Show>
<Show when={!open() && usageVisible()}>
<text fg={theme().text.muted}>· today {todayText()}</text>
</Show>
</box>
</box>
<Show when={open()}>
<Switch>
<Match when={status() === "ready"}>
<box flexDirection="row" gap={1} justifyContent="space-between">
<text fg={theme().text.muted}>Balance</text>
<box flexDirection="row" gap={1}>
<text fg={theme().text.feedback[balanceColor()].base}>{balanceText()}</text>
<text fg={theme().text.muted}>{updatedAt()}</text>
</box>
<box paddingLeft={2}>
<text fg={theme().text.muted}>
today {todayText()} · month {monthText()} · {updatedAt()}
</text>
</box>
</Match>
<Match when={status() === "loading"}>
<text fg={theme().text.muted}>loading…</text>
<box paddingLeft={2}>
<text fg={theme().text.muted}>loading…</text>
</box>
</Match>
<Match when={credentialReason() !== undefined && props.showWhenUnavailable}>
<CredentialHint reason={credentialReason()!} theme={theme()} />
<box paddingLeft={2}>
<CredentialHint reason={credentialReason()!} theme={theme()} />
</box>
</Match>
<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>
</Switch>
<Show when={usageVisible()}>
<box flexDirection="row" gap={1} justifyContent="space-between">
<text fg={theme().text.muted}>Spent</text>
<box flexDirection="row" gap={1}>
<text fg={theme().text.base}>today {todayText()}</text>
<text fg={theme().text.muted}>month {monthText()}</text>
</box>
</box>
<For each={modelRows()}>
{(row) => (
<box flexDirection="row" gap={1} justifyContent="space-between">
<text fg={theme().text.muted}>{row.model}</text>
<box flexDirection="row" gap={1}>
<text fg={theme().text.muted}>
{formatTokens(row.usage.tokens.input + row.usage.tokens.output)} tok
</text>
<text fg={theme().text.base}>
~{formatCredits(row.usage.cost)} {unit()}
</text>
<box flexDirection="column" marginTop={1}>
<For each={modelRows()}>
{(row) => (
<box flexDirection="column" paddingLeft={2}>
<text fg={theme().text.muted}>{shortModel(row.model)}</text>
<box flexDirection="row" justifyContent="space-between" paddingLeft={2}>
<text fg={theme().text.muted}>
{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>
)}
</For>
</Show>
</box>
</Show>
</Show>
</box>