Vendor in v0.3.2 of docker/go-units

docker/go-units is used in converting bytes to human readable format

Signed-off-by: umohnani8 <umohnani@redhat.com>

Closes: #82
Approved by: rhatdan
This commit is contained in:
umohnani8
2017-11-28 09:26:13 -05:00
committed by Atomic Bot
parent ab62fe17ff
commit c0eceaa403
2 changed files with 23 additions and 9 deletions

View File

@ -12,19 +12,21 @@ import (
func HumanDuration(d time.Duration) string { func HumanDuration(d time.Duration) string {
if seconds := int(d.Seconds()); seconds < 1 { if seconds := int(d.Seconds()); seconds < 1 {
return "Less than a second" return "Less than a second"
} else if seconds == 1 {
return "1 second"
} else if seconds < 60 { } else if seconds < 60 {
return fmt.Sprintf("%d seconds", seconds) return fmt.Sprintf("%d seconds", seconds)
} else if minutes := int(d.Minutes()); minutes == 1 { } else if minutes := int(d.Minutes()); minutes == 1 {
return "About a minute" return "About a minute"
} else if minutes < 60 { } else if minutes < 46 {
return fmt.Sprintf("%d minutes", minutes) return fmt.Sprintf("%d minutes", minutes)
} else if hours := int(d.Hours()); hours == 1 { } else if hours := int(d.Hours() + 0.5); hours == 1 {
return "About an hour" return "About an hour"
} else if hours < 48 { } else if hours < 48 {
return fmt.Sprintf("%d hours", hours) return fmt.Sprintf("%d hours", hours)
} else if hours < 24*7*2 { } else if hours < 24*7*2 {
return fmt.Sprintf("%d days", hours/24) return fmt.Sprintf("%d days", hours/24)
} else if hours < 24*30*3 { } else if hours < 24*30*2 {
return fmt.Sprintf("%d weeks", hours/24/7) return fmt.Sprintf("%d weeks", hours/24/7)
} else if hours < 24*365*2 { } else if hours < 24*365*2 {
return fmt.Sprintf("%d months", hours/24/30) return fmt.Sprintf("%d months", hours/24/30)

View File

@ -37,28 +37,40 @@ var (
var decimapAbbrs = []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"} var decimapAbbrs = []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
var binaryAbbrs = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"} var binaryAbbrs = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}
// CustomSize returns a human-readable approximation of a size func getSizeAndUnit(size float64, base float64, _map []string) (float64, string) {
// using custom format.
func CustomSize(format string, size float64, base float64, _map []string) string {
i := 0 i := 0
unitsLimit := len(_map) - 1 unitsLimit := len(_map) - 1
for size >= base && i < unitsLimit { for size >= base && i < unitsLimit {
size = size / base size = size / base
i++ i++
} }
return fmt.Sprintf(format, size, _map[i]) return size, _map[i]
}
// CustomSize returns a human-readable approximation of a size
// using custom format.
func CustomSize(format string, size float64, base float64, _map []string) string {
size, unit := getSizeAndUnit(size, base, _map)
return fmt.Sprintf(format, size, unit)
}
// HumanSizeWithPrecision allows the size to be in any precision,
// instead of 4 digit precision used in units.HumanSize.
func HumanSizeWithPrecision(size float64, precision int) string {
size, unit := getSizeAndUnit(size, 1000.0, decimapAbbrs)
return fmt.Sprintf("%.*g%s", precision, size, unit)
} }
// HumanSize returns a human-readable approximation of a size // HumanSize returns a human-readable approximation of a size
// capped at 4 valid numbers (eg. "2.746 MB", "796 KB"). // capped at 4 valid numbers (eg. "2.746 MB", "796 KB").
func HumanSize(size float64) string { func HumanSize(size float64) string {
return CustomSize("%.4g %s", size, 1000.0, decimapAbbrs) return HumanSizeWithPrecision(size, 4)
} }
// BytesSize returns a human-readable size in bytes, kibibytes, // BytesSize returns a human-readable size in bytes, kibibytes,
// mebibytes, gibibytes, or tebibytes (eg. "44kiB", "17MiB"). // mebibytes, gibibytes, or tebibytes (eg. "44kiB", "17MiB").
func BytesSize(size float64) string { func BytesSize(size float64) string {
return CustomSize("%.4g %s", size, 1024.0, binaryAbbrs) return CustomSize("%.4g%s", size, 1024.0, binaryAbbrs)
} }
// FromHumanSize returns an integer from a human-readable specification of a // FromHumanSize returns an integer from a human-readable specification of a