Skip to content

Commit

Permalink
fix(cmd.list): qri list now displays datasets size in KBs, MBs, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
ramfox committed Jun 1, 2018
1 parent 0ef9b80 commit c4805ca
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
56 changes: 55 additions & 1 deletion cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ import (
"github.com/spf13/cobra"
)

const (
bite = 1 << (10 * iota)
kilobyte
megabyte
gigabyte
terabyte
petabyte
exabyte
zettabyte
yottabyte
)

var printPrompt = color.New(color.FgWhite).PrintfFunc()
var spinner = sp.New(sp.CharSets[24], 100*time.Millisecond)

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

func printByteInfo(l int) string {
length := struct {
name string
value int
}{"", 0}

switch {
// yottabyte and zettabyte overflow int
// case l > yottabyte:
// length.name = "YB"
// length.value = l / yottabyte
// case l > zettabyte:
// length.name = "ZB"
// length.value = l / zettabyte
case l >= exabyte:
length.name = "EB"
length.value = l / exabyte
case l >= petabyte:
length.name = "PB"
length.value = l / petabyte
case l >= terabyte:
length.name = "TB"
length.value = l / terabyte
case l >= gigabyte:
length.name = "GB"
length.value = l / gigabyte
case l >= megabyte:
length.name = "MB"
length.value = l / megabyte
case l >= kilobyte:
length.name = "KB"
length.value = l / kilobyte
default:
length.name = "byte"
length.value = l
}
if length.value != 1 {
length.name += "s"
}
return fmt.Sprintf("%v %s", length.value, length.name)
}

func printDatasetRefInfo(i int, ref repo.DatasetRef) {
white := color.New(color.FgWhite).SprintFunc()
cyan := color.New(color.FgCyan).SprintFunc()
Expand All @@ -68,7 +122,7 @@ func printDatasetRefInfo(i int, ref repo.DatasetRef) {
}
}
if ds != nil && ds.Structure != nil {
fmt.Printf(" %d bytes, %d entries, %d errors", ds.Structure.Length, ds.Structure.Entries, ds.Structure.ErrCount)
fmt.Printf(" %s, %d entries, %d errors", printByteInfo(ds.Structure.Length), ds.Structure.Entries, ds.Structure.ErrCount)
}

fmt.Println()
Expand Down
29 changes: 29 additions & 0 deletions cmd/print_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cmd

import (
"testing"
)

func TestPrintByteInfo(t *testing.T) {
cases := []struct {
bytes int
expect string
}{
{1, "1 byte"},
{2, "2 bytes"},
{kilobyte * 4, "4 KBs"},
{megabyte * 3, "3 MBs"},
{gigabyte + 1000, "1 GB"},
{terabyte * 2, "2 TBs"},
{petabyte * 100, "100 PBs"},
{exabyte, "1 EB"},
}

for i, c := range cases {
got := printByteInfo(c.bytes)
if got != c.expect {
t.Errorf("case %d expect != got: %s != %s", i, c.expect, got)
continue
}
}
}

0 comments on commit c4805ca

Please sign in to comment.