refactor SetTotal
Vladimir Bauer
4 years ago
| 147 | 147 | } |
| 148 | 148 | } |
| 149 | 149 | |
| 150 | // SetTotal sets total dynamically. | |
| 151 | // If total is negative it takes progress' current value. | |
| 152 | func (b *Bar) SetTotal(total int64, triggerComplete bool) { | |
| 153 | select { | |
| 154 | case b.operateState <- func(s *bState) { | |
| 155 | s.triggerComplete = triggerComplete | |
| 150 | // SetTotal sets total to an arbitrary value. Setting it to negative | |
| 151 | // value is equivalent to (*Bar).SetTotal((*Bar).Current(), bool). | |
| 152 | func (b *Bar) SetTotal(total int64, triggerCompleteNow bool) { | |
| 153 | select { | |
| 154 | case b.operateState <- func(s *bState) { | |
| 156 | 155 | if total < 0 { |
| 157 | 156 | s.total = s.current |
| 158 | 157 | } else { |
| 159 | 158 | s.total = total |
| 160 | 159 | } |
| 161 | if s.triggerComplete && !s.completed && !s.aborted { | |
| 160 | if triggerCompleteNow { | |
| 162 | 161 | s.current = s.total |
| 163 | 162 | s.completed = true |
| 164 | 163 | go b.forceRefresh() |
| 165 | 164 | } |
| 166 | 165 | }: |
| 167 | if triggerComplete { | |
| 166 | if triggerCompleteNow { | |
| 168 | 167 | <-b.done |
| 169 | 168 | } |
| 170 | 169 | case <-b.done: |