forked from pterm/pterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
progressbar_printer.go
305 lines (253 loc) · 8.1 KB
/
progressbar_printer.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package pterm
import (
"strconv"
"strings"
"time"
"github.com/gookit/color"
"github.com/pterm/pterm/internal"
)
// ActiveProgressBarPrinters contains all running ProgressbarPrinters.
// Generally, there should only be one active ProgressbarPrinter at a time.
var ActiveProgressBarPrinters []*ProgressbarPrinter
var (
// DefaultProgressbar is the default ProgressbarPrinter.
DefaultProgressbar = ProgressbarPrinter{
Total: 100,
BarCharacter: "█",
LastCharacter: "█",
ElapsedTimeRoundingFactor: time.Second,
BarStyle: &ThemeDefault.ProgressbarBarStyle,
TitleStyle: &ThemeDefault.ProgressbarTitleStyle,
ShowTitle: true,
ShowCount: true,
ShowPercentage: true,
ShowElapsedTime: true,
BarFiller: "░",
MaxWidth: 80,
}
)
// ProgressbarPrinter shows a progress animation in the terminal.
type ProgressbarPrinter struct {
Title string
Total int
Current int
BarCharacter string
LastCharacter string
ElapsedTimeRoundingFactor time.Duration
BarFiller string
MaxWidth int
ShowElapsedTime bool
ShowCount bool
ShowTitle bool
ShowPercentage bool
RemoveWhenDone bool
TitleStyle *Style
BarStyle *Style
IsActive bool
startedAt time.Time
}
// WithTitle sets the name of the ProgressbarPrinter.
func (p ProgressbarPrinter) WithTitle(name string) *ProgressbarPrinter {
p.Title = name
return &p
}
// WithMaxWidth sets the maximum width of the ProgressbarPrinter.
// If the terminal is smaller than the given width, the terminal width will be used instead.
// If the width is set to zero, or below, the terminal width will be used.
func (p ProgressbarPrinter) WithMaxWidth(maxWidth int) *ProgressbarPrinter {
p.MaxWidth = maxWidth
return &p
}
// WithTotal sets the total value of the ProgressbarPrinter.
func (p ProgressbarPrinter) WithTotal(total int) *ProgressbarPrinter {
p.Total = total
return &p
}
// WithCurrent sets the current value of the ProgressbarPrinter.
func (p ProgressbarPrinter) WithCurrent(current int) *ProgressbarPrinter {
p.Current = current
return &p
}
// WithBarCharacter sets the bar character of the ProgressbarPrinter.
func (p ProgressbarPrinter) WithBarCharacter(char string) *ProgressbarPrinter {
p.BarCharacter = char
return &p
}
// WithLastCharacter sets the last character of the ProgressbarPrinter.
func (p ProgressbarPrinter) WithLastCharacter(char string) *ProgressbarPrinter {
p.LastCharacter = char
return &p
}
// WithElapsedTimeRoundingFactor sets the rounding factor of the elapsed time.
func (p ProgressbarPrinter) WithElapsedTimeRoundingFactor(duration time.Duration) *ProgressbarPrinter {
p.ElapsedTimeRoundingFactor = duration
return &p
}
// WithShowElapsedTime sets if the elapsed time should be displayed in the ProgressbarPrinter.
func (p ProgressbarPrinter) WithShowElapsedTime(b ...bool) *ProgressbarPrinter {
p.ShowElapsedTime = internal.WithBoolean(b)
return &p
}
// WithShowCount sets if the total and current count should be displayed in the ProgressbarPrinter.
func (p ProgressbarPrinter) WithShowCount(b ...bool) *ProgressbarPrinter {
p.ShowCount = internal.WithBoolean(b)
return &p
}
// WithShowTitle sets if the title should be displayed in the ProgressbarPrinter.
func (p ProgressbarPrinter) WithShowTitle(b ...bool) *ProgressbarPrinter {
p.ShowTitle = internal.WithBoolean(b)
return &p
}
// WithShowPercentage sets if the completed percentage should be displayed in the ProgressbarPrinter.
func (p ProgressbarPrinter) WithShowPercentage(b ...bool) *ProgressbarPrinter {
p.ShowPercentage = internal.WithBoolean(b)
return &p
}
// WithTitleStyle sets the style of the title.
func (p ProgressbarPrinter) WithTitleStyle(style *Style) *ProgressbarPrinter {
p.TitleStyle = style
return &p
}
// WithBarStyle sets the style of the bar.
func (p ProgressbarPrinter) WithBarStyle(style *Style) *ProgressbarPrinter {
p.BarStyle = style
return &p
}
// WithRemoveWhenDone sets if the ProgressbarPrinter should be removed when it is done.
func (p ProgressbarPrinter) WithRemoveWhenDone(b ...bool) *ProgressbarPrinter {
p.RemoveWhenDone = internal.WithBoolean(b)
return &p
}
// WithBarFiller sets the filler character for the ProgressbarPrinter.
func (p ProgressbarPrinter) WithBarFiller(char string) *ProgressbarPrinter {
p.BarFiller = char
return &p
}
// Increment current value by one.
func (p *ProgressbarPrinter) Increment() *ProgressbarPrinter {
p.Add(1)
return p
}
// This method changed the title and re-renders the progressbar
func (p *ProgressbarPrinter) UpdateTitle(title string) *ProgressbarPrinter {
p.Title = title
p.updateProgress()
return p
}
// This is the update logic, renders the progressbar
func (p *ProgressbarPrinter) updateProgress() *ProgressbarPrinter {
if p.TitleStyle == nil {
p.TitleStyle = NewStyle()
}
if p.BarStyle == nil {
p.BarStyle = NewStyle()
}
if p.Total == 0 {
return nil
}
var before string
var after string
var width int
if p.MaxWidth <= 0 {
width = GetTerminalWidth()
} else if GetTerminalWidth() < p.MaxWidth {
width = GetTerminalWidth()
} else {
width = p.MaxWidth
}
currentPercentage := int(internal.PercentageRound(float64(int64(p.Total)), float64(int64(p.Current))))
decoratorCount := Gray("[") + LightWhite(p.Current) + Gray("/") + LightWhite(p.Total) + Gray("]")
decoratorCurrentPercentage := color.RGB(NewRGB(255, 0, 0).Fade(0, float32(p.Total), float32(p.Current), NewRGB(0, 255, 0)).GetValues()).
Sprint(strconv.Itoa(currentPercentage) + "%")
decoratorTitle := p.TitleStyle.Sprint(p.Title)
if p.ShowTitle {
before += decoratorTitle + " "
}
if p.ShowCount {
before += decoratorCount + " "
}
after += " "
if p.ShowPercentage {
after += decoratorCurrentPercentage + " "
}
if p.ShowElapsedTime {
after += "| " + p.parseElapsedTime()
}
barMaxLength := width - len(RemoveColorFromString(before)) - len(RemoveColorFromString(after)) - 1
barCurrentLength := (p.Current * barMaxLength) / p.Total
var barFiller string
if barMaxLength-barCurrentLength > 0 {
barFiller = strings.Repeat(p.BarFiller, barMaxLength-barCurrentLength)
}
var bar string
if barCurrentLength > 0 {
bar = p.BarStyle.Sprint(strings.Repeat(p.BarCharacter, barCurrentLength)+p.LastCharacter) + barFiller
} else {
bar = ""
}
if !RawOutput {
Printo(before + bar + after)
}
return p
}
// Add to current value.
func (p *ProgressbarPrinter) Add(count int) *ProgressbarPrinter {
if p.Total == 0 {
return nil
}
p.Current += count
p.updateProgress()
if p.Current == p.Total {
p.Stop()
}
return p
}
// Start the ProgressbarPrinter.
func (p ProgressbarPrinter) Start() (*ProgressbarPrinter, error) {
if RawOutput && p.ShowTitle {
Println(p.Title)
}
p.IsActive = true
ActiveProgressBarPrinters = append(ActiveProgressBarPrinters, &p)
p.startedAt = time.Now()
p.updateProgress()
return &p, nil
}
// Stop the ProgressbarPrinter.
func (p *ProgressbarPrinter) Stop() (*ProgressbarPrinter, error) {
if !p.IsActive {
return p, nil
}
p.IsActive = false
if p.RemoveWhenDone {
clearLine()
Printo()
} else {
Println()
}
return p, nil
}
// GenericStart runs Start, but returns a LivePrinter.
// This is used for the interface LivePrinter.
// You most likely want to use Start instead of this in your program.
func (p ProgressbarPrinter) GenericStart() (*LivePrinter, error) {
p2, _ := p.Start()
lp := LivePrinter(p2)
return &lp, nil
}
// GenericStop runs Stop, but returns a LivePrinter.
// This is used for the interface LivePrinter.
// You most likely want to use Stop instead of this in your program.
func (p ProgressbarPrinter) GenericStop() (*LivePrinter, error) {
p2, _ := p.Stop()
lp := LivePrinter(p2)
return &lp, nil
}
// GetElapsedTime returns the elapsed time, since the ProgressbarPrinter was started.
func (p *ProgressbarPrinter) GetElapsedTime() time.Duration {
return time.Since(p.startedAt)
}
func (p *ProgressbarPrinter) parseElapsedTime() string {
s := p.GetElapsedTime().Round(p.ElapsedTimeRoundingFactor).String()
return s
}