forked from gosuri/uiprogress
/
progress.go
78 lines (66 loc) · 1.85 KB
/
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
package uiprogress
import (
"fmt"
"io"
"os"
"time"
"github.com/gosuri/uilive"
)
var (
// Out is the default writer to render progress bars to
Out = os.Stdout
// DefaultProgress is the default progress
DefaultProgress = New()
// RefreshInterval in the default time duration to wait for refreshing the output
RefreshInterval = time.Millisecond
)
// Progress represents the container that renders progress bars
type Progress struct {
// Out is the writer to render progress bars to
Out io.Writer
// Width is the width of the progress bars
Width int
// Bars is the collection of progress bars
Bars []*Bar
// RefreshInterval in the time duration to wait for refreshing the output
RefreshInterval time.Duration
}
// New returns a new progress bar with defaults
func New() *Progress {
return &Progress{
Width: Width,
Out: Out,
Bars: make([]*Bar, 0),
RefreshInterval: RefreshInterval,
}
}
// AddBar creates a new progress bar and adds it to the default progress container
func AddBar(total int) *Bar {
return DefaultProgress.AddBar(total)
}
// Start starts the rendering the progress of progress bars using the DefaultProgress. It listens for updates using `bar.Set(n)` and new bars when added using `AddBar`
func Start() {
DefaultProgress.Start()
}
// AddBar creates a new progress bar and adds to the container
func (p *Progress) AddBar(total int) *Bar {
bar := NewBar(total)
bar.Width = p.Width
p.Bars = append(p.Bars, bar)
return bar
}
// Start starts the rendering the progress of progress bars. It listens for updates using `bar.Set(n)` and new bars when added using `AddBar`
func (p *Progress) Start() {
lw := uilive.New()
lw.Out = p.Out
lw.RefreshInterval = p.RefreshInterval
go func() {
for {
for _, bar := range p.Bars {
fmt.Fprintln(lw, bar.String())
}
lw.Flush()
lw.Wait()
}
}()
}