Codebase list golang-github-vbauerster-mpb / 395c49b
prepend with padding param Vladimir Bauer 9 years ago
3 changed file(s) with 130 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
11
22 import (
33 "fmt"
4 "strconv"
45 "sync"
56 "time"
67 )
170171 return b
171172 }
172173
173 func (b *Bar) PrependETA() *Bar {
174 func (b *Bar) PrependName(name string, padding int) *Bar {
175 layout := "%" + strconv.Itoa(padding) + "s"
176 b.PrependFunc(func(s *Statistics) string {
177 return fmt.Sprintf(layout, name)
178 })
179 return b
180 }
181
182 func (b *Bar) PrependETA(padding int) *Bar {
183 layout := "ETA%" + strconv.Itoa(padding) + "s"
174184 b.PrependFunc(func(s *Statistics) string {
175185 eta := time.Duration(s.Total-s.Completed) * s.TimePerItemEstimate
176 return fmt.Sprintf("ETA %-5v", time.Duration(eta.Seconds())*time.Second)
186 return fmt.Sprintf(layout, time.Duration(eta.Seconds())*time.Second)
177187 })
178188 return b
179189 }
194204 return b
195205 }
196206
197 func (b *Bar) PrependPercentage() *Bar {
207 func (b *Bar) PrependPercentage(padding int) *Bar {
208 layout := "%" + strconv.Itoa(padding) + "d %%"
198209 b.PrependFunc(func(s *Statistics) string {
199210 completed := int(100 * float64(s.Completed) / float64(s.Total))
200 str := fmt.Sprintf("%3d %%", completed)
201 return fmt.Sprintf("%-5s", str)
211 return fmt.Sprintf(layout, completed)
202212 })
203213 return b
204214 }
0 package main
1
2 import (
3 "fmt"
4 "math/rand"
5 "time"
6
7 "github.com/vbauerster/mpb"
8 )
9
10 const (
11 maxBlockSize = 12
12 )
13
14 func main() {
15
16 p := mpb.New().RefreshRate(80 * time.Millisecond).SetWidth(64)
17
18 name1 := "Bar#1:"
19 bar1 := p.AddBar(50).AppendPercentage().PrependETA(4).PrependName(name1, len(name1))
20 go func() {
21 blockSize := rand.Intn(maxBlockSize) + 1
22 for i := 0; i < 50; i++ {
23 time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond)))))
24 bar1.Incr(1)
25 blockSize = rand.Intn(maxBlockSize) + 1
26 }
27 }()
28
29 bar2 := p.AddBar(100).AppendPercentage().PrependETA(4).PrependName("", 0-len(name1))
30 go func() {
31 blockSize := rand.Intn(maxBlockSize) + 1
32 for i := 0; i < 100; i++ {
33 time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond)))))
34 bar2.Incr(1)
35 blockSize = rand.Intn(maxBlockSize) + 1
36 }
37 }()
38
39 bar3 := p.AddBar(80).AppendPercentage().PrependETA(4).PrependName("Bar#3:", 0)
40 go func() {
41 blockSize := rand.Intn(maxBlockSize) + 1
42 for i := 0; i < 80; i++ {
43 time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond)))))
44 bar3.Incr(1)
45 blockSize = rand.Intn(maxBlockSize) + 1
46 }
47 }()
48
49 // time.Sleep(time.Second)
50 // p.RemoveBar(bar2)
51
52 p.WaitAndStop()
53 bar2.Incr(2)
54 fmt.Println("stop")
55 // p.AddBar(1) // panic: send on closed channnel
56 }
0 package main
1
2 import (
3 "fmt"
4 "math/rand"
5 "time"
6
7 "github.com/vbauerster/mpb"
8 )
9
10 const (
11 maxBlockSize = 12
12 )
13
14 func main() {
15
16 p := mpb.New().RefreshRate(80 * time.Millisecond).SetWidth(64)
17
18 name1 := "Bar#1:"
19 bar1 := p.AddBar(50).AppendETA().PrependPercentage(3).PrependName(name1, len(name1))
20 go func() {
21 blockSize := rand.Intn(maxBlockSize) + 1
22 for i := 0; i < 50; i++ {
23 time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond)))))
24 bar1.Incr(1)
25 blockSize = rand.Intn(maxBlockSize) + 1
26 }
27 }()
28
29 bar2 := p.AddBar(100).AppendETA().PrependPercentage(3).PrependName("", 0-len(name1))
30 go func() {
31 blockSize := rand.Intn(maxBlockSize) + 1
32 for i := 0; i < 100; i++ {
33 time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond)))))
34 bar2.Incr(1)
35 blockSize = rand.Intn(maxBlockSize) + 1
36 }
37 }()
38
39 name3 := "Bar#3:"
40 bar3 := p.AddBar(80).AppendETA().PrependPercentage(3).PrependName(name3, len(name3))
41 go func() {
42 blockSize := rand.Intn(maxBlockSize) + 1
43 for i := 0; i < 80; i++ {
44 time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond)))))
45 bar3.Incr(1)
46 blockSize = rand.Intn(maxBlockSize) + 1
47 }
48 }()
49
50 // time.Sleep(time.Second)
51 // p.RemoveBar(bar2)
52
53 p.WaitAndStop()
54 bar2.Incr(2)
55 fmt.Println("stop")
56 // p.AddBar(1) // panic: send on closed channnel
57 }