Skip to content

feat(cost): auto-format duration fields as human-readable#164

Open
stephenleo wants to merge 1 commit into
mainfrom
feat/auto-format-duration
Open

feat(cost): auto-format duration fields as human-readable#164
stephenleo wants to merge 1 commit into
mainfrom
feat/auto-format-duration

Conversation

@stephenleo
Copy link
Copy Markdown
Owner

Summary

  • Render $cship.cost.total_duration_ms and $cship.cost.total_api_duration_ms as human-readable durations (45s, 1m30s, 2h15m, or 750ms for sub-second) instead of raw millisecond integers. Mirrors starship's opinionated cmd_duration output — no new config flags.
  • Add $cship.cost.total_duration and $cship.cost.total_api_duration as accepted aliases for the _ms-suffixed names, on both surfaces: format-string variables and TOML config keys.
  • Threshold comparisons still operate on raw milliseconds, so existing warn_threshold = 30000.0 configs keep working unchanged.

Closes #162.

Breaking change

Users with a custom format = "[$value ms]($style)" on either of the duration sub-fields will now see "45s ms". Migration is a one-character edit: drop the literal ms from the format string. CHANGELOG entry added under [Unreleased] calling this out explicitly.

Design notes

  • Followed starship's fully-opinionated path (single human-readable representation, no raw-ms escape hatch). cship is starship-flavored by design and the goal was to keep the config surface small.
  • format_duration_ms uses integer u64 arithmetic throughout — no f64, no decimal drift, truncation is intentional and matches starship's whole-second granularity.
  • Sub-second values (< 1000ms) keep ms precision so a fast 500ms response doesn't read as "0s".
  • Aliases route to a single render function via a | pattern in the dispatch match — no duplicated logic.
  • Config aliases use #[serde(alias = "...")] on the existing field — canonical Rust name stays total_duration_ms, so no cascading code changes.

Test plan

  • cargo fmt --check — clean
  • cargo clippy --all-targets -- -D warnings — clean
  • cargo test — 387 lib + 68 integration tests pass (added 11 new formatter tests, 2 dispatch-alias tests, 2 config-alias tests; updated 5 cost tests + 1 integration test for new format)
  • cargo build --release — clean
  • Manual smoke (default path): total_duration_ms = 4500045s
  • Manual smoke (sub-second): total_duration_ms = 750750ms (not 0s)
  • Manual smoke (threshold still works): warn_threshold = 30000.0 fires yellow ANSI on 45s
  • Manual smoke (no regression on shared codepath): $cship.cost.total_lines_added still renders as 156
  • Manual smoke (variable alias parity): $cship.cost.total_duration_ms and $cship.cost.total_duration produce byte-identical output
  • Manual smoke (config-key alias parity): [cship.cost.total_duration_ms] and [cship.cost.total_duration] produce byte-identical warn-styled output

🤖 Generated with Claude Code

Render `$cship.cost.total_duration_ms` and `$cship.cost.total_api_duration_ms`
as `45s` / `1m30s` / `2h15m` (or `750ms` for sub-second values) instead of raw
millisecond integers. Mirrors starship's opinionated `cmd_duration` output —
no new config flags. Threshold comparisons still operate on raw milliseconds,
so existing `warn_threshold = 30000.0` configs keep working unchanged.

Adds `total_duration` / `total_api_duration` as accepted aliases for the
`_ms`-suffixed names, on both surfaces: format-string variables (via `|` in
the dispatch match arm) and TOML config keys (via `#[serde(alias)]`). Once
the rendered value is human-readable, the `_ms` suffix is misleading, so
either spelling reads correctly.

Closes #162.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@stephenleo
Copy link
Copy Markdown
Owner Author

Wait for #153 as the intent might be duplicated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: auto-format duration fields (ms → human-readable)

1 participant