SpeedFormatter
Vladimir Bauer
6 years ago
| 8 | 8 | "github.com/VividCortex/ewma" |
| 9 | 9 | ) |
| 10 | 10 | |
| 11 | type speedType struct { | |
| 11 | // SpeedFormatter is wrapper for SizeB1024 and SizeB1000 to format value as speed/s. | |
| 12 | type SpeedFormatter struct { | |
| 12 | 13 | fmt.Formatter |
| 13 | 14 | } |
| 14 | 15 | |
| 15 | func (self *speedType) Format(st fmt.State, verb rune) { | |
| 16 | func (self *SpeedFormatter) Format(st fmt.State, verb rune) { | |
| 16 | 17 | self.Formatter.Format(st, verb) |
| 17 | 18 | io.WriteString(st, "/s") |
| 18 | 19 | } |
| 182 | 183 | switch unit { |
| 183 | 184 | case UnitKiB: |
| 184 | 185 | return func(speed float64) string { |
| 185 | return fmt.Sprintf(format, &speedType{SizeB1024(math.Round(speed))}) | |
| 186 | return fmt.Sprintf(format, &SpeedFormatter{SizeB1024(math.Round(speed))}) | |
| 186 | 187 | } |
| 187 | 188 | case UnitKB: |
| 188 | 189 | return func(speed float64) string { |
| 189 | return fmt.Sprintf(format, &speedType{SizeB1000(math.Round(speed))}) | |
| 190 | return fmt.Sprintf(format, &SpeedFormatter{SizeB1000(math.Round(speed))}) | |
| 190 | 191 | } |
| 191 | 192 | default: |
| 192 | 193 | return func(speed float64) string { |