Documentation ¶
Overview ¶
Package human provides types that support parsing and formatting human-friendly representations of values in various units.
The package only exposes type names that are not that common to find in Go programs (in our experience). For that reason, it can be interesting to import the package as '.' (dot) to inject the symbols in the namespace of the importer, especially in the common case where it's being used in the main package of a program, for example:
import ( . "github.com/segmentio/cli/human" )
This can help improve code readability by importing constants in the package namespace, allowing constructs like:
type clientConfig{ DialTimeout Duration BufferSize Bytes RateLimit Rate } ... config := clientConfig{ DialTimeout: 10 * Second, BufferSize: 64 * KiB, RateLimit: 20 * PerSecond, }
Index ¶
- func ParseBytesFloat64(s string) (float64, error)
- type Boolean
- func (b Boolean) Format(w fmt.State, v rune)
- func (b Boolean) Get() any
- func (b Boolean) GoString() string
- func (b Boolean) MarshalJSON() ([]byte, error)
- func (b Boolean) MarshalText() ([]byte, error)
- func (b Boolean) MarshalYAML() (any, error)
- func (b *Boolean) Set(s string) error
- func (b Boolean) String() string
- func (b *Boolean) UnmarshalJSON(j []byte) error
- func (b *Boolean) UnmarshalText(t []byte) error
- func (b *Boolean) UnmarshalYAML(y *yaml.Node) error
- type Bytes
- func (b Bytes) Format(w fmt.State, v rune)
- func (b Bytes) Get() any
- func (b Bytes) GoString() string
- func (b Bytes) MarshalJSON() ([]byte, error)
- func (b Bytes) MarshalText() ([]byte, error)
- func (b Bytes) MarshalYAML() (any, error)
- func (b *Bytes) Set(s string) error
- func (b Bytes) String() string
- func (b *Bytes) UnmarshalJSON(j []byte) error
- func (b *Bytes) UnmarshalText(t []byte) error
- func (b *Bytes) UnmarshalYAML(y *yaml.Node) error
- type Count
- func (c Count) Format(w fmt.State, v rune)
- func (c Count) Get() any
- func (c Count) GoString() string
- func (c Count) MarshalJSON() ([]byte, error)
- func (c Count) MarshalText() ([]byte, error)
- func (c Count) MarshalYAML() (any, error)
- func (c *Count) Set(s string) error
- func (c Count) String() string
- func (c *Count) UnmarshalJSON(b []byte) error
- func (c *Count) UnmarshalText(b []byte) error
- func (c *Count) UnmarshalYAML(y *yaml.Node) error
- type Duration
- func (d Duration) Days() int
- func (d Duration) Format(w fmt.State, v rune)
- func (d Duration) Formatter(now time.Time) fmt.Formatter
- func (d Duration) Get() any
- func (d Duration) GoString() string
- func (d Duration) Hours() int
- func (d Duration) MarshalJSON() ([]byte, error)
- func (d Duration) MarshalText() ([]byte, error)
- func (d Duration) MarshalYAML() (any, error)
- func (d Duration) Microseconds() int
- func (d Duration) Milliseconds() int
- func (d Duration) Minutes() int
- func (d Duration) Months(until time.Time) int
- func (d Duration) Nanoseconds() int
- func (d Duration) Seconds() int
- func (d *Duration) Set(s string) error
- func (d Duration) String() string
- func (d Duration) Text(now time.Time) string
- func (d *Duration) UnmarshalJSON(b []byte) error
- func (d *Duration) UnmarshalText(b []byte) error
- func (d *Duration) UnmarshalYAML(y *yaml.Node) error
- func (d Duration) Weeks() int
- func (d Duration) Years(until time.Time) int
- type Number
- func (n Number) Format(w fmt.State, v rune)
- func (n Number) Get() any
- func (n Number) GoString() string
- func (n Number) MarshalJSON() ([]byte, error)
- func (n Number) MarshalText() ([]byte, error)
- func (n Number) MarshalYAML() (any, error)
- func (n *Number) Set(s string) error
- func (n Number) String() string
- func (n *Number) UnmarshalJSON(b []byte) error
- func (n *Number) UnmarshalText(b []byte) error
- func (n *Number) UnmarshalYAML(y *yaml.Node) error
- type Path
- type Rate
- func (r Rate) Format(w fmt.State, v rune)
- func (r Rate) Formatter(d Duration) fmt.Formatter
- func (r Rate) Get() any
- func (r Rate) GoString() string
- func (r Rate) MarshalJSON() ([]byte, error)
- func (r Rate) MarshalText() ([]byte, error)
- func (r Rate) MarshalYAML() (any, error)
- func (r *Rate) Set(s string) error
- func (r Rate) String() string
- func (r Rate) Text(d Duration) string
- func (r *Rate) UnmarshalJSON(b []byte) error
- func (r *Rate) UnmarshalText(b []byte) error
- func (r *Rate) UnmarshalYAML(y *yaml.Node) error
- type Ratio
- func (r Ratio) Format(w fmt.State, v rune)
- func (r Ratio) Formatter(precision int) fmt.Formatter
- func (r Ratio) Get() any
- func (r Ratio) GoString() string
- func (r Ratio) MarshalJSON() ([]byte, error)
- func (r Ratio) MarshalText() ([]byte, error)
- func (r Ratio) MarshalYAML() (any, error)
- func (r *Ratio) Set(s string) error
- func (r Ratio) String() string
- func (r Ratio) Text(precision int) string
- func (r *Ratio) UnmarshalJSON(b []byte) error
- func (r *Ratio) UnmarshalText(b []byte) error
- func (r *Ratio) UnmarshalYAML(y *yaml.Node) error
- type Time
- func (t Time) Format(w fmt.State, v rune)
- func (t Time) Formatter(now time.Time) fmt.Formatter
- func (t Time) Get() any
- func (t Time) GoString() string
- func (t Time) IsZero() bool
- func (t Time) MarshalJSON() ([]byte, error)
- func (t Time) MarshalText() ([]byte, error)
- func (t Time) MarshalYAML() (any, error)
- func (t *Time) Set(s string) error
- func (t Time) String() string
- func (t Time) Text(now time.Time) string
- func (t *Time) UnmarshalJSON(b []byte) error
- func (t *Time) UnmarshalText(b []byte) error
- func (t *Time) UnmarshalYAML(y *yaml.Node) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseBytesFloat64 ¶
Types ¶
type Boolean ¶
type Boolean bool
Boolean returns a boolean value.
The type supports parsing values as "true", "false", "yes", or "no", all case insensitive.
func ParseBoolean ¶
func (Boolean) Format ¶
Format satisfies the fmt.Formatter interface.
The method supports the following formatting verbse:
s "yes" or "no" t "true" or "false" v same as 's'
For each of these options, these extra flags are also intepreted:
Capitalized + All uppercase #
func (Boolean) MarshalJSON ¶
func (Boolean) MarshalText ¶
func (Boolean) MarshalYAML ¶
func (*Boolean) UnmarshalJSON ¶
func (*Boolean) UnmarshalText ¶
type Bytes ¶
type Bytes uint64
Bytes represents a number of bytes.
The type support parsing values in formats like:
42 KB 8Gi 1.5KiB ...
Two models are supported, using factors of 1000 and factors of 1024 via units like KB, MB, GB for the former, or Ki, Mi, MiB for the latter.
In the current implementation, formatting is always done in factors of 1024, using units like Ki, Mi, Gi etc...
Values may be decimals when using units larger than B. Partial bytes cannot be represnted (e.g. 0.5B is not supported).
func ParseBytes ¶
func (Bytes) Format ¶
Format satisfies the fmt.Formatter interface.
The method supports the following formatting verbs:
d base 10, unit-less b base 10, with unit using 1000 factors s base 10, with unit using 1024 factors (same as calling String) v same as the 's' format, unless '#' is set to print the go value
func (Bytes) MarshalJSON ¶
func (Bytes) MarshalText ¶
func (Bytes) MarshalYAML ¶
func (*Bytes) UnmarshalJSON ¶
func (*Bytes) UnmarshalText ¶
type Count ¶
type Count float64
Count represents a count without a unit.
The type supports parsing and formatting values like:
1234 10 K 1.5M ...
func ParseCount ¶
func (Count) Format ¶
Format satisfies the fmt.Formatter interface.
The method supports the following formatting verbs:
d base 10, unit-less, rounded to the nearest integer e base 10, unit-less, scientific notation f base 10, unit-less, decimal notation g base 10, unit-less, act like 'e' or 'f' depending on scale s base 10, with unit (same as calling String) v same as the 's' format, unless '#' is set to print the go value
func (Count) MarshalJSON ¶
func (Count) MarshalText ¶
func (Count) MarshalYAML ¶
func (*Count) UnmarshalJSON ¶
func (*Count) UnmarshalText ¶
type Duration ¶
Duration is based on time.Duration, but supports parsing and formatting more human-friendly representations.
Here are examples of supported values:
5m30s 1d 4 weeks 1.5y ...
The current implementation does not support decimal values, however, contributions are welcome to add this feature.
Time being what it is, months and years are hard to represent because their durations vary in unpredictable ways. This is why the package only exposes constants up to a 1 week duration. For the sake of accuracy, years and months are always represented relative to a given date. Technically, leap seconds can cause any unit above the second to be variable, but in order to remain mentaly sane, we chose to ignore this detail in the implementation of this package.
const ( Nanosecond Duration = 1 Microsecond Duration = 1000 * Nanosecond Millisecond Duration = 1000 * Microsecond Second Duration = 1000 * Millisecond Minute Duration = 60 * Second Hour Duration = 60 * Minute Day Duration = 24 * Hour Week Duration = 7 * Day )
func ParseDuration ¶
func (Duration) Format ¶
Format satisfies the fmt.Formatter interface.
The method supports the following formatting verbs:
s outputs a string representation of the duration (same as calling String) v same as the 's' format, unless '#' is set to print the go value
The 's' and 'v' formatting verbs also interpret the options:
'-' outputs full names of the time units instead of abbreviations '.' followed by a digit to limit the precision of the output
func (Duration) MarshalJSON ¶
func (Duration) MarshalText ¶
func (Duration) MarshalYAML ¶
func (Duration) Microseconds ¶
func (Duration) Milliseconds ¶
func (Duration) Nanoseconds ¶
func (*Duration) UnmarshalJSON ¶
func (*Duration) UnmarshalText ¶
type Number ¶
type Number float64
Number is similar to Count, but supports values with separators for readability purposes.
The type supports parsing and formatting values likes:
123 1.5 2,000,000 ...
func ParseNumber ¶
func (Number) Format ¶
Format satisfies the fmt.Formatter interface.
The method supports the following formatting verbs:
e base 10, separator-free, scientific notation f base 10, separator-free, decimal notation g base 10, separator-free, act like 'e' or 'f' depending on scale s base 10, with separators (same as calling String) v same as the 's' format, unless '#' is set to print the go value
func (Number) MarshalJSON ¶
func (Number) MarshalText ¶
func (Number) MarshalYAML ¶
func (*Number) UnmarshalJSON ¶
func (*Number) UnmarshalText ¶
type Path ¶
type Path string
Path represents a path on the file system.
The type interprets the special prefix "~/" as representing the home directory of the user that the program is running as.
func (*Path) UnmarshalText ¶
type Rate ¶
type Rate float64
Rate represents a count devided by a unit of time.
The type supports parsing and formatting values like:
200/s 1 / minute 0.5/week ...
Rate values are always stored in their per-second form in Go programs, and properly converted during parsing and formatting.
func (Rate) Format ¶
Format satisfies the fmt.Formatter interface.
The method supports the following formatting verbs:
e base 10, unit-less, scientific notation f base 10, unit-less, decimal notation g base 10, unit-less, act like 'e' or 'f' depending on scale s base 10, with units (same as calling String) v same as the 's' format, unless '#' is set to print the go value
func (Rate) MarshalJSON ¶
func (Rate) MarshalText ¶
func (Rate) MarshalYAML ¶
func (*Rate) UnmarshalJSON ¶
func (*Rate) UnmarshalText ¶
type Ratio ¶
type Ratio float64
Ratio represents percentage-like values.
The type supports parsing and formatting values like:
0.1 25% 0.5 % ...
Ratio values are stored as floating pointer numbers between 0 and 1 (assuming they stay within the 0-100% bounds), and formatted as percentages.
func ParseRatio ¶
func (Ratio) Format ¶
Format satisfies the fmt.Formatter interface.
The method supports the following formatting verbs:
e base 10, unit-less, scientific notation f base 10, unit-less, decimal notation g base 10, unit-less, act like 'e' or 'f' depending on scale s base 10, with units (same as calling String) v same as the 's' format, unless '#' is set to print the go value
func (Ratio) MarshalJSON ¶
func (Ratio) MarshalText ¶
func (Ratio) MarshalYAML ¶
func (*Ratio) UnmarshalJSON ¶
func (*Ratio) UnmarshalText ¶
type Time ¶
Time represents absolute point in times. The implementation is based on time.Time.
The type supports all default time formats provided by the standard time package, as well as parsing and formatting values relative to a given time point, for example:
5 minutes ago 1h later ...
func (Time) Format ¶
Format satisfies the fmt.Formatter interface.
The method supports the following formatting verbs:
s duration relative to now (same as calling String) v sam as the 's' format, unless '#' is set to print the go value
The 's' and 'v' formatting verbs also interpret the options:
'-' outputs full names of the time units instead of abbreviations '.' followed by a digit to limit the precision of the output