/
archive_progress.go
84 lines (74 loc) · 1.77 KB
/
archive_progress.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package core
import (
"io"
"math"
"strconv"
"time"
)
var (
suffixes = [5]string{"B", "KB", "MB", "GB", "TB"}
)
func round(val float64, roundOn float64, places int) (newVal float64) {
var round float64
pow := math.Pow(10, float64(places))
digit := pow * val
_, div := math.Modf(digit)
if div >= roundOn {
round = math.Ceil(digit)
} else {
round = math.Floor(digit)
}
newVal = round / pow
return
}
func humanFileSize(size float64) string {
if size < 1 {
return "0 B"
}
base := math.Log(size) / math.Log(1024)
getSize := round(math.Pow(1024, base-math.Floor(base)), .5, 2)
getSuffix := suffixes[int(math.Floor(base))]
return strconv.FormatFloat(getSize, 'f', -1, 64) + " " + getSuffix
}
type archiveProgressCounter struct {
written int64
total string
onProgress func(written string, total string)
printTimer *time.Ticker
done chan struct{}
}
func (this *archiveProgressCounter) Write(p []byte) (n int, e error) {
n = len(p)
this.written += int64(n)
return
}
func (this *archiveProgressCounter) Read(p []byte) (n int, err error) {
n = len(p)
this.written += int64(n)
return
}
func (this *archiveProgressCounter) Close() error {
this.reportProgress()
this.printTimer.Stop()
close(this.done)
return nil
}
func (this *archiveProgressCounter) reportProgress() {
this.onProgress(humanFileSize(float64(this.written)), this.total)
}
func newArchiveProgressCounter(size int64, onProgress func(written, total string)) io.WriteCloser {
this := &archiveProgressCounter{total: humanFileSize(float64(size)), onProgress: onProgress}
this.printTimer = time.NewTicker(2 * time.Second)
this.done = make(chan struct{})
go func() {
for {
select {
case <-this.printTimer.C:
this.reportProgress()
case <-this.done:
return
}
}
}()
return this
}