diff --git a/_examples/multiBars/main.go b/_examples/multiBars/main.go new file mode 100644 index 0000000..790a174 --- /dev/null +++ b/_examples/multiBars/main.go @@ -0,0 +1,55 @@ +package main + +import ( + "fmt" + "math/rand" + "sync" + "time" + + "github.com/vbauerster/mpb/v4" + "github.com/vbauerster/mpb/v4/decor" +) + +func init() { + rand.Seed(time.Now().UnixNano()) +} + +func main() { + var wg sync.WaitGroup + // pass &wg (optional), so p will wait for it eventually + p := mpb.New(mpb.WithWaitGroup(&wg)) + total, numBars := 100, 3 + wg.Add(numBars) + + for i := 0; i < numBars; i++ { + name := fmt.Sprintf("Bar#%d:", i) + bar := p.AddBar(int64(total), + mpb.PrependDecorators( + // simple name decorator + decor.Name(name), + // decor.DSyncWidth bit enables column width synchronization + decor.Percentage(decor.WCSyncSpace), + ), + mpb.AppendDecorators( + // replace ETA decorator with "done" message, OnComplete event + decor.OnComplete( + // ETA decorator with ewma age of 60 + decor.EwmaETA(decor.ET_STYLE_GO, 60), "done", + ), + ), + ) + // simulating some work + go func() { + defer wg.Done() + max := 100 * time.Millisecond + for i := 0; i < total; i++ { + start := time.Now() + time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) + // ewma based decorators require work duration measurement + bar.IncrBy(1, time.Since(start)) + } + }() + } + // Waiting for passed &wg and for all bars to complete and flush + p.Wait() +} diff --git a/_examples/simple/main.go b/_examples/simple/main.go deleted file mode 100644 index 790a174..0000000 --- a/_examples/simple/main.go +++ /dev/null @@ -1,55 +0,0 @@ -package main - -import ( - "fmt" - "math/rand" - "sync" - "time" - - "github.com/vbauerster/mpb/v4" - "github.com/vbauerster/mpb/v4/decor" -) - -func init() { - rand.Seed(time.Now().UnixNano()) -} - -func main() { - var wg sync.WaitGroup - // pass &wg (optional), so p will wait for it eventually - p := mpb.New(mpb.WithWaitGroup(&wg)) - total, numBars := 100, 3 - wg.Add(numBars) - - for i := 0; i < numBars; i++ { - name := fmt.Sprintf("Bar#%d:", i) - bar := p.AddBar(int64(total), - mpb.PrependDecorators( - // simple name decorator - decor.Name(name), - // decor.DSyncWidth bit enables column width synchronization - decor.Percentage(decor.WCSyncSpace), - ), - mpb.AppendDecorators( - // replace ETA decorator with "done" message, OnComplete event - decor.OnComplete( - // ETA decorator with ewma age of 60 - decor.EwmaETA(decor.ET_STYLE_GO, 60), "done", - ), - ), - ) - // simulating some work - go func() { - defer wg.Done() - max := 100 * time.Millisecond - for i := 0; i < total; i++ { - start := time.Now() - time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) - // ewma based decorators require work duration measurement - bar.IncrBy(1, time.Since(start)) - } - }() - } - // Waiting for passed &wg and for all bars to complete and flush - p.Wait() -} diff --git a/_examples/sort/main.go b/_examples/sort/main.go deleted file mode 100644 index 210591a..0000000 --- a/_examples/sort/main.go +++ /dev/null @@ -1,55 +0,0 @@ -package main - -import ( - "fmt" - "math/rand" - "sync" - "time" - - "github.com/vbauerster/mpb/v4" - "github.com/vbauerster/mpb/v4/decor" -) - -func init() { - rand.Seed(time.Now().UnixNano()) -} - -func main() { - var wg sync.WaitGroup - p := mpb.New(mpb.WithWaitGroup(&wg)) - total := 100 - numBars := 3 - wg.Add(numBars) - - for i := 0; i < numBars; i++ { - var name string - if i != 1 { - name = fmt.Sprintf("Bar#%d:", i) - } - b := p.AddBar(int64(total), - mpb.PrependDecorators( - decor.Name(name, decor.WCSyncWidth), - decor.CountersNoUnit("%d / %d", decor.WCSyncSpace), - ), - mpb.AppendDecorators( - decor.EwmaETA(decor.ET_STYLE_GO, 60, decor.WC{W: 3}), - ), - ) - go func() { - defer wg.Done() - max := 100 * time.Millisecond - for i := 0; i < total; i++ { - start := time.Now() - time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) - if i&1 == 1 { - priority := total - int(b.Current()) - p.UpdateBarPriority(b, priority) - } - // ewma based decorators require work duration measurement - b.IncrBy(1, time.Since(start)) - } - }() - } - - p.Wait() -} diff --git a/_examples/sortBars/main.go b/_examples/sortBars/main.go new file mode 100644 index 0000000..9a8afc5 --- /dev/null +++ b/_examples/sortBars/main.go @@ -0,0 +1,53 @@ +package main + +import ( + "fmt" + "math/rand" + "sync" + "time" + + "github.com/vbauerster/mpb/v4" + "github.com/vbauerster/mpb/v4/decor" +) + +func init() { + rand.Seed(time.Now().UnixNano()) +} + +func main() { + var wg sync.WaitGroup + p := mpb.New(mpb.WithWaitGroup(&wg)) + total := 100 + numBars := 3 + wg.Add(numBars) + + for i := 0; i < numBars; i++ { + name := fmt.Sprintf("Bar#%d:", i) + b := p.AddBar(int64(total), + mpb.BarOptOnCond(mpb.BarWidth(40), func() bool { return i > 0 }), + mpb.PrependDecorators( + decor.Name(name, decor.WCSyncWidth), + decor.CountersNoUnit("%d / %d", decor.WCSyncSpace), + ), + mpb.AppendDecorators( + decor.EwmaETA(decor.ET_STYLE_GO, 60, decor.WC{W: 3}), + ), + ) + go func() { + defer wg.Done() + max := 100 * time.Millisecond + for i := 0; i < total; i++ { + start := time.Now() + time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) + if i&1 == 1 { + priority := total - int(b.Current()) + p.UpdateBarPriority(b, priority) + } + // ewma based decorators require work duration measurement + b.IncrBy(1, time.Since(start)) + } + }() + } + + p.Wait() +} diff --git a/_examples/spinner/main.go b/_examples/spinner/main.go deleted file mode 100644 index fc28755..0000000 --- a/_examples/spinner/main.go +++ /dev/null @@ -1,77 +0,0 @@ -package main - -import ( - "fmt" - "math/rand" - "sync" - "time" - - "github.com/vbauerster/mpb/v4" - "github.com/vbauerster/mpb/v4/decor" -) - -func init() { - rand.Seed(time.Now().UnixNano()) -} - -func main() { - var wg sync.WaitGroup - p := mpb.New( - mpb.WithWaitGroup(&wg), - mpb.WithWidth(13), - ) - total, numBars := 101, 3 - wg.Add(numBars) - - for i := 0; i < numBars; i++ { - name := fmt.Sprintf("Bar#%d:", i) - var bar *mpb.Bar - if i == 0 { - bar = p.AddBar(int64(total), - // set custom bar style, default one is "[=>-]" - mpb.BarStyle("╢▌▌░╟"), - mpb.PrependDecorators( - // simple name decorator - decor.Name(name), - ), - mpb.AppendDecorators( - // replace ETA decorator with "done" message, OnComplete event - decor.OnComplete( - // ETA decorator with ewma age of 60 - decor.EwmaETA(decor.ET_STYLE_GO, 60), "done", - ), - ), - ) - } else { - bar = p.AddSpinner(int64(total), mpb.SpinnerOnMiddle, - // set custom spinner style, default one is {"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"} - mpb.SpinnerStyle([]string{"∙∙∙", "●∙∙", "∙●∙", "∙∙●", "∙∙∙"}), - mpb.PrependDecorators( - // simple name decorator - decor.Name(name), - ), - mpb.AppendDecorators( - // replace ETA decorator with "done" message, OnComplete event - decor.OnComplete( - // ETA decorator with ewma age of 60 - decor.EwmaETA(decor.ET_STYLE_GO, 60), "done", - ), - ), - ) - } - - // simulating some work - go func() { - defer wg.Done() - max := 100 * time.Millisecond - for i := 0; i < total; i++ { - start := time.Now() - time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) - // ewma based decorators require work duration measurement - bar.IncrBy(1, time.Since(start)) - } - }() - } - // wait for all bars to complete and flush - p.Wait() -} diff --git a/_examples/spinnerBar/main.go b/_examples/spinnerBar/main.go new file mode 100644 index 0000000..fc28755 --- /dev/null +++ b/_examples/spinnerBar/main.go @@ -0,0 +1,77 @@ +package main + +import ( + "fmt" + "math/rand" + "sync" + "time" + + "github.com/vbauerster/mpb/v4" + "github.com/vbauerster/mpb/v4/decor" +) + +func init() { + rand.Seed(time.Now().UnixNano()) +} + +func main() { + var wg sync.WaitGroup + p := mpb.New( + mpb.WithWaitGroup(&wg), + mpb.WithWidth(13), + ) + total, numBars := 101, 3 + wg.Add(numBars) + + for i := 0; i < numBars; i++ { + name := fmt.Sprintf("Bar#%d:", i) + var bar *mpb.Bar + if i == 0 { + bar = p.AddBar(int64(total), + // set custom bar style, default one is "[=>-]" + mpb.BarStyle("╢▌▌░╟"), + mpb.PrependDecorators( + // simple name decorator + decor.Name(name), + ), + mpb.AppendDecorators( + // replace ETA decorator with "done" message, OnComplete event + decor.OnComplete( + // ETA decorator with ewma age of 60 + decor.EwmaETA(decor.ET_STYLE_GO, 60), "done", + ), + ), + ) + } else { + bar = p.AddSpinner(int64(total), mpb.SpinnerOnMiddle, + // set custom spinner style, default one is {"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"} + mpb.SpinnerStyle([]string{"∙∙∙", "●∙∙", "∙●∙", "∙∙●", "∙∙∙"}), + mpb.PrependDecorators( + // simple name decorator + decor.Name(name), + ), + mpb.AppendDecorators( + // replace ETA decorator with "done" message, OnComplete event + decor.OnComplete( + // ETA decorator with ewma age of 60 + decor.EwmaETA(decor.ET_STYLE_GO, 60), "done", + ), + ), + ) + } + + // simulating some work + go func() { + defer wg.Done() + max := 100 * time.Millisecond + for i := 0; i < total; i++ { + start := time.Now() + time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) + // ewma based decorators require work duration measurement + bar.IncrBy(1, time.Since(start)) + } + }() + } + // wait for all bars to complete and flush + p.Wait() +}