wgDoneReported
Vladimir Bauer
9 years ago
| 27 | 27 | flushedCh chan struct{} |
| 28 | 28 | stopCh chan struct{} |
| 29 | 29 | done chan struct{} |
| 30 | } | |
| 31 | ||
| 32 | type redrawRequest struct { | |
| 33 | width int | |
| 34 | respCh chan []byte | |
| 30 | 35 | } |
| 31 | 36 | |
| 32 | 37 | // Statistics represents statistics of the progress bar |
| 72 | 77 | return b |
| 73 | 78 | } |
| 74 | 79 | |
| 80 | // TrimLeftSpace removes space befor LeftEnd charater | |
| 75 | 81 | func (b *Bar) TrimLeftSpace() *Bar { |
| 76 | 82 | b.trimLeftSpace = true |
| 77 | 83 | return b |
| 78 | 84 | } |
| 79 | 85 | |
| 86 | // TrimRightSpace removes space after RightEnd charater | |
| 80 | 87 | func (b *Bar) TrimRightSpace() *Bar { |
| 81 | 88 | b.trimRightSpace = true |
| 82 | 89 | return b |
| 137 | 144 | } |
| 138 | 145 | |
| 139 | 146 | // Current returns the actual current. |
| 147 | // returns 0 after bar was stopped | |
| 140 | 148 | func (b *Bar) Current() int { |
| 149 | if b.isDone() { | |
| 150 | return 0 | |
| 151 | } | |
| 141 | 152 | respCh := make(chan int) |
| 142 | 153 | b.currentReqCh <- respCh |
| 143 | 154 | return <-respCh |
| 168 | 179 | return b |
| 169 | 180 | } |
| 170 | 181 | |
| 171 | type redrawRequest struct { | |
| 172 | width int | |
| 173 | respCh chan []byte | |
| 174 | } | |
| 175 | ||
| 176 | 182 | func (b *Bar) Bytes(width int) []byte { |
| 177 | 183 | if width <= 0 { |
| 178 | 184 | width = b.width |
| 185 | 191 | func (b *Bar) server(wg *sync.WaitGroup, total int) { |
| 186 | 192 | timeStarted := time.Now() |
| 187 | 193 | 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 | |
| 193 | 197 | var current int |
| 194 | var termWidth int | |
| 195 | 198 | for { |
| 196 | 199 | select { |
| 197 | 200 | case i := <-b.incrCh: |
| 199 | 202 | if n > total { |
| 200 | 203 | current = total |
| 201 | 204 | completed = true |
| 202 | break | |
| 205 | break // break out of select | |
| 203 | 206 | } |
| 204 | 207 | timeElapsed = time.Since(timeStarted) |
| 205 | tpie = calcTimePerItemEstimate(tpie, blockStartTime, b.alpha, i) | |
| 208 | timePerItem = calcTimePerItemEstimate(timePerItem, blockStartTime, b.alpha, i) | |
| 206 | 209 | blockStartTime = time.Now() |
| 207 | 210 | current = n |
| 208 | if current == total && !completed { | |
| 211 | if current == total { | |
| 209 | 212 | completed = true |
| 210 | 213 | } |
| 211 | 214 | case d := <-b.decoratorCh: |
| 218 | 221 | case respCh := <-b.currentReqCh: |
| 219 | 222 | respCh <- current |
| 220 | 223 | 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} | |
| 223 | 225 | r.respCh <- b.draw(stat, appendFuncs, prependFuncs) |
| 224 | 226 | case respCh := <-b.statusReqCh: |
| 225 | 227 | respCh <- percentage(total, current, 100) |
| 226 | 228 | case <-b.flushedCh: |
| 227 | if completed && !b.isDone() { | |
| 228 | close(b.done) | |
| 229 | if completed && !wgDoneReported { | |
| 230 | wgDoneReported = true | |
| 229 | 231 | wg.Done() |
| 230 | 232 | } |
| 231 | 233 | case <-b.stopCh: |
| 234 | if !wgDoneReported { | |
| 235 | wg.Done() | |
| 236 | } | |
| 232 | 237 | close(b.done) |
| 233 | if !completed { | |
| 234 | wg.Done() | |
| 235 | } | |
| 238 | return | |
| 236 | 239 | } |
| 237 | 240 | } |
| 238 | 241 | } |