diff --git a/bar.go b/bar.go index 1fa1a26..f24c9f1 100644 --- a/bar.go +++ b/bar.go @@ -8,7 +8,6 @@ "io/ioutil" "log" "strings" - "sync" "time" "unicode/utf8" @@ -50,11 +49,6 @@ done chan struct{} // cacheState is populated, right after close(shutdown) cacheState *bState - - arbitraryCurrent struct { - sync.Mutex - current int64 - } container *Progress dlogger *log.Logger @@ -220,14 +214,20 @@ // SetCurrent sets progress' current to arbitrary amount. func (b *Bar) SetCurrent(current int64, wdd ...time.Duration) { - if current <= 0 { - return - } - b.arbitraryCurrent.Lock() - last := b.arbitraryCurrent.current - b.IncrBy(int(current-last), wdd...) - b.arbitraryCurrent.current = current - b.arbitraryCurrent.Unlock() + select { + case b.operateState <- func(s *bState) { + for _, ar := range s.amountReceivers { + ar.NextAmount(current-s.current, wdd...) + } + s.current = current + if s.total > 0 && s.current >= s.total { + s.current = s.total + s.toComplete = true + go b.refreshNowTillShutdown() + } + }: + case <-b.done: + } } // Increment is a shorthand for b.IncrInt64(1, wdd...). @@ -246,14 +246,14 @@ func (b *Bar) IncrInt64(n int64, wdd ...time.Duration) { select { case b.operateState <- func(s *bState) { + for _, ar := range s.amountReceivers { + ar.NextAmount(n, wdd...) + } s.current += n if s.total > 0 && s.current >= s.total { s.current = s.total s.toComplete = true go b.refreshNowTillShutdown() - } - for _, ar := range s.amountReceivers { - ar.NextAmount(n, wdd...) } }: case <-b.done: