Skip to content

Commit 8241790

Browse files
committed
refactor(usage): consolidate duplicate formatProviderName into shared helper
The formatProviderName function (title-casing a provider id like 'opencode-go' → 'Opencode Go') was duplicated identically across three files: - command-controller.ts (TUI rendering path) - usage-cli.ts (CLI rendering path) - usage-report.ts (ACP rendering path) Moved the single implementation to the shared format.ts helper (already home to formatDuration and renderAsciiBar), and updated all three call sites to import from there.
1 parent af2e53e commit 8241790

5 files changed

Lines changed: 15 additions & 22 deletions

File tree

packages/coding-agent/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Changed
6+
7+
- Consolidated duplicate `formatProviderName` function (title-casing provider ids) from three usage rendering paths into a single shared helper in `slash-commands/helpers/format.ts`
8+
59
## [16.1.16] - 2026-06-23
610

711
### Breaking Changes

packages/coding-agent/src/cli/usage-cli.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { formatDuration, formatNumber } from "@oh-my-pi/pi-utils";
1919
import chalk from "chalk";
2020
import { ModelRegistry } from "../config/model-registry";
2121
import { discoverAuthStorage } from "../sdk";
22+
import { formatProviderName } from "../slash-commands/helpers/format";
2223

2324
const BAR_WIDTH = 28;
2425

@@ -175,13 +176,6 @@ function aggregateStatus(limits: UsageLimit[]): LimitStatus {
175176
return "unknown";
176177
}
177178

178-
function formatProviderName(provider: string): string {
179-
return provider
180-
.split(/[-_]/g)
181-
.map(part => (part ? part[0].toUpperCase() + part.slice(1) : ""))
182-
.join(" ");
183-
}
184-
185179
function formatUnitValue(value: number, unit: UsageUnit): string {
186180
if (unit === "usd") return `$${value.toFixed(2)}`;
187181
return formatNumber(value);

packages/coding-agent/src/modes/controllers/command-controller.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import type { CompactMode } from "../../session/compact-modes";
4242
import type { NewSessionOptions } from "../../session/session-entries";
4343
import { formatShakeSummary, type ShakeMode, type ShakeResult } from "../../session/shake-types";
4444
import { limitMatchesActiveAccount } from "../../slash-commands/helpers/active-oauth-account";
45+
import { formatProviderName } from "../../slash-commands/helpers/format";
4546
import { outputMeta } from "../../tools/output-meta";
4647
import { resolveToCwd, stripOuterDoubleQuotes } from "../../tools/path-utils";
4748
import { replaceTabs } from "../../tools/render-utils";
@@ -1270,13 +1271,6 @@ function truncateJobLabel(label: string, maxWidth: number): string {
12701271
return `${out}…`;
12711272
}
12721273

1273-
function formatProviderName(provider: string): string {
1274-
return provider
1275-
.split(/[-_]/g)
1276-
.map(part => (part ? part[0].toUpperCase() + part.slice(1) : ""))
1277-
.join(" ");
1278-
}
1279-
12801274
function formatNumber(value: number, maxFractionDigits = 1): string {
12811275
return new Intl.NumberFormat("en-US", { maximumFractionDigits: maxFractionDigits }).format(value);
12821276
}

packages/coding-agent/src/slash-commands/helpers/format.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { shimmerText } from "../../modes/theme/shimmer";
22
import { theme as currentTheme, type Theme } from "../../modes/theme/theme";
33

4+
/** Title-case a provider id for display (e.g. "opencode-go" → "Opencode Go"). */
5+
export function formatProviderName(provider: string): string {
6+
return provider
7+
.split(/[-_]/g)
8+
.map(part => (part ? part[0]!.toUpperCase() + part.slice(1) : ""))
9+
.join(" ");
10+
}
11+
412
/** Format a millisecond duration as a coarse-grained human label. */
513
export function formatDuration(ms: number): string {
614
const seconds = Math.max(0, Math.round(ms / 1000));

packages/coding-agent/src/slash-commands/helpers/usage-report.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@ import type { UsageLimit, UsageReport } from "@oh-my-pi/pi-ai";
22
import type { OAuthAccountIdentity } from "../../session/auth-storage";
33
import type { SlashCommandRuntime } from "../types";
44
import { reportMatchesActiveAccount } from "./active-oauth-account";
5-
import { formatDuration, renderAsciiBar } from "./format";
6-
7-
function formatProviderName(provider: string): string {
8-
return provider
9-
.split(/[-_]/g)
10-
.map(part => (part ? part[0].toUpperCase() + part.slice(1) : ""))
11-
.join(" ");
12-
}
5+
import { formatDuration, formatProviderName, renderAsciiBar } from "./format";
136

147
function formatUsageAmount(limit: UsageLimit): string {
158
const amount = limit.amount;

0 commit comments

Comments
 (0)