Refactoring
Vladimir Bauer
9 years ago
| 28 | 28 | trimLeftCh chan bool |
| 29 | 29 | trimRightCh chan bool |
| 30 | 30 | refillCh chan *refill |
| 31 | decoratorCh chan *decorator | |
| 31 | dCommandCh chan *dCommandData | |
| 32 | 32 | flushedCh chan struct{} |
| 33 | 33 | removeReqCh chan struct{} |
| 34 | 34 | completeReqCh chan struct{} |
| 88 | 88 | trimLeftCh: make(chan bool), |
| 89 | 89 | trimRightCh: make(chan bool), |
| 90 | 90 | refillCh: make(chan *refill), |
| 91 | decoratorCh: make(chan *decorator), | |
| 91 | dCommandCh: make(chan *dCommandData), | |
| 92 | 92 | flushedCh: make(chan struct{}, 1), |
| 93 | 93 | removeReqCh: make(chan struct{}), |
| 94 | 94 | completeReqCh: make(chan struct{}), |
| 211 | 211 | if isClosed(b.done) { |
| 212 | 212 | return b |
| 213 | 213 | } |
| 214 | b.decoratorCh <- &decorator{decPrepend, f} | |
| 214 | b.dCommandCh <- &dCommandData{dPrepend, f} | |
| 215 | 215 | return b |
| 216 | 216 | } |
| 217 | 217 | |
| 220 | 220 | if isClosed(b.done) { |
| 221 | 221 | return |
| 222 | 222 | } |
| 223 | b.decoratorCh <- &decorator{decPrependZero, nil} | |
| 223 | b.dCommandCh <- &dCommandData{dPrependZero, nil} | |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | // AppendFunc appends DecoratorFunc |
| 228 | 228 | if isClosed(b.done) { |
| 229 | 229 | return b |
| 230 | 230 | } |
| 231 | b.decoratorCh <- &decorator{decAppend, f} | |
| 231 | b.dCommandCh <- &dCommandData{dAppend, f} | |
| 232 | 232 | return b |
| 233 | 233 | } |
| 234 | 234 | |
| 237 | 237 | if isClosed(b.done) { |
| 238 | 238 | return |
| 239 | 239 | } |
| 240 | b.decoratorCh <- &decorator{decAppendZero, nil} | |
| 240 | b.dCommandCh <- &dCommandData{dAppendZero, nil} | |
| 241 | 241 | } |
| 242 | 242 | |
| 243 | 243 | // Completed signals to the bar, that process has been completed. |
| 297 | 297 | } |
| 298 | 298 | barState.current = n |
| 299 | 299 | prevStartTime = blockStartTime |
| 300 | case d := <-b.decoratorCh: | |
| 301 | switch d.kind { | |
| 302 | case decAppend: | |
| 303 | barState.appendFuncs = append(barState.appendFuncs, d.f) | |
| 304 | case decAppendZero: | |
| 300 | case data := <-b.dCommandCh: | |
| 301 | switch data.action { | |
| 302 | case dAppend: | |
| 303 | barState.appendFuncs = append(barState.appendFuncs, data.f) | |
| 304 | case dAppendZero: | |
| 305 | 305 | barState.appendFuncs = nil |
| 306 | case decPrepend: | |
| 307 | barState.prependFuncs = append(barState.prependFuncs, d.f) | |
| 308 | case decPrependZero: | |
| 306 | case dPrepend: | |
| 307 | barState.prependFuncs = append(barState.prependFuncs, data.f) | |
| 308 | case dPrependZero: | |
| 309 | 309 | barState.prependFuncs = nil |
| 310 | 310 | } |
| 311 | 311 | case ch := <-b.stateReqCh: |
| 20 | 20 | DextraSpace |
| 21 | 21 | ) |
| 22 | 22 | |
| 23 | type decoratorOperation uint | |
| 23 | type decoratorAction uint | |
| 24 | 24 | |
| 25 | 25 | const ( |
| 26 | decAppend decoratorOperation = iota | |
| 27 | decPrepend | |
| 28 | decAppendZero | |
| 29 | decPrependZero | |
| 26 | dAppend decoratorAction = iota | |
| 27 | dPrepend | |
| 28 | dAppendZero | |
| 29 | dPrependZero | |
| 30 | 30 | ) |
| 31 | 31 | |
| 32 | 32 | // DecoratorFunc is a function that can be prepended and appended to the progress bar |
| 33 | 33 | type DecoratorFunc func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string |
| 34 | 34 | |
| 35 | type decorator struct { | |
| 36 | kind decoratorOperation | |
| 37 | f DecoratorFunc | |
| 35 | type dCommandData struct { | |
| 36 | action decoratorAction | |
| 37 | f DecoratorFunc | |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | // PrependName prepends name argument to the bar. |
| 19 | 19 | type ( |
| 20 | 20 | // BeforeRender is a func, which gets called before render process |
| 21 | 21 | BeforeRender func([]*Bar) |
| 22 | barOpType uint | |
| 23 | ||
| 24 | operation struct { | |
| 25 | kind barOpType | |
| 22 | barAction uint | |
| 23 | ||
| 24 | bCommandData struct { | |
| 25 | action barAction | |
| 26 | 26 | bar *Bar |
| 27 | 27 | result chan bool |
| 28 | } | |
| 29 | ||
| 30 | indexedBarBuffer struct { | |
| 31 | index int | |
| 32 | buf []byte | |
| 33 | } | |
| 34 | ||
| 35 | indexedBar struct { | |
| 36 | index int | |
| 37 | termWidth int | |
| 38 | bar *Bar | |
| 39 | 28 | } |
| 40 | 29 | |
| 41 | 30 | widthSync struct { |
| 45 | 34 | ) |
| 46 | 35 | |
| 47 | 36 | const ( |
| 48 | barAdd barOpType = iota | |
| 49 | barRemove | |
| 37 | bAdd barAction = iota | |
| 38 | bRemove | |
| 50 | 39 | ) |
| 51 | 40 | |
| 52 | 41 | const ( |
| 69 | 58 | width int |
| 70 | 59 | format string |
| 71 | 60 | |
| 72 | operationCh chan *operation | |
| 61 | bCommandCh chan *bCommandData | |
| 73 | 62 | rrChangeReqCh chan time.Duration |
| 74 | 63 | outChangeReqCh chan io.Writer |
| 75 | 64 | barCountReqCh chan chan int |
| 84 | 73 | func New() *Progress { |
| 85 | 74 | p := &Progress{ |
| 86 | 75 | width: pwidth, |
| 87 | operationCh: make(chan *operation), | |
| 76 | bCommandCh: make(chan *bCommandData), | |
| 88 | 77 | rrChangeReqCh: make(chan time.Duration), |
| 89 | 78 | outChangeReqCh: make(chan io.Writer), |
| 90 | 79 | barCountReqCh: make(chan chan int), |
| 164 | 153 | } |
| 165 | 154 | result := make(chan bool) |
| 166 | 155 | bar := newBar(id, total, p.width, p.format, p.wg, p.cancel) |
| 167 | p.operationCh <- &operation{barAdd, bar, result} | |
| 156 | p.bCommandCh <- &bCommandData{bAdd, bar, result} | |
| 168 | 157 | if <-result { |
| 169 | 158 | p.wg.Add(1) |
| 170 | 159 | } |
| 178 | 167 | panic(ErrCallAfterStop) |
| 179 | 168 | } |
| 180 | 169 | result := make(chan bool) |
| 181 | p.operationCh <- &operation{barRemove, b, result} | |
| 170 | p.bCommandCh <- &bCommandData{bRemove, b, result} | |
| 182 | 171 | return <-result |
| 183 | 172 | } |
| 184 | 173 | |
| 212 | 201 | if isClosed(p.done) { |
| 213 | 202 | return |
| 214 | 203 | } |
| 215 | close(p.operationCh) | |
| 204 | close(p.bCommandCh) | |
| 216 | 205 | } |
| 217 | 206 | |
| 218 | 207 | // server monitors underlying channels and renders any progress bars |
| 244 | 233 | case w := <-p.outChangeReqCh: |
| 245 | 234 | cw.Flush() |
| 246 | 235 | cw = cwriter.New(w) |
| 247 | case op, ok := <-p.operationCh: | |
| 236 | case data, ok := <-p.bCommandCh: | |
| 248 | 237 | if !ok { |
| 249 | 238 | return |
| 250 | 239 | } |
| 251 | switch op.kind { | |
| 252 | case barAdd: | |
| 253 | bars = append(bars, op.bar) | |
| 254 | op.result <- true | |
| 255 | case barRemove: | |
| 240 | switch data.action { | |
| 241 | case bAdd: | |
| 242 | bars = append(bars, data.bar) | |
| 243 | data.result <- true | |
| 244 | case bRemove: | |
| 256 | 245 | var ok bool |
| 257 | 246 | for i, b := range bars { |
| 258 | if b == op.bar { | |
| 247 | if b == data.bar { | |
| 259 | 248 | bars = append(bars[:i], bars[i+1:]...) |
| 260 | 249 | ok = true |
| 261 | 250 | b.remove() |
| 262 | 251 | break |
| 263 | 252 | } |
| 264 | 253 | } |
| 265 | op.result <- ok | |
| 254 | data.result <- ok | |
| 266 | 255 | } |
| 267 | 256 | case respCh := <-p.barCountReqCh: |
| 268 | 257 | respCh <- len(bars) |