-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.go
More file actions
45 lines (36 loc) · 811 Bytes
/
format.go
File metadata and controls
45 lines (36 loc) · 811 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package code
import (
"fmt"
"strconv"
"strings"
"unicode"
"github.com/inhies/go-bytesize"
)
func Format(size int, path string, human bool) string {
if human {
humanSize := bytesize.New(float64(size))
return normalizeHumanByteSize(humanSize)
}
return fmt.Sprintf("%sB", strconv.Itoa(size))
}
func normalizeHumanByteSize(humanSize bytesize.ByteSize) string {
str := humanSize.String()
divider := 0
for i, r := range str {
if !unicode.IsDigit(r) && r != '.' {
divider = i
break
}
}
num := str[:divider]
unit := str[divider:]
if unit == "B" {
num = strings.TrimRight(num, "0")
num = strings.TrimRight(num, ".")
} else {
if strings.Contains(num, ".") && strings.HasSuffix(num, "00") {
num = strings.TrimSuffix(num, "0")
}
}
return fmt.Sprintf("%s%s", num, unit)
}