diff --git a/decor/decorators.go b/decor/decorators.go index 2f178a8..8ada6cc 100644 --- a/decor/decorators.go +++ b/decor/decorators.go @@ -46,27 +46,43 @@ // DecoratorFunc is a function that can be prepended and appended to the progress bar type DecoratorFunc func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string -// StaticName to be used, when there is no plan to change the name during whole -// life of a progress rendering process -func StaticName(name string, minWidth int, conf byte) DecoratorFunc { - nameFn := func(s *Statistics) string { +// OnComplete wraps provided decorator `fn` with on complete event `message`. +// If you set `DwidthSync` bit in `conf` param, `minWidth` is ignored. +// `DwidthSync` is effective with multiple bars only, if set decorator will participate +// in width synchronization process with other decorators in the same column group. +func OnComplete(fn DecoratorFunc, message string, minWidth, conf int) DecoratorFunc { + msgDecorator := StaticName(message, minWidth, conf) + return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string { + if s.Completed { + return msgDecorator(s, widthAccumulator, widthDistributor) + } + return fn(s, widthAccumulator, widthDistributor) + } +} + +// StaticName is a simple name/message decorator. +// If you set `DwidthSync` bit in `conf` param, `minWidth` is ignored. +// `DwidthSync` is effective with multiple bars only, if set decorator will participate +// in width synchronization process with other decorators in the same column group. +func StaticName(name string, minWidth, conf int) DecoratorFunc { + nameFn := func(*Statistics) string { return name } return DynamicName(nameFn, minWidth, conf) } -// DynamicName to be used, when there is a plan to change the name once or -// several times during progress rendering process. If there're more than one -// bar, and you'd like to synchronize column width, conf param should have -// DwidthSync bit set. -func DynamicName(nameFn func(*Statistics) string, minWidth int, conf byte) DecoratorFunc { - format := "%%" - if (conf & DidentRight) != 0 { - format += "-" - } - format += "%ds" - return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string { - name := nameFn(s) +// DynamicName is a name/message decorator, with ability to change message via provided `messageFn`. +// If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored. +// `DwidthSync` is effective with multiple bars only, if set decorator will participate +// in width synchronization process with other decorators in the same column group. +func DynamicName(messageFn func(*Statistics) string, minWidth, conf int) DecoratorFunc { + format := "%%" + if (conf & DidentRight) != 0 { + format += "-" + } + format += "%ds" + return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string { + name := messageFn(s) if (conf & DwidthSync) != 0 { widthAccumulator <- utf8.RuneCountInString(name) max := <-widthDistributor @@ -80,28 +96,46 @@ } // CountersNoUnit returns raw counters decorator -func CountersNoUnit(pairFormat string, minWidth int, conf byte) DecoratorFunc { +// If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored. +// `DwidthSync` is effective with multiple bars only, if set decorator will participate +// in width synchronization process with other decorators in the same column group. +func CountersNoUnit(pairFormat string, minWidth, conf int) DecoratorFunc { return Counters(pairFormat, 0, minWidth, conf) } -// CountersKibiByte returns human friendly byte counters decorator, where -// counters unit is multiple by 1024. -func CountersKibiByte(pairFormat string, minWidth int, conf byte) DecoratorFunc { +// CountersKibiByte returns human friendly byte counters decorator, +// where counters unit is multiple by 1024. +// `pairFormat` must contain two printf compatible verbs, like "%f" or "%d". +// First verb substituted with Current, second one with Total. +// Example: `"%.1f / %.1f" = "1.0MiB / 12.0MiB"` or `"% .1f / % .1f" = "1.0 MiB / 12.0 MiB"`. +// If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored. +// `DwidthSync` is effective with multiple bars only, if set decorator will participate +// in width synchronization process with other decorators in the same column group. +func CountersKibiByte(pairFormat string, minWidth, conf int) DecoratorFunc { return Counters(pairFormat, Unit_KiB, minWidth, conf) } // CountersKiloByte returns human friendly byte counters decorator, where // counters unit is multiple by 1000. -func CountersKiloByte(pairFormat string, minWidth int, conf byte) DecoratorFunc { +// `pairFormat` must contain two printf compatible verbs, like "%f" or "%d". +// First verb substituted with Current, second one with Total. +// Example: `"%.1f / %.1f" = "1.0MiB / 12.0MiB"` or `"% .1f / % .1f" = "1.0 MiB / 12.0 MiB"`. +// If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored. +// `DwidthSync` is effective with multiple bars only, if set decorator will participate +// in width synchronization process with other decorators in the same column group. +func CountersKiloByte(pairFormat string, minWidth, conf int) DecoratorFunc { return Counters(pairFormat, Unit_kB, minWidth, conf) } // Counters provides basic counters decorator. -// pairFormat must contain two printf compatible verbs, like "%f" or "%d". -// First verb substituted with Current, second one with Total. For example (assuming decor.Unit_KiB used): -// "%.1f / %.1f" = "1.0MiB / 12.0MiB" or "% .1f / % .1f" = "1.0 MiB / 12.0 MiB" -// unit is one of decor.Unit_KiB/decor.Unit_kB or just zero if you need raw unitless numbers. -func Counters(pairFormat string, unit Unit, minWidth int, conf byte) DecoratorFunc { +// `pairFormat` must contain two printf compatible verbs, like "%f" or "%d". +// First verb substituted with Current, second one with Total. +// Example: `"%.1f / %.1f" = "1.0MiB / 12.0MiB"` or `"% .1f / % .1f" = "1.0 MiB / 12.0 MiB"`. +// `unit` is one of decor.Unit_KiB/decor.Unit_kB or just zero if you need raw unitless numbers. +// If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored. +// `DwidthSync` is effective with multiple bars only, if set decorator will participate +// in width synchronization process with other decorators in the same column group. +func Counters(pairFormat string, unit Unit, minWidth, conf int) DecoratorFunc { format := "%%" if (conf & DidentRight) != 0 { format += "-" @@ -130,9 +164,10 @@ } // ETA provides exponential-weighted-moving-average ETA decorator. -// If there're more than one bar, and you'd like to synchronize column width, -// conf param should have DwidthSync bit set. -func ETA(minWidth int, conf byte) DecoratorFunc { +// If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored. +// `DwidthSync` is effective with multiple bars only, if set decorator will participate +// in width synchronization process with other decorators in the same column group. +func ETA(minWidth, conf int) DecoratorFunc { format := "%%" if (conf & DidentRight) != 0 { format += "-" @@ -153,9 +188,10 @@ } // Elapsed provides elapsed time decorator. -// If there're more than one bar, and you'd like to synchronize column width, -// conf param should have DwidthSync bit set. -func Elapsed(minWidth int, conf byte) DecoratorFunc { +// If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored. +// `DwidthSync` is effective with multiple bars only, if set decorator will participate +// in width synchronization process with other decorators in the same column group. +func Elapsed(minWidth, conf int) DecoratorFunc { format := "%%" if (conf & DidentRight) != 0 { format += "-" @@ -176,9 +212,10 @@ } // Percentage provides percentage decorator. -// If there're more than one bar, and you'd like to synchronize column width, -// conf param should have DwidthSync bit set. -func Percentage(minWidth int, conf byte) DecoratorFunc { +// If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored. +// `DwidthSync` is effective with multiple bars only, if set decorator will participate +// in width synchronization process with other decorators in the same column group. +func Percentage(minWidth, conf int) DecoratorFunc { format := "%%" if (conf & DidentRight) != 0 { format += "-" diff --git a/examples/prependETA/main.go b/examples/prependETA/main.go index af43658..60b75e5 100644 --- a/examples/prependETA/main.go +++ b/examples/prependETA/main.go @@ -28,8 +28,8 @@ } b := p.AddBar(int64(total), mpb.PrependDecorators( - decor.StaticName(name, 0, decor.DwidthSync|decor.DidentRight), - decor.ETA(4, decor.DSyncSpace), + decor.StaticName(name, 0, decor.DwidthSync), + decor.OnComplete(decor.ETA(4, 0), "Done", 0, decor.DSyncSpace), ), mpb.AppendDecorators( decor.Percentage(5, 0), @@ -46,5 +46,4 @@ } p.Wait() - fmt.Println("done") }