forked from TrueCloudLab/rclone
Make file size render more neatly and prevent from being < 0
This commit is contained in:
parent
c643e4585e
commit
09d71239b6
2 changed files with 27 additions and 12 deletions
29
fs/config.go
29
fs/config.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path"
|
"path"
|
||||||
|
@ -46,21 +47,30 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
pflag.VarP(&bwLimit, "bwlimit", "", "Bandwidth limit in kBytes/s, or use suffix K|M|G")
|
pflag.VarP(&bwLimit, "bwlimit", "", "Bandwidth limit in kBytes/s, or use suffix k|M|G")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Turn SizeSuffix into a string
|
// Turn SizeSuffix into a string
|
||||||
func (x *SizeSuffix) String() string {
|
func (x SizeSuffix) String() string {
|
||||||
|
scaled := float64(0)
|
||||||
|
suffix := ""
|
||||||
switch {
|
switch {
|
||||||
case *x == 0:
|
case x == 0:
|
||||||
return "0"
|
return "0"
|
||||||
case *x < 1024*1024:
|
case x < 1024*1024:
|
||||||
return fmt.Sprintf("%.3fk", float64(*x)/1024)
|
scaled = float64(x) / 1024
|
||||||
case *x < 1024*1024*1024:
|
suffix = "k"
|
||||||
return fmt.Sprintf("%.3fM", float64(*x)/1024/1024)
|
case x < 1024*1024*1024:
|
||||||
|
scaled = float64(x) / 1024 / 1024
|
||||||
|
suffix = "M"
|
||||||
default:
|
default:
|
||||||
return fmt.Sprintf("%.3fG", float64(*x)/1024/1024/1024)
|
scaled = float64(x) / 1024 / 1024 / 1024
|
||||||
|
suffix = "G"
|
||||||
}
|
}
|
||||||
|
if math.Floor(scaled) == scaled {
|
||||||
|
return fmt.Sprintf("%.0f%s", scaled, suffix)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%.3f%s", scaled, suffix)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set a SizeSuffix
|
// Set a SizeSuffix
|
||||||
|
@ -89,6 +99,9 @@ func (x *SizeSuffix) Set(s string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if value < 0 {
|
||||||
|
return fmt.Errorf("Size can't be negative %q", s)
|
||||||
|
}
|
||||||
value *= multiplier
|
value *= multiplier
|
||||||
*x = SizeSuffix(value)
|
*x = SizeSuffix(value)
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -9,10 +9,11 @@ func TestSizeSuffixString(t *testing.T) {
|
||||||
}{
|
}{
|
||||||
{0, "0"},
|
{0, "0"},
|
||||||
{102, "0.100k"},
|
{102, "0.100k"},
|
||||||
{1024, "1.000k"},
|
{1024, "1k"},
|
||||||
{1024 * 1024, "1.000M"},
|
{1024 * 1024, "1M"},
|
||||||
{1024 * 1024 * 1024, "1.000G"},
|
{1024 * 1024 * 1024, "1G"},
|
||||||
{10 * 1024 * 1024 * 1024, "10.000G"},
|
{10 * 1024 * 1024 * 1024, "10G"},
|
||||||
|
{10.1 * 1024 * 1024 * 1024, "10.100G"},
|
||||||
} {
|
} {
|
||||||
ss := SizeSuffix(test.in)
|
ss := SizeSuffix(test.in)
|
||||||
got := ss.String()
|
got := ss.String()
|
||||||
|
@ -41,6 +42,7 @@ func TestSizeSuffixSet(t *testing.T) {
|
||||||
{"1p", 0, true},
|
{"1p", 0, true},
|
||||||
{"1.p", 0, true},
|
{"1.p", 0, true},
|
||||||
{"1p", 0, true},
|
{"1p", 0, true},
|
||||||
|
{"-1K", 0, true},
|
||||||
} {
|
} {
|
||||||
ss := SizeSuffix(0)
|
ss := SizeSuffix(0)
|
||||||
err := ss.Set(test.in)
|
err := ss.Set(test.in)
|
||||||
|
|
Loading…
Reference in a new issue