diff --git a/decorators.go b/decorators.go index 3df3f31..e7a31dc 100644 --- a/decorators.go +++ b/decorators.go @@ -60,7 +60,7 @@ return b } -func (b *Bar) PrependCounters(unit Units, minWidth int, conf byte) *Bar { +func (b *Bar) PrependCounters(pairFormat string, unit Units, minWidth int, conf byte) *Bar { format := "%%" if (conf & DidentRight) != 0 { format += "-" @@ -69,7 +69,7 @@ b.PrependFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string { current := Format(s.Current).To(unit) total := Format(s.Total).To(unit) - str := fmt.Sprintf("%s / %s", current, total) + str := fmt.Sprintf(pairFormat, current, total) if (conf & DwidthSync) != 0 { myWidth <- utf8.RuneCountInString(str) max := <-maxWidth diff --git a/example/sort/main.go b/example/sort/main.go index 0683ef5..33e1cee 100644 --- a/example/sort/main.go +++ b/example/sort/main.go @@ -46,10 +46,11 @@ var wg sync.WaitGroup p := mpb.New(nil).SetWidth(60).BeforeRenderFunc(sortByProgressFunc()) - name1 := "Bar#1: " + name1 := "Bar#1:" bar1 := p.AddBar(100). - PrependName(name1, len(name1)).PrependFunc(getDecor()). - AppendETA(-6) + PrependName(name1, 0, mpb.DwidthSync). + PrependCounters("%3s/%3s", 0, 10, mpb.DwidthSync|mpb.DextraSpace). + AppendETA(3, 0) wg.Add(1) go func() { defer wg.Done() @@ -62,8 +63,9 @@ }() bar2 := p.AddBar(60). - PrependName("", 0-len(name1)).PrependFunc(getDecor()). - AppendETA(-6) + PrependName("", 0, mpb.DwidthSync). + PrependCounters("%3s/%3s", 0, 10, mpb.DwidthSync|mpb.DextraSpace). + AppendETA(3, 0) wg.Add(1) go func() { defer wg.Done() @@ -76,8 +78,9 @@ }() bar3 := p.AddBar(80). - PrependName("Bar#3: ", 0).PrependFunc(getDecor()). - AppendETA(-6) + PrependName("Bar#3:", 0, mpb.DwidthSync). + PrependCounters("%3s/%3s", 0, 10, mpb.DwidthSync|mpb.DextraSpace). + AppendETA(3, 0) wg.Add(1) go func() { defer wg.Done() @@ -95,13 +98,6 @@ fmt.Println("stop") } -func getDecor() mpb.DecoratorFunc { - return func(s *mpb.Statistics, myWidth chan<- int, maxWidth <-chan int) string { - str := fmt.Sprintf("%d/%d", s.Current, s.Total) - return fmt.Sprintf("%-7s", str) - } -} - func sleep(blockSize int) { time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) }