| 51 | 51 |
|
| 52 | 52 |
type (
|
| 53 | 53 |
bState struct {
|
| 54 | |
id int
|
| 55 | |
width int
|
| 56 | |
total int64
|
| 57 | |
current int64
|
| 58 | |
totalAutoIncrTrigger int64
|
| 59 | |
totalAutoIncrBy int64
|
| 60 | |
runes barRunes
|
| 61 | |
trimLeftSpace bool
|
| 62 | |
trimRightSpace bool
|
| 63 | |
toComplete bool
|
| 64 | |
dynamic bool
|
| 65 | |
removeOnComplete bool
|
| 66 | |
barClearOnComplete bool
|
| 67 | |
completeFlushed bool
|
| 68 | |
aDecorators []decor.Decorator
|
| 69 | |
pDecorators []decor.Decorator
|
| 70 | |
amountReceivers []decor.AmountReceiver
|
| 71 | |
shutdownListeners []decor.ShutdownListener
|
| 72 | |
refill *refill
|
| 73 | |
bufP, bufB, bufA *bytes.Buffer
|
| 74 | |
panicMsg string
|
|
54 |
id int
|
|
55 |
width int
|
|
56 |
total int64
|
|
57 |
current int64
|
|
58 |
runes barRunes
|
|
59 |
trimLeftSpace bool
|
|
60 |
trimRightSpace bool
|
|
61 |
toComplete bool
|
|
62 |
removeOnComplete bool
|
|
63 |
barClearOnComplete bool
|
|
64 |
completeFlushed bool
|
|
65 |
aDecorators []decor.Decorator
|
|
66 |
pDecorators []decor.Decorator
|
|
67 |
amountReceivers []decor.AmountReceiver
|
|
68 |
shutdownListeners []decor.ShutdownListener
|
|
69 |
refill *refill
|
|
70 |
bufP, bufB, bufA *bytes.Buffer
|
|
71 |
panicMsg string
|
| 75 | 72 |
|
| 76 | 73 |
// following options are assigned to the *Bar
|
| 77 | 74 |
priority int
|
|
| 89 | 86 |
)
|
| 90 | 87 |
|
| 91 | 88 |
func newBar(wg *sync.WaitGroup, id int, total int64, cancel <-chan struct{}, options ...BarOption) *Bar {
|
| 92 | |
dynamic := total <= 0
|
| 93 | |
if dynamic {
|
|
89 |
if total <= 0 {
|
| 94 | 90 |
total = time.Now().Unix()
|
| 95 | 91 |
}
|
| 96 | 92 |
|
|
| 98 | 94 |
id: id,
|
| 99 | 95 |
priority: id,
|
| 100 | 96 |
total: total,
|
| 101 | |
dynamic: dynamic,
|
| 102 | 97 |
}
|
| 103 | 98 |
|
| 104 | 99 |
for _, opt := range options {
|
|
| 194 | 189 |
}
|
| 195 | 190 |
}
|
| 196 | 191 |
|
| 197 | |
// SetTotal sets total dynamically. The final param indicates the very last set,
|
| 198 | |
// in other words you should set it to true when total is determined.
|
| 199 | |
// After final has been set, IncrBy should be called at least once.
|
|
192 |
// SetTotal sets total dynamically.
|
|
193 |
// Set final to true, when total is known, it will trigger bar complete event.
|
| 200 | 194 |
func (b *Bar) SetTotal(total int64, final bool) {
|
| 201 | 195 |
b.operateState <- func(s *bState) {
|
| 202 | |
if total != 0 {
|
|
196 |
if total > 0 {
|
| 203 | 197 |
s.total = total
|
| 204 | 198 |
}
|
| 205 | |
s.dynamic = !final
|
|
199 |
if final {
|
|
200 |
s.current = s.total
|
|
201 |
s.toComplete = true
|
|
202 |
}
|
| 206 | 203 |
}
|
| 207 | 204 |
}
|
| 208 | 205 |
|
|
| 226 | 223 |
select {
|
| 227 | 224 |
case b.operateState <- func(s *bState) {
|
| 228 | 225 |
s.current += int64(n)
|
| 229 | |
if s.dynamic {
|
| 230 | |
curp := internal.Percentage(s.total, s.current, 100)
|
| 231 | |
if 100-curp <= s.totalAutoIncrTrigger {
|
| 232 | |
s.total += s.totalAutoIncrBy
|
| 233 | |
}
|
| 234 | |
} else if s.current >= s.total {
|
|
226 |
if s.current >= s.total {
|
| 235 | 227 |
s.current = s.total
|
| 236 | 228 |
s.toComplete = true
|
| 237 | 229 |
}
|