formatx

package
v1.25.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package formatx renders values for human consumption — block numbers, counts, rates, durations, sizes.

It exists so that the same quantity does not get formatted three different ways in three different commands. Anything that turns a number into something a user reads belongs here, and nothing here knows about substreams types.

Every function takes the same Option list rather than coming in named variants, so a caller that wants "N/A" instead of "0s", or decimal units instead of binary ones, asks for that at the call site instead of growing a wrapper next to it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bytes

func Bytes(bytes uint64, opts ...Option) string

Bytes renders a byte count in binary units by default — 1.5 MiB — since that is what a file size or a transfer volume is measured in. WithDecimalUnits switches to 1.5 MB.

func Count

func Count(n uint64, opts ...Option) string

Count renders a quantity compactly: 912, 3.4k, 6.8M, 1.2B. Use it where the order of magnitude is the point and the exact figure is not; use BlockNumber when the number identifies something and has to be readable digit by digit.

func Duration

func Duration(d time.Duration, opts ...Option) string

Duration is shorter than time.Duration.String() at the scales that matter to a reader: an age or an estimate never needs sub-second precision, and hours never need seconds.

func Integer

func Integer[T ~uint64](value T, opts ...Option) string

Integer renders a whole number with thousands separators, for block numbers and for exact counts alike — anything a reader has to be able to take in digit by digit, as opposed to Count, which trades those digits for an order of magnitude. The values are uint64 and can in principle exceed what humanize.Comma accepts, hence the big.Int path.

func JoinNonEmpty

func JoinNonEmpty(sep string, parts ...string) string

JoinNonEmpty assembles "a · b · c" segments, dropping the parts that had no data rather than rendering them as empty separators.

func Millis

func Millis(ms float64, opts ...Option) string

Millis renders a millisecond figure, keeping a decimal only while it carries signal.

func ModuleLeafName

func ModuleLeafName(name string) string

ModuleLeafName strips the imported-package qualification from a module name: "erc20_metadata:erc20_stores:map_events" becomes "map_events".

func Rate

func Rate(perSecond float64, unit string, opts ...Option) string

Rate renders a per-second rate of the given unit, e.g. Rate(1700, "blk") is "1.7k blk/s".

Types

type Option

type Option func(*config)

Option adjusts how a value is rendered. Options are shared across every function here; one that does not apply to a given value is simply ignored.

func WithDecimalUnits

func WithDecimalUnits() Option

WithDecimalUnits switches Bytes from binary units to powers of a thousand: 1.5 MB rather than 1.5 MiB.

func WithZero

func WithZero(text string) Option

WithZero renders a zero value as the given text instead of formatting it. Use it where zero means "not known" or "not applicable" rather than an actual measurement — an unset duration reading "N/A" says more than one reading "0s".

Jump to

Keyboard shortcuts

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