Codebase list golang-github-vbauerster-mpb / 9171c5a
drop bar.Total method Vladimir Bauer 7 years ago
2 changed file(s) with 15 addition(s) and 27 deletion(s). Raw diff Collapse all Expand all
2222
2323 const (
2424 cmdId = 1 << iota
25 cmdTotal
2625 cmdCurrent
2726 cmdALen
2827 cmdPLen
4140 runningBar *Bar
4241 cacheState *bState
4342 operateState chan func(*bState)
44 cmdValue chan int64
43 cmdValue chan int
4544 frameReaderCh chan io.Reader
4645
4746 // done is closed by Bar's goroutine, after cacheState is written
116115 priority: s.priority,
117116 runningBar: s.runningBar,
118117 operateState: make(chan func(*bState)),
119 cmdValue: make(chan int64),
118 cmdValue: make(chan int),
120119 frameReaderCh: make(chan io.Reader, 1),
121120 done: make(chan struct{}),
122121 shutdown: make(chan struct{}),
159158 func (b *Bar) NumOfAppenders() int {
160159 select {
161160 case b.cmdValue <- cmdALen:
162 return int(<-b.cmdValue)
161 return <-b.cmdValue
163162 case <-b.done:
164163 return len(b.cacheState.aDecorators)
165164 }
169168 func (b *Bar) NumOfPrependers() int {
170169 select {
171170 case b.cmdValue <- cmdPLen:
172 return int(<-b.cmdValue)
171 return <-b.cmdValue
173172 case <-b.done:
174173 return len(b.cacheState.pDecorators)
175174 }
179178 func (b *Bar) ID() int {
180179 select {
181180 case b.cmdValue <- cmdId:
182 return int(<-b.cmdValue)
181 return <-b.cmdValue
183182 case <-b.done:
184183 return b.cacheState.id
185184 }
189188 func (b *Bar) Current() int64 {
190189 select {
191190 case b.cmdValue <- cmdCurrent:
192 return <-b.cmdValue
191 return int64(<-b.cmdValue)
193192 case <-b.done:
194193 return b.cacheState.current
195 }
196 }
197
198 // Total returns bar's total number.
199 func (b *Bar) Total() int64 {
200 select {
201 case b.cmdValue <- cmdTotal:
202 return <-b.cmdValue
203 case <-b.done:
204 return b.cacheState.total
205194 }
206195 }
207196
272261 case cmd := <-b.cmdValue:
273262 switch {
274263 case cmd&cmdId != 0:
275 b.cmdValue <- int64(s.id)
276 case cmd&cmdTotal != 0:
277 b.cmdValue <- s.total
264 b.cmdValue <- s.id
278265 case cmd&cmdCurrent != 0:
279 b.cmdValue <- s.current
266 b.cmdValue <- int(s.current)
280267 case cmd&cmdPLen != 0:
281 b.cmdValue <- int64(len(s.pDecorators))
268 b.cmdValue <- len(s.pDecorators)
282269 case cmd&cmdALen != 0:
283 b.cmdValue <- int64(len(s.aDecorators))
270 b.cmdValue <- len(s.aDecorators)
284271 case cmd&cmdCompleted != 0:
285 var v int64
272 var v int
286273 if s.toComplete {
287274 v = 1
288275 }
1919 mpb.WithShutdownNotifier(shutdown),
2020 )
2121
22 total := 1000
2223 numBars := 3
2324 bars := make([]*mpb.Bar, 0, numBars)
2425 for i := 0; i < numBars; i++ {
25 bar := p.AddBar(int64(1000), mpb.BarID(i))
26 bar := p.AddBar(int64(total))
2627 bars = append(bars, bar)
2728 go func() {
2829 for !bar.Completed() {
3637
3738 p.Wait()
3839 for _, bar := range bars {
39 if bar.Current() >= bar.Total() {
40 t.Errorf("bar %d: total = %d, current = %d\n", bar.ID(), bar.Total(), bar.Current())
40 if bar.Current() >= int64(total) {
41 t.Errorf("bar %d: total = %d, current = %d\n", bar.ID(), total, bar.Current())
4142 }
4243 }
4344 select {