Codebase list golang-github-vbauerster-mpb / bdbcd52
FmtAsSpeed Vladimir Bauer 6 years ago
1 changed file(s) with 13 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
88 "github.com/VividCortex/ewma"
99 )
1010
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 {
1321 fmt.Formatter
1422 }
1523
16 func (self *SpeedFormatter) Format(st fmt.State, verb rune) {
24 func (self *speedFormatter) Format(st fmt.State, verb rune) {
1725 self.Formatter.Format(st, verb)
1826 io.WriteString(st, "/s")
1927 }
152160 switch unit {
153161 case UnitKiB:
154162 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))))
156164 }
157165 case UnitKB:
158166 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))))
160168 }
161169 default:
162170 return func(speed float64) string {