Codebase list golang-github-vbauerster-mpb / aea8554
wgDoneReported Vladimir Bauer 9 years ago
2 changed file(s) with 27 addition(s) and 21 deletion(s). Raw diff Collapse all Expand all
2727 flushedCh chan struct{}
2828 stopCh chan struct{}
2929 done chan struct{}
30 }
31
32 type redrawRequest struct {
33 width int
34 respCh chan []byte
3035 }
3136
3237 // Statistics represents statistics of the progress bar
7277 return b
7378 }
7479
80 // TrimLeftSpace removes space befor LeftEnd charater
7581 func (b *Bar) TrimLeftSpace() *Bar {
7682 b.trimLeftSpace = true
7783 return b
7884 }
7985
86 // TrimRightSpace removes space after RightEnd charater
8087 func (b *Bar) TrimRightSpace() *Bar {
8188 b.trimRightSpace = true
8289 return b
137144 }
138145
139146 // Current returns the actual current.
147 // returns 0 after bar was stopped
140148 func (b *Bar) Current() int {
149 if b.isDone() {
150 return 0
151 }
141152 respCh := make(chan int)
142153 b.currentReqCh <- respCh
143154 return <-respCh
168179 return b
169180 }
170181
171 type redrawRequest struct {
172 width int
173 respCh chan []byte
174 }
175
176182 func (b *Bar) Bytes(width int) []byte {
177183 if width <= 0 {
178184 width = b.width
185191 func (b *Bar) server(wg *sync.WaitGroup, total int) {
186192 timeStarted := time.Now()
187193 blockStartTime := timeStarted
188 var tpie time.Duration
189 var timeElapsed time.Duration
190 var appendFuncs []DecoratorFunc
191 var prependFuncs []DecoratorFunc
192 var completed bool
194 var timePerItem, timeElapsed time.Duration
195 var appendFuncs, prependFuncs []DecoratorFunc
196 var completed, wgDoneReported bool
193197 var current int
194 var termWidth int
195198 for {
196199 select {
197200 case i := <-b.incrCh:
199202 if n > total {
200203 current = total
201204 completed = true
202 break
205 break // break out of select
203206 }
204207 timeElapsed = time.Since(timeStarted)
205 tpie = calcTimePerItemEstimate(tpie, blockStartTime, b.alpha, i)
208 timePerItem = calcTimePerItemEstimate(timePerItem, blockStartTime, b.alpha, i)
206209 blockStartTime = time.Now()
207210 current = n
208 if current == total && !completed {
211 if current == total {
209212 completed = true
210213 }
211214 case d := <-b.decoratorCh:
218221 case respCh := <-b.currentReqCh:
219222 respCh <- current
220223 case r := <-b.redrawReqCh:
221 termWidth = r.width
222 stat := &Statistics{total, current, termWidth, timeElapsed, tpie}
224 stat := &Statistics{total, current, r.width, timeElapsed, timePerItem}
223225 r.respCh <- b.draw(stat, appendFuncs, prependFuncs)
224226 case respCh := <-b.statusReqCh:
225227 respCh <- percentage(total, current, 100)
226228 case <-b.flushedCh:
227 if completed && !b.isDone() {
228 close(b.done)
229 if completed && !wgDoneReported {
230 wgDoneReported = true
229231 wg.Done()
230232 }
231233 case <-b.stopCh:
234 if !wgDoneReported {
235 wg.Done()
236 }
232237 close(b.done)
233 if !completed {
234 wg.Done()
235 }
238 return
236239 }
237240 }
238241 }
162162 case op, ok := <-p.op:
163163 if !ok {
164164 t.Stop()
165 for _, b := range bars {
166 b.Stop()
167 }
165168 return
166169 }
167170 switch op.kind {