-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogress.go
40 lines (34 loc) · 865 Bytes
/
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
package bifrost
import (
"fmt"
"time"
)
type Progress struct {
Start time.Time
LastPrint time.Time
Current uint64
Total uint64
}
func (p *Progress) Reset(total uint64) {
p.Start = time.Now()
p.Current = 0
p.Total = total
p.LastPrint = time.Now()
}
func (p *Progress) Increment() {
p.Current++
}
// Print prints the current progress bar to the console with current, total, percentage and ETA
func (p *Progress) Print() {
if time.Since(p.LastPrint) < time.Second {
return
}
p.LastPrint = time.Now()
fmt.Printf("\r%v/%v (%.2f%%) ETA: %v", p.Current, p.Total, float64(p.Current)/float64(p.Total)*100, p.ETA())
}
// ETA returns the estimated time of arrival
func (p *Progress) ETA() string {
elapsed := time.Since(p.Start)
eta := time.Duration(float64(elapsed) / float64(p.Current) * float64(p.Total-p.Current))
return eta.String()
}