Codebase list golang-github-vbauerster-mpb / 73c36d3
use IsClosed to check if ch is closed Vladimir Bauer 9 years ago
2 changed file(s) with 71 addition(s) and 66 deletion(s). Raw diff Collapse all Expand all
7272 trimRightCh: make(chan bool),
7373 stateReqCh: make(chan chan state, 1),
7474 decoratorCh: make(chan *decorator),
75 flushedCh: make(chan struct{}),
75 flushedCh: make(chan struct{}, 1),
7676 removeReqCh: make(chan struct{}),
7777 done: make(chan struct{}),
7878 }
9191
9292 // TrimLeftSpace removes space befor LeftEnd charater
9393 func (b *Bar) TrimLeftSpace() *Bar {
94 if !b.isDone() {
95 b.trimLeftCh <- true
96 }
94 if IsClosed(b.done) {
95 return b
96 }
97 b.trimLeftCh <- true
9798 return b
9899 }
99100
100101 // TrimRightSpace removes space after RightEnd charater
101102 func (b *Bar) TrimRightSpace() *Bar {
102 if !b.isDone() {
103 b.trimRightCh <- true
104 }
103 if IsClosed(b.done) {
104 return b
105 }
106 b.trimRightCh <- true
105107 return b
106108 }
107109
155157
156158 // Incr increments progress bar
157159 func (b *Bar) Incr(n int) {
158 if n > 0 && !b.isDone() {
160 if IsClosed(b.done) {
161 return
162 }
163 if n > 0 {
159164 b.incrCh <- int64(n)
160165 }
161166 }
170175
171176 // Current returns the actual current.
172177 func (b *Bar) Current() int64 {
173 if b.isDone() {
178 if IsClosed(b.done) {
174179 return b.lastState.current
175180 }
176181 ch := make(chan state, 1)
182187 // InProgress returns true, while progress is running
183188 // Can be used as condition in for loop
184189 func (b *Bar) InProgress() bool {
185 return !b.isDone()
190 return !IsClosed(b.done)
186191 }
187192
188193 // PrependFunc prepends DecoratorFunc
189194 func (b *Bar) PrependFunc(f DecoratorFunc) *Bar {
190 if !b.isDone() {
191 b.decoratorCh <- &decorator{decPrepend, f}
192 }
193 return b
194 }
195
195 if IsClosed(b.done) {
196 return b
197 }
198 b.decoratorCh <- &decorator{decPrepend, f}
199 return b
200 }
201
202 // RemoveAllPrependers removes all prepend functions
196203 func (b *Bar) RemoveAllPrependers() {
197 if !b.isDone() {
198 b.decoratorCh <- &decorator{decPrependZero, nil}
199 }
204 if IsClosed(b.done) {
205 return
206 }
207 b.decoratorCh <- &decorator{decPrependZero, nil}
200208 }
201209
202210 // AppendFunc appends DecoratorFunc
203211 func (b *Bar) AppendFunc(f DecoratorFunc) *Bar {
204 if !b.isDone() {
205 b.decoratorCh <- &decorator{decAppend, f}
206 }
207 return b
208 }
209
212 if IsClosed(b.done) {
213 return b
214 }
215 b.decoratorCh <- &decorator{decAppend, f}
216 return b
217 }
218
219 // RemoveAllAppenders removes all append functions
210220 func (b *Bar) RemoveAllAppenders() {
211 if !b.isDone() {
212 b.decoratorCh <- &decorator{decAppendZero, nil}
213 }
221 if IsClosed(b.done) {
222 return
223 }
224 b.decoratorCh <- &decorator{decAppendZero, nil}
214225 }
215226
216227 func (b *Bar) bytes(width int) []byte {
217228 if width <= 0 {
218229 width = b.width
219230 }
220 if b.isDone() {
231 if IsClosed(b.done) {
221232 return b.draw(b.lastState, width)
222233 }
223234 ch := make(chan state, 1)
283294 close(b.done)
284295 }
285296
286 func (b *Bar) flushDone() {
287 if !b.isDone() {
288 b.flushedCh <- struct{}{}
289 }
297 func (b *Bar) flushed() {
298 if IsClosed(b.done) {
299 return
300 }
301 b.flushedCh <- struct{}{}
290302 }
291303
292304 func (b *Bar) remove() {
293 if !b.isDone() {
294 b.removeReqCh <- struct{}{}
295 }
305 if IsClosed(b.done) {
306 return
307 }
308 b.removeReqCh <- struct{}{}
296309 }
297310
298311 func (b *Bar) draw(s state, termWidth int) []byte {
384397 return buf
385398 }
386399
387 func (b *Bar) isDone() bool {
388 select {
389 case <-b.done:
390 return true
391 default:
392 return false
393 }
394 }
395
396400 func (b *Bar) status() int {
397401 var total, current int64
398 if b.isDone() {
402 if IsClosed(b.done) {
399403 total = b.lastState.total
400404 current = b.lastState.current
401405 } else {
103103 // SetOut sets underlying writer of progress. Default is os.Stdout
104104 // pancis, if called on stopped Progress instance, i.e after Stop()
105105 func (p *Progress) SetOut(w io.Writer) *Progress {
106 if p.isDone() {
106 if IsClosed(p.done) {
107107 panic(ErrCallAfterStop)
108108 }
109109 if w == nil {
116116 // RefreshRate overrides default (30ms) refreshRate value
117117 // pancis, if called on stopped Progress instance, i.e after Stop()
118118 func (p *Progress) RefreshRate(d time.Duration) *Progress {
119 if p.isDone() {
119 if IsClosed(p.done) {
120120 panic(ErrCallAfterStop)
121121 }
122122 p.rrChangeReqCh <- d
132132 // AddBar creates a new progress bar and adds to the container
133133 // pancis, if called on stopped Progress instance, i.e after Stop()
134134 func (p *Progress) AddBar(total int64) *Bar {
135 if p.isDone() {
135 if IsClosed(p.done) {
136136 panic(ErrCallAfterStop)
137137 }
138138 result := make(chan bool)
147147 // RemoveBar removes bar at any time
148148 // pancis, if called on stopped Progress instance, i.e after Stop()
149149 func (p *Progress) RemoveBar(b *Bar) bool {
150 if p.isDone() {
150 if IsClosed(p.done) {
151151 panic(ErrCallAfterStop)
152152 }
153153 result := make(chan bool)
158158 // BarCount returns bars count in the container.
159159 // Pancis if called on stopped Progress instance, i.e after Stop()
160160 func (p *Progress) BarCount() int {
161 if p.isDone() {
161 if IsClosed(p.done) {
162162 panic(ErrCallAfterStop)
163163 }
164164 respCh := make(chan int)
169169 // Stop waits for bars to finish rendering and stops the rendering goroutine
170170 func (p *Progress) Stop() {
171171 p.wg.Wait()
172 if !p.isDone() {
173 close(p.operationCh)
174 }
175 }
176
177 func (p *Progress) isDone() bool {
178 select {
179 case <-p.done:
180 return true
181 default:
182 return false
183 }
172 if IsClosed(p.done) {
173 return
174 }
175 close(p.operationCh)
184176 }
185177
186178 // server monitors underlying channels and renders any progress bars
253245 cw.Flush()
254246
255247 for _, b := range bars {
256 go func(b *Bar) {
257 b.flushDone()
258 }(b)
248 b.flushed()
259249 }
260250 case d := <-p.rrChangeReqCh:
261251 t.Stop()
284274 }()
285275 return ibars
286276 }
277
278 // IsClosed check if ch closed
279 // caution see: http://www.tapirgames.com/blog/golang-channel-closing
280 func IsClosed(ch <-chan struct{}) bool {
281 select {
282 case <-ch:
283 return true
284 default:
285 return false
286 }
287 }