Documentation
¶
Overview ¶
Package format provides display helpers for rendering numbers, byte counts, percentages, timestamps and error messages. The functions match the semantics of the upstream lowlighter/metrics formatter helpers but are written from scratch in Go.
Index ¶
- func Ellipsis(s string, maxRunes int) string
- func Format(n int64, opts Options) string
- func FormatBytes(n int64) string
- func FormatDate(t time.Time, opts DateOptions) (string, error)
- func FormatDiskKB(kb int) string
- func FormatError(err error, opts Options) string
- func FormatPercentage(n float64, opts Options) string
- func RelativeAge(t, now time.Time) string
- func S(n int64, suffix string) string
- type DateOptions
- type Options
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Ellipsis ¶
Ellipsis truncates s to at most maxRunes runes, appending a single U+2026 (…) when truncation occurs. maxRunes <= 0 returns s unchanged.
func Format ¶
Format formats n with k / m / b / t magnitude suffixes. Values below 1000 are emitted as integers. Values above are rounded to one decimal place using banker-friendly truncation (math.Floor on the absolute value).
0 -> "0" 999 -> "999" 1000 -> "1k" 1500 -> "1.5k" 1000000 -> "1m"
func FormatBytes ¶
FormatBytes formats a byte count using binary (1024-based) magnitudes: "B", "KB", "MB", "GB", "TB". One decimal place is kept for non-zero fractional parts.
func FormatDate ¶
func FormatDate(t time.Time, opts DateOptions) (string, error)
FormatDate renders t in the requested timezone and layout. On an unknown timezone the function falls back to UTC and signals the failure via the returned error; the formatted string is still produced.
func FormatDiskKB ¶ added in v1.1.0
FormatDiskKB renders a kilobyte count as the most natural binary unit (KB / MB / GB / TB) with up to one fractional digit. Mirrors upstream `base.repositories.ejs`'s `format.bytes(diskUsage * 1024)` helper used for the "<disk-usage> used" label. Negative values are treated as their absolute magnitude with a leading "-".
0 -> "0 KB" 500 -> "500 KB" 1024 -> "1 MB" 1536 -> "1.5 MB" 1048576 -> "1 GB" 5242880 -> "5 GB" 1073741824 -> "1 TB"
func FormatError ¶
FormatError returns a user-facing string for err. nil yields "". When opts.Suffix is non-empty it is appended (no separator).
func FormatPercentage ¶
FormatPercentage renders 0..1 as a percentage with no fractional digits, or 0..100 when Options.Sign asserts the value is already in percent units (caller convention). To keep the API focused we always treat n as a fraction in [0, 1]; values above 1 are clamped via math.Min only when they exceed 100 (i.e. clearly out of range).
func RelativeAge ¶ added in v1.1.0
RelativeAge renders the elapsed time between t and now as the kind of human-readable label upstream base.header.ejs emits via the `s(...)` helper, e.g. "5 years ago", "1 month ago", "3 days ago".
The function picks the largest unit (year, month, day) for which the elapsed duration has at least one whole unit, and pluralises the noun when the count is not 1. now == zero falls back to time.Now() so callers can omit the parameter in production. A zero or future t returns "today" — base.header.ejs has no special case for the future, but the project's render flow never embeds future timestamps and a non-empty fallback keeps the partial dense.
Types ¶
type DateOptions ¶
type DateOptions struct {
// Layout is a Go reference time layout. Defaults to time.DateOnly.
Layout string
// Timezone is an IANA timezone name. Empty value uses UTC.
Timezone string
}
DateOptions controls FormatDate output.