Documentation
¶
Overview ¶
Package format provides human-readable string formatting helpers for the TaskSmith terminal UI. It converts raw numeric and duration values into compact, display-ready strings suitable for status lines, meters, and informational output.
Cost ¶
FormatCostNano renders a nano-USD amount as a dollar figure. Amounts under one cent keep four decimal places so small costs stay visible, while larger amounts use two decimals. When unpriced is true the amount is treated as a lower bound and prefixed with "≥ ".
Duration ¶
Duration renders a time.Duration in its two largest units (for example "3h 52m" or "45ms"), clamping negative durations to zero. DurationMilis provides the same rendering from a millisecond count.
Text ¶
CompactSingleLine collapses consecutive whitespace and newlines into single spaces, trims the ends, and truncates the result to maxRunes runes with an ellipsis ("…") when it is longer; a maxRunes of zero or less disables truncation.
Tokens ¶
Tokens formats a token count into a human-readable string: raw integers under 1,000, a "k" suffix for thousands, and an "M" suffix for millions, each formatted to one decimal place. Negative counts are prefixed with a minus sign.
Index ¶
- func Bytes(n int64) string
- func CompactSingleLine(s string, maxRunes int) string
- func Duration(d time.Duration, opts ...DurationOption) string
- func DurationMilis(ms int64, opts ...DurationOption) string
- func FormatCostNano(nano int64, unpriced bool) string
- func Rate(perSecond float64) string
- func Tokens(tokens int) string
- type DurationOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CompactSingleLine ¶
CompactSingleLine collapses consecutive whitespace and newlines into single spaces, trims leading and trailing whitespace, and truncates the string to maxRunes with an ellipsis ("…") if it exceeds that length. If maxRunes <= 0, no truncation is performed.
func Duration ¶
func Duration(d time.Duration, opts ...DurationOption) string
Duration renders a duration in its two largest units (e.g. "3h 52m"). Pass Compact for the tight single-token form. Callers are responsible for non-positive durations: pass only spans that make sense to render.
func DurationMilis ¶
func DurationMilis(ms int64, opts ...DurationOption) string
DurationMilis renders a duration in its two largest units (e.g. "3h 52m") from milliseconds.
func FormatCostNano ¶
FormatCostNano formats a nano-USD amount as dollars. Amounts under one cent keep four decimals so small costs stay visible. When unpriced is true the amount is a lower bound and is prefixed with "≥ ".
func Rate ¶
Rate formats a per-second rate, such as tokens generated per second. Values under 100 keep one decimal place, larger values are rounded to whole numbers so a column of rates stays the same width. The decision is made on the rounded value, otherwise 99.96 would print as "100.0" beside a neighbour's "100". A rate that is not finite or is negative means it could not be measured and renders as an em dash.
Types ¶
type DurationOption ¶
type DurationOption func(*durationConfig)
DurationOption tunes how Duration renders a span.
func Compact ¶
func Compact() DurationOption
Compact renders each span in its tightest form: no space between units and a trailing zero unit dropped ("3h40m", "42m", "3h" instead of "3h 40m", "42m 00s", "3h 00m"). Use it inside badges, countdowns, and error sentences where width is scarce.