/
progressbar.go
191 lines (147 loc) · 3.77 KB
/
progressbar.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package ui
import (
"fmt"
"io"
"math"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/gofrs/uuid"
"github.com/gookit/color"
"github.com/pterm/pterm"
)
type progressBar struct {
ID uuid.UUID
Title string
titleStyle *pterm.Style
barStyle *pterm.Style
Current, Total int64 // Absolute done and total
Percent float32 // Percentage done
RoundingFactor time.Duration
Started, Lastupdate time.Time
mutex sync.Mutex
barCharacter string
lastCharacter string
barFiller string
lastReport int64
Done bool
writer io.Writer
mu sync.Mutex
}
var (
pbLock sync.Mutex
progressbars = map[*progressBar]struct{}{}
)
func GetProgressBars() []*progressBar {
pbLock.Lock()
pbs := make([]*progressBar, len(progressbars))
var i int
for pb, _ := range progressbars {
if pb.Done && pb.lastReport == pb.Current {
delete(progressbars, pb)
continue
}
pb.lastReport = pb.Current
pbs[i] = pb
i++
}
pbLock.Unlock()
return pbs[:i]
}
func ProgressBar(title string, max int) *progressBar {
if max == 0 {
max = 1 // avoid division by zero in pterm
}
id, _ := uuid.NewV7()
pb := progressBar{
ID: id,
Title: title,
Total: int64(max),
RoundingFactor: time.Second,
barCharacter: "█",
lastCharacter: "█",
barFiller: " ",
}
if pb.titleStyle == nil {
pb.titleStyle = pterm.NewStyle()
}
if pb.barStyle == nil {
pb.barStyle = pterm.NewStyle()
}
pb.Start()
// Save it
pbLock.Lock()
progressbars[&pb] = struct{}{}
pbLock.Unlock()
return &pb
}
func (pb *progressBar) ChangeMax(newmax int) {
if newmax == 0 {
Fatal().Msg("Cannot set max to 0")
}
pb.Total = int64(newmax)
}
func (pb *progressBar) GetMax() int {
return int(pb.Total)
}
func (pb *progressBar) Start() {
pb.Started = time.Now()
}
func (pb *progressBar) Add(i int) {
atomic.AddInt64(&pb.Current, int64(i))
pb.update()
}
func (pb *progressBar) Set(i int) {
atomic.StoreInt64(&pb.Current, int64(i))
pb.update()
}
func (pb *progressBar) Finish() {
// Save it
pbLock.Lock()
delete(progressbars, pb)
pbLock.Unlock()
pb.Done = true
}
func (pb *progressBar) update() {
if time.Since(pb.Lastupdate) < 1*time.Second {
return
}
outputMutex.Lock()
clearneeded = true
pb.Lastupdate = time.Now()
var before string
var after string
width := pterm.GetTerminalWidth()
var currentPercentage float32
if pb.Total > 0 {
currentPercentage = float32(pb.Current) * 100 / float32(pb.Total)
}
if currentPercentage > 100 {
currentPercentage = 100
}
pb.Percent = currentPercentage
decoratorCount := pterm.Gray("[") + pterm.LightWhite(pb.Current) + pterm.Gray("/") + pterm.LightWhite(pb.Total) + pterm.Gray("]")
decoratorCurrentPercentage := color.RGB(pterm.NewRGB(255, 0, 0).Fade(0, float32(pb.Total), float32(pb.Current), pterm.NewRGB(0, 255, 0)).GetValues()).
Sprint(fmt.Sprintf("%.2f%%", currentPercentage))
decoratorTitle := pb.titleStyle.Sprint(pb.Title)
before += decoratorTitle + " "
before += decoratorCount + " "
after += " "
after += decoratorCurrentPercentage + " "
after += "| " + time.Since(pb.Started).Round(pb.RoundingFactor).String()
barMaxLength := width - len(pterm.RemoveColorFromString(before)) - len(pterm.RemoveColorFromString(after)) - 1
barCurrentLength := int(math.Round(float64(currentPercentage * float32(barMaxLength) / 100)))
var barFiller string
if barMaxLength-barCurrentLength > 0 {
barFiller = strings.Repeat(pb.barFiller, barMaxLength-barCurrentLength)
}
var bar string
if pb.Total > 0 && barCurrentLength > 0 {
bar = pb.barStyle.Sprint(strings.Repeat(pb.barCharacter, barCurrentLength)+pb.lastCharacter) + barFiller
} else {
bar = ""
}
pterm.Fprinto(pb.writer, before+bar+after)
outputMutex.Unlock()
}