Codebase list golang-github-vbauerster-mpb / 6013267
Refactoring Vladimir Bauer 9 years ago
3 changed file(s) with 41 addition(s) and 52 deletion(s). Raw diff Collapse all Expand all
2828 trimLeftCh chan bool
2929 trimRightCh chan bool
3030 refillCh chan *refill
31 decoratorCh chan *decorator
31 dCommandCh chan *dCommandData
3232 flushedCh chan struct{}
3333 removeReqCh chan struct{}
3434 completeReqCh chan struct{}
8888 trimLeftCh: make(chan bool),
8989 trimRightCh: make(chan bool),
9090 refillCh: make(chan *refill),
91 decoratorCh: make(chan *decorator),
91 dCommandCh: make(chan *dCommandData),
9292 flushedCh: make(chan struct{}, 1),
9393 removeReqCh: make(chan struct{}),
9494 completeReqCh: make(chan struct{}),
211211 if isClosed(b.done) {
212212 return b
213213 }
214 b.decoratorCh <- &decorator{decPrepend, f}
214 b.dCommandCh <- &dCommandData{dPrepend, f}
215215 return b
216216 }
217217
220220 if isClosed(b.done) {
221221 return
222222 }
223 b.decoratorCh <- &decorator{decPrependZero, nil}
223 b.dCommandCh <- &dCommandData{dPrependZero, nil}
224224 }
225225
226226 // AppendFunc appends DecoratorFunc
228228 if isClosed(b.done) {
229229 return b
230230 }
231 b.decoratorCh <- &decorator{decAppend, f}
231 b.dCommandCh <- &dCommandData{dAppend, f}
232232 return b
233233 }
234234
237237 if isClosed(b.done) {
238238 return
239239 }
240 b.decoratorCh <- &decorator{decAppendZero, nil}
240 b.dCommandCh <- &dCommandData{dAppendZero, nil}
241241 }
242242
243243 // Completed signals to the bar, that process has been completed.
297297 }
298298 barState.current = n
299299 prevStartTime = blockStartTime
300 case d := <-b.decoratorCh:
301 switch d.kind {
302 case decAppend:
303 barState.appendFuncs = append(barState.appendFuncs, d.f)
304 case decAppendZero:
300 case data := <-b.dCommandCh:
301 switch data.action {
302 case dAppend:
303 barState.appendFuncs = append(barState.appendFuncs, data.f)
304 case dAppendZero:
305305 barState.appendFuncs = nil
306 case decPrepend:
307 barState.prependFuncs = append(barState.prependFuncs, d.f)
308 case decPrependZero:
306 case dPrepend:
307 barState.prependFuncs = append(barState.prependFuncs, data.f)
308 case dPrependZero:
309309 barState.prependFuncs = nil
310310 }
311311 case ch := <-b.stateReqCh:
2020 DextraSpace
2121 )
2222
23 type decoratorOperation uint
23 type decoratorAction uint
2424
2525 const (
26 decAppend decoratorOperation = iota
27 decPrepend
28 decAppendZero
29 decPrependZero
26 dAppend decoratorAction = iota
27 dPrepend
28 dAppendZero
29 dPrependZero
3030 )
3131
3232 // DecoratorFunc is a function that can be prepended and appended to the progress bar
3333 type DecoratorFunc func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string
3434
35 type decorator struct {
36 kind decoratorOperation
37 f DecoratorFunc
35 type dCommandData struct {
36 action decoratorAction
37 f DecoratorFunc
3838 }
3939
4040 // PrependName prepends name argument to the bar.
1919 type (
2020 // BeforeRender is a func, which gets called before render process
2121 BeforeRender func([]*Bar)
22 barOpType uint
23
24 operation struct {
25 kind barOpType
22 barAction uint
23
24 bCommandData struct {
25 action barAction
2626 bar *Bar
2727 result chan bool
28 }
29
30 indexedBarBuffer struct {
31 index int
32 buf []byte
33 }
34
35 indexedBar struct {
36 index int
37 termWidth int
38 bar *Bar
3928 }
4029
4130 widthSync struct {
4534 )
4635
4736 const (
48 barAdd barOpType = iota
49 barRemove
37 bAdd barAction = iota
38 bRemove
5039 )
5140
5241 const (
6958 width int
7059 format string
7160
72 operationCh chan *operation
61 bCommandCh chan *bCommandData
7362 rrChangeReqCh chan time.Duration
7463 outChangeReqCh chan io.Writer
7564 barCountReqCh chan chan int
8473 func New() *Progress {
8574 p := &Progress{
8675 width: pwidth,
87 operationCh: make(chan *operation),
76 bCommandCh: make(chan *bCommandData),
8877 rrChangeReqCh: make(chan time.Duration),
8978 outChangeReqCh: make(chan io.Writer),
9079 barCountReqCh: make(chan chan int),
164153 }
165154 result := make(chan bool)
166155 bar := newBar(id, total, p.width, p.format, p.wg, p.cancel)
167 p.operationCh <- &operation{barAdd, bar, result}
156 p.bCommandCh <- &bCommandData{bAdd, bar, result}
168157 if <-result {
169158 p.wg.Add(1)
170159 }
178167 panic(ErrCallAfterStop)
179168 }
180169 result := make(chan bool)
181 p.operationCh <- &operation{barRemove, b, result}
170 p.bCommandCh <- &bCommandData{bRemove, b, result}
182171 return <-result
183172 }
184173
212201 if isClosed(p.done) {
213202 return
214203 }
215 close(p.operationCh)
204 close(p.bCommandCh)
216205 }
217206
218207 // server monitors underlying channels and renders any progress bars
244233 case w := <-p.outChangeReqCh:
245234 cw.Flush()
246235 cw = cwriter.New(w)
247 case op, ok := <-p.operationCh:
236 case data, ok := <-p.bCommandCh:
248237 if !ok {
249238 return
250239 }
251 switch op.kind {
252 case barAdd:
253 bars = append(bars, op.bar)
254 op.result <- true
255 case barRemove:
240 switch data.action {
241 case bAdd:
242 bars = append(bars, data.bar)
243 data.result <- true
244 case bRemove:
256245 var ok bool
257246 for i, b := range bars {
258 if b == op.bar {
247 if b == data.bar {
259248 bars = append(bars[:i], bars[i+1:]...)
260249 ok = true
261250 b.remove()
262251 break
263252 }
264253 }
265 op.result <- ok
254 data.result <- ok
266255 }
267256 case respCh := <-p.barCountReqCh:
268257 respCh <- len(bars)