Skip to content

Commit

Permalink
update PrependFunc and AppendFunc examples
Browse files Browse the repository at this point in the history
  • Loading branch information
vbauerster committed Mar 10, 2017
1 parent d95fe5a commit 94d5824
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mpb_test
import (
"fmt"
"math/rand"
"sync"
"time"
"unicode/utf8"

Expand Down Expand Up @@ -52,35 +53,61 @@ func ExampleBar_InProgress() {
func ExampleBar_PrependFunc() {
decor := func(s *mpb.Statistics, myWidth chan<- int, maxWidth <-chan int) string {
str := fmt.Sprintf("%3d/%3d", s.Current, s.Total)
// send width to Progress' goroutine
myWidth <- utf8.RuneCountInString(str)
// receive max width
max := <-maxWidth
return fmt.Sprintf(fmt.Sprintf("%%%ds", max+1), str)
}

totalItem := 100
var wg sync.WaitGroup
p := mpb.New()
bar := p.AddBar(int64(totalItem)).PrependFunc(decor)

for i := 0; i < totalItem; i++ {
bar.Incr(1) // increment progress bar
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
wg.Add(3) // add wg delta
for i := 0; i < 3; i++ {
name := fmt.Sprintf("Bar#%d:", i)
bar := p.AddBar(int64(totalItem)).
PrependName(name, len(name), 0).
PrependFunc(decor)
go func() {
defer wg.Done()
for i := 0; i < totalItem; i++ {
bar.Incr(1)
time.Sleep(time.Duration(rand.Intn(totalItem)) * time.Millisecond)
}
}()
}
wg.Wait() // Wait for goroutines to finish
p.Stop() // Stop mpb's rendering goroutine
}

func ExampleBar_AppendFunc() {
decor := func(s *mpb.Statistics, myWidth chan<- int, maxWidth <-chan int) string {
str := fmt.Sprintf("%3d/%3d", s.Current, s.Total)
// send width to Progress' goroutine
myWidth <- utf8.RuneCountInString(str)
// receive max width
max := <-maxWidth
return fmt.Sprintf(fmt.Sprintf("%%%ds", max+1), str)
}

totalItem := 100
var wg sync.WaitGroup
p := mpb.New()
bar := p.AddBar(int64(totalItem)).AppendFunc(decor)

for i := 0; i < totalItem; i++ {
bar.Incr(1) // increment progress bar
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
wg.Add(3) // add wg delta
for i := 0; i < 3; i++ {
name := fmt.Sprintf("Bar#%d:", i)
bar := p.AddBar(int64(totalItem)).
PrependName(name, len(name), 0).
AppendFunc(decor)
go func() {
defer wg.Done()
for i := 0; i < totalItem; i++ {
bar.Incr(1)
time.Sleep(time.Duration(rand.Intn(totalItem)) * time.Millisecond)
}
}()
}
wg.Wait() // Wait for goroutines to finish
p.Stop() // Stop mpb's rendering goroutine
}

0 comments on commit 94d5824

Please sign in to comment.