diff --git a/decor/speed.go b/decor/speed.go index 39b4691..f743766 100644 --- a/decor/speed.go +++ b/decor/speed.go @@ -22,8 +22,8 @@ // Note that it's necessary to supply bar.Incr* methods with incremental // work duration as second argument, in order for this decorator to // work correctly. This decorator is a wrapper of MovingAverageSpeed. -func EwmaSpeed(unit int, fmt string, age float64, wcc ...WC) Decorator { - return MovingAverageSpeed(unit, fmt, ewma.NewMovingAverage(age), wcc...) +func EwmaSpeed(unit int, format string, age float64, wcc ...WC) Decorator { + return MovingAverageSpeed(unit, format, ewma.NewMovingAverage(age), wcc...) } // MovingAverageSpeed decorator relies on MovingAverage implementation @@ -31,41 +31,52 @@ // // `unit` one of [0|UnitKiB|UnitKB] zero for no unit // -// `fmt` printf compatible verb for value, like "%f" or "%d" +// `format` printf compatible verb for value, like "%f" or "%d" // // `average` MovingAverage implementation // // `wcc` optional WC config // -// fmt examples: +// format examples: // -// unit=UnitKiB, fmt="%.1f" output: "1.0MiB/s" -// unit=UnitKiB, fmt="% .1f" output: "1.0 MiB/s" -// unit=UnitKB, fmt="%.1f" output: "1.0MB/s" -// unit=UnitKB, fmt="% .1f" output: "1.0 MB/s" +// unit=UnitKiB, format="%.1f" output: "1.0MiB/s" +// unit=UnitKiB, format="% .1f" output: "1.0 MiB/s" +// unit=UnitKB, format="%.1f" output: "1.0MB/s" +// unit=UnitKB, format="% .1f" output: "1.0 MB/s" // -func MovingAverageSpeed(unit int, fmt string, average MovingAverage, wcc ...WC) Decorator { +func MovingAverageSpeed(unit int, format string, average MovingAverage, wcc ...WC) Decorator { var wc WC for _, widthConf := range wcc { wc = widthConf } + if format == "" { + format = "%.0f" + } wc.Init() - if fmt == "" { - fmt = "%.0f" - } d := &movingAverageSpeed{ WC: wc, - unit: unit, - fmt: fmt, average: average, + } + switch unit { + case UnitKiB: + d.producer = func(speed float64) string { + return fmt.Sprintf(format, &speedType{SizeB1024(math.Round(speed))}) + } + case UnitKB: + d.producer = func(speed float64) string { + return fmt.Sprintf(format, &speedType{SizeB1000(math.Round(speed))}) + } + default: + d.producer = func(speed float64) string { + return fmt.Sprintf(format, speed) + } } return d } type movingAverageSpeed struct { WC - unit int - fmt string + producer func(float64) string average ewma.MovingAverage msg string completeMsg *string @@ -79,18 +90,7 @@ return d.FormatMsg(d.msg) } - speed := d.average.Value() - - var val interface{} - switch d.unit { - case UnitKiB: - val = &speedType{SizeB1024(math.Round(speed))} - case UnitKB: - val = &speedType{SizeB1000(math.Round(speed))} - default: - val = speed - } - d.msg = fmt.Sprintf(d.fmt, val) + d.msg = d.producer(d.average.Value()) return d.FormatMsg(d.msg) } @@ -113,8 +113,8 @@ // AverageSpeed decorator with dynamic unit measure adjustment. It's // a wrapper of NewAverageSpeed. -func AverageSpeed(unit int, fmt string, wcc ...WC) Decorator { - return NewAverageSpeed(unit, fmt, time.Now(), wcc...) +func AverageSpeed(unit int, format string, wcc ...WC) Decorator { + return NewAverageSpeed(unit, format, time.Now(), wcc...) } // NewAverageSpeed decorator with dynamic unit measure adjustment and @@ -122,42 +122,53 @@ // // `unit` one of [0|UnitKiB|UnitKB] zero for no unit // -// `fmt` printf compatible verb for value, like "%f" or "%d" +// `format` printf compatible verb for value, like "%f" or "%d" // // `startTime` start time // // `wcc` optional WC config // -// fmt examples: +// format examples: // -// unit=UnitKiB, fmt="%.1f" output: "1.0MiB/s" -// unit=UnitKiB, fmt="% .1f" output: "1.0 MiB/s" -// unit=UnitKB, fmt="%.1f" output: "1.0MB/s" -// unit=UnitKB, fmt="% .1f" output: "1.0 MB/s" +// unit=UnitKiB, format="%.1f" output: "1.0MiB/s" +// unit=UnitKiB, format="% .1f" output: "1.0 MiB/s" +// unit=UnitKB, format="%.1f" output: "1.0MB/s" +// unit=UnitKB, format="% .1f" output: "1.0 MB/s" // -func NewAverageSpeed(unit int, fmt string, startTime time.Time, wcc ...WC) Decorator { +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" + } wc.Init() - if fmt == "" { - fmt = "%.0f" - } d := &averageSpeed{ WC: wc, - unit: unit, startTime: startTime, - fmt: fmt, + } + switch unit { + case UnitKiB: + d.producer = func(speed float64) string { + return fmt.Sprintf(format, &speedType{SizeB1024(math.Round(speed))}) + } + case UnitKB: + d.producer = func(speed float64) string { + return fmt.Sprintf(format, &speedType{SizeB1000(math.Round(speed))}) + } + default: + d.producer = func(speed float64) string { + return fmt.Sprintf(format, speed) + } } return d } type averageSpeed struct { WC - unit int startTime time.Time - fmt string + producer func(float64) string msg string completeMsg *string } @@ -170,19 +181,8 @@ return d.FormatMsg(d.msg) } - timeElapsed := time.Since(d.startTime) - speed := float64(st.Current) / timeElapsed.Seconds() - - var val interface{} - switch d.unit { - case UnitKiB: - val = &speedType{SizeB1024(math.Round(speed))} - case UnitKB: - val = &speedType{SizeB1000(math.Round(speed))} - default: - val = speed - } - d.msg = fmt.Sprintf(d.fmt, val) + speed := float64(st.Current) / time.Since(d.startTime).Seconds() + d.msg = d.producer(speed) return d.FormatMsg(d.msg) }