better example names
Vladimir Bauer
7 years ago
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "math/rand" | |
| 5 | "sync" | |
| 6 | "time" | |
| 7 | ||
| 8 | "github.com/vbauerster/mpb/v4" | |
| 9 | "github.com/vbauerster/mpb/v4/decor" | |
| 10 | ) | |
| 11 | ||
| 12 | func init() { | |
| 13 | rand.Seed(time.Now().UnixNano()) | |
| 14 | } | |
| 15 | ||
| 16 | func main() { | |
| 17 | var wg sync.WaitGroup | |
| 18 | // pass &wg (optional), so p will wait for it eventually | |
| 19 | p := mpb.New(mpb.WithWaitGroup(&wg)) | |
| 20 | total, numBars := 100, 3 | |
| 21 | wg.Add(numBars) | |
| 22 | ||
| 23 | for i := 0; i < numBars; i++ { | |
| 24 | name := fmt.Sprintf("Bar#%d:", i) | |
| 25 | bar := p.AddBar(int64(total), | |
| 26 | mpb.PrependDecorators( | |
| 27 | // simple name decorator | |
| 28 | decor.Name(name), | |
| 29 | // decor.DSyncWidth bit enables column width synchronization | |
| 30 | decor.Percentage(decor.WCSyncSpace), | |
| 31 | ), | |
| 32 | mpb.AppendDecorators( | |
| 33 | // replace ETA decorator with "done" message, OnComplete event | |
| 34 | decor.OnComplete( | |
| 35 | // ETA decorator with ewma age of 60 | |
| 36 | decor.EwmaETA(decor.ET_STYLE_GO, 60), "done", | |
| 37 | ), | |
| 38 | ), | |
| 39 | ) | |
| 40 | // simulating some work | |
| 41 | go func() { | |
| 42 | defer wg.Done() | |
| 43 | max := 100 * time.Millisecond | |
| 44 | for i := 0; i < total; i++ { | |
| 45 | start := time.Now() | |
| 46 | time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) | |
| 47 | // ewma based decorators require work duration measurement | |
| 48 | bar.IncrBy(1, time.Since(start)) | |
| 49 | } | |
| 50 | }() | |
| 51 | } | |
| 52 | // Waiting for passed &wg and for all bars to complete and flush | |
| 53 | p.Wait() | |
| 54 | } |
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "math/rand" | |
| 5 | "sync" | |
| 6 | "time" | |
| 7 | ||
| 8 | "github.com/vbauerster/mpb/v4" | |
| 9 | "github.com/vbauerster/mpb/v4/decor" | |
| 10 | ) | |
| 11 | ||
| 12 | func init() { | |
| 13 | rand.Seed(time.Now().UnixNano()) | |
| 14 | } | |
| 15 | ||
| 16 | func main() { | |
| 17 | var wg sync.WaitGroup | |
| 18 | // pass &wg (optional), so p will wait for it eventually | |
| 19 | p := mpb.New(mpb.WithWaitGroup(&wg)) | |
| 20 | total, numBars := 100, 3 | |
| 21 | wg.Add(numBars) | |
| 22 | ||
| 23 | for i := 0; i < numBars; i++ { | |
| 24 | name := fmt.Sprintf("Bar#%d:", i) | |
| 25 | bar := p.AddBar(int64(total), | |
| 26 | mpb.PrependDecorators( | |
| 27 | // simple name decorator | |
| 28 | decor.Name(name), | |
| 29 | // decor.DSyncWidth bit enables column width synchronization | |
| 30 | decor.Percentage(decor.WCSyncSpace), | |
| 31 | ), | |
| 32 | mpb.AppendDecorators( | |
| 33 | // replace ETA decorator with "done" message, OnComplete event | |
| 34 | decor.OnComplete( | |
| 35 | // ETA decorator with ewma age of 60 | |
| 36 | decor.EwmaETA(decor.ET_STYLE_GO, 60), "done", | |
| 37 | ), | |
| 38 | ), | |
| 39 | ) | |
| 40 | // simulating some work | |
| 41 | go func() { | |
| 42 | defer wg.Done() | |
| 43 | max := 100 * time.Millisecond | |
| 44 | for i := 0; i < total; i++ { | |
| 45 | start := time.Now() | |
| 46 | time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) | |
| 47 | // ewma based decorators require work duration measurement | |
| 48 | bar.IncrBy(1, time.Since(start)) | |
| 49 | } | |
| 50 | }() | |
| 51 | } | |
| 52 | // Waiting for passed &wg and for all bars to complete and flush | |
| 53 | p.Wait() | |
| 54 | } |
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "math/rand" | |
| 5 | "sync" | |
| 6 | "time" | |
| 7 | ||
| 8 | "github.com/vbauerster/mpb/v4" | |
| 9 | "github.com/vbauerster/mpb/v4/decor" | |
| 10 | ) | |
| 11 | ||
| 12 | func init() { | |
| 13 | rand.Seed(time.Now().UnixNano()) | |
| 14 | } | |
| 15 | ||
| 16 | func main() { | |
| 17 | var wg sync.WaitGroup | |
| 18 | p := mpb.New(mpb.WithWaitGroup(&wg)) | |
| 19 | total := 100 | |
| 20 | numBars := 3 | |
| 21 | wg.Add(numBars) | |
| 22 | ||
| 23 | for i := 0; i < numBars; i++ { | |
| 24 | var name string | |
| 25 | if i != 1 { | |
| 26 | name = fmt.Sprintf("Bar#%d:", i) | |
| 27 | } | |
| 28 | b := p.AddBar(int64(total), | |
| 29 | mpb.PrependDecorators( | |
| 30 | decor.Name(name, decor.WCSyncWidth), | |
| 31 | decor.CountersNoUnit("%d / %d", decor.WCSyncSpace), | |
| 32 | ), | |
| 33 | mpb.AppendDecorators( | |
| 34 | decor.EwmaETA(decor.ET_STYLE_GO, 60, decor.WC{W: 3}), | |
| 35 | ), | |
| 36 | ) | |
| 37 | go func() { | |
| 38 | defer wg.Done() | |
| 39 | max := 100 * time.Millisecond | |
| 40 | for i := 0; i < total; i++ { | |
| 41 | start := time.Now() | |
| 42 | time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) | |
| 43 | if i&1 == 1 { | |
| 44 | priority := total - int(b.Current()) | |
| 45 | p.UpdateBarPriority(b, priority) | |
| 46 | } | |
| 47 | // ewma based decorators require work duration measurement | |
| 48 | b.IncrBy(1, time.Since(start)) | |
| 49 | } | |
| 50 | }() | |
| 51 | } | |
| 52 | ||
| 53 | p.Wait() | |
| 54 | } |
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "math/rand" | |
| 5 | "sync" | |
| 6 | "time" | |
| 7 | ||
| 8 | "github.com/vbauerster/mpb/v4" | |
| 9 | "github.com/vbauerster/mpb/v4/decor" | |
| 10 | ) | |
| 11 | ||
| 12 | func init() { | |
| 13 | rand.Seed(time.Now().UnixNano()) | |
| 14 | } | |
| 15 | ||
| 16 | func main() { | |
| 17 | var wg sync.WaitGroup | |
| 18 | p := mpb.New(mpb.WithWaitGroup(&wg)) | |
| 19 | total := 100 | |
| 20 | numBars := 3 | |
| 21 | wg.Add(numBars) | |
| 22 | ||
| 23 | for i := 0; i < numBars; i++ { | |
| 24 | name := fmt.Sprintf("Bar#%d:", i) | |
| 25 | b := p.AddBar(int64(total), | |
| 26 | mpb.BarOptOnCond(mpb.BarWidth(40), func() bool { return i > 0 }), | |
| 27 | mpb.PrependDecorators( | |
| 28 | decor.Name(name, decor.WCSyncWidth), | |
| 29 | decor.CountersNoUnit("%d / %d", decor.WCSyncSpace), | |
| 30 | ), | |
| 31 | mpb.AppendDecorators( | |
| 32 | decor.EwmaETA(decor.ET_STYLE_GO, 60, decor.WC{W: 3}), | |
| 33 | ), | |
| 34 | ) | |
| 35 | go func() { | |
| 36 | defer wg.Done() | |
| 37 | max := 100 * time.Millisecond | |
| 38 | for i := 0; i < total; i++ { | |
| 39 | start := time.Now() | |
| 40 | time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) | |
| 41 | if i&1 == 1 { | |
| 42 | priority := total - int(b.Current()) | |
| 43 | p.UpdateBarPriority(b, priority) | |
| 44 | } | |
| 45 | // ewma based decorators require work duration measurement | |
| 46 | b.IncrBy(1, time.Since(start)) | |
| 47 | } | |
| 48 | }() | |
| 49 | } | |
| 50 | ||
| 51 | p.Wait() | |
| 52 | } |
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "math/rand" | |
| 5 | "sync" | |
| 6 | "time" | |
| 7 | ||
| 8 | "github.com/vbauerster/mpb/v4" | |
| 9 | "github.com/vbauerster/mpb/v4/decor" | |
| 10 | ) | |
| 11 | ||
| 12 | func init() { | |
| 13 | rand.Seed(time.Now().UnixNano()) | |
| 14 | } | |
| 15 | ||
| 16 | func main() { | |
| 17 | var wg sync.WaitGroup | |
| 18 | p := mpb.New( | |
| 19 | mpb.WithWaitGroup(&wg), | |
| 20 | mpb.WithWidth(13), | |
| 21 | ) | |
| 22 | total, numBars := 101, 3 | |
| 23 | wg.Add(numBars) | |
| 24 | ||
| 25 | for i := 0; i < numBars; i++ { | |
| 26 | name := fmt.Sprintf("Bar#%d:", i) | |
| 27 | var bar *mpb.Bar | |
| 28 | if i == 0 { | |
| 29 | bar = p.AddBar(int64(total), | |
| 30 | // set custom bar style, default one is "[=>-]" | |
| 31 | mpb.BarStyle("╢▌▌░╟"), | |
| 32 | mpb.PrependDecorators( | |
| 33 | // simple name decorator | |
| 34 | decor.Name(name), | |
| 35 | ), | |
| 36 | mpb.AppendDecorators( | |
| 37 | // replace ETA decorator with "done" message, OnComplete event | |
| 38 | decor.OnComplete( | |
| 39 | // ETA decorator with ewma age of 60 | |
| 40 | decor.EwmaETA(decor.ET_STYLE_GO, 60), "done", | |
| 41 | ), | |
| 42 | ), | |
| 43 | ) | |
| 44 | } else { | |
| 45 | bar = p.AddSpinner(int64(total), mpb.SpinnerOnMiddle, | |
| 46 | // set custom spinner style, default one is {"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"} | |
| 47 | mpb.SpinnerStyle([]string{"∙∙∙", "●∙∙", "∙●∙", "∙∙●", "∙∙∙"}), | |
| 48 | mpb.PrependDecorators( | |
| 49 | // simple name decorator | |
| 50 | decor.Name(name), | |
| 51 | ), | |
| 52 | mpb.AppendDecorators( | |
| 53 | // replace ETA decorator with "done" message, OnComplete event | |
| 54 | decor.OnComplete( | |
| 55 | // ETA decorator with ewma age of 60 | |
| 56 | decor.EwmaETA(decor.ET_STYLE_GO, 60), "done", | |
| 57 | ), | |
| 58 | ), | |
| 59 | ) | |
| 60 | } | |
| 61 | ||
| 62 | // simulating some work | |
| 63 | go func() { | |
| 64 | defer wg.Done() | |
| 65 | max := 100 * time.Millisecond | |
| 66 | for i := 0; i < total; i++ { | |
| 67 | start := time.Now() | |
| 68 | time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) | |
| 69 | // ewma based decorators require work duration measurement | |
| 70 | bar.IncrBy(1, time.Since(start)) | |
| 71 | } | |
| 72 | }() | |
| 73 | } | |
| 74 | // wait for all bars to complete and flush | |
| 75 | p.Wait() | |
| 76 | } |
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "math/rand" | |
| 5 | "sync" | |
| 6 | "time" | |
| 7 | ||
| 8 | "github.com/vbauerster/mpb/v4" | |
| 9 | "github.com/vbauerster/mpb/v4/decor" | |
| 10 | ) | |
| 11 | ||
| 12 | func init() { | |
| 13 | rand.Seed(time.Now().UnixNano()) | |
| 14 | } | |
| 15 | ||
| 16 | func main() { | |
| 17 | var wg sync.WaitGroup | |
| 18 | p := mpb.New( | |
| 19 | mpb.WithWaitGroup(&wg), | |
| 20 | mpb.WithWidth(13), | |
| 21 | ) | |
| 22 | total, numBars := 101, 3 | |
| 23 | wg.Add(numBars) | |
| 24 | ||
| 25 | for i := 0; i < numBars; i++ { | |
| 26 | name := fmt.Sprintf("Bar#%d:", i) | |
| 27 | var bar *mpb.Bar | |
| 28 | if i == 0 { | |
| 29 | bar = p.AddBar(int64(total), | |
| 30 | // set custom bar style, default one is "[=>-]" | |
| 31 | mpb.BarStyle("╢▌▌░╟"), | |
| 32 | mpb.PrependDecorators( | |
| 33 | // simple name decorator | |
| 34 | decor.Name(name), | |
| 35 | ), | |
| 36 | mpb.AppendDecorators( | |
| 37 | // replace ETA decorator with "done" message, OnComplete event | |
| 38 | decor.OnComplete( | |
| 39 | // ETA decorator with ewma age of 60 | |
| 40 | decor.EwmaETA(decor.ET_STYLE_GO, 60), "done", | |
| 41 | ), | |
| 42 | ), | |
| 43 | ) | |
| 44 | } else { | |
| 45 | bar = p.AddSpinner(int64(total), mpb.SpinnerOnMiddle, | |
| 46 | // set custom spinner style, default one is {"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"} | |
| 47 | mpb.SpinnerStyle([]string{"∙∙∙", "●∙∙", "∙●∙", "∙∙●", "∙∙∙"}), | |
| 48 | mpb.PrependDecorators( | |
| 49 | // simple name decorator | |
| 50 | decor.Name(name), | |
| 51 | ), | |
| 52 | mpb.AppendDecorators( | |
| 53 | // replace ETA decorator with "done" message, OnComplete event | |
| 54 | decor.OnComplete( | |
| 55 | // ETA decorator with ewma age of 60 | |
| 56 | decor.EwmaETA(decor.ET_STYLE_GO, 60), "done", | |
| 57 | ), | |
| 58 | ), | |
| 59 | ) | |
| 60 | } | |
| 61 | ||
| 62 | // simulating some work | |
| 63 | go func() { | |
| 64 | defer wg.Done() | |
| 65 | max := 100 * time.Millisecond | |
| 66 | for i := 0; i < total; i++ { | |
| 67 | start := time.Now() | |
| 68 | time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) | |
| 69 | // ewma based decorators require work duration measurement | |
| 70 | bar.IncrBy(1, time.Since(start)) | |
| 71 | } | |
| 72 | }() | |
| 73 | } | |
| 74 | // wait for all bars to complete and flush | |
| 75 | p.Wait() | |
| 76 | } |