Codebase list golang-github-vbauerster-mpb / 44bf997
straightforward SetCurrent Vladimir Bauer 6 years ago
1 changed file(s) with 17 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
77 "io/ioutil"
88 "log"
99 "strings"
10 "sync"
1110 "time"
1211 "unicode/utf8"
1312
4948 done chan struct{}
5049 // cacheState is populated, right after close(shutdown)
5150 cacheState *bState
52
53 arbitraryCurrent struct {
54 sync.Mutex
55 current int64
56 }
5751
5852 container *Progress
5953 dlogger *log.Logger
219213
220214 // SetCurrent sets progress' current to arbitrary amount.
221215 func (b *Bar) SetCurrent(current int64, wdd ...time.Duration) {
222 if current <= 0 {
223 return
224 }
225 b.arbitraryCurrent.Lock()
226 last := b.arbitraryCurrent.current
227 b.IncrBy(int(current-last), wdd...)
228 b.arbitraryCurrent.current = current
229 b.arbitraryCurrent.Unlock()
216 select {
217 case b.operateState <- func(s *bState) {
218 for _, ar := range s.amountReceivers {
219 ar.NextAmount(current-s.current, wdd...)
220 }
221 s.current = current
222 if s.total > 0 && s.current >= s.total {
223 s.current = s.total
224 s.toComplete = true
225 go b.refreshNowTillShutdown()
226 }
227 }:
228 case <-b.done:
229 }
230230 }
231231
232232 // Increment is a shorthand for b.IncrInt64(1, wdd...).
245245 func (b *Bar) IncrInt64(n int64, wdd ...time.Duration) {
246246 select {
247247 case b.operateState <- func(s *bState) {
248 for _, ar := range s.amountReceivers {
249 ar.NextAmount(n, wdd...)
250 }
248251 s.current += n
249252 if s.total > 0 && s.current >= s.total {
250253 s.current = s.total
251254 s.toComplete = true
252255 go b.refreshNowTillShutdown()
253 }
254 for _, ar := range s.amountReceivers {
255 ar.NextAmount(n, wdd...)
256256 }
257257 }:
258258 case <-b.done: