@@ -4,24 +4,28 @@ import (
44 "fmt"
55)
66
7- // A quantity that can be made human-readable using Human().
7+ // Humanable is a quantity that can be made human-readable using
8+ // `Humaner.Format()`.
89type Humanable interface {
9- // Return the value as a uint64, and a boolean telling whether it
10- // overflowed.
10+ // ToUint64 returns the value as a uint64, and a boolean telling
11+ // whether it overflowed.
1112 ToUint64 () (uint64 , bool )
1213}
1314
14- // An object that can format a Humanable in human-readable format.
15+ // Humaner is an object that can format a Humanable in human-readable
16+ // format.
1517type Humaner struct {
1618 name string
1719 prefixes []Prefix
1820}
1921
22+ // Prefix is a metric-like prefix that implies a scaling factor.
2023type Prefix struct {
2124 Name string
2225 Multiplier uint64
2326}
2427
28+ // Metric is a Humaner representing metric prefixes.
2529var Metric = Humaner {
2630 name : "metric" ,
2731 prefixes : []Prefix {
@@ -34,6 +38,8 @@ var Metric = Humaner{
3438 },
3539}
3640
41+ // Binary is a Humaner representing power-of-1024 based prefixes,
42+ // typically used for bytes.
3743var Binary = Humaner {
3844 name : "binary" ,
3945 prefixes : []Prefix {
@@ -46,13 +52,15 @@ var Binary = Humaner{
4652 },
4753}
4854
55+ // Name returns the name of `h` ("metric" or "binary").
4956func (h * Humaner ) Name () string {
5057 return h .name
5158}
5259
53- // Format n, aligned, in `len(unit) + 10` or fewer characters (except
54- // for extremely large numbers).
55- func (h * Humaner ) FormatNumber (n uint64 , unit string ) (string , string ) {
60+ // FormatNumber formats n, aligned, in `len(unit) + 10` or fewer
61+ // characters (except for extremely large numbers). It returns strings
62+ // representing the numeral and the unit string.
63+ func (h * Humaner ) FormatNumber (n uint64 , unit string ) (numeral string , unitString string ) {
5664 prefix := h .prefixes [0 ]
5765
5866 wholePart := n
@@ -66,25 +74,28 @@ func (h *Humaner) FormatNumber(n uint64, unit string) (string, string) {
6674
6775 if prefix .Multiplier == 1 {
6876 return fmt .Sprintf ("%d" , n ), unit
69- } else {
70- mantissa := float64 (n ) / float64 (prefix .Multiplier )
71- var format string
77+ }
7278
73- if wholePart >= 100 {
74- // `mantissa` can actually be up to 1023.999.
75- format = "%.0f"
76- } else if wholePart >= 10 {
77- format = "%.1f"
78- } else {
79- format = "%.2f"
80- }
81- return fmt .Sprintf (format , mantissa ), prefix .Name + unit
79+ mantissa := float64 (n ) / float64 (prefix .Multiplier )
80+ var format string
81+
82+ switch {
83+ case wholePart >= 100 :
84+ // `mantissa` can actually be up to 1023.999.
85+ format = "%.0f"
86+ case wholePart >= 10 :
87+ format = "%.1f"
88+ default :
89+ format = "%.2f"
8290 }
91+
92+ return fmt .Sprintf (format , mantissa ), prefix .Name + unit
8393}
8494
85- // Format values, aligned, in `len(unit) + 10` or fewer characters
86- // (except for extremely large numbers).
87- func (h * Humaner ) Format (value Humanable , unit string ) (string , string ) {
95+ // Format formats values, aligned, in `len(unit) + 10` or fewer
96+ // characters (except for extremely large numbers). It returns strings
97+ // representing the numeral and the unit string.
98+ func (h * Humaner ) Format (value Humanable , unit string ) (numeral string , unitString string ) {
8899 n , overflow := value .ToUint64 ()
89100 if overflow {
90101 return "∞" , unit
0 commit comments