-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformat.go
40 lines (33 loc) · 1.01 KB
/
format.go
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
package main
import (
"fmt"
"math"
"net"
"strings"
"time"
)
const unitBase = 1000
var suffixes = [...]string{"b", "kb", "Mb", "Gb", "Tb", "Pb", "Eb"}
// formatBytes returns the humanized bandwidth based on `bytes` and `seconds`.
func formatBytes(bytes int64, seconds float64) string {
raw := float64(bytes*8) / seconds
if raw <= 10 {
return fmt.Sprintf("%.2f b/s", raw)
}
exp := math.Floor(math.Log(raw) / math.Log(unitBase))
suffix := suffixes[int(exp)]
bandwidth := raw / math.Pow(unitBase, exp)
return fmt.Sprintf("%.2f %s/s", bandwidth, suffix)
}
// formatIP extracts an IP address out of `addr`.
func formatIP(addr net.Addr) string {
chunks := strings.Split(addr.String(), ":")
ip := strings.Join(chunks[0:len(chunks)-1], ":")
return strings.Trim(ip, "[]")
}
// formatProgress constructs a progress bar string for `n / d`.
func formatProgress(n, d time.Duration) string {
ratio := float64(n) / float64(d)
bar := strings.Repeat("=", int(ratio*20+0.5))
return fmt.Sprintf("[%-20s] %3.0f%%", bar, ratio*100)
}