diff --git a/bar.go b/bar.go index e2367c2..fa926ed 100644 --- a/bar.go +++ b/bar.go @@ -48,8 +48,8 @@ leftEnd: '[', rightEnd: ']', alpha: 0.25, - // total: total, - width: width, + width: width, + incrCh: make(chan int), redrawReqCh: make(chan chan []byte), currentReqCh: make(chan chan int), diff --git a/example/hang/main.go b/example/hang/main.go index d45ac02..1b17e59 100644 --- a/example/hang/main.go +++ b/example/hang/main.go @@ -20,7 +20,6 @@ } p := mpb.New() - p.Wg.Add(1) bar := p.AddBar(totalItem).AppendETA().PrependFunc(decor) blockSize := rand.Intn(maxBlockSize) + 1 @@ -33,6 +32,6 @@ blockSize = rand.Intn(maxBlockSize) + 1 } - p.WaitAndStop() + p.Stop() fmt.Println("stop") } diff --git a/example/interrupt/main.go b/example/interrupt/main.go index 858367c..9051f00 100644 --- a/example/interrupt/main.go +++ b/example/interrupt/main.go @@ -21,7 +21,6 @@ p := mpb.New() bar := p.AddBar(totalItem).AppendETA().PrependFunc(decor) - p.Wg.Add(1) blockSize := rand.Intn(maxBlockSize) + 1 for i := 0; bar.InProgress(); i++ { @@ -33,6 +32,6 @@ blockSize = rand.Intn(maxBlockSize) + 1 } - p.WaitAndStop() + p.Stop() fmt.Println("stop") } diff --git a/example/prependETA/main.go b/example/prependETA/main.go index 03c2ac0..213f4fc 100644 --- a/example/prependETA/main.go +++ b/example/prependETA/main.go @@ -3,6 +3,7 @@ import ( "fmt" "math/rand" + "sync" "time" "github.com/vbauerster/mpb" @@ -14,13 +15,15 @@ func main() { + var wg sync.WaitGroup p := mpb.New().SetWidth(64) // p := mpb.New().RefreshRate(80 * time.Millisecond).SetWidth(64) name1 := "Bar#1:" bar1 := p.AddBar(50).AppendPercentage().PrependETA(4).PrependName(name1, len(name1)) - p.Wg.Add(1) + wg.Add(1) go func() { + defer wg.Done() 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))))) @@ -30,8 +33,9 @@ }() bar2 := p.AddBar(100).AppendPercentage().PrependETA(4).PrependName("", 0-len(name1)) - p.Wg.Add(1) + wg.Add(1) go func() { + defer wg.Done() 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))))) @@ -41,8 +45,9 @@ }() bar3 := p.AddBar(80).AppendPercentage().PrependETA(4).PrependName("Bar#3:", 0) - p.Wg.Add(1) + wg.Add(1) go func() { + defer wg.Done() 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))))) @@ -51,10 +56,8 @@ } }() - // time.Sleep(time.Second) - // p.RemoveBar(bar2) - - p.WaitAndStop() + wg.Wait() + p.Stop() fmt.Println("stop") // p.AddBar(1) // panic: send on closed channnel } diff --git a/example/prependElapsed/main.go b/example/prependElapsed/main.go index 6359c02..0886f9f 100644 --- a/example/prependElapsed/main.go +++ b/example/prependElapsed/main.go @@ -3,6 +3,7 @@ import ( "fmt" "math/rand" + "sync" "time" "github.com/vbauerster/mpb" @@ -14,13 +15,15 @@ func main() { + var wg sync.WaitGroup p := mpb.New().SetWidth(64) // p := mpb.New().RefreshRate(80 * time.Millisecond).SetWidth(64) name1 := "Bar#1:" bar1 := p.AddBar(50).AppendPercentage().PrependElapsed(3).PrependName(name1, len(name1)) - p.Wg.Add(1) + wg.Add(1) go func() { + defer wg.Done() 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))))) @@ -30,8 +33,9 @@ }() bar2 := p.AddBar(100).AppendPercentage().PrependElapsed(3).PrependName("", 0-len(name1)) - p.Wg.Add(1) + wg.Add(1) go func() { + defer wg.Done() 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))))) @@ -41,8 +45,9 @@ }() bar3 := p.AddBar(80).AppendPercentage().PrependElapsed(3).PrependName("Bar#3:", 0) - p.Wg.Add(1) + wg.Add(1) go func() { + defer wg.Done() 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))))) @@ -51,10 +56,8 @@ } }() - // time.Sleep(time.Second) - // p.RemoveBar(bar2) - - p.WaitAndStop() + wg.Wait() + p.Stop() fmt.Println("stop") // p.AddBar(1) // panic: send on closed channnel } diff --git a/example/remove/main.go b/example/remove/main.go index aae14f8..64af42d 100644 --- a/example/remove/main.go +++ b/example/remove/main.go @@ -3,6 +3,7 @@ import ( "fmt" "math/rand" + "sync" "time" "github.com/vbauerster/mpb" @@ -14,13 +15,15 @@ func main() { + var wg sync.WaitGroup p := mpb.New().SetWidth(64) // p := mpb.New().RefreshRate(100 * time.Millisecond).SetWidth(64) name1 := "Bar#1:" bar1 := p.AddBar(50).AppendETA().PrependPercentage(3).PrependName(name1, len(name1)) - p.Wg.Add(1) + wg.Add(1) go func() { + defer wg.Done() 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))))) @@ -30,8 +33,9 @@ }() bar2 := p.AddBar(100).AppendETA().PrependPercentage(3).PrependName("", 0-len(name1)) - p.Wg.Add(1) + wg.Add(1) go func() { + defer wg.Done() 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))))) @@ -41,8 +45,9 @@ }() bar3 := p.AddBar(80).AppendETA().PrependPercentage(3).PrependName("Bar#3:", 0) - p.Wg.Add(1) + wg.Add(1) go func() { + defer wg.Done() 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))))) @@ -52,9 +57,12 @@ }() time.Sleep(3 * time.Second) + // After removing the bar, it is good practice to ask underlying goroutine + // (2nd one in our example) to stop, so its wg.Done() will execute in time p.RemoveBar(bar2) - p.WaitAndStop() + wg.Wait() + p.Stop() fmt.Println("stop") // p.AddBar(1) // panic: send on closed channnel } diff --git a/example/sort/main.go b/example/sort/main.go index b158546..b162716 100644 --- a/example/sort/main.go +++ b/example/sort/main.go @@ -3,6 +3,7 @@ import ( "fmt" "math/rand" + "sync" "time" "github.com/vbauerster/mpb" @@ -14,12 +15,14 @@ func main() { + var wg sync.WaitGroup p := mpb.New().WithSort(mpb.SortTop).SetWidth(60) name1 := "Bar#1:" bar1 := p.AddBar(100).AppendETA().PrependFunc(getDecor()).PrependName(name1, len(name1)) - p.Wg.Add(1) + wg.Add(1) go func() { + defer wg.Done() 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))))) @@ -29,8 +32,9 @@ }() bar2 := p.AddBar(60).AppendETA().PrependFunc(getDecor()).PrependName("", 0-len(name1)) - p.Wg.Add(1) + wg.Add(1) go func() { + defer wg.Done() blockSize := rand.Intn(maxBlockSize) + 1 for i := 0; i < 60; i++ { time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) @@ -40,8 +44,9 @@ }() bar3 := p.AddBar(80).AppendETA().PrependFunc(getDecor()).PrependName("Bar#3:", 0) - p.Wg.Add(1) + wg.Add(1) go func() { + defer wg.Done() 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))))) @@ -50,10 +55,8 @@ } }() - // time.Sleep(time.Second) - // p.RemoveBar(bar2) - - p.WaitAndStop() + wg.Wait() + p.Stop() fmt.Println("stop") // p.AddBar(1) // panic: send on closed channnel }