format

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: MIT 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 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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ellipsis

func Ellipsis(s string, maxRunes int) string

Ellipsis truncates s to at most maxRunes runes, appending a single U+2026 (…) when truncation occurs. maxRunes <= 0 returns s unchanged.

func Format

func Format(n int64, opts Options) string

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

func FormatBytes(n int64) string

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

func FormatDiskKB(kb int) string

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

func FormatError(err error, opts Options) string

FormatError returns a user-facing string for err. nil yields "". When opts.Suffix is non-empty it is appended (no separator).

func FormatPercentage

func FormatPercentage(n float64, opts Options) string

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

func RelativeAge(t, now time.Time) string

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.

func S

func S(n int64, suffix string) string

S returns "" when n == 1 and suffix otherwise. Useful for "1 item" vs "2 items" pluralization.

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.

type Options

type Options struct {
	// Sign forces a leading '+' on positive numbers when true.
	Sign bool
	// Suffix is appended to the formatted string after the magnitude unit.
	Suffix string
}

Options controls number formatting.

Jump to

Keyboard shortcuts

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