Codebase list golang-github-vbauerster-mpb / 2489bb4
refactoring counters decorator Vladimir Bauer 6 years ago
2 changed file(s) with 44 addition(s) and 43 deletion(s). Raw diff Collapse all Expand all
3030 type CounterKiB int64
3131
3232 func (c CounterKiB) Format(st fmt.State, verb rune) {
33 prec, ok := st.Precision()
34
35 if verb == 'd' || !ok {
36 prec = 0
37 }
38 if verb == 'f' && !ok {
39 prec = 6
40 }
41 // retain old beahavior if s verb used
42 if verb == 's' {
43 prec = 1
33 var prec int
34 switch verb {
35 case 'd':
36 case 's':
37 prec = -1
38 default:
39 if p, ok := st.Precision(); ok {
40 prec = p
41 } else {
42 prec = 6
43 }
4444 }
4545
4646 var res, unit string
8484 type CounterKB int64
8585
8686 func (c CounterKB) Format(st fmt.State, verb rune) {
87 prec, ok := st.Precision()
88
89 if verb == 'd' || !ok {
90 prec = 0
91 }
92 if verb == 'f' && !ok {
93 prec = 6
94 }
95 // retain old beahavior if s verb used
96 if verb == 's' {
97 prec = 1
87 var prec int
88 switch verb {
89 case 'd':
90 case 's':
91 prec = -1
92 default:
93 if p, ok := st.Precision(); ok {
94 prec = p
95 } else {
96 prec = 6
97 }
9898 }
9999
100100 var res, unit string
136136 }
137137
138138 // CountersNoUnit is a wrapper around Counters with no unit param.
139 func CountersNoUnit(pairFormat string, wcc ...WC) Decorator {
140 return Counters(0, pairFormat, wcc...)
139 func CountersNoUnit(pairFmt string, wcc ...WC) Decorator {
140 return Counters(0, pairFmt, wcc...)
141141 }
142142
143143 // CountersKibiByte is a wrapper around Counters with predefined unit
144144 // UnitKiB (bytes/1024).
145 func CountersKibiByte(pairFormat string, wcc ...WC) Decorator {
146 return Counters(UnitKiB, pairFormat, wcc...)
145 func CountersKibiByte(pairFmt string, wcc ...WC) Decorator {
146 return Counters(UnitKiB, pairFmt, wcc...)
147147 }
148148
149149 // CountersKiloByte is a wrapper around Counters with predefined unit
150150 // UnitKB (bytes/1000).
151 func CountersKiloByte(pairFormat string, wcc ...WC) Decorator {
152 return Counters(UnitKB, pairFormat, wcc...)
151 func CountersKiloByte(pairFmt string, wcc ...WC) Decorator {
152 return Counters(UnitKB, pairFmt, wcc...)
153153 }
154154
155155 // Counters decorator with dynamic unit measure adjustment.
156156 //
157157 // `unit` one of [0|UnitKiB|UnitKB] zero for no unit
158158 //
159 // `pairFormat` printf compatible verbs for current and total, like "%f" or "%d"
159 // `pairFmt` printf compatible verbs for current and total, like "%f" or "%d"
160160 //
161161 // `wcc` optional WC config
162162 //
163 // pairFormat example if UnitKB is chosen:
163 // pairFmt example if UnitKB is chosen:
164164 //
165165 // "%.1f / %.1f" = "1.0MB / 12.0MB" or "% .1f / % .1f" = "1.0 MB / 12.0 MB"
166 func Counters(unit int, pairFormat string, wcc ...WC) Decorator {
166 //
167 func Counters(unit int, pairFmt string, wcc ...WC) Decorator {
167168 var wc WC
168169 for _, widthConf := range wcc {
169170 wc = widthConf
170171 }
171172 wc.Init()
172173 d := &countersDecorator{
173 WC: wc,
174 unit: unit,
175 pairFormat: pairFormat,
174 WC: wc,
175 unit: unit,
176 pairFmt: pairFmt,
176177 }
177178 return d
178179 }
180181 type countersDecorator struct {
181182 WC
182183 unit int
183 pairFormat string
184 pairFmt string
184185 completeMsg *string
185186 }
186187
192193 var str string
193194 switch d.unit {
194195 case UnitKiB:
195 str = fmt.Sprintf(d.pairFormat, CounterKiB(st.Current), CounterKiB(st.Total))
196 str = fmt.Sprintf(d.pairFmt, CounterKiB(st.Current), CounterKiB(st.Total))
196197 case UnitKB:
197 str = fmt.Sprintf(d.pairFormat, CounterKB(st.Current), CounterKB(st.Total))
198 default:
199 str = fmt.Sprintf(d.pairFormat, st.Current, st.Total)
198 str = fmt.Sprintf(d.pairFmt, CounterKB(st.Current), CounterKB(st.Total))
199 default:
200 str = fmt.Sprintf(d.pairFmt, st.Current, st.Total)
200201 }
201202
202203 return d.FormatMsg(str)
4848 "1024 %f": {1024, "%f", "1.000000KiB"},
4949 "1024 %d": {1024, "%d", "1KiB"},
5050 "1024 %.1f": {1024, "%.1f", "1.0KiB"},
51 "1024 %s": {1024, "%s", "1.0KiB"},
51 "1024 %s": {1024, "%s", "1KiB"},
5252 "3*MiB+140KiB %f": {3*MiB + 140*KiB, "%f", "3.136719MiB"},
5353 "3*MiB+140KiB %d": {3*MiB + 140*KiB, "%d", "3MiB"},
5454 "3*MiB+140KiB %.1f": {3*MiB + 140*KiB, "%.1f", "3.1MiB"},
55 "3*MiB+140KiB %s": {3*MiB + 140*KiB, "%s", "3.1MiB"},
55 "3*MiB+140KiB %s": {3*MiB + 140*KiB, "%s", "3.13671875MiB"},
5656 "2*GiB %f": {2 * GiB, "%f", "2.000000GiB"},
5757 "2*GiB %d": {2 * GiB, "%d", "2GiB"},
5858 "2*GiB %.1f": {2 * GiB, "%.1f", "2.0GiB"},
59 "2*GiB %s": {2 * GiB, "%s", "2.0GiB"},
59 "2*GiB %s": {2 * GiB, "%s", "2GiB"},
6060 "4*TiB %f": {4 * TiB, "%f", "4.000000TiB"},
6161 "4*TiB %d": {4 * TiB, "%d", "4TiB"},
6262 "4*TiB %.1f": {4 * TiB, "%.1f", "4.0TiB"},
63 "4*TiB %s": {4 * TiB, "%s", "4.0TiB"},
63 "4*TiB %s": {4 * TiB, "%s", "4TiB"},
6464 }
6565 for name, tc := range cases {
6666 t.Run(name, func(t *testing.T) {