From f40dc731e9d7e3d452d03c632d99ef9577d1fb2d Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Thu, 27 May 2021 22:03:11 +0200 Subject: [PATCH] refactor: improved humanNumber and humanSI This hides "(n)" when value is same as SI, and makes SI "3k" (instead of "3 k") --- core/commands/stat_provide.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/core/commands/stat_provide.go b/core/commands/stat_provide.go index 86fe96efb99..4224efcafc0 100644 --- a/core/commands/stat_provide.go +++ b/core/commands/stat_provide.go @@ -55,12 +55,10 @@ This interface is not stable and may change from release to release. wtr := tabwriter.NewWriter(w, 1, 2, 1, ' ', 0) defer wtr.Flush() - tp := float64(s.TotalProvides) - fmt.Fprintf(wtr, "TotalProvides:\t%s\t(%s)\n", humanSI(tp, 0), humanFull(tp, 0)) - fmt.Fprintf(wtr, "AvgProvideDuration:\t%v\n", humanDuration(s.AvgProvideDuration)) - fmt.Fprintf(wtr, "LastReprovideDuration:\t%v\n", humanDuration(s.LastReprovideDuration)) - lrbs := float64(s.LastReprovideBatchSize) - fmt.Fprintf(wtr, "LastReprovideBatchSize:\t%s\t(%s)\n", humanSI(lrbs, 0), humanFull(lrbs, 0)) + fmt.Fprintf(wtr, "TotalProvides:\t%s\n", humanNumber(s.TotalProvides)) + fmt.Fprintf(wtr, "AvgProvideDuration:\t%s\n", humanDuration(s.AvgProvideDuration)) + fmt.Fprintf(wtr, "LastReprovideDuration:\t%s\n", humanDuration(s.LastReprovideDuration)) + fmt.Fprintf(wtr, "LastReprovideBatchSize:\t%s\n", humanNumber(s.LastReprovideBatchSize)) return nil }), }, @@ -71,9 +69,19 @@ func humanDuration(val time.Duration) string { return val.Truncate(time.Microsecond).String() } +func humanNumber(n int) string { + nf := float64(n) + str := humanSI(nf, 0) + fullStr := humanFull(nf, 0) + if str != fullStr { + return fmt.Sprintf("%s\t(%s)", str, fullStr) + } + return str +} + func humanSI(val float64, decimals int) string { v, unit := humanize.ComputeSI(val) - return humanize.SIWithDigits(v, decimals, unit) + return fmt.Sprintf("%s%s", humanFull(v, decimals), unit) } func humanFull(val float64, decimals int) string {