refactoring: triggerComplete
Vladimir Bauer
5 years ago
| 53 | 53 | lastN int64 |
| 54 | 54 | iterated bool |
| 55 | 55 | trimSpace bool |
| 56 | toComplete bool | |
| 56 | completed bool | |
| 57 | 57 | completeFlushed bool |
| 58 | ignoreComplete bool | |
| 58 | triggerComplete bool | |
| 59 | 59 | dropOnComplete bool |
| 60 | 60 | noPop bool |
| 61 | 61 | aDecorators []decor.Decorator |
| 158 | 158 | |
| 159 | 159 | // SetTotal sets total dynamically. |
| 160 | 160 | // If total is less than or equal to zero it takes progress' current value. |
| 161 | // A complete flag enables or disables complete event on `current >= total`. | |
| 162 | func (b *Bar) SetTotal(total int64, complete bool) { | |
| 163 | select { | |
| 164 | case b.operateState <- func(s *bState) { | |
| 165 | s.ignoreComplete = !complete | |
| 161 | func (b *Bar) SetTotal(total int64, triggerComplete bool) { | |
| 162 | select { | |
| 163 | case b.operateState <- func(s *bState) { | |
| 164 | s.triggerComplete = triggerComplete | |
| 166 | 165 | if total <= 0 { |
| 167 | 166 | s.total = s.current |
| 168 | 167 | } else { |
| 169 | 168 | s.total = total |
| 170 | 169 | } |
| 171 | if !s.ignoreComplete && !s.toComplete { | |
| 170 | if s.triggerComplete && !s.completed { | |
| 172 | 171 | s.current = s.total |
| 173 | s.toComplete = true | |
| 172 | s.completed = true | |
| 174 | 173 | go b.refreshTillShutdown() |
| 175 | 174 | } |
| 176 | 175 | }: |
| 186 | 185 | s.iterated = true |
| 187 | 186 | s.lastN = current - s.current |
| 188 | 187 | s.current = current |
| 189 | if !s.ignoreComplete && s.current >= s.total { | |
| 188 | if s.triggerComplete && s.current >= s.total { | |
| 190 | 189 | s.current = s.total |
| 191 | s.toComplete = true | |
| 190 | s.completed = true | |
| 192 | 191 | go b.refreshTillShutdown() |
| 193 | 192 | } |
| 194 | 193 | }: |
| 213 | 212 | s.iterated = true |
| 214 | 213 | s.lastN = n |
| 215 | 214 | s.current += n |
| 216 | if !s.ignoreComplete && s.current >= s.total { | |
| 215 | if s.triggerComplete && s.current >= s.total { | |
| 217 | 216 | s.current = s.total |
| 218 | s.toComplete = true | |
| 217 | s.completed = true | |
| 219 | 218 | go b.refreshTillShutdown() |
| 220 | 219 | } |
| 221 | 220 | }: |
| 279 | 278 | // Completed reports whether the bar is in completed state. |
| 280 | 279 | func (b *Bar) Completed() bool { |
| 281 | 280 | select { |
| 282 | case b.operateState <- func(s *bState) { b.completed <- s.toComplete }: | |
| 281 | case b.operateState <- func(s *bState) { b.completed <- s.completed }: | |
| 283 | 282 | return <-b.completed |
| 284 | 283 | case <-b.done: |
| 285 | 284 | return true |
| 321 | 320 | b.frameCh <- frame |
| 322 | 321 | b.dlogger.Println(p) |
| 323 | 322 | } |
| 324 | s.completeFlushed = s.toComplete | |
| 323 | s.completeFlushed = s.completed | |
| 325 | 324 | }() |
| 326 | 325 | frame, lines := s.extender(s.draw(stat), s.reqWidth, stat) |
| 327 | 326 | b.extendedLines = lines |
| 328 | b.toShutdown = s.toComplete && !s.completeFlushed | |
| 327 | b.toShutdown = s.completed && !s.completeFlushed | |
| 329 | 328 | b.frameCh <- frame |
| 330 | 329 | }: |
| 331 | 330 | case <-b.done: |