straightforward SetCurrent
Vladimir Bauer
6 years ago
| 7 | 7 | "io/ioutil" |
| 8 | 8 | "log" |
| 9 | 9 | "strings" |
| 10 | "sync" | |
| 11 | 10 | "time" |
| 12 | 11 | "unicode/utf8" |
| 13 | 12 | |
| 49 | 48 | done chan struct{} |
| 50 | 49 | // cacheState is populated, right after close(shutdown) |
| 51 | 50 | cacheState *bState |
| 52 | ||
| 53 | arbitraryCurrent struct { | |
| 54 | sync.Mutex | |
| 55 | current int64 | |
| 56 | } | |
| 57 | 51 | |
| 58 | 52 | container *Progress |
| 59 | 53 | dlogger *log.Logger |
| 219 | 213 | |
| 220 | 214 | // SetCurrent sets progress' current to arbitrary amount. |
| 221 | 215 | 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 | } | |
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | // Increment is a shorthand for b.IncrInt64(1, wdd...). |
| 245 | 245 | func (b *Bar) IncrInt64(n int64, wdd ...time.Duration) { |
| 246 | 246 | select { |
| 247 | 247 | case b.operateState <- func(s *bState) { |
| 248 | for _, ar := range s.amountReceivers { | |
| 249 | ar.NextAmount(n, wdd...) | |
| 250 | } | |
| 248 | 251 | s.current += n |
| 249 | 252 | if s.total > 0 && s.current >= s.total { |
| 250 | 253 | s.current = s.total |
| 251 | 254 | s.toComplete = true |
| 252 | 255 | go b.refreshNowTillShutdown() |
| 253 | } | |
| 254 | for _, ar := range s.amountReceivers { | |
| 255 | ar.NextAmount(n, wdd...) | |
| 256 | 256 | } |
| 257 | 257 | }: |
| 258 | 258 | case <-b.done: |