diff --git a/decor/counters.go b/decor/counters.go index 32bcdf7..297bf93 100644 --- a/decor/counters.go +++ b/decor/counters.go @@ -43,24 +43,7 @@ // pairFmt="% d / % d" output: "1 MB / 12 MB" // func Counters(unit int, pairFmt string, wcc ...WC) Decorator { - var wc WC - for _, widthConf := range wcc { - wc = widthConf - } - d := &countersDecorator{ - WC: wc.Init(), - producer: chooseSizeProducer(unit, pairFmt), - } - return d -} - -type countersDecorator struct { - WC - producer func(*Statistics) string -} - -func (d *countersDecorator) Decor(st *Statistics) string { - return d.FormatMsg(d.producer(st)) + return Any(chooseSizeProducer(unit, pairFmt), wcc...) } func chooseSizeProducer(unit int, format string) func(*Statistics) string { @@ -69,16 +52,16 @@ } switch unit { case UnitKiB: - return func(st *Statistics) string { - return fmt.Sprintf(format, SizeB1024(st.Current), SizeB1024(st.Total)) + return func(s *Statistics) string { + return fmt.Sprintf(format, SizeB1024(s.Current), SizeB1024(s.Total)) } case UnitKB: - return func(st *Statistics) string { - return fmt.Sprintf(format, SizeB1000(st.Current), SizeB1000(st.Total)) + return func(s *Statistics) string { + return fmt.Sprintf(format, SizeB1000(s.Current), SizeB1000(s.Total)) } default: - return func(st *Statistics) string { - return fmt.Sprintf(format, st.Current, st.Total) + return func(s *Statistics) string { + return fmt.Sprintf(format, s.Current, s.Total) } } } diff --git a/decor/elapsed.go b/decor/elapsed.go index ac28731..c9999a3 100644 --- a/decor/elapsed.go +++ b/decor/elapsed.go @@ -9,6 +9,7 @@ // `style` one of [ET_STYLE_GO|ET_STYLE_HHMMSS|ET_STYLE_HHMM|ET_STYLE_MMSS] // // `wcc` optional WC config +// func Elapsed(style TimeStyle, wcc ...WC) Decorator { return NewElapsed(style, time.Now(), wcc...) } @@ -20,29 +21,15 @@ // `startTime` start time // // `wcc` optional WC config +// func NewElapsed(style TimeStyle, startTime time.Time, wcc ...WC) Decorator { - var wc WC - for _, widthConf := range wcc { - wc = widthConf + var msg string + producer := chooseTimeProducer(style) + f := func(s *Statistics) string { + if !s.Completed { + msg = producer(time.Since(startTime)) + } + return msg } - d := &elapsedDecorator{ - WC: wc.Init(), - startTime: startTime, - producer: chooseTimeProducer(style), - } - return d + return Any(f, wcc...) } - -type elapsedDecorator struct { - WC - startTime time.Time - producer func(time.Duration) string - msg string -} - -func (d *elapsedDecorator) Decor(st *Statistics) string { - if !st.Completed { - d.msg = d.producer(time.Since(d.startTime)) - } - return d.FormatMsg(d.msg) -} diff --git a/decor/eta.go b/decor/eta.go index 818cded..5955f1e 100644 --- a/decor/eta.go +++ b/decor/eta.go @@ -45,13 +45,10 @@ // `normalizer` available implementations are [FixedIntervalTimeNormalizer|MaxTolerateTimeNormalizer] // // `wcc` optional WC config +// func MovingAverageETA(style TimeStyle, average MovingAverage, normalizer TimeNormalizer, wcc ...WC) Decorator { - var wc WC - for _, widthConf := range wcc { - wc = widthConf - } d := &movingAverageETA{ - WC: wc.Init(), + WC: initWC(wcc...), average: average, normalizer: normalizer, producer: chooseTimeProducer(style), @@ -66,9 +63,9 @@ producer func(time.Duration) string } -func (d *movingAverageETA) Decor(st *Statistics) string { +func (d *movingAverageETA) Decor(s *Statistics) string { v := math.Round(d.average.Value()) - remaining := time.Duration((st.Total - st.Current) * int64(v)) + remaining := time.Duration((s.Total - s.Current) * int64(v)) if d.normalizer != nil { remaining = d.normalizer.Normalize(remaining) } @@ -92,6 +89,7 @@ // `style` one of [ET_STYLE_GO|ET_STYLE_HHMMSS|ET_STYLE_HHMM|ET_STYLE_MMSS] // // `wcc` optional WC config +// func AverageETA(style TimeStyle, wcc ...WC) Decorator { return NewAverageETA(style, time.Now(), nil, wcc...) } @@ -105,13 +103,10 @@ // `normalizer` available implementations are [FixedIntervalTimeNormalizer|MaxTolerateTimeNormalizer] // // `wcc` optional WC config +// func NewAverageETA(style TimeStyle, startTime time.Time, normalizer TimeNormalizer, wcc ...WC) Decorator { - var wc WC - for _, widthConf := range wcc { - wc = widthConf - } d := &averageETA{ - WC: wc.Init(), + WC: initWC(wcc...), startTime: startTime, normalizer: normalizer, producer: chooseTimeProducer(style), @@ -126,12 +121,12 @@ producer func(time.Duration) string } -func (d *averageETA) Decor(st *Statistics) string { +func (d *averageETA) Decor(s *Statistics) string { var remaining time.Duration - if st.Current != 0 { - durPerItem := float64(time.Since(d.startTime)) / float64(st.Current) + if s.Current != 0 { + durPerItem := float64(time.Since(d.startTime)) / float64(s.Current) durPerItem = math.Round(durPerItem) - remaining = time.Duration((st.Total - st.Current) * int64(durPerItem)) + remaining = time.Duration((s.Total - s.Current) * int64(durPerItem)) if d.normalizer != nil { remaining = d.normalizer.Normalize(remaining) } diff --git a/decor/merge.go b/decor/merge.go index 7238692..520f13a 100644 --- a/decor/merge.go +++ b/decor/merge.go @@ -64,8 +64,8 @@ return d.Decorator } -func (d *mergeDecorator) Decor(st *Statistics) string { - msg := d.Decorator.Decor(st) +func (d *mergeDecorator) Decor(s *Statistics) string { + msg := d.Decorator.Decor(s) msgLen := utf8.RuneCountInString(msg) if (d.wc.C & DextraSpace) != 0 { msgLen++ @@ -101,6 +101,6 @@ WC } -func (d *placeHolderDecorator) Decor(_ *Statistics) string { +func (d *placeHolderDecorator) Decor(*Statistics) string { return "" } diff --git a/decor/name.go b/decor/name.go index 2d5865f..a7d477e 100644 --- a/decor/name.go +++ b/decor/name.go @@ -1,27 +1,12 @@ package decor -// Name returns name decorator. +// Name decorator displays text that is set once and can't be changed +// during decorator's lifetime. // -// `name` string to display +// `str` string to display // // `wcc` optional WC config -func Name(name string, wcc ...WC) Decorator { - var wc WC - for _, widthConf := range wcc { - wc = widthConf - } - d := &nameDecorator{ - WC: wc.Init(), - msg: name, - } - return d +// +func Name(str string, wcc ...WC) Decorator { + return Any(func(*Statistics) string { return str }, wcc...) } - -type nameDecorator struct { - WC - msg string -} - -func (d *nameDecorator) Decor(st *Statistics) string { - return d.FormatMsg(d.msg) -} diff --git a/decor/on_complete.go b/decor/on_complete.go index 714a0de..6640ecb 100644 --- a/decor/on_complete.go +++ b/decor/on_complete.go @@ -23,12 +23,12 @@ msg string } -func (d *onCompleteWrapper) Decor(st *Statistics) string { - if st.Completed { +func (d *onCompleteWrapper) Decor(s *Statistics) string { + if s.Completed { wc := d.GetConf() return wc.FormatMsg(d.msg) } - return d.Decorator.Decor(st) + return d.Decorator.Decor(s) } func (d *onCompleteWrapper) Base() Decorator { diff --git a/decor/percentage.go b/decor/percentage.go index abf343a..efb2f3e 100644 --- a/decor/percentage.go +++ b/decor/percentage.go @@ -37,36 +37,22 @@ return NewPercentage("% d", wcc...) } -// NewPercentage percentage decorator with custom fmt string. +// NewPercentage percentage decorator with custom format string. // -// fmt examples: +// format examples: // -// fmt="%.1f" output: "1.0%" -// fmt="% .1f" output: "1.0 %" -// fmt="%d" output: "1%" -// fmt="% d" output: "1 %" +// format="%.1f" output: "1.0%" +// format="% .1f" output: "1.0 %" +// format="%d" output: "1%" +// format="% d" output: "1 %" // -func NewPercentage(fmt string, wcc ...WC) Decorator { - var wc WC - for _, widthConf := range wcc { - wc = widthConf +func NewPercentage(format string, wcc ...WC) Decorator { + if format == "" { + format = "% d" } - if fmt == "" { - fmt = "% d" + f := func(s *Statistics) string { + p := internal.Percentage(s.Total, s.Current, 100) + return fmt.Sprintf(format, percentageType(p)) } - d := &percentageDecorator{ - WC: wc.Init(), - fmt: fmt, - } - return d + return Any(f, wcc...) } - -type percentageDecorator struct { - WC - fmt string -} - -func (d *percentageDecorator) Decor(st *Statistics) string { - p := internal.Percentage(st.Total, st.Current, 100) - return d.FormatMsg(fmt.Sprintf(d.fmt, percentageType(p))) -} diff --git a/decor/speed.go b/decor/speed.go index 795a553..7822d5d 100644 --- a/decor/speed.go +++ b/decor/speed.go @@ -52,15 +52,11 @@ // unit=UnitKB, format="% .1f" output: "1.0 MB/s" // func MovingAverageSpeed(unit int, format string, average MovingAverage, wcc ...WC) Decorator { - var wc WC - for _, widthConf := range wcc { - wc = widthConf - } if format == "" { format = "%.0f" } d := &movingAverageSpeed{ - WC: wc.Init(), + WC: initWC(wcc...), average: average, producer: chooseSpeedProducer(unit, format), } @@ -74,8 +70,8 @@ msg string } -func (d *movingAverageSpeed) Decor(st *Statistics) string { - if !st.Completed { +func (d *movingAverageSpeed) Decor(s *Statistics) string { + if !s.Completed { var speed float64 if v := d.average.Value(); v > 0 { speed = 1 / v @@ -122,15 +118,11 @@ // unit=UnitKB, format="% .1f" output: "1.0 MB/s" // func NewAverageSpeed(unit int, format string, startTime time.Time, wcc ...WC) Decorator { - var wc WC - for _, widthConf := range wcc { - wc = widthConf - } if format == "" { format = "%.0f" } d := &averageSpeed{ - WC: wc.Init(), + WC: initWC(wcc...), startTime: startTime, producer: chooseSpeedProducer(unit, format), } @@ -144,9 +136,9 @@ msg string } -func (d *averageSpeed) Decor(st *Statistics) string { - if !st.Completed { - speed := float64(st.Current) / float64(time.Since(d.startTime)) +func (d *averageSpeed) Decor(s *Statistics) string { + if !s.Completed { + speed := float64(s.Current) / float64(time.Since(d.startTime)) d.msg = d.producer(speed * 1e9) } diff --git a/decor/spinner.go b/decor/spinner.go index 24f5531..abfb2f7 100644 --- a/decor/spinner.go +++ b/decor/spinner.go @@ -8,28 +8,14 @@ // // `wcc` optional WC config func Spinner(frames []string, wcc ...WC) Decorator { - var wc WC - for _, widthConf := range wcc { - wc = widthConf - } if len(frames) == 0 { frames = defaultSpinnerStyle } - d := &spinnerDecorator{ - WC: wc.Init(), - frames: frames, + var count uint + f := func(s *Statistics) string { + frame := frames[count%uint(len(frames))] + count++ + return frame } - return d + return Any(f, wcc...) } - -type spinnerDecorator struct { - WC - frames []string - count uint -} - -func (d *spinnerDecorator) Decor(st *Statistics) string { - frame := d.frames[d.count%uint(len(d.frames))] - d.count++ - return d.FormatMsg(frame) -}