Codebase list golang-github-vbauerster-mpb / 35db6ab
bar: add WithSpinner option Signed-off-by: Yoan Blanc <yoan.blanc@exoscale.ch> Yoan Blanc authored 7 years ago Vladimir Bauer committed 7 years ago
5 changed file(s) with 100 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
5151 total int64
5252 current int64
5353 runes barRunes
54 spinner []rune
5455 trimLeftSpace bool
5556 trimRightSpace bool
5657 toComplete bool
344345 return io.MultiReader(s.bufP, s.bufA)
345346 }
346347
347 s.fillBar(s.width)
348 s.fill(s.width)
348349 barCount := utf8.RuneCount(s.bufB.Bytes())
349350 totalCount := prependCount + barCount + appendCount
350351 if spaceCount := 0; totalCount > termWidth {
354355 if !s.trimRightSpace {
355356 spaceCount++
356357 }
357 s.fillBar(termWidth - prependCount - appendCount - spaceCount)
358 s.fill(termWidth - prependCount - appendCount - spaceCount)
358359 }
359360
360361 return io.MultiReader(s.bufP, s.bufB, s.bufA)
362 }
363
364 func (s *bState) fill(width int) {
365 if len(s.spinner) != 0 {
366 s.fillSpinner(width)
367 } else {
368 s.fillBar(width)
369 }
370 }
371
372 func (s *bState) fillSpinner(width int) {
373 s.bufB.Reset()
374
375 if !s.trimLeftSpace {
376 s.bufB.WriteByte(' ')
377 }
378
379 spin := []byte(string(s.spinner[s.current%int64(len(s.spinner))]))
380 for _, b := range spin {
381 s.bufB.WriteByte(b)
382 }
383
384 for i := len(spin); i < width; i++ {
385 s.bufB.WriteRune(' ')
386 }
361387 }
362388
363389 func (s *bState) fillBar(width int) {
110110 }
111111 }
112112
113 func barSpinner(spinner string) BarOption {
114 return func(s *bState) {
115 s.spinner = []rune(spinner)
116 }
117 }
118
113119 func barWidth(w int) BarOption {
114120 return func(s *bState) {
115121 s.width = w
0 package main
1
2 import (
3 "fmt"
4 "math/rand"
5 "sync"
6 "time"
7
8 "github.com/vbauerster/mpb"
9 "github.com/vbauerster/mpb/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.WithSpinner("⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"),
21 )
22 total, numBars := 100, 3
23 wg.Add(numBars)
24
25 for i := 0; i < numBars; i++ {
26 name := fmt.Sprintf("Bar#%d:", i)
27 bar := p.AddBar(int64(total),
28 mpb.PrependDecorators(
29 // simple name decorator
30 decor.Name(name),
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 // wait for all bars to complete and flush
53 p.Wait()
54 }
2828 if w >= 0 {
2929 s.width = w
3030 }
31 }
32 }
33
34 // WithSpinner overrides default bar format to use a spinner
35 func WithSpinner(spinner string) ProgressOption {
36 return func(s *pState) {
37 s.spinner = spinner
3138 }
3239 }
3340
3636 idCounter int
3737 width int
3838 format string
39 spinner string
3940 rr time.Duration
4041 cw *cwriter.Writer
4142 pMatrix map[int][]chan int
8889 select {
8990 case p.operateState <- func(s *pState) {
9091 options = append(options, barWidth(s.width), barFormat(s.format))
92 if s.spinner != "" {
93 options = append(options, barSpinner(s.spinner))
94 }
9195 b := newBar(p.wg, s.idCounter, total, s.cancel, options...)
9296 if b.runningBar != nil {
9397 s.waitBars[b.runningBar] = b