-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress.go
More file actions
66 lines (55 loc) · 1.39 KB
/
progress.go
File metadata and controls
66 lines (55 loc) · 1.39 KB
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
package main
import (
"fmt"
"sync"
"time"
"github.com/schollz/progressbar/v2"
)
type ProgressTracker struct {
bar *progressbar.ProgressBar
total int64
current int64
mu sync.Mutex
startTime time.Time
}
func NewProgressTracker(total int64, description string) *ProgressTracker {
bar := progressbar.NewOptions64(
total,
progressbar.OptionSetDescription(description),
progressbar.OptionSetWidth(40),
progressbar.OptionShowCount(),
progressbar.OptionSetTheme(progressbar.Theme{
Saucer: "=",
SaucerHead: ">",
SaucerPadding: " ",
BarStart: "[",
BarEnd: "]",
}),
)
return &ProgressTracker{
bar: bar,
total: total,
current: 0,
startTime: time.Now(),
}
}
func (pt *ProgressTracker) Increment(n int64) {
pt.mu.Lock()
defer pt.mu.Unlock()
pt.current += n
_ = pt.bar.Add64(n)
}
func (pt *ProgressTracker) IncrementFiles(count int) {
pt.mu.Lock()
defer pt.mu.Unlock()
for i := 0; i < count; i++ {
_ = pt.bar.Add(1)
}
}
func (pt *ProgressTracker) Finish() {
pt.mu.Lock()
defer pt.mu.Unlock()
_ = pt.bar.Finish()
duration := time.Since(pt.startTime).Round(time.Millisecond)
fmt.Printf("\nCompleted in %v\n", duration)
}