diff --git a/decor/counters.go b/decor/counters.go index f41b679..36709f9 100644 --- a/decor/counters.go +++ b/decor/counters.go @@ -47,33 +47,38 @@ for _, widthConf := range wcc { wc = widthConf } - if pairFmt == "" { - pairFmt = "%d / %d" - } d := &countersDecorator{ - WC: wc.Init(), - unit: unit, - pairFmt: pairFmt, + WC: wc.Init(), + producer: chooseSizeProducer(unit, pairFmt), } return d } type countersDecorator struct { WC - unit int - pairFmt string + producer func(*Statistics) string } func (d *countersDecorator) Decor(st *Statistics) string { - var res string - switch d.unit { + return d.producer(st) +} + +func chooseSizeProducer(unit int, format string) func(*Statistics) string { + if format == "" { + format = "%d / %d" + } + switch unit { case UnitKiB: - res = fmt.Sprintf(d.pairFmt, SizeB1024(st.Current), SizeB1024(st.Total)) + return func(st *Statistics) string { + return fmt.Sprintf(format, SizeB1024(st.Current), SizeB1024(st.Total)) + } case UnitKB: - res = fmt.Sprintf(d.pairFmt, SizeB1000(st.Current), SizeB1000(st.Total)) + return func(st *Statistics) string { + return fmt.Sprintf(format, SizeB1000(st.Current), SizeB1000(st.Total)) + } default: - res = fmt.Sprintf(d.pairFmt, st.Current, st.Total) + return func(st *Statistics) string { + return fmt.Sprintf(format, st.Current, st.Total) + } } - - return d.FormatMsg(res) }