From a20d80565b40f9eef2dcd469d012f044f9d29606 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Mon, 11 Jul 2016 13:04:30 +0100 Subject: [PATCH] Tidy stats output - fixes #541 --- fs/accounting.go | 4 ++-- fs/config.go | 27 +++++++++++++++++++++------ fs/config_test.go | 21 +++++++++++++++++++++ rclone.go | 2 +- 4 files changed, 45 insertions(+), 9 deletions(-) diff --git a/fs/accounting.go b/fs/accounting.go index 0c5c57554..84a414ace 100644 --- a/fs/accounting.go +++ b/fs/accounting.go @@ -124,13 +124,13 @@ func (s *StatsInfo) String() string { dtRounded := dt - (dt % (time.Second / 10)) buf := &bytes.Buffer{} fmt.Fprintf(buf, ` -Transferred: %10vBytes (%vByte/s) +Transferred: %10s (%s) Errors: %10d Checks: %10d Transferred: %10d Elapsed time: %10v `, - SizeSuffix(s.bytes), SizeSuffix(speed), + SizeSuffix(s.bytes).Unit("Bytes"), SizeSuffix(speed).Unit("Bytes/s"), s.errors, s.checks, s.transfers, diff --git a/fs/config.go b/fs/config.go index e94483e7b..ba26a6de6 100644 --- a/fs/config.go +++ b/fs/config.go @@ -100,15 +100,15 @@ func init() { pflag.VarP(&bwLimit, "bwlimit", "", "Bandwidth limit in kBytes/s, or use suffix b|k|M|G") } -// Turn SizeSuffix into a string -func (x SizeSuffix) String() string { +// Turn SizeSuffix into a string and a suffix +func (x SizeSuffix) string() (string, string) { scaled := float64(0) suffix := "" switch { case x < 0: - return "off" + return "off", "" case x == 0: - return "0" + return "0", "" case x < 1024: scaled = float64(x) suffix = "" @@ -123,9 +123,24 @@ func (x SizeSuffix) String() string { suffix = "G" } 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 diff --git a/fs/config_test.go b/fs/config_test.go index 7d0c34b59..a9803dd54 100644 --- a/fs/config_test.go +++ b/fs/config_test.go @@ -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) { for _, test := range []struct { in string diff --git a/rclone.go b/rclone.go index c9f9d9c57..df421bd09 100644 --- a/rclone.go +++ b/rclone.go @@ -172,7 +172,7 @@ var Commands = []Command{ return err } 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 }, MinArgs: 1,