git-lfs/vendor/github.com/cheggaaa/pb/format.go

46 lines
949 B
Go
Raw Normal View History

2014-04-16 21:11:52 +00:00
package pb
import (
"fmt"
"strconv"
"strings"
)
2015-04-24 17:48:49 +00:00
type Units int
2014-04-16 21:11:52 +00:00
const (
// By default, without type handle
2015-04-24 17:48:49 +00:00
U_NO Units = iota
2014-04-16 21:11:52 +00:00
// Handle as b, Kb, Mb, etc
2015-04-24 17:48:49 +00:00
U_BYTES
2014-04-16 21:11:52 +00:00
)
// Format integer
2015-04-24 17:48:49 +00:00
func Format(i int64, units Units) string {
2014-04-16 21:11:52 +00:00
switch units {
case U_BYTES:
return FormatBytes(i)
2015-04-24 17:48:49 +00:00
default:
// by default just convert to string
return strconv.FormatInt(i, 10)
2014-04-16 21:11:52 +00:00
}
}
// Convert bytes to human readable string. Like a 2 MB, 64.2 KB, 52 B
func FormatBytes(i int64) (result string) {
switch {
case i > (1024 * 1024 * 1024 * 1024):
result = fmt.Sprintf("%#.02f TB", float64(i)/1024/1024/1024/1024)
case i > (1024 * 1024 * 1024):
result = fmt.Sprintf("%#.02f GB", float64(i)/1024/1024/1024)
case i > (1024 * 1024):
result = fmt.Sprintf("%#.02f MB", float64(i)/1024/1024)
case i > 1024:
result = fmt.Sprintf("%#.02f KB", float64(i)/1024)
default:
result = fmt.Sprintf("%d B", i)
}
result = strings.Trim(result, " ")
return
}