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 Bytes
- func (b Bytes) MarshalJSON() ([]byte, error)
- func (b Bytes) MarshalText() ([]byte, error)
- func (b Bytes) MarshalYAML() (interface{}, 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) MarshalJSON() ([]byte, error)
- func (c Count) MarshalText() ([]byte, error)
- func (c Count) MarshalYAML() (interface{}, 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) Hours() int
- func (d Duration) MarshalJSON() ([]byte, error)
- func (d Duration) MarshalText() ([]byte, error)
- func (d Duration) MarshalYAML() (interface{}, 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) String() string
- func (d Duration) Text(until 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) MarshalJSON() ([]byte, error)
- func (n Number) MarshalText() ([]byte, error)
- func (n Number) MarshalYAML() (interface{}, 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 Rate
- func (r Rate) MarshalJSON() ([]byte, error)
- func (r Rate) MarshalText() ([]byte, error)
- func (r Rate) MarshalYAML() (interface{}, 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) MarshalJSON() ([]byte, error)
- func (r Ratio) MarshalText() ([]byte, error)
- func (r Ratio) MarshalYAML() (interface{}, 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) IsZero() bool
- func (t Time) MarshalJSON() ([]byte, error)
- func (t Time) MarshalText() ([]byte, error)
- func (t Time) MarshalYAML() (interface{}, 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 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) 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) 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:
1d 4 weeks ...
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) 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) MarshalJSON ¶
func (Number) MarshalText ¶
func (Number) MarshalYAML ¶
func (*Number) UnmarshalJSON ¶
func (*Number) 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) 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) 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 ...