diff --git a/decor/decorators.go b/decor/decorators.go index d0758bb..06b19bf 100644 --- a/decor/decorators.go +++ b/decor/decorators.go @@ -8,7 +8,7 @@ ) const ( - // DidentRight bit specifies identation direction. + // DidentRight specifies identation direction. // |foo |b | With DidentRight // | foo| b| Without DidentRight DidentRight = 1 << iota @@ -17,15 +17,10 @@ // Effective on multiple bars only. DwidthSync - // DextraSpace bit adds extra space, makes sense with DwidthSync only. + // DextraSpace adds extra space, makes sense with DwidthSync only. // When DidentRight bit set, the space will be added to the right, // otherwise to the left. DextraSpace - - // DslowMotion bit instructs decorator to skip refreshing its output - // every second tick. Normally not necessary, but if flickering is too fast, - // you may try it out. - DslowMotion // DSyncSpace is shortcut for DwidthSync|DextraSpace DSyncSpace = DwidthSync | DextraSpace @@ -99,26 +94,17 @@ format += "-" } format += "%ds" - count, times := 0, 1 - if (conf & DslowMotion) != 0 { - times++ - } - var out string - return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string { - if count == 0 || s.Completed { - count = times - out = messageFn(s) - } - count-- - if (conf & DwidthSync) != 0 { - widthAccumulator <- utf8.RuneCountInString(out) - max := <-widthDistributor - if (conf & DextraSpace) != 0 { - max++ - } - return fmt.Sprintf(fmt.Sprintf(format, max), out) - } - return fmt.Sprintf(fmt.Sprintf(format, width), out) + return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string { + name := messageFn(s) + if (conf & DwidthSync) != 0 { + widthAccumulator <- utf8.RuneCountInString(name) + max := <-widthDistributor + if (conf & DextraSpace) != 0 { + max++ + } + return fmt.Sprintf(fmt.Sprintf(format, max), name) + } + return fmt.Sprintf(fmt.Sprintf(format, width), name) } } @@ -169,33 +155,25 @@ format += "-" } format += "%ds" - count, times := 0, 1 - if (conf & DslowMotion) != 0 { - times++ - } - var out string - return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string { - if count == 0 || s.Completed { - count = times - switch unit { - case unitKiB: - out = fmt.Sprintf(pairFormat, CounterKiB(s.Current), CounterKiB(s.Total)) - case unitKB: - out = fmt.Sprintf(pairFormat, CounterKB(s.Current), CounterKB(s.Total)) - default: - out = fmt.Sprintf(pairFormat, s.Current, s.Total) - } - } - count-- - if (conf & DwidthSync) != 0 { - widthAccumulator <- utf8.RuneCountInString(out) - max := <-widthDistributor - if (conf & DextraSpace) != 0 { - max++ - } - return fmt.Sprintf(fmt.Sprintf(format, max), out) - } - return fmt.Sprintf(fmt.Sprintf(format, width), out) + return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string { + var str string + switch unit { + case unitKiB: + str = fmt.Sprintf(pairFormat, CounterKiB(s.Current), CounterKiB(s.Total)) + case unitKB: + str = fmt.Sprintf(pairFormat, CounterKB(s.Current), CounterKB(s.Total)) + default: + str = fmt.Sprintf(pairFormat, s.Current, s.Total) + } + if (conf & DwidthSync) != 0 { + widthAccumulator <- utf8.RuneCountInString(str) + max := <-widthDistributor + if (conf & DextraSpace) != 0 { + max++ + } + return fmt.Sprintf(fmt.Sprintf(format, max), str) + } + return fmt.Sprintf(fmt.Sprintf(format, width), str) } } @@ -210,26 +188,17 @@ format += "-" } format += "%ds" - count, times := 0, 1 - if (conf & DslowMotion) != 0 { - times++ - } - var out string - return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string { - if count == 0 || s.Completed { - count = times - out = fmt.Sprint(time.Duration(s.Eta().Seconds()) * time.Second) - } - count-- - if (conf & DwidthSync) != 0 { - widthAccumulator <- utf8.RuneCountInString(out) - max := <-widthDistributor - if (conf & DextraSpace) != 0 { - max++ - } - return fmt.Sprintf(fmt.Sprintf(format, max), out) - } - return fmt.Sprintf(fmt.Sprintf(format, width), out) + return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string { + str := fmt.Sprint(time.Duration(s.Eta().Seconds()) * time.Second) + if (conf & DwidthSync) != 0 { + widthAccumulator <- utf8.RuneCountInString(str) + max := <-widthDistributor + if (conf & DextraSpace) != 0 { + max++ + } + return fmt.Sprintf(fmt.Sprintf(format, max), str) + } + return fmt.Sprintf(fmt.Sprintf(format, width), str) } } @@ -244,26 +213,17 @@ format += "-" } format += "%ds" - count, times := 0, 1 - if (conf & DslowMotion) != 0 { - times++ - } - var out string - return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string { - if count == 0 || s.Completed { - count = times - out = fmt.Sprint(time.Duration(s.TimeElapsed.Seconds()) * time.Second) - } - count-- - if (conf & DwidthSync) != 0 { - widthAccumulator <- utf8.RuneCountInString(out) - max := <-widthDistributor - if (conf & DextraSpace) != 0 { - max++ - } - return fmt.Sprintf(fmt.Sprintf(format, max), out) - } - return fmt.Sprintf(fmt.Sprintf(format, width), out) + return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string { + str := fmt.Sprint(time.Duration(s.TimeElapsed.Seconds()) * time.Second) + if (conf & DwidthSync) != 0 { + widthAccumulator <- utf8.RuneCountInString(str) + max := <-widthDistributor + if (conf & DextraSpace) != 0 { + max++ + } + return fmt.Sprintf(fmt.Sprintf(format, max), str) + } + return fmt.Sprintf(fmt.Sprintf(format, width), str) } } @@ -278,26 +238,17 @@ format += "-" } format += "%ds" - count, times := 0, 1 - if (conf & DslowMotion) != 0 { - times++ - } - var out string - return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string { - if count == 0 || s.Completed { - count = times - out = fmt.Sprintf("%d %%", CalcPercentage(s.Total, s.Current, 100)) - } - count-- - if (conf & DwidthSync) != 0 { - widthAccumulator <- utf8.RuneCountInString(out) - max := <-widthDistributor - if (conf & DextraSpace) != 0 { - max++ - } - return fmt.Sprintf(fmt.Sprintf(format, max), out) - } - return fmt.Sprintf(fmt.Sprintf(format, width), out) + return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string { + str := fmt.Sprintf("%d %%", CalcPercentage(s.Total, s.Current, 100)) + if (conf & DwidthSync) != 0 { + widthAccumulator <- utf8.RuneCountInString(str) + max := <-widthDistributor + if (conf & DextraSpace) != 0 { + max++ + } + return fmt.Sprintf(fmt.Sprintf(format, max), str) + } + return fmt.Sprintf(fmt.Sprintf(format, width), str) } } diff --git a/examples/complex/main.go b/examples/complex/main.go index 1600abc..7408caf 100644 --- a/examples/complex/main.go +++ b/examples/complex/main.go @@ -53,7 +53,7 @@ mpb.PrependDecorators( decor.StaticName(task, len(task)+1, decor.DidentRight), decor.OnComplete(decor.StaticName(job, 0, decor.DSyncSpaceR), "done!", 0, decor.DSyncSpaceR), - decor.OnComplete(decor.ETA(0, decor.DwidthSync|decor.DslowMotion), "", 0, decor.DwidthSync), + decor.OnComplete(decor.ETA(0, decor.DwidthSync), "", 0, decor.DwidthSync), ), mpb.AppendDecorators( decor.OnComplete(decor.Percentage(5, 0), "", 0, 0),