diff --git a/bar.go b/bar.go index 82a76eb..aafa1d1 100644 --- a/bar.go +++ b/bar.go @@ -156,9 +156,6 @@ // String returns the string representation of the bar func (b *Bar) String() string { - // if b.IsStopped() { - // return "bar stopped" - // } bufCh := make(chan []byte) b.redrawRequestCh <- &redrawRequest{bufCh} return string(<-bufCh) diff --git a/example/hang/main.go b/example/hang/main.go new file mode 100644 index 0000000..4d1c0ef --- /dev/null +++ b/example/hang/main.go @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" + "math/rand" + "runtime" + "time" + + "github.com/vbauerster/uiprogress" +) + +const ( + totalItem = 100 + maxBlockSize = 10 +) + +func main() { + runtime.GOMAXPROCS(runtime.NumCPU()) + decor := func(s *uiprogress.Statistics) string { + str := fmt.Sprintf("%d/%d", s.Completed, s.Total) + return fmt.Sprintf("%-7s", str) + } + + p := uiprogress.New() + bar := p.AddBar(totalItem).AppendETA().PrependFunc(decor) + + blockSize := rand.Intn(maxBlockSize) + 1 + // Fallowing will hang, in order not to hang + // use !bar.IsCompleted in loop condition + // for i := 0; !bar.IsCompleted(); i += blockSize { + for i := 0; i < totalItem; i += blockSize { + time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) + bar.Incr(blockSize) + blockSize = rand.Intn(maxBlockSize) + 1 + } + + p.WaitAndStop() + fmt.Println("stop") +} diff --git a/example/multi/multi.go b/example/multi/multi.go index 7df40b6..cae0e03 100644 --- a/example/multi/multi.go +++ b/example/multi/multi.go @@ -56,7 +56,8 @@ // time.Sleep(time.Second) // p.RemoveBar(bar2) - p.Stop() + p.WaitAndStop() + bar2.Incr(2) fmt.Println("stop") // p.AddBar(1) // panic: send on closed channnel } diff --git a/example/remove/main.go b/example/remove/main.go new file mode 100644 index 0000000..bce4755 --- /dev/null +++ b/example/remove/main.go @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" + "math/rand" + "runtime" + "time" + + "github.com/vbauerster/uiprogress" +) + +const ( + totalItem = 100 + maxBlockSize = 20 +) + +func main() { + runtime.GOMAXPROCS(runtime.NumCPU()) + decor := func(s *uiprogress.Statistics) string { + str := fmt.Sprintf("%d/%d", s.Completed, s.Total) + return fmt.Sprintf("%-7s", str) + } + + p := uiprogress.New() + bar := p.AddBar(totalItem).AppendETA().PrependFunc(decor) + + blockSize := rand.Intn(maxBlockSize) + 1 + for i := 0; !bar.IsCompleted(); i += 1 { + time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) + bar.Incr(1) + if i == 42 { + p.RemoveBar(bar) + } + blockSize = rand.Intn(maxBlockSize) + 1 + } + + p.WaitAndStop() + fmt.Println("stop") +} diff --git a/example/simple/simple.go b/example/simple/simple.go index f387623..f4c39fd 100644 --- a/example/simple/simple.go +++ b/example/simple/simple.go @@ -3,32 +3,34 @@ import ( "fmt" "math/rand" + "runtime" "time" "github.com/vbauerster/uiprogress" ) const ( - totalItem = 1000 - maxBlockSize = 20 + totalItem = 100 + maxBlockSize = 12 ) func main() { + runtime.GOMAXPROCS(runtime.NumCPU()) + decor := func(s *uiprogress.Statistics) string { + str := fmt.Sprintf("%d/%d", s.Completed, s.Total) + return fmt.Sprintf("%-7s", str) + } + p := uiprogress.New() - bar := p.AddBar(totalItem) // Add a new bar - - // optionally, append and prepend completion and elapsed time - bar.AppendETA() - // bar.PrependElapsed() + bar := p.AddBar(totalItem).AppendETA().PrependFunc(decor) blockSize := rand.Intn(maxBlockSize) + 1 - for i := 1; i <= totalItem; i += blockSize { + for i := 0; i < 100; i += 1 { time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) - bar.Incr(blockSize) + bar.Incr(1) blockSize = rand.Intn(maxBlockSize) + 1 } + p.WaitAndStop() fmt.Println("stop") - - p.Stop() }