| 62 | 62 |
// New creates new Progress instance, which orchestrates bars rendering process.
|
| 63 | 63 |
// Accepts mpb.ProgressOption funcs for customization.
|
| 64 | 64 |
func New(options ...ProgressOption) *Progress {
|
| 65 | |
// defaults
|
| 66 | |
conf := pState{
|
|
65 |
s := &pState{
|
| 67 | 66 |
bars: make([]*Bar, 0, 3),
|
| 68 | 67 |
width: pwidth,
|
| 69 | 68 |
format: pformat,
|
|
| 74 | 73 |
}
|
| 75 | 74 |
|
| 76 | 75 |
for _, opt := range options {
|
| 77 | |
opt(&conf)
|
|
76 |
opt(s)
|
| 78 | 77 |
}
|
| 79 | 78 |
|
| 80 | 79 |
p := &Progress{
|
| 81 | |
ewg: conf.ewg,
|
|
80 |
ewg: s.ewg,
|
| 82 | 81 |
wg: new(sync.WaitGroup),
|
| 83 | 82 |
done: make(chan struct{}),
|
| 84 | 83 |
ops: make(chan func(*pState)),
|
| 85 | 84 |
quit: make(chan struct{}),
|
| 86 | 85 |
}
|
| 87 | |
go p.server(conf)
|
|
86 |
go p.server(s)
|
| 88 | 87 |
return p
|
| 89 | 88 |
}
|
| 90 | 89 |
|
|
| 93 | 92 |
p.wg.Add(1)
|
| 94 | 93 |
result := make(chan *Bar, 1)
|
| 95 | 94 |
select {
|
| 96 | |
case p.ops <- func(c *pState) {
|
| 97 | |
options = append(options, barWidth(c.width), barFormat(c.format))
|
| 98 | |
b := newBar(c.idCounter, total, p.wg, c.cancel, options...)
|
| 99 | |
c.bars = append(c.bars, b)
|
| 100 | |
c.idCounter++
|
|
95 |
case p.ops <- func(s *pState) {
|
|
96 |
options = append(options, barWidth(s.width), barFormat(s.format))
|
|
97 |
b := newBar(s.idCounter, total, p.wg, s.cancel, options...)
|
|
98 |
s.bars = append(s.bars, b)
|
|
99 |
s.idCounter++
|
| 101 | 100 |
result <- b
|
| 102 | 101 |
}:
|
| 103 | 102 |
return <-result
|
|
| 110 | 109 |
func (p *Progress) RemoveBar(b *Bar) bool {
|
| 111 | 110 |
result := make(chan bool, 1)
|
| 112 | 111 |
select {
|
| 113 | |
case p.ops <- func(c *pState) {
|
|
112 |
case p.ops <- func(s *pState) {
|
| 114 | 113 |
var ok bool
|
| 115 | |
for i, bar := range c.bars {
|
|
114 |
for i, bar := range s.bars {
|
| 116 | 115 |
if bar == b {
|
| 117 | 116 |
bar.Complete()
|
| 118 | |
c.bars = append(c.bars[:i], c.bars[i+1:]...)
|
|
117 |
s.bars = append(s.bars[:i], s.bars[i+1:]...)
|
| 119 | 118 |
ok = true
|
| 120 | 119 |
break
|
| 121 | 120 |
}
|
|
| 132 | 131 |
func (p *Progress) BarCount() int {
|
| 133 | 132 |
result := make(chan int, 1)
|
| 134 | 133 |
select {
|
| 135 | |
case p.ops <- func(c *pState) {
|
| 136 | |
result <- len(c.bars)
|
|
134 |
case p.ops <- func(s *pState) {
|
|
135 |
result <- len(s.bars)
|
| 137 | 136 |
}:
|
| 138 | 137 |
return <-result
|
| 139 | 138 |
case <-p.quit:
|