Skip to content

Commit c4805ca

Browse files
committed
fix(cmd.list): qri list now displays datasets size in KBs, MBs, etc
1 parent 0ef9b80 commit c4805ca

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

cmd/print.go

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ import (
1919
"github.com/spf13/cobra"
2020
)
2121

22+
const (
23+
bite = 1 << (10 * iota)
24+
kilobyte
25+
megabyte
26+
gigabyte
27+
terabyte
28+
petabyte
29+
exabyte
30+
zettabyte
31+
yottabyte
32+
)
33+
2234
var printPrompt = color.New(color.FgWhite).PrintfFunc()
2335
var spinner = sp.New(sp.CharSets[24], 100*time.Millisecond)
2436

@@ -46,6 +58,48 @@ func printNotYetFinished(cmd *cobra.Command) {
4658
color.Yellow("%s command is not yet implemented", cmd.Name())
4759
}
4860

61+
func printByteInfo(l int) string {
62+
length := struct {
63+
name string
64+
value int
65+
}{"", 0}
66+
67+
switch {
68+
// yottabyte and zettabyte overflow int
69+
// case l > yottabyte:
70+
// length.name = "YB"
71+
// length.value = l / yottabyte
72+
// case l > zettabyte:
73+
// length.name = "ZB"
74+
// length.value = l / zettabyte
75+
case l >= exabyte:
76+
length.name = "EB"
77+
length.value = l / exabyte
78+
case l >= petabyte:
79+
length.name = "PB"
80+
length.value = l / petabyte
81+
case l >= terabyte:
82+
length.name = "TB"
83+
length.value = l / terabyte
84+
case l >= gigabyte:
85+
length.name = "GB"
86+
length.value = l / gigabyte
87+
case l >= megabyte:
88+
length.name = "MB"
89+
length.value = l / megabyte
90+
case l >= kilobyte:
91+
length.name = "KB"
92+
length.value = l / kilobyte
93+
default:
94+
length.name = "byte"
95+
length.value = l
96+
}
97+
if length.value != 1 {
98+
length.name += "s"
99+
}
100+
return fmt.Sprintf("%v %s", length.value, length.name)
101+
}
102+
49103
func printDatasetRefInfo(i int, ref repo.DatasetRef) {
50104
white := color.New(color.FgWhite).SprintFunc()
51105
cyan := color.New(color.FgCyan).SprintFunc()
@@ -68,7 +122,7 @@ func printDatasetRefInfo(i int, ref repo.DatasetRef) {
68122
}
69123
}
70124
if ds != nil && ds.Structure != nil {
71-
fmt.Printf(" %d bytes, %d entries, %d errors", ds.Structure.Length, ds.Structure.Entries, ds.Structure.ErrCount)
125+
fmt.Printf(" %s, %d entries, %d errors", printByteInfo(ds.Structure.Length), ds.Structure.Entries, ds.Structure.ErrCount)
72126
}
73127

74128
fmt.Println()

cmd/print_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cmd
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestPrintByteInfo(t *testing.T) {
8+
cases := []struct {
9+
bytes int
10+
expect string
11+
}{
12+
{1, "1 byte"},
13+
{2, "2 bytes"},
14+
{kilobyte * 4, "4 KBs"},
15+
{megabyte * 3, "3 MBs"},
16+
{gigabyte + 1000, "1 GB"},
17+
{terabyte * 2, "2 TBs"},
18+
{petabyte * 100, "100 PBs"},
19+
{exabyte, "1 EB"},
20+
}
21+
22+
for i, c := range cases {
23+
got := printByteInfo(c.bytes)
24+
if got != c.expect {
25+
t.Errorf("case %d expect != got: %s != %s", i, c.expect, got)
26+
continue
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)