Tidy stats output - fixes #541

This commit is contained in:
Nick Craig-Wood 2016-07-11 13:04:30 +01:00
parent 56adb52a21
commit a20d80565b
4 changed files with 45 additions and 9 deletions

View File

@ -124,13 +124,13 @@ func (s *StatsInfo) String() string {
dtRounded := dt - (dt % (time.Second / 10)) dtRounded := dt - (dt % (time.Second / 10))
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
fmt.Fprintf(buf, ` fmt.Fprintf(buf, `
Transferred: %10vBytes (%vByte/s) Transferred: %10s (%s)
Errors: %10d Errors: %10d
Checks: %10d Checks: %10d
Transferred: %10d Transferred: %10d
Elapsed time: %10v Elapsed time: %10v
`, `,
SizeSuffix(s.bytes), SizeSuffix(speed), SizeSuffix(s.bytes).Unit("Bytes"), SizeSuffix(speed).Unit("Bytes/s"),
s.errors, s.errors,
s.checks, s.checks,
s.transfers, s.transfers,

View File

@ -100,15 +100,15 @@ func init() {
pflag.VarP(&bwLimit, "bwlimit", "", "Bandwidth limit in kBytes/s, or use suffix b|k|M|G") pflag.VarP(&bwLimit, "bwlimit", "", "Bandwidth limit in kBytes/s, or use suffix b|k|M|G")
} }
// Turn SizeSuffix into a string // Turn SizeSuffix into a string and a suffix
func (x SizeSuffix) String() string { func (x SizeSuffix) string() (string, string) {
scaled := float64(0) scaled := float64(0)
suffix := "" suffix := ""
switch { switch {
case x < 0: case x < 0:
return "off" return "off", ""
case x == 0: case x == 0:
return "0" return "0", ""
case x < 1024: case x < 1024:
scaled = float64(x) scaled = float64(x)
suffix = "" suffix = ""
@ -123,9 +123,24 @@ func (x SizeSuffix) String() string {
suffix = "G" suffix = "G"
} }
if math.Floor(scaled) == scaled { if math.Floor(scaled) == scaled {
return fmt.Sprintf("%.0f%s", scaled, suffix) return fmt.Sprintf("%.0f", scaled), suffix
} }
return fmt.Sprintf("%.3f%s", scaled, suffix) return fmt.Sprintf("%.3f", scaled), suffix
}
// String turns SizeSuffix into a string
func (x SizeSuffix) String() string {
val, suffix := x.string()
return val + suffix
}
// Unit turns SizeSuffix into a string with a unit
func (x SizeSuffix) Unit(unit string) string {
val, suffix := x.string()
if val == "off" {
return val
}
return val + " " + suffix + unit
} }
// Set a SizeSuffix // Set a SizeSuffix

View File

@ -28,6 +28,27 @@ func TestSizeSuffixString(t *testing.T) {
} }
} }
func TestSizeSuffixUnit(t *testing.T) {
for _, test := range []struct {
in float64
want string
}{
{0, "0 Bytes"},
{102, "102 Bytes"},
{1024, "1 kBytes"},
{1024 * 1024, "1 MBytes"},
{1024 * 1024 * 1024, "1 GBytes"},
{10 * 1024 * 1024 * 1024, "10 GBytes"},
{10.1 * 1024 * 1024 * 1024, "10.100 GBytes"},
{-1, "off"},
{-100, "off"},
} {
ss := SizeSuffix(test.in)
got := ss.Unit("Bytes")
assert.Equal(t, test.want, got)
}
}
func TestSizeSuffixSet(t *testing.T) { func TestSizeSuffixSet(t *testing.T) {
for _, test := range []struct { for _, test := range []struct {
in string in string

View File

@ -172,7 +172,7 @@ var Commands = []Command{
return err return err
} }
fmt.Printf("Total objects: %d\n", objects) fmt.Printf("Total objects: %d\n", objects)
fmt.Printf("Total size: %v (%d bytes)\n", fs.SizeSuffix(size), size) fmt.Printf("Total size: %s (%d Bytes)\n", fs.SizeSuffix(size).Unit("Bytes"), size)
return nil return nil
}, },
MinArgs: 1, MinArgs: 1,