Codebase list golang-github-vbauerster-mpb / ef0fe28
use speedType directly Vladimir Bauer 6 years ago
1 changed file(s) with 13 addition(s) and 21 deletion(s). Raw diff Collapse all Expand all
88 "github.com/VividCortex/ewma"
99 )
1010
11 const (
12 perSecond = "/s"
13 )
14
1511 type speedType struct {
16 sizeT fmt.Formatter
17 perSecond string
12 fmt.Formatter
1813 }
1914
2015 func (self *speedType) Format(st fmt.State, verb rune) {
21 self.sizeT.Format(st, verb)
22 io.WriteString(st, self.perSecond)
23 }
24
25 func speedPerSecond(sizeT fmt.Formatter) fmt.Formatter {
26 return &speedType{
27 sizeT: sizeT,
28 perSecond: perSecond,
29 }
16 self.Formatter.Format(st, verb)
17 io.WriteString(st, "/s")
3018 }
3119
3220 // EwmaSpeed exponential-weighted-moving-average based speed decorator.
9280
9381 speed := d.average.Value()
9482
83 var val interface{}
9584 switch d.unit {
9685 case UnitKiB:
97 d.msg = fmt.Sprintf(d.fmt, speedPerSecond(SizeB1024(math.Round(speed))))
86 val = &speedType{SizeB1024(math.Round(speed))}
9887 case UnitKB:
99 d.msg = fmt.Sprintf(d.fmt, speedPerSecond(SizeB1000(math.Round(speed))))
88 val = &speedType{SizeB1000(math.Round(speed))}
10089 default:
101 d.msg = fmt.Sprintf(d.fmt, speed)
90 val = speed
10291 }
92 d.msg = fmt.Sprintf(d.fmt, val)
10393
10494 return d.FormatMsg(d.msg)
10595 }
182172 timeElapsed := time.Since(d.startTime)
183173 speed := float64(st.Current) / timeElapsed.Seconds()
184174
175 var val interface{}
185176 switch d.unit {
186177 case UnitKiB:
187 d.msg = fmt.Sprintf(d.fmt, speedPerSecond(SizeB1024(math.Round(speed))))
178 val = &speedType{SizeB1024(math.Round(speed))}
188179 case UnitKB:
189 d.msg = fmt.Sprintf(d.fmt, speedPerSecond(SizeB1000(math.Round(speed))))
180 val = &speedType{SizeB1000(math.Round(speed))}
190181 default:
191 d.msg = fmt.Sprintf(d.fmt, speed)
182 val = speed
192183 }
184 d.msg = fmt.Sprintf(d.fmt, val)
193185
194186 return d.FormatMsg(d.msg)
195187 }