Codebase list golang-github-vbauerster-mpb / 7d4c2a6
New public api Vladimir Bauer 9 years ago
5 changed file(s) with 106 addition(s) and 163 deletion(s). Raw diff Collapse all Expand all
8484 }
8585 )
8686
87 func newBar(id int, total int64, width int, format string, wg *sync.WaitGroup, cancel <-chan struct{}) *Bar {
87 func newBar(total int64, wg *sync.WaitGroup, cancel <-chan struct{}, options ...BarOption) *Bar {
8888 b := &Bar{
89 width: width,
89 // width: width,
9090 stateCh: make(chan state),
9191 incrCh: make(chan incrReq),
9292 flushedCh: make(chan struct{}),
9494 completeReqCh: make(chan struct{}),
9595 done: make(chan struct{}),
9696 inProgress: make(chan struct{}),
97 cancel: cancel,
9897 }
9998
10099 s := state{
101 id: id,
102100 total: total,
103 width: width,
104101 etaAlpha: 0.25,
105102 }
106103
107104 if total <= 0 {
108105 s.simpleSpinner = getSpinner()
109 } else {
110 s.updateFormat(format)
111 }
112
113 go b.server(wg, s)
106 }
107
108 for _, opt := range options {
109 opt(&s)
110 }
111
112 b.width = s.width
113
114 go b.server(s, wg, cancel)
114115 return b
115116 }
116117
297298 }
298299 }
299300
300 func (b *Bar) server(wg *sync.WaitGroup, s state) {
301 func (b *Bar) server(s state, wg *sync.WaitGroup, cancel <-chan struct{}) {
301302 var incrStartTime time.Time
302303
303304 defer func() {
0 package mpb
1
2 type BarOption func(*state)
3
4 func AppendDecorators(appenders ...DecoratorFunc) BarOption {
5 return func(bs *state) {
6 bs.appendFuncs = append(bs.appendFuncs, appenders...)
7 }
8 }
9
10 func PrependDecorators(prependers ...DecoratorFunc) BarOption {
11 return func(bs *state) {
12 bs.prependFuncs = append(bs.prependFuncs, prependers...)
13 }
14 }
15
16 func BarTrimLeft(bs *state) {
17 bs.trimLeftSpace = true
18 }
19
20 func BarTrimRight(bs *state) {
21 bs.trimRightSpace = true
22 }
23
24 func BarTrim(bs *state) {
25 bs.trimLeftSpace = true
26 bs.trimRightSpace = true
27 }
28
29 func BarID(id int) BarOption {
30 return func(bs *state) {
31 bs.id = id
32 }
33 }
34
35 func barWidth(w int) BarOption {
36 return func(bs *state) {
37 bs.width = w
38 }
39 }
40
41 func barFormat(format string) BarOption {
42 return func(bs *state) {
43 bs.updateFormat(format)
44 }
45 }
2323 // DecoratorFunc is a function that can be prepended and appended to the progress bar
2424 type DecoratorFunc func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string
2525
26 // PrependName prepends name argument to the bar.
27 // The conf argument defines the formatting properties
28 func (b *Bar) PrependName(name string, minWidth int, conf byte) *Bar {
26 func Name(name string, minWidth int, conf byte) DecoratorFunc {
2927 format := "%%"
3028 if (conf & DidentRight) != 0 {
3129 format += "-"
3230 }
3331 format += "%ds"
34 b.PrependFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
32 return func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
3533 if (conf & DwidthSync) != 0 {
3634 myWidth <- utf8.RuneCountInString(name)
3735 max := <-maxWidth
4139 return fmt.Sprintf(fmt.Sprintf(format, max), name)
4240 }
4341 return fmt.Sprintf(fmt.Sprintf(format, minWidth), name)
44 })
45 return b
42 }
4643 }
4744
48 func (b *Bar) PrependCounters(pairFormat string, unit Units, minWidth int, conf byte) *Bar {
45 func Counters(pairFormat string, unit Units, minWidth int, conf byte) DecoratorFunc {
4946 format := "%%"
5047 if (conf & DidentRight) != 0 {
5148 format += "-"
5249 }
5350 format += "%ds"
54 b.PrependFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
51 return func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
5552 current := Format(s.Current).To(unit)
5653 total := Format(s.Total).To(unit)
5754 str := fmt.Sprintf(pairFormat, current, total)
6461 return fmt.Sprintf(fmt.Sprintf(format, max), str)
6562 }
6663 return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
67 })
68 return b
64 }
6965 }
7066
71 func (b *Bar) PrependETA(minWidth int, conf byte) *Bar {
67 func ETA(minWidth int, conf byte) DecoratorFunc {
7268 format := "%%"
7369 if (conf & DidentRight) != 0 {
7470 format += "-"
7571 }
7672 format += "%ds"
77 b.PrependFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
73 return func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
7874 str := fmt.Sprint(time.Duration(s.Eta().Seconds()) * time.Second)
7975 if (conf & DwidthSync) != 0 {
8076 myWidth <- utf8.RuneCountInString(str)
8581 return fmt.Sprintf(fmt.Sprintf(format, max), str)
8682 }
8783 return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
88 })
89 return b
84 }
9085 }
9186
92 func (b *Bar) AppendETA(minWidth int, conf byte) *Bar {
87 func (b *Bar) Elapsed(minWidth int, conf byte) DecoratorFunc {
9388 format := "%%"
9489 if (conf & DidentRight) != 0 {
9590 format += "-"
9691 }
9792 format += "%ds"
98 b.AppendFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
99 str := fmt.Sprint(time.Duration(s.Eta().Seconds()) * time.Second)
100 if (conf & DwidthSync) != 0 {
101 myWidth <- utf8.RuneCountInString(str)
102 max := <-maxWidth
103 if (conf & DextraSpace) != 0 {
104 max++
105 }
106 return fmt.Sprintf(fmt.Sprintf(format, max), str)
107 }
108 return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
109 })
110 return b
111 }
112
113 func (b *Bar) PrependElapsed(minWidth int, conf byte) *Bar {
114 format := "%%"
115 if (conf & DidentRight) != 0 {
116 format += "-"
117 }
118 format += "%ds"
119 b.PrependFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
93 return func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
12094 str := fmt.Sprint(time.Duration(s.TimeElapsed.Seconds()) * time.Second)
12195 if (conf & DwidthSync) != 0 {
12296 myWidth <- utf8.RuneCountInString(str)
127101 return fmt.Sprintf(fmt.Sprintf(format, max), str)
128102 }
129103 return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
130 })
131 return b
104 }
132105 }
133106
134 func (b *Bar) AppendElapsed(minWidth int, conf byte) *Bar {
107 func Percentage(minWidth int, conf byte) DecoratorFunc {
135108 format := "%%"
136109 if (conf & DidentRight) != 0 {
137110 format += "-"
138111 }
139112 format += "%ds"
140 b.AppendFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
141 str := fmt.Sprint(time.Duration(s.TimeElapsed.Seconds()) * time.Second)
142 if (conf & DwidthSync) != 0 {
143 myWidth <- utf8.RuneCountInString(str)
144 max := <-maxWidth
145 if (conf & DextraSpace) != 0 {
146 max++
147 }
148 return fmt.Sprintf(fmt.Sprintf(format, max), str)
149 }
150 return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
151 })
152 return b
153 }
154
155 func (b *Bar) AppendPercentage(minWidth int, conf byte) *Bar {
156 format := "%%"
157 if (conf & DidentRight) != 0 {
158 format += "-"
159 }
160 format += "%ds"
161 b.AppendFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
113 return func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
162114 str := fmt.Sprintf("%d %%", percentage(s.Total, s.Current, 100))
163115 if (conf & DwidthSync) != 0 {
164116 myWidth <- utf8.RuneCountInString(str)
169121 return fmt.Sprintf(fmt.Sprintf(format, max), str)
170122 }
171123 return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
172 })
173 return b
124 }
174125 }
175
176 func (b *Bar) PrependPercentage(minWidth int, conf byte) *Bar {
177 format := "%%"
178 if (conf & DidentRight) != 0 {
179 format += "-"
180 }
181 format += "%ds"
182 b.PrependFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
183 str := fmt.Sprintf("%d %%", percentage(s.Total, s.Current, 100))
184 if (conf & DwidthSync) != 0 {
185 myWidth <- utf8.RuneCountInString(str)
186 max := <-maxWidth
187 if (conf & DextraSpace) != 0 {
188 max++
189 }
190 return fmt.Sprintf(fmt.Sprintf(format, max), str)
191 }
192 return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
193 })
194 return b
195 }
1414
1515 func main() {
1616
17 p := mpb.New(mpb.WithWidth(64))
18
19 total := 100
20 numBars := 3
1721 var wg sync.WaitGroup
18 p := mpb.New().SetWidth(64)
22 wg.Add(numBars)
1923
20 name1 := "Bar#1:"
21 bar1 := p.AddBar(50).
22 PrependName(name1, 0, mpb.DwidthSync|mpb.DidentRight).
23 PrependETA(4, mpb.DwidthSync|mpb.DextraSpace).
24 AppendPercentage(5, 0)
25
26 wg.Add(1)
27 go func() {
28 defer wg.Done()
29 blockSize := rand.Intn(maxBlockSize) + 1
30 for i := 0; i < 50; i++ {
31 sleep(blockSize)
32 bar1.Incr(1)
33 blockSize = rand.Intn(maxBlockSize) + 1
24 for i := 0; i < numBars; i++ {
25 var name string
26 if i != 1 {
27 name = fmt.Sprintf("Bar#%d:", i)
3428 }
35 }()
36
37 bar2 := p.AddBar(100).
38 PrependName("", 0, mpb.DwidthSync).
39 PrependETA(4, mpb.DwidthSync|mpb.DextraSpace).
40 AppendPercentage(5, 0)
41
42 wg.Add(1)
43 go func() {
44 defer wg.Done()
45 blockSize := rand.Intn(maxBlockSize) + 1
46 for i := 0; i < 100; i++ {
47 sleep(blockSize)
48 bar2.Incr(1)
49 blockSize = rand.Intn(maxBlockSize) + 1
50 }
51 }()
52
53 bar3 := p.AddBar(80).
54 PrependName("Bar#3:", 0, mpb.DwidthSync|mpb.DidentRight).
55 PrependETA(4, mpb.DwidthSync|mpb.DextraSpace).
56 AppendPercentage(5, 0)
57
58 wg.Add(1)
59 go func() {
60 defer wg.Done()
61 blockSize := rand.Intn(maxBlockSize) + 1
62 for i := 0; i < 80; i++ {
63 sleep(blockSize)
64 bar3.Incr(1)
65 blockSize = rand.Intn(maxBlockSize) + 1
66 }
67 }()
29 b := p.AddBar(int64(total),
30 mpb.PrependDecorators(
31 mpb.Name(name, 0, mpb.DwidthSync|mpb.DidentRight),
32 mpb.ETA(4, mpb.DwidthSync|mpb.DextraSpace),
33 ),
34 mpb.AppendDecorators(
35 mpb.Percentage(5, 0),
36 ),
37 )
38 go func() {
39 defer wg.Done()
40 blockSize := rand.Intn(maxBlockSize) + 1
41 for i := 0; i < total; i++ {
42 sleep(blockSize)
43 b.Incr(1)
44 blockSize = rand.Intn(maxBlockSize) + 1
45 }
46 }()
47 }
6848
6949 wg.Wait()
7050 p.Stop()
71 // p.AddBar(1) // panic: you cannot reuse p, create new one!
7251 fmt.Println("stop")
7352 }
7453
117117 })
118118 }
119119
120 // RefreshRate Deprecated, use mpb.WithRefreshRate
121 func (p *Progress) RefreshRate(d time.Duration) *Progress {
122 return updateConf(p, func(c *pConf) {
123 c.ticker.Stop()
124 c.ticker = time.NewTicker(d)
125 c.rr = d
126 })
127 }
128
129120 // AddBar creates a new progress bar and adds to the container.
130 func (p *Progress) AddBar(total int64) *Bar {
131 return p.AddBarWithID(0, total)
132 }
133
134 // AddBarWithID creates a new progress bar and adds to the container.
135 func (p *Progress) AddBarWithID(id int, total int64) *Bar {
121 func (p *Progress) AddBar(total int64, options ...BarOption) *Bar {
136122 result := make(chan *Bar, 1)
137123 op := func(c *pConf) {
138 bar := newBar(id, total, c.width, c.format, p.wg, c.cancel)
139 c.bars = append(c.bars, bar)
124 options = append(options, barWidth(c.width), barFormat(c.format))
125 b := newBar(total, p.wg, c.cancel, options...)
126 c.bars = append(c.bars, b)
140127 p.wg.Add(1)
141 result <- bar
128 result <- b
142129 }
143130 select {
144131 case p.ops <- op: