FmtAsSpeed
Vladimir Bauer
6 years ago
| 8 | 8 | "github.com/VividCortex/ewma" |
| 9 | 9 | ) |
| 10 | 10 | |
| 11 | // SpeedFormatter is wrapper for SizeB1024 and SizeB1000 to format value as speed/s. | |
| 12 | type SpeedFormatter struct { | |
| 11 | // FmtAsSpeed adds "/s" to the end of the input formatter. To be | |
| 12 | // used with SizeB1000 or SizeB1024 types, for example: | |
| 13 | // | |
| 14 | // fmt.Printf("%.1f", FmtAsSpeed(SizeB1024(2048))) | |
| 15 | // | |
| 16 | func FmtAsSpeed(input fmt.Formatter) fmt.Formatter { | |
| 17 | return &speedFormatter{input} | |
| 18 | } | |
| 19 | ||
| 20 | type speedFormatter struct { | |
| 13 | 21 | fmt.Formatter |
| 14 | 22 | } |
| 15 | 23 | |
| 16 | func (self *SpeedFormatter) Format(st fmt.State, verb rune) { | |
| 24 | func (self *speedFormatter) Format(st fmt.State, verb rune) { | |
| 17 | 25 | self.Formatter.Format(st, verb) |
| 18 | 26 | io.WriteString(st, "/s") |
| 19 | 27 | } |
| 152 | 160 | switch unit { |
| 153 | 161 | case UnitKiB: |
| 154 | 162 | return func(speed float64) string { |
| 155 | return fmt.Sprintf(format, &SpeedFormatter{SizeB1024(math.Round(speed))}) | |
| 163 | return fmt.Sprintf(format, FmtAsSpeed(SizeB1024(math.Round(speed)))) | |
| 156 | 164 | } |
| 157 | 165 | case UnitKB: |
| 158 | 166 | return func(speed float64) string { |
| 159 | return fmt.Sprintf(format, &SpeedFormatter{SizeB1000(math.Round(speed))}) | |
| 167 | return fmt.Sprintf(format, FmtAsSpeed(SizeB1000(math.Round(speed)))) | |
| 160 | 168 | } |
| 161 | 169 | default: |
| 162 | 170 | return func(speed float64) string { |