format

package
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bytes

func Bytes(n int64) string

Bytes formats a byte count concisely (e.g., "512 B", "142 KB", "2.4 MB").

func CompactSingleLine

func CompactSingleLine(s string, maxRunes int) string

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

func FormatCostNano(nano int64, unpriced bool) string

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

func Rate(perSecond float64) string

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.

func Tokens

func Tokens(tokens int) string

Tokens formats a token count into a human-readable string (e.g. 0, 840, 6.0k, 109.5k, 1.0M). It formats counts under 1,000 as raw integers, thousands with a 'k' suffix, and millions with an 'M' suffix, formatted to one decimal place. Negative counts are prefixed with a minus sign.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL