diff --git a/bar.go b/bar.go index cdb0942..00c187a 100644 --- a/bar.go +++ b/bar.go @@ -2,6 +2,7 @@ import ( "fmt" + "strconv" "sync" "time" ) @@ -171,10 +172,19 @@ return b } -func (b *Bar) PrependETA() *Bar { +func (b *Bar) PrependName(name string, padding int) *Bar { + layout := "%" + strconv.Itoa(padding) + "s" + b.PrependFunc(func(s *Statistics) string { + return fmt.Sprintf(layout, name) + }) + return b +} + +func (b *Bar) PrependETA(padding int) *Bar { + layout := "ETA%" + strconv.Itoa(padding) + "s" b.PrependFunc(func(s *Statistics) string { eta := time.Duration(s.Total-s.Completed) * s.TimePerItemEstimate - return fmt.Sprintf("ETA %-5v", time.Duration(eta.Seconds())*time.Second) + return fmt.Sprintf(layout, time.Duration(eta.Seconds())*time.Second) }) return b } @@ -195,11 +205,11 @@ return b } -func (b *Bar) PrependPercentage() *Bar { +func (b *Bar) PrependPercentage(padding int) *Bar { + layout := "%" + strconv.Itoa(padding) + "d %%" b.PrependFunc(func(s *Statistics) string { completed := int(100 * float64(s.Completed) / float64(s.Total)) - str := fmt.Sprintf("%3d %%", completed) - return fmt.Sprintf("%-5s", str) + return fmt.Sprintf(layout, completed) }) return b } diff --git a/example/prependETA/main.go b/example/prependETA/main.go new file mode 100644 index 0000000..022217f --- /dev/null +++ b/example/prependETA/main.go @@ -0,0 +1,57 @@ +package main + +import ( + "fmt" + "math/rand" + "time" + + "github.com/vbauerster/mpb" +) + +const ( + maxBlockSize = 12 +) + +func main() { + + p := mpb.New().RefreshRate(80 * time.Millisecond).SetWidth(64) + + name1 := "Bar#1:" + bar1 := p.AddBar(50).AppendPercentage().PrependETA(4).PrependName(name1, len(name1)) + go func() { + blockSize := rand.Intn(maxBlockSize) + 1 + for i := 0; i < 50; i++ { + time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) + bar1.Incr(1) + blockSize = rand.Intn(maxBlockSize) + 1 + } + }() + + bar2 := p.AddBar(100).AppendPercentage().PrependETA(4).PrependName("", 0-len(name1)) + go func() { + blockSize := rand.Intn(maxBlockSize) + 1 + for i := 0; i < 100; i++ { + time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) + bar2.Incr(1) + blockSize = rand.Intn(maxBlockSize) + 1 + } + }() + + bar3 := p.AddBar(80).AppendPercentage().PrependETA(4).PrependName("Bar#3:", 0) + go func() { + blockSize := rand.Intn(maxBlockSize) + 1 + for i := 0; i < 80; i++ { + time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) + bar3.Incr(1) + blockSize = rand.Intn(maxBlockSize) + 1 + } + }() + + // time.Sleep(time.Second) + // p.RemoveBar(bar2) + + p.WaitAndStop() + bar2.Incr(2) + fmt.Println("stop") + // p.AddBar(1) // panic: send on closed channnel +} diff --git a/example/prependPercent/main.go b/example/prependPercent/main.go new file mode 100644 index 0000000..5c4356d --- /dev/null +++ b/example/prependPercent/main.go @@ -0,0 +1,58 @@ +package main + +import ( + "fmt" + "math/rand" + "time" + + "github.com/vbauerster/mpb" +) + +const ( + maxBlockSize = 12 +) + +func main() { + + p := mpb.New().RefreshRate(80 * time.Millisecond).SetWidth(64) + + name1 := "Bar#1:" + bar1 := p.AddBar(50).AppendETA().PrependPercentage(3).PrependName(name1, len(name1)) + go func() { + blockSize := rand.Intn(maxBlockSize) + 1 + for i := 0; i < 50; i++ { + time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) + bar1.Incr(1) + blockSize = rand.Intn(maxBlockSize) + 1 + } + }() + + bar2 := p.AddBar(100).AppendETA().PrependPercentage(3).PrependName("", 0-len(name1)) + go func() { + blockSize := rand.Intn(maxBlockSize) + 1 + for i := 0; i < 100; i++ { + time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) + bar2.Incr(1) + blockSize = rand.Intn(maxBlockSize) + 1 + } + }() + + name3 := "Bar#3:" + bar3 := p.AddBar(80).AppendETA().PrependPercentage(3).PrependName(name3, len(name3)) + go func() { + blockSize := rand.Intn(maxBlockSize) + 1 + for i := 0; i < 80; i++ { + time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) + bar3.Incr(1) + blockSize = rand.Intn(maxBlockSize) + 1 + } + }() + + // time.Sleep(time.Second) + // p.RemoveBar(bar2) + + p.WaitAndStop() + bar2.Incr(2) + fmt.Println("stop") + // p.AddBar(1) // panic: send on closed channnel +}