diff --git a/decor/counters.go b/decor/counters.go index 7d581ee..5848fcc 100644 --- a/decor/counters.go +++ b/decor/counters.go @@ -31,17 +31,17 @@ type CounterKiB int64 func (c CounterKiB) Format(st fmt.State, verb rune) { - prec, ok := st.Precision() - - if verb == 'd' || !ok { - prec = 0 - } - if verb == 'f' && !ok { - prec = 6 - } - // retain old beahavior if s verb used - if verb == 's' { - prec = 1 + var prec int + switch verb { + case 'd': + case 's': + prec = -1 + default: + if p, ok := st.Precision(); ok { + prec = p + } else { + prec = 6 + } } var res, unit string @@ -85,17 +85,17 @@ type CounterKB int64 func (c CounterKB) Format(st fmt.State, verb rune) { - prec, ok := st.Precision() - - if verb == 'd' || !ok { - prec = 0 - } - if verb == 'f' && !ok { - prec = 6 - } - // retain old beahavior if s verb used - if verb == 's' { - prec = 1 + var prec int + switch verb { + case 'd': + case 's': + prec = -1 + default: + if p, ok := st.Precision(); ok { + prec = p + } else { + prec = 6 + } } var res, unit string @@ -137,43 +137,44 @@ } // CountersNoUnit is a wrapper around Counters with no unit param. -func CountersNoUnit(pairFormat string, wcc ...WC) Decorator { - return Counters(0, pairFormat, wcc...) +func CountersNoUnit(pairFmt string, wcc ...WC) Decorator { + return Counters(0, pairFmt, wcc...) } // CountersKibiByte is a wrapper around Counters with predefined unit // UnitKiB (bytes/1024). -func CountersKibiByte(pairFormat string, wcc ...WC) Decorator { - return Counters(UnitKiB, pairFormat, wcc...) +func CountersKibiByte(pairFmt string, wcc ...WC) Decorator { + return Counters(UnitKiB, pairFmt, wcc...) } // CountersKiloByte is a wrapper around Counters with predefined unit // UnitKB (bytes/1000). -func CountersKiloByte(pairFormat string, wcc ...WC) Decorator { - return Counters(UnitKB, pairFormat, wcc...) +func CountersKiloByte(pairFmt string, wcc ...WC) Decorator { + return Counters(UnitKB, pairFmt, wcc...) } // Counters decorator with dynamic unit measure adjustment. // // `unit` one of [0|UnitKiB|UnitKB] zero for no unit // -// `pairFormat` printf compatible verbs for current and total, like "%f" or "%d" +// `pairFmt` printf compatible verbs for current and total, like "%f" or "%d" // // `wcc` optional WC config // -// pairFormat example if UnitKB is chosen: +// pairFmt example if UnitKB is chosen: // // "%.1f / %.1f" = "1.0MB / 12.0MB" or "% .1f / % .1f" = "1.0 MB / 12.0 MB" -func Counters(unit int, pairFormat string, wcc ...WC) Decorator { +// +func Counters(unit int, pairFmt string, wcc ...WC) Decorator { var wc WC for _, widthConf := range wcc { wc = widthConf } wc.Init() d := &countersDecorator{ - WC: wc, - unit: unit, - pairFormat: pairFormat, + WC: wc, + unit: unit, + pairFmt: pairFmt, } return d } @@ -181,7 +182,7 @@ type countersDecorator struct { WC unit int - pairFormat string + pairFmt string completeMsg *string } @@ -193,11 +194,11 @@ var str string switch d.unit { case UnitKiB: - str = fmt.Sprintf(d.pairFormat, CounterKiB(st.Current), CounterKiB(st.Total)) + str = fmt.Sprintf(d.pairFmt, CounterKiB(st.Current), CounterKiB(st.Total)) case UnitKB: - str = fmt.Sprintf(d.pairFormat, CounterKB(st.Current), CounterKB(st.Total)) - default: - str = fmt.Sprintf(d.pairFormat, st.Current, st.Total) + str = fmt.Sprintf(d.pairFmt, CounterKB(st.Current), CounterKB(st.Total)) + default: + str = fmt.Sprintf(d.pairFmt, st.Current, st.Total) } return d.FormatMsg(str) diff --git a/decor/counters_test.go b/decor/counters_test.go index 9988d50..591b398 100644 --- a/decor/counters_test.go +++ b/decor/counters_test.go @@ -49,19 +49,19 @@ "1024 %f": {1024, "%f", "1.000000KiB"}, "1024 %d": {1024, "%d", "1KiB"}, "1024 %.1f": {1024, "%.1f", "1.0KiB"}, - "1024 %s": {1024, "%s", "1.0KiB"}, + "1024 %s": {1024, "%s", "1KiB"}, "3*MiB+140KiB %f": {3*MiB + 140*KiB, "%f", "3.136719MiB"}, "3*MiB+140KiB %d": {3*MiB + 140*KiB, "%d", "3MiB"}, "3*MiB+140KiB %.1f": {3*MiB + 140*KiB, "%.1f", "3.1MiB"}, - "3*MiB+140KiB %s": {3*MiB + 140*KiB, "%s", "3.1MiB"}, + "3*MiB+140KiB %s": {3*MiB + 140*KiB, "%s", "3.13671875MiB"}, "2*GiB %f": {2 * GiB, "%f", "2.000000GiB"}, "2*GiB %d": {2 * GiB, "%d", "2GiB"}, "2*GiB %.1f": {2 * GiB, "%.1f", "2.0GiB"}, - "2*GiB %s": {2 * GiB, "%s", "2.0GiB"}, + "2*GiB %s": {2 * GiB, "%s", "2GiB"}, "4*TiB %f": {4 * TiB, "%f", "4.000000TiB"}, "4*TiB %d": {4 * TiB, "%d", "4TiB"}, "4*TiB %.1f": {4 * TiB, "%.1f", "4.0TiB"}, - "4*TiB %s": {4 * TiB, "%s", "4.0TiB"}, + "4*TiB %s": {4 * TiB, "%s", "4TiB"}, } for name, tc := range cases { t.Run(name, func(t *testing.T) {