Codebase list golang-github-vbauerster-mpb / 81f4b4a
default decorator's formats Vladimir Bauer 6 years ago
4 changed file(s) with 42 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
4444 wc = widthConf
4545 }
4646 wc.Init()
47 if pairFmt == "" {
48 pairFmt = "%d / %d"
49 }
4750 d := &countersDecorator{
4851 WC: wc,
4952 unit: unit,
2424 }
2525 }
2626
27 res := strconv.FormatFloat(float64(s), 'f', prec, 64)
27 var b strings.Builder
28 b.WriteString(strconv.FormatFloat(float64(s), 'f', prec, 64))
2829
2930 if st.Flag(' ') {
30 res += " "
31 b.WriteString(" ")
3132 }
32 res += "%"
33 b.WriteString("%")
3334
3435 if w, ok := st.Width(); ok {
35 if len(res) < w {
36 pad := strings.Repeat(" ", w-len(res))
36 if l := b.Len(); l < w {
37 pad := strings.Repeat(" ", w-l)
3738 if st.Flag('-') {
38 res += pad
39 b.WriteString(pad)
3940 } else {
40 res = pad + res
41 tmp := b.String()
42 b.Reset()
43 b.WriteString(pad)
44 b.WriteString(tmp)
4145 }
4246 }
4347 }
4448
45 io.WriteString(st, res)
49 io.WriteString(st, b.String())
4650 }
4751
4852 // Percentage returns percentage decorator. It's a wrapper of NewPercentage.
6569 wc = widthConf
6670 }
6771 wc.Init()
72 if fmt == "" {
73 fmt = "% d"
74 }
6875 d := &percentageDecorator{
6976 WC: wc,
7077 fmt: fmt,
3939 wc = widthConf
4040 }
4141 wc.Init()
42 switch unit {
43 case UnitKiB, UnitKB:
42 if fmt == "" {
43 fmt = "%.0f"
44 }
45 if unit > 0 {
4446 fmt += "/s"
4547 }
4648 d := &movingAverageSpeed{
129131 wc = widthConf
130132 }
131133 wc.Init()
132 switch unit {
133 case UnitKiB, UnitKB:
134 if fmt == "" {
135 fmt = "%.0f"
136 }
137 if unit > 0 {
134138 fmt += "/s"
135139 }
136140 d := &averageSpeed{
1414 expected string
1515 }{
1616 {
17 name: "empty fmt",
18 unit: UnitKiB,
19 fmt: "",
20 current: 0,
21 elapsed: time.Second,
22 expected: "0b/s",
23 },
24 {
1725 name: "UnitKiB:%d:0b",
1826 unit: UnitKiB,
1927 fmt: "%d",
134142 expected string
135143 }{
136144 {
145 name: "empty fmt",
146 unit: UnitKB,
147 fmt: "",
148 current: 0,
149 elapsed: time.Second,
150 expected: "0b/s",
151 },
152 {
137153 name: "UnitKB:%d:0b",
138154 unit: UnitKB,
139155 fmt: "%d",