Skip to content

Commit cdf6b1d

Browse files
committed
Introduce a Humaner interface
Use it in place of the old `[]counts.Prefix`. Give it a name.
1 parent 515ba3c commit cdf6b1d

File tree

3 files changed

+63
-51
lines changed

3 files changed

+63
-51
lines changed

counts/human.go

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,53 @@ import (
55
"math"
66
)
77

8-
type Prefix struct {
9-
Name string
10-
Multiplier uint64
11-
}
12-
138
// A quantity that can be made human-readable using Human().
149
type Humanable interface {
15-
Human([]Prefix, string) (string, string)
10+
Human(Humaner, string) (string, string)
1611
ToUint64() uint64
1712
}
1813

19-
var MetricPrefixes []Prefix = []Prefix{
20-
{"", 1},
21-
{"k", 1e3},
22-
{"M", 1e6},
23-
{"G", 1e9},
24-
{"T", 1e12},
25-
{"P", 1e15},
14+
// An object that can format a Humanable in human-readable format.
15+
type Humaner struct {
16+
name string
17+
prefixes []Prefix
18+
}
19+
20+
type Prefix struct {
21+
Name string
22+
Multiplier uint64
23+
}
24+
25+
var Metric = Humaner{
26+
name: "metric",
27+
prefixes: []Prefix{
28+
{"", 1},
29+
{"k", 1e3},
30+
{"M", 1e6},
31+
{"G", 1e9},
32+
{"T", 1e12},
33+
{"P", 1e15},
34+
},
2635
}
2736

28-
var BinaryPrefixes []Prefix = []Prefix{
29-
{"", 1 << (10 * 0)},
30-
{"Ki", 1 << (10 * 1)},
31-
{"Mi", 1 << (10 * 2)},
32-
{"Gi", 1 << (10 * 3)},
33-
{"Ti", 1 << (10 * 4)},
34-
{"Pi", 1 << (10 * 5)},
37+
var Binary = Humaner{
38+
name: "binary",
39+
prefixes: []Prefix{
40+
{"", 1 << (10 * 0)},
41+
{"Ki", 1 << (10 * 1)},
42+
{"Mi", 1 << (10 * 2)},
43+
{"Gi", 1 << (10 * 3)},
44+
{"Ti", 1 << (10 * 4)},
45+
{"Pi", 1 << (10 * 5)},
46+
},
3547
}
3648

3749
// Format values, aligned, in `len(unit) + 10` or fewer characters
3850
// (except for extremely large numbers).
39-
func Human(n uint64, prefixes []Prefix, unit string) (string, string) {
40-
prefix := prefixes[0]
51+
func (h *Humaner) Format(n uint64, unit string) (string, string) {
52+
prefix := h.prefixes[0]
4153
wholePart := n
42-
for _, p := range prefixes {
54+
for _, p := range h.prefixes {
4355
w := n / p.Multiplier
4456
if w >= 1 {
4557
wholePart = w
@@ -65,18 +77,18 @@ func Human(n uint64, prefixes []Prefix, unit string) (string, string) {
6577
}
6678
}
6779

68-
func (n Count32) Human(prefixes []Prefix, unit string) (string, string) {
80+
func (n Count32) Human(humaner Humaner, unit string) (string, string) {
6981
if n == math.MaxUint32 {
7082
return "∞", unit
7183
} else {
72-
return Human(uint64(n), prefixes, unit)
84+
return humaner.Format(uint64(n), unit)
7385
}
7486
}
7587

76-
func (n Count64) Human(prefixes []Prefix, unit string) (string, string) {
88+
func (n Count64) Human(humaner Humaner, unit string) (string, string) {
7789
if n == math.MaxUint64 {
7890
return "∞", unit
7991
} else {
80-
return Human(uint64(n), prefixes, unit)
92+
return humaner.Format(uint64(n), unit)
8193
}
8294
}

counts/human_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@ func TestMetric(t *testing.T) {
5252
{12345678900000000000, "12346", "Pcd"}, // Not ideal, but ok
5353
{0xffffffffffffffff, "18447", "Pcd"}, // Not ideal, but ok
5454
} {
55-
number, unit := counts.Human(ht.n, counts.MetricPrefixes, "cd")
55+
number, unit := counts.Metric.Format(ht.n, "cd")
5656
assert.Equalf(ht.number, number, "Number for %d in metric", ht.n)
5757
assert.Equalf(ht.unit, unit, "Unit for %d in metric", ht.n)
5858
if ht.n < 0xffffffff {
5959
c := counts.NewCount32(ht.n)
60-
number, unit := c.Human(counts.MetricPrefixes, "cd")
60+
number, unit := c.Human(counts.Metric, "cd")
6161
assert.Equalf(ht.number, number, "Number for Count32(%d) in metric", ht.n)
6262
assert.Equalf(ht.unit, unit, "Unit for Count32(%d) in metric", ht.n)
6363
}
6464
if ht.n < 0xffffffffffffffff {
6565
c := counts.NewCount64(ht.n)
66-
number, unit := c.Human(counts.MetricPrefixes, "cd")
66+
number, unit := c.Human(counts.Metric, "cd")
6767
assert.Equalf(ht.number, number, "Number for Count64(%d) in metric", ht.n)
6868
assert.Equalf(ht.unit, unit, "Unit for Count64(%d) in metric", ht.n)
6969
}
@@ -91,18 +91,18 @@ func TestBinary(t *testing.T) {
9191
{1152921504606846976, "1024", "PiB"},
9292
{0xffffffffffffffff, "16384", "PiB"},
9393
} {
94-
number, unit := counts.Human(ht.n, counts.BinaryPrefixes, "B")
94+
number, unit := counts.Binary.Format(ht.n, "B")
9595
assert.Equalf(ht.number, number, "Number for %d in binary", ht.n)
9696
assert.Equalf(ht.unit, unit, "Unit for %d in binary", ht.n)
9797
if ht.n < 0xffffffff {
9898
c := counts.NewCount32(ht.n)
99-
number, unit := c.Human(counts.BinaryPrefixes, "B")
99+
number, unit := c.Human(counts.Binary, "B")
100100
assert.Equalf(ht.number, number, "Number for Count32(%d) in binary", ht.n)
101101
assert.Equalf(ht.unit, unit, "Unit for Count32(%d) in binary", ht.n)
102102
}
103103
if ht.n < 0xffffffffffffffff {
104104
c := counts.NewCount64(ht.n)
105-
number, unit := c.Human(counts.BinaryPrefixes, "B")
105+
number, unit := c.Human(counts.Binary, "B")
106106
assert.Equalf(ht.number, number, "Number for Count64(%d) in binary", ht.n)
107107
assert.Equalf(ht.unit, unit, "Unit for Count64(%d) in binary", ht.n)
108108
}
@@ -113,7 +113,7 @@ func TestLimits32(t *testing.T) {
113113
assert := assert.New(t)
114114

115115
c := counts.NewCount32(0xffffffff)
116-
number, unit := c.Human(counts.MetricPrefixes, "cd")
116+
number, unit := c.Human(counts.Metric, "cd")
117117
assert.Equalf("∞", number, "Number for Count32(%d) in metric", c.ToUint64())
118118
assert.Equalf("cd", unit, "Unit for Count32(%d) in metric", c.ToUint64())
119119
}
@@ -122,7 +122,7 @@ func TestLimits64(t *testing.T) {
122122
assert := assert.New(t)
123123

124124
c := counts.NewCount64(0xffffffffffffffff)
125-
number, unit := c.Human(counts.MetricPrefixes, "B")
125+
number, unit := c.Human(counts.Metric, "B")
126126
assert.Equalf("∞", number, "Number for Count64(%d) in metric", c.ToUint64())
127127
assert.Equalf("B", unit, "Unit for Count64(%d) in metric", c.ToUint64())
128128
}

sizes/output.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,29 +100,29 @@ func (s *section) Emit(t *table) {
100100

101101
// A line containing data in the tabular output.
102102
type item struct {
103-
name string
104-
path *Path
105-
value counts.Humanable
106-
prefixes []counts.Prefix
107-
unit string
108-
scale float64
103+
name string
104+
path *Path
105+
value counts.Humanable
106+
humaner counts.Humaner
107+
unit string
108+
scale float64
109109
}
110110

111111
func newItem(
112112
name string,
113113
path *Path,
114114
value counts.Humanable,
115-
prefixes []counts.Prefix,
115+
humaner counts.Humaner,
116116
unit string,
117117
scale float64,
118118
) *item {
119119
return &item{
120-
name: name,
121-
path: path,
122-
value: value,
123-
prefixes: prefixes,
124-
unit: unit,
125-
scale: scale,
120+
name: name,
121+
path: path,
122+
value: value,
123+
humaner: humaner,
124+
unit: unit,
125+
scale: scale,
126126
}
127127
}
128128

@@ -131,7 +131,7 @@ func (l *item) Emit(t *table) {
131131
if !interesting {
132132
return
133133
}
134-
valueString, unitString := l.value.Human(l.prefixes, l.unit)
134+
valueString, unitString := l.value.Human(l.humaner, l.unit)
135135
t.formatRow(
136136
l.name, t.footnotes.CreateCitation(l.Footnote(t.nameStyle)),
137137
valueString, unitString,
@@ -375,8 +375,8 @@ func (t *table) formatRow(
375375
func (s HistorySize) Contents() tableContents {
376376
S := newSection
377377
I := newItem
378-
metric := counts.MetricPrefixes
379-
binary := counts.BinaryPrefixes
378+
metric := counts.Metric
379+
binary := counts.Binary
380380
return S(
381381
"",
382382
S(

0 commit comments

Comments
 (0)