Codebase list golang-github-vbauerster-mpb / 4d03743
Refactor bar.go Vladimir Bauer 9 years ago
1 changed file(s) with 47 addition(s) and 29 deletion(s). Raw diff Collapse all Expand all
2727
2828 // Bar represents a progress Bar
2929 type Bar struct {
30 completeReqCh chan struct{}
31 done chan struct{}
32 inProgress chan struct{}
33 ops chan func(*state)
34
35 // following are used after (*Bar.done) is closed
36 width int
37 state state
30 // quit channel to request b.server to quit
31 quit chan struct{}
32 // done channel is receiveable after b.server has been quit
33 done chan struct{}
34 ops chan func(*state)
35
36 // following are used after b.done is receiveable
37 cacheState state
3838 }
3939
4040 type (
7979 }
8080
8181 b := &Bar{
82 completeReqCh: make(chan struct{}),
83 done: make(chan struct{}),
84 inProgress: make(chan struct{}),
85 ops: make(chan func(*state)),
86 }
87 b.width = s.width
82 quit: make(chan struct{}),
83 done: make(chan struct{}),
84 ops: make(chan func(*state)),
85 }
8886
8987 go b.server(s, wg, cancel)
9088 return b
9694 case b.ops <- func(s *state) {
9795 s.prependFuncs = nil
9896 }:
99 case <-b.done:
97 case <-b.quit:
10098 return
10199 }
102100 }
107105 case b.ops <- func(s *state) {
108106 s.appendFuncs = nil
109107 }:
110 case <-b.done:
108 case <-b.quit:
111109 return
112110 }
113111 }
139137 s.current = sum
140138 s.blockStartTime = time.Now()
141139 }:
142 case <-b.done:
140 case <-b.quit:
143141 return
144142 }
145143 }
154152 case b.ops <- func(s *state) {
155153 s.refill = &refill{r, till}
156154 }:
157 case <-b.done:
155 case <-b.quit:
158156 return
159157 }
160158 }
165163 case b.ops <- func(s *state) { result <- len(s.appendFuncs) }:
166164 return <-result
167165 case <-b.done:
168 return len(b.state.appendFuncs)
166 return len(b.cacheState.appendFuncs)
169167 }
170168 }
171169
175173 case b.ops <- func(s *state) { result <- len(s.prependFuncs) }:
176174 return <-result
177175 case <-b.done:
178 return len(b.state.prependFuncs)
176 return len(b.cacheState.prependFuncs)
179177 }
180178 }
181179
186184 case b.ops <- func(s *state) { result <- s.id }:
187185 return <-result
188186 case <-b.done:
189 return b.state.id
187 return b.cacheState.id
188 }
189 }
190
191 func (b *Bar) Current() int64 {
192 result := make(chan int64, 1)
193 select {
194 case b.ops <- func(s *state) { result <- s.current }:
195 return <-result
196 case <-b.done:
197 return b.cacheState.current
198 }
199 }
200
201 func (b *Bar) Total() int64 {
202 result := make(chan int64, 1)
203 select {
204 case b.ops <- func(s *state) { result <- s.total }:
205 return <-result
206 case <-b.done:
207 return b.cacheState.total
190208 }
191209 }
192210
194212 // Can be used as condition in for loop
195213 func (b *Bar) InProgress() bool {
196214 select {
197 case <-b.completeReqCh:
215 case <-b.quit:
198216 return false
199217 default:
200218 return true
207225 // implicitly, upon p.Stop() call.
208226 func (b *Bar) Complete() {
209227 select {
210 case <-b.completeReqCh:
228 case <-b.quit:
211229 default:
212 close(b.completeReqCh)
230 close(b.quit)
213231 }
214232 }
215233
228246 func (b *Bar) server(s state, wg *sync.WaitGroup, cancel <-chan struct{}) {
229247
230248 defer func() {
231 b.state = s
249 b.cacheState = s
232250 close(b.done)
233251 wg.Done()
234252 }()
237255 select {
238256 case op := <-b.ops:
239257 op(&s)
240 case <-b.completeReqCh:
258 case <-b.quit:
241259 s.completed = true
242260 return
243261 case <-cancel:
271289 }:
272290 st = <-result
273291 case <-b.done:
274 st = b.state
292 st = b.cacheState
275293 }
276294 buf := draw(&st, tw, prependWs, appendWs)
277295 buf = append(buf, '\n')
348366 barCount := utf8.RuneCount(barBlock)
349367 totalCount := prependCount + barCount + appendCount
350368 if totalCount > termWidth {
351 newWidth := termWidth - prependCount - appendCount
352 barBlock = fillBar(s.total, s.current, newWidth, fmtBytes, s.refill)
369 shrinkWidth := termWidth - prependCount - appendCount
370 barBlock = fillBar(s.total, s.current, shrinkWidth, fmtBytes, s.refill)
353371 }
354372
355373 return concatenateBlocks(buf, prependBlock, leftSpace, barBlock, rightSpace, appendBlock)