Codebase list golang-github-vbauerster-mpb / 1646691
drop BarAutoIncrTotal BarOption Vladimir Bauer 7 years ago
2 changed file(s) with 27 addition(s) and 53 deletion(s). Raw diff Collapse all Expand all
5151
5252 type (
5353 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
7572
7673 // following options are assigned to the *Bar
7774 priority int
8986 )
9087
9188 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 {
9490 total = time.Now().Unix()
9591 }
9692
9894 id: id,
9995 priority: id,
10096 total: total,
101 dynamic: dynamic,
10297 }
10398
10499 for _, opt := range options {
194189 }
195190 }
196191
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.
200194 func (b *Bar) SetTotal(total int64, final bool) {
201195 b.operateState <- func(s *bState) {
202 if total != 0 {
196 if total > 0 {
203197 s.total = total
204198 }
205 s.dynamic = !final
199 if final {
200 s.current = s.total
201 s.toComplete = true
202 }
206203 }
207204 }
208205
226223 select {
227224 case b.operateState <- func(s *bState) {
228225 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 {
235227 s.current = s.total
236228 s.toComplete = true
237229 }
6666 }
6767 }
6868
69 // BarDynamicTotal is a flag, if set enables dynamic total behaviour.
70 // If provided total <= 0, it is set implicitly.
71 func BarDynamicTotal() BarOption {
72 return func(s *bState) {
73 s.dynamic = true
74 }
75 }
76
77 // BarAutoIncrTotal auto increment total by n, when trigger percentage remained till bar completion.
78 // In other words: say you've set trigger = 10, then auto increment will start after bar reaches 90 %.
79 // Effective only if BarDynamicTotal option is set.
80 func BarAutoIncrTotal(trigger, n int64) BarOption {
81 return func(s *bState) {
82 s.totalAutoIncrTrigger = trigger
83 s.totalAutoIncrBy = n
84 }
85 }
86
8769 // BarRemoveOnComplete is a flag, if set whole bar line will be removed on complete event.
8870 // If both BarRemoveOnComplete and BarClearOnComplete are set, first bar section gets cleared
8971 // and then whole bar line gets removed completely.