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 FormatError(err error, opts Options) string
- func FormatPercentage(n float64, opts Options) 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 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).
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.