Codebase list golang-github-vbauerster-mpb / edadc65
refactor: sort example Vladimir Bauer 9 years ago
2 changed file(s) with 12 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
5959 return b
6060 }
6161
62 func (b *Bar) PrependCounters(unit Units, minWidth int, conf byte) *Bar {
62 func (b *Bar) PrependCounters(pairFormat string, unit Units, minWidth int, conf byte) *Bar {
6363 format := "%%"
6464 if (conf & DidentRight) != 0 {
6565 format += "-"
6868 b.PrependFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
6969 current := Format(s.Current).To(unit)
7070 total := Format(s.Total).To(unit)
71 str := fmt.Sprintf("%s / %s", current, total)
71 str := fmt.Sprintf(pairFormat, current, total)
7272 if (conf & DwidthSync) != 0 {
7373 myWidth <- utf8.RuneCountInString(str)
7474 max := <-maxWidth
4545 var wg sync.WaitGroup
4646 p := mpb.New(nil).SetWidth(60).BeforeRenderFunc(sortByProgressFunc())
4747
48 name1 := "Bar#1: "
48 name1 := "Bar#1:"
4949 bar1 := p.AddBar(100).
50 PrependName(name1, len(name1)).PrependFunc(getDecor()).
51 AppendETA(-6)
50 PrependName(name1, 0, mpb.DwidthSync).
51 PrependCounters("%3s/%3s", 0, 10, mpb.DwidthSync|mpb.DextraSpace).
52 AppendETA(3, 0)
5253 wg.Add(1)
5354 go func() {
5455 defer wg.Done()
6162 }()
6263
6364 bar2 := p.AddBar(60).
64 PrependName("", 0-len(name1)).PrependFunc(getDecor()).
65 AppendETA(-6)
65 PrependName("", 0, mpb.DwidthSync).
66 PrependCounters("%3s/%3s", 0, 10, mpb.DwidthSync|mpb.DextraSpace).
67 AppendETA(3, 0)
6668 wg.Add(1)
6769 go func() {
6870 defer wg.Done()
7577 }()
7678
7779 bar3 := p.AddBar(80).
78 PrependName("Bar#3: ", 0).PrependFunc(getDecor()).
79 AppendETA(-6)
80 PrependName("Bar#3:", 0, mpb.DwidthSync).
81 PrependCounters("%3s/%3s", 0, 10, mpb.DwidthSync|mpb.DextraSpace).
82 AppendETA(3, 0)
8083 wg.Add(1)
8184 go func() {
8285 defer wg.Done()
9497 fmt.Println("stop")
9598 }
9699
97 func getDecor() mpb.DecoratorFunc {
98 return func(s *mpb.Statistics, myWidth chan<- int, maxWidth <-chan int) string {
99 str := fmt.Sprintf("%d/%d", s.Current, s.Total)
100 return fmt.Sprintf("%-7s", str)
101 }
102 }
103
104100 func sleep(blockSize int) {
105101 time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond)))))
106102 }