Codebase list golang-github-vbauerster-mpb / 5d0fdbb
refactoring: IsClosed => isClosed Vladimir Bauer 9 years ago
2 changed file(s) with 25 addition(s) and 25 deletion(s). Raw diff Collapse all Expand all
100100
101101 // SetWidth overrides width of individual bar
102102 func (b *Bar) SetWidth(n int) *Bar {
103 if n < 2 || IsClosed(b.done) {
103 if n < 2 || isClosed(b.done) {
104104 return b
105105 }
106106 b.widthCh <- n
109109
110110 // TrimLeftSpace removes space befor LeftEnd charater
111111 func (b *Bar) TrimLeftSpace() *Bar {
112 if IsClosed(b.done) {
112 if isClosed(b.done) {
113113 return b
114114 }
115115 b.trimLeftCh <- true
118118
119119 // TrimRightSpace removes space after RightEnd charater
120120 func (b *Bar) TrimRightSpace() *Bar {
121 if IsClosed(b.done) {
121 if isClosed(b.done) {
122122 return b
123123 }
124124 b.trimRightCh <- true
127127
128128 // Format overrides format of individual bar
129129 func (b *Bar) Format(format string) *Bar {
130 if utf8.RuneCountInString(format) != numFmtRunes || IsClosed(b.done) {
130 if utf8.RuneCountInString(format) != numFmtRunes || isClosed(b.done) {
131131 return b
132132 }
133133 b.formatCh <- format
138138 // Defaults to 0.25
139139 // Normally you shouldn't touch this
140140 func (b *Bar) SetEtaAlpha(a float64) *Bar {
141 if IsClosed(b.done) {
141 if isClosed(b.done) {
142142 return b
143143 }
144144 b.etaAlphaCh <- a
152152
153153 // Incr increments progress bar
154154 func (b *Bar) Incr(n int) {
155 if n < 1 || IsClosed(b.done) {
155 if n < 1 || isClosed(b.done) {
156156 return
157157 }
158158 b.incrCh <- int64(n)
160160
161161 // IncrWithReFill increments pb with different fill character
162162 func (b *Bar) IncrWithReFill(n int, r rune) {
163 if IsClosed(b.done) {
163 if isClosed(b.done) {
164164 return
165165 }
166166 b.Incr(n)
195195 // InProgress returns true, while progress is running
196196 // Can be used as condition in for loop
197197 func (b *Bar) InProgress() bool {
198 return !IsClosed(b.done)
198 return !isClosed(b.done)
199199 }
200200
201201 // PrependFunc prepends DecoratorFunc
202202 func (b *Bar) PrependFunc(f DecoratorFunc) *Bar {
203 if IsClosed(b.done) {
203 if isClosed(b.done) {
204204 return b
205205 }
206206 b.decoratorCh <- &decorator{decPrepend, f}
209209
210210 // RemoveAllPrependers removes all prepend functions
211211 func (b *Bar) RemoveAllPrependers() {
212 if IsClosed(b.done) {
212 if isClosed(b.done) {
213213 return
214214 }
215215 b.decoratorCh <- &decorator{decPrependZero, nil}
217217
218218 // AppendFunc appends DecoratorFunc
219219 func (b *Bar) AppendFunc(f DecoratorFunc) *Bar {
220 if IsClosed(b.done) {
220 if isClosed(b.done) {
221221 return b
222222 }
223223 b.decoratorCh <- &decorator{decAppend, f}
226226
227227 // RemoveAllAppenders removes all append functions
228228 func (b *Bar) RemoveAllAppenders() {
229 if IsClosed(b.done) {
229 if isClosed(b.done) {
230230 return
231231 }
232232 b.decoratorCh <- &decorator{decAppendZero, nil}
236236 // You should call this method when total is unknown and you've reached the point
237237 // of process completion.
238238 func (b *Bar) Completed() {
239 if IsClosed(b.done) {
239 if isClosed(b.done) {
240240 return
241241 }
242242 b.completeReqCh <- struct{}{}
243243 }
244244
245245 func (b *Bar) getState() state {
246 if IsClosed(b.done) {
246 if isClosed(b.done) {
247247 return b.state
248248 }
249249 ch := make(chan state, 1)
333333 }
334334
335335 func (b *Bar) flushed() {
336 if IsClosed(b.done) {
336 if isClosed(b.done) {
337337 return
338338 }
339339 b.flushedCh <- struct{}{}
340340 }
341341
342342 func (b *Bar) remove() {
343 if IsClosed(b.done) {
343 if isClosed(b.done) {
344344 return
345345 }
346346 b.removeReqCh <- struct{}{}
108108 // SetOut sets underlying writer of progress. Default is os.Stdout
109109 // pancis, if called on stopped Progress instance, i.e after Stop()
110110 func (p *Progress) SetOut(w io.Writer) *Progress {
111 if IsClosed(p.done) {
111 if isClosed(p.done) {
112112 panic(ErrCallAfterStop)
113113 }
114114 if w == nil {
121121 // RefreshRate overrides default (100ms) refresh rate value
122122 // pancis, if called on stopped Progress instance, i.e after Stop()
123123 func (p *Progress) RefreshRate(d time.Duration) *Progress {
124 if IsClosed(p.done) {
124 if isClosed(p.done) {
125125 panic(ErrCallAfterStop)
126126 }
127127 p.rrChangeReqCh <- d
130130
131131 // BeforeRenderFunc accepts a func, which gets called before render process.
132132 func (p *Progress) BeforeRenderFunc(f BeforeRender) *Progress {
133 if IsClosed(p.done) {
133 if isClosed(p.done) {
134134 panic(ErrCallAfterStop)
135135 }
136136 p.brCh <- f
146146 // AddBarWithID creates a new progress bar and adds to the container
147147 // pancis, if called on stopped Progress instance, i.e after Stop()
148148 func (p *Progress) AddBarWithID(id int, total int64) *Bar {
149 if IsClosed(p.done) {
149 if isClosed(p.done) {
150150 panic(ErrCallAfterStop)
151151 }
152152 result := make(chan bool)
161161 // RemoveBar removes bar at any time
162162 // pancis, if called on stopped Progress instance, i.e after Stop()
163163 func (p *Progress) RemoveBar(b *Bar) bool {
164 if IsClosed(p.done) {
164 if isClosed(p.done) {
165165 panic(ErrCallAfterStop)
166166 }
167167 result := make(chan bool)
172172 // BarCount returns bars count in the container.
173173 // Pancis if called on stopped Progress instance, i.e after Stop()
174174 func (p *Progress) BarCount() int {
175 if IsClosed(p.done) {
175 if isClosed(p.done) {
176176 panic(ErrCallAfterStop)
177177 }
178178 respCh := make(chan int)
193193 // Stop waits for bars to finish rendering and stops the rendering goroutine
194194 func (p *Progress) Stop() {
195195 p.wg.Wait()
196 if IsClosed(p.done) {
196 if isClosed(p.done) {
197197 return
198198 }
199199 close(p.operationCh)
304304 return ibars
305305 }
306306
307 // IsClosed check if ch closed
307 // isClosed check if ch closed
308308 // caution see: http://www.tapirgames.com/blog/golang-channel-closing
309 func IsClosed(ch <-chan struct{}) bool {
309 func isClosed(ch <-chan struct{}) bool {
310310 select {
311311 case <-ch:
312312 return true