| 16 | 16 |
type (
|
| 17 | 17 |
// BeforeRender is a func, which gets called before render process
|
| 18 | 18 |
BeforeRender func([]*Bar)
|
| 19 | |
barAction uint
|
| 20 | |
|
| 21 | |
bCommandData struct {
|
| 22 | |
action barAction
|
| 23 | |
bar *Bar
|
| 24 | |
result chan bool
|
| 25 | |
}
|
| 26 | 19 |
|
| 27 | 20 |
widthSync struct {
|
| 28 | 21 |
listen []chan int
|
|
| 33 | 26 |
userConf struct {
|
| 34 | 27 |
width int
|
| 35 | 28 |
format string
|
|
29 |
bars []*Bar
|
| 36 | 30 |
beforeRender BeforeRender
|
| 37 | 31 |
cw *cwriter.Writer
|
| 38 | 32 |
ticker *time.Ticker
|
|
| 40 | 34 |
shutdownNotifier chan struct{}
|
| 41 | 35 |
cancel <-chan struct{}
|
| 42 | 36 |
}
|
| 43 | |
)
|
| 44 | |
|
| 45 | |
const (
|
| 46 | |
bAdd barAction = iota
|
| 47 | |
bRemove
|
| 48 | 37 |
)
|
| 49 | 38 |
|
| 50 | 39 |
const (
|
|
| 61 | 50 |
// WaitGroup for internal rendering sync
|
| 62 | 51 |
wg *sync.WaitGroup
|
| 63 | 52 |
|
| 64 | |
done chan struct{}
|
| 65 | |
userConfCh chan userConf
|
| 66 | |
bCommandCh chan *bCommandData
|
| 67 | |
barCountCh chan int
|
| 68 | |
stopReqCh chan struct{}
|
| 69 | |
uncompletedBarStopReqCh chan struct{}
|
|
53 |
done chan struct{}
|
|
54 |
ops chan func(*userConf)
|
|
55 |
stopReqCh chan struct{}
|
| 70 | 56 |
|
| 71 | 57 |
// following is used after (*Progress.done) is closed
|
| 72 | 58 |
conf userConf
|
|
| 77 | 63 |
// If you don't plan to cancel, it is safe to feed with nil
|
| 78 | 64 |
func New() *Progress {
|
| 79 | 65 |
p := &Progress{
|
| 80 | |
wg: new(sync.WaitGroup),
|
| 81 | |
done: make(chan struct{}),
|
| 82 | |
userConfCh: make(chan userConf),
|
| 83 | |
bCommandCh: make(chan *bCommandData),
|
| 84 | |
barCountCh: make(chan int),
|
| 85 | |
stopReqCh: make(chan struct{}),
|
| 86 | |
uncompletedBarStopReqCh: make(chan struct{}),
|
|
66 |
wg: new(sync.WaitGroup),
|
|
67 |
done: make(chan struct{}),
|
|
68 |
ops: make(chan func(*userConf)),
|
|
69 |
stopReqCh: make(chan struct{}),
|
| 87 | 70 |
}
|
| 88 | 71 |
go p.server(userConf{
|
|
72 |
bars: make([]*Bar, 0, 3),
|
| 89 | 73 |
width: pwidth,
|
| 90 | 74 |
format: pformat,
|
| 91 | 75 |
cw: cwriter.New(os.Stdout),
|
|
| 100 | 84 |
if ch == nil {
|
| 101 | 85 |
panic("nil cancel channel")
|
| 102 | 86 |
}
|
| 103 | |
p.updateConf(func(c *userConf) {
|
|
87 |
return updateConf(p, func(c *userConf) {
|
| 104 | 88 |
c.cancel = ch
|
| 105 | 89 |
})
|
| 106 | |
return p
|
| 107 | 90 |
}
|
| 108 | 91 |
|
| 109 | 92 |
// SetWidth overrides default (80) width of bar(s).
|
|
| 111 | 94 |
if width < 2 {
|
| 112 | 95 |
return p
|
| 113 | 96 |
}
|
| 114 | |
p.updateConf(func(c *userConf) {
|
|
97 |
return updateConf(p, func(c *userConf) {
|
| 115 | 98 |
c.width = width
|
| 116 | 99 |
})
|
| 117 | |
return p
|
| 118 | 100 |
}
|
| 119 | 101 |
|
| 120 | 102 |
// SetOut sets underlying writer of progress. Default one is os.Stdout.
|
|
| 122 | 104 |
if w == nil {
|
| 123 | 105 |
return p
|
| 124 | 106 |
}
|
| 125 | |
p.updateConf(func(c *userConf) {
|
|
107 |
return updateConf(p, func(c *userConf) {
|
| 126 | 108 |
c.cw.Flush()
|
| 127 | 109 |
c.cw = cwriter.New(w)
|
| 128 | 110 |
})
|
| 129 | |
return p
|
| 130 | 111 |
}
|
| 131 | 112 |
|
| 132 | 113 |
// RefreshRate overrides default (100ms) refresh rate value
|
| 133 | 114 |
func (p *Progress) RefreshRate(d time.Duration) *Progress {
|
| 134 | |
p.updateConf(func(c *userConf) {
|
|
115 |
rr = d // TODO: don't update global var
|
|
116 |
return updateConf(p, func(c *userConf) {
|
| 135 | 117 |
c.ticker.Stop()
|
| 136 | 118 |
c.ticker = time.NewTicker(d)
|
| 137 | |
rr = d
|
| 138 | |
})
|
| 139 | |
return p
|
|
119 |
})
|
| 140 | 120 |
}
|
| 141 | 121 |
|
| 142 | 122 |
// BeforeRenderFunc accepts a func, which gets called before render process.
|
| 143 | 123 |
func (p *Progress) BeforeRenderFunc(f BeforeRender) *Progress {
|
| 144 | |
p.updateConf(func(c *userConf) {
|
|
124 |
return updateConf(p, func(c *userConf) {
|
| 145 | 125 |
c.beforeRender = f
|
| 146 | 126 |
})
|
| 147 | |
return p
|
| 148 | 127 |
}
|
| 149 | 128 |
|
| 150 | 129 |
// AddBar creates a new progress bar and adds to the container.
|
|
| 154 | 133 |
|
| 155 | 134 |
// AddBarWithID creates a new progress bar and adds to the container.
|
| 156 | 135 |
func (p *Progress) AddBarWithID(id int, total int64) *Bar {
|
| 157 | |
conf := p.getConf()
|
| 158 | |
bar := newBar(id, total, p.wg, &conf)
|
| 159 | |
p.bCommandCh <- &bCommandData{
|
| 160 | |
action: bAdd,
|
| 161 | |
bar: bar,
|
| 162 | |
}
|
| 163 | |
return bar
|
|
136 |
result := make(chan *Bar, 1)
|
|
137 |
op := func(c *userConf) {
|
|
138 |
bar := newBar(id, total, c.width, c.format, p.wg, c.cancel)
|
|
139 |
c.bars = append(c.bars, bar)
|
|
140 |
p.wg.Add(1)
|
|
141 |
result <- bar
|
|
142 |
}
|
|
143 |
select {
|
|
144 |
case p.ops <- op:
|
|
145 |
return <-result
|
|
146 |
case <-p.done:
|
|
147 |
return nil
|
|
148 |
}
|
| 164 | 149 |
}
|
| 165 | 150 |
|
| 166 | 151 |
// RemoveBar removes bar at any time.
|
| 167 | 152 |
func (p *Progress) RemoveBar(b *Bar) bool {
|
| 168 | |
result := make(chan bool)
|
| 169 | |
select {
|
| 170 | |
case p.bCommandCh <- &bCommandData{bRemove, b, result}:
|
|
153 |
result := make(chan bool, 1)
|
|
154 |
op := func(c *userConf) {
|
|
155 |
var ok bool
|
|
156 |
for i, bar := range c.bars {
|
|
157 |
if bar == b {
|
|
158 |
c.bars = append(c.bars[:i], c.bars[i+1:]...)
|
|
159 |
bar.remove()
|
|
160 |
ok = true
|
|
161 |
break
|
|
162 |
}
|
|
163 |
}
|
|
164 |
result <- ok
|
|
165 |
}
|
|
166 |
select {
|
|
167 |
case p.ops <- op:
|
| 171 | 168 |
return <-result
|
| 172 | 169 |
case <-p.done:
|
| 173 | 170 |
return false
|
|
| 176 | 173 |
|
| 177 | 174 |
// BarCount returns bars count in the container.
|
| 178 | 175 |
func (p *Progress) BarCount() int {
|
| 179 | |
select {
|
| 180 | |
case count := <-p.barCountCh:
|
| 181 | |
return count
|
|
176 |
result := make(chan int, 1)
|
|
177 |
op := func(c *userConf) {
|
|
178 |
result <- len(c.bars)
|
|
179 |
}
|
|
180 |
select {
|
|
181 |
case p.ops <- op:
|
|
182 |
return <-result
|
| 182 | 183 |
case <-p.done:
|
| 183 | 184 |
return 0
|
| 184 | 185 |
}
|
|
| 186 | 187 |
|
| 187 | 188 |
// ShutdownNotify means to be notified when main rendering goroutine quits, usualy after p.Stop() call.
|
| 188 | 189 |
func (p *Progress) ShutdownNotify(ch chan struct{}) *Progress {
|
| 189 | |
p.updateConf(func(c *userConf) {
|
|
190 |
return updateConf(p, func(c *userConf) {
|
| 190 | 191 |
c.shutdownNotifier = ch
|
| 191 | 192 |
})
|
| 192 | |
return p
|
| 193 | 193 |
}
|
| 194 | 194 |
|
| 195 | 195 |
// Format sets custom format for underlying bar(s), default one is "[=>-]".
|
|
| 197 | 197 |
if utf8.RuneCountInString(format) != numFmtRunes {
|
| 198 | 198 |
return p
|
| 199 | 199 |
}
|
| 200 | |
p.updateConf(func(c *userConf) {
|
|
200 |
return updateConf(p, func(c *userConf) {
|
| 201 | 201 |
c.format = format
|
| 202 | 202 |
})
|
| 203 | |
return p
|
| 204 | 203 |
}
|
| 205 | 204 |
|
| 206 | 205 |
// Stop shutdowns Progress' goroutine.
|
|
| 213 | 212 |
return
|
| 214 | 213 |
default:
|
| 215 | 214 |
// complete Total unknown bars
|
| 216 | |
p.uncompletedBarStopReqCh <- struct{}{}
|
|
215 |
p.ops <- func(c *userConf) {
|
|
216 |
for _, b := range c.bars {
|
|
217 |
s := b.getState()
|
|
218 |
if !s.completed && !s.aborted {
|
|
219 |
b.Complete()
|
|
220 |
}
|
|
221 |
}
|
|
222 |
}
|
| 217 | 223 |
// wait for all bars to quit
|
| 218 | 224 |
p.wg.Wait()
|
| 219 | 225 |
// stop request
|
|
| 223 | 229 |
}
|
| 224 | 230 |
}
|
| 225 | 231 |
|
| 226 | |
func (p *Progress) getConf() userConf {
|
| 227 | |
select {
|
| 228 | |
case conf := <-p.userConfCh:
|
| 229 | |
return conf
|
| 230 | |
case <-p.done:
|
| 231 | |
return p.conf
|
| 232 | |
}
|
| 233 | |
}
|
| 234 | |
|
| 235 | |
func (p *Progress) updateConf(cb func(*userConf)) {
|
| 236 | |
c := p.getConf()
|
| 237 | |
cb(&c)
|
| 238 | |
select {
|
| 239 | |
case p.userConfCh <- c:
|
| 240 | |
case <-p.done:
|
| 241 | |
return
|
|
232 |
// func (p *Progress) getConf() userConf {
|
|
233 |
// select {
|
|
234 |
// case conf := <-p.userConfCh:
|
|
235 |
// return conf
|
|
236 |
// case <-p.done:
|
|
237 |
// return p.conf
|
|
238 |
// }
|
|
239 |
// }
|
|
240 |
|
|
241 |
// func (p *Progress) updateConf(op func(*userConf)) {
|
|
242 |
// // c := p.getConf()
|
|
243 |
// // cb(&c)
|
|
244 |
// select {
|
|
245 |
// case p.ops <- op:
|
|
246 |
// case <-p.done:
|
|
247 |
// return
|
|
248 |
// }
|
|
249 |
// }
|
|
250 |
|
|
251 |
func updateConf(p *Progress, op func(*userConf)) *Progress {
|
|
252 |
select {
|
|
253 |
case p.ops <- op:
|
|
254 |
return p
|
|
255 |
case <-p.done:
|
|
256 |
return nil
|
| 242 | 257 |
}
|
| 243 | 258 |
}
|
| 244 | 259 |
|
|
| 262 | 277 |
close(ch)
|
| 263 | 278 |
}
|
| 264 | 279 |
|
| 265 | |
bars := make([]*Bar, 0, 3)
|
| 266 | |
|
| 267 | 280 |
for {
|
| 268 | 281 |
select {
|
| 269 | |
case p.userConfCh <- conf:
|
| 270 | |
case conf = <-p.userConfCh:
|
| 271 | |
case data := <-p.bCommandCh:
|
| 272 | |
switch data.action {
|
| 273 | |
case bAdd:
|
| 274 | |
bars = append(bars, data.bar)
|
| 275 | |
p.wg.Add(1)
|
| 276 | |
case bRemove:
|
| 277 | |
var ok bool
|
| 278 | |
for i, b := range bars {
|
| 279 | |
if b == data.bar {
|
| 280 | |
bars = append(bars[:i], bars[i+1:]...)
|
| 281 | |
ok = true
|
| 282 | |
b.remove()
|
| 283 | |
break
|
| 284 | |
}
|
| 285 | |
}
|
| 286 | |
data.result <- ok
|
| 287 | |
}
|
| 288 | |
case p.barCountCh <- len(bars):
|
|
282 |
case op := <-p.ops:
|
|
283 |
op(&conf)
|
| 289 | 284 |
case <-conf.ticker.C:
|
| 290 | 285 |
var notick bool
|
| 291 | 286 |
select {
|
|
| 296 | 291 |
default:
|
| 297 | 292 |
}
|
| 298 | 293 |
|
| 299 | |
numBars := len(bars)
|
|
294 |
numBars := len(conf.bars)
|
| 300 | 295 |
if notick || numBars == 0 {
|
| 301 | 296 |
break
|
| 302 | 297 |
}
|
| 303 | 298 |
|
| 304 | 299 |
if conf.beforeRender != nil {
|
| 305 | |
conf.beforeRender(bars)
|
|
300 |
conf.beforeRender(conf.bars)
|
| 306 | 301 |
}
|
| 307 | 302 |
|
| 308 | 303 |
quitWidthSyncCh := make(chan struct{})
|
|
| 310 | 305 |
close(quitWidthSyncCh)
|
| 311 | 306 |
})
|
| 312 | 307 |
|
| 313 | |
b0 := bars[0]
|
|
308 |
b0 := conf.bars[0]
|
| 314 | 309 |
prependWs := newWidthSync(quitWidthSyncCh, numBars, b0.NumOfPrependers())
|
| 315 | 310 |
appendWs := newWidthSync(quitWidthSyncCh, numBars, b0.NumOfAppenders())
|
| 316 | 311 |
|
| 317 | 312 |
width, _, _ := cwriter.GetTermSize()
|
| 318 | 313 |
|
| 319 | 314 |
sequence := make([]<-chan []byte, numBars)
|
| 320 | |
for i, b := range bars {
|
|
315 |
for i, b := range conf.bars {
|
| 321 | 316 |
sequence[i] = b.render(recoverFn, width, prependWs, appendWs)
|
| 322 | 317 |
}
|
| 323 | 318 |
|
|
| 329 | 324 |
|
| 330 | 325 |
conf.cw.Flush()
|
| 331 | 326 |
|
| 332 | |
for _, b := range bars {
|
|
327 |
for _, b := range conf.bars {
|
| 333 | 328 |
b.flushed()
|
| 334 | |
}
|
| 335 | |
case <-p.uncompletedBarStopReqCh:
|
| 336 | |
for _, b := range bars {
|
| 337 | |
s := b.getState()
|
| 338 | |
if !s.completed && !s.aborted {
|
| 339 | |
b.Complete()
|
| 340 | |
}
|
| 341 | 329 |
}
|
| 342 | 330 |
case <-p.stopReqCh:
|
| 343 | 331 |
return
|