Codebase list golang-github-vbauerster-mpb / 0d4708a
refactoring counters: unit is either [0|SizeB1024(0)|SizeB1000(0)] Vladimir Bauer 3 years ago
1 changed file(s) with 94 addition(s) and 89 deletion(s). Raw diff Collapse all Expand all
1616 }
1717
1818 // CountersKibiByte is a wrapper around Counters with predefined unit
19 // UnitKiB (bytes/1024).
19 // as SizeB1024(0).
2020 func CountersKibiByte(pairFmt string, wcc ...WC) Decorator {
21 return Counters(UnitKiB, pairFmt, wcc...)
21 return Counters(SizeB1024(0), pairFmt, wcc...)
2222 }
2323
2424 // CountersKiloByte is a wrapper around Counters with predefined unit
25 // UnitKB (bytes/1000).
25 // as SizeB1000(0).
2626 func CountersKiloByte(pairFmt string, wcc ...WC) Decorator {
27 return Counters(UnitKB, pairFmt, wcc...)
27 return Counters(SizeB1000(0), pairFmt, wcc...)
2828 }
2929
3030 // Counters decorator with dynamic unit measure adjustment.
3131 //
32 // `unit` one of [0|UnitKiB|UnitKB] zero for no unit
33 //
34 // `pairFmt` printf compatible verbs for current and total pair
35 //
36 // `wcc` optional WC config
37 //
38 // pairFmt example if unit=UnitKB:
39 //
32 // `unit` one of [0|SizeB1024(0)|SizeB1000(0)]
33 //
34 // `pairFmt` printf compatible verbs for current and total
35 //
36 // `wcc` optional WC config
37 //
38 // pairFmt example if unit=SizeB1000(0):
39 //
40 // pairFmt="%d / %d" output: "1MB / 12MB"
41 // pairFmt="% d / % d" output: "1 MB / 12 MB"
4042 // pairFmt="%.1f / %.1f" output: "1.0MB / 12.0MB"
4143 // pairFmt="% .1f / % .1f" output: "1.0 MB / 12.0 MB"
42 // pairFmt="%d / %d" output: "1MB / 12MB"
43 // pairFmt="% d / % d" output: "1 MB / 12 MB"
44 func Counters(unit int, pairFmt string, wcc ...WC) Decorator {
45 producer := func(unit int, pairFmt string) DecorFunc {
46 if pairFmt == "" {
47 pairFmt = "%d / %d"
48 } else if strings.Count(pairFmt, "%") != 2 {
49 panic("expected pairFmt with exactly 2 verbs")
50 }
51 switch unit {
52 case UnitKiB:
44 // pairFmt="%f / %f" output: "1.000000MB / 12.000000MB"
45 // pairFmt="% f / % f" output: "1.000000 MB / 12.000000 MB"
46 func Counters(unit interface{}, pairFmt string, wcc ...WC) Decorator {
47 if pairFmt == "" {
48 pairFmt = "%d / %d"
49 } else if strings.Count(pairFmt, "%") != 2 {
50 panic("expected pairFmt with exactly 2 verbs")
51 }
52 producer := func() DecorFunc {
53 switch unit.(type) {
54 case SizeB1024:
5355 return func(s Statistics) string {
5456 return fmt.Sprintf(pairFmt, SizeB1024(s.Current), SizeB1024(s.Total))
5557 }
56 case UnitKB:
58 case SizeB1000:
5759 return func(s Statistics) string {
5860 return fmt.Sprintf(pairFmt, SizeB1000(s.Current), SizeB1000(s.Total))
5961 }
6365 }
6466 }
6567 }
66 return Any(producer(unit, pairFmt), wcc...)
68 return Any(producer(), wcc...)
6769 }
6870
6971 // TotalNoUnit is a wrapper around Total with no unit param.
7274 }
7375
7476 // TotalKibiByte is a wrapper around Total with predefined unit
75 // UnitKiB (bytes/1024).
77 // as SizeB1024(0).
7678 func TotalKibiByte(format string, wcc ...WC) Decorator {
77 return Total(UnitKiB, format, wcc...)
79 return Total(SizeB1024(0), format, wcc...)
7880 }
7981
8082 // TotalKiloByte is a wrapper around Total with predefined unit
81 // UnitKB (bytes/1000).
83 // as SizeB1000(0).
8284 func TotalKiloByte(format string, wcc ...WC) Decorator {
83 return Total(UnitKB, format, wcc...)
85 return Total(SizeB1000(0), format, wcc...)
8486 }
8587
8688 // Total decorator with dynamic unit measure adjustment.
8789 //
88 // `unit` one of [0|UnitKiB|UnitKB] zero for no unit
90 // `unit` one of [0|SizeB1024(0)|SizeB1000(0)]
8991 //
9092 // `format` printf compatible verb for Total
9193 //
9294 // `wcc` optional WC config
9395 //
94 // format example if unit=UnitKiB:
95 //
96 // format example if unit=SizeB1024(0):
97 //
98 // format="%d" output: "12MiB"
99 // format="% d" output: "12 MiB"
96100 // format="%.1f" output: "12.0MiB"
97101 // format="% .1f" output: "12.0 MiB"
98 // format="%d" output: "12MiB"
99 // format="% d" output: "12 MiB"
100 func Total(unit int, format string, wcc ...WC) Decorator {
101 producer := func(unit int, format string) DecorFunc {
102 if format == "" {
103 format = "%d"
104 } else if strings.Count(format, "%") != 1 {
105 panic("expected format with exactly 1 verb")
106 }
107
108 switch unit {
109 case UnitKiB:
102 // format="%f" output: "12.000000MiB"
103 // format="% f" output: "12.000000 MiB"
104 func Total(unit interface{}, format string, wcc ...WC) Decorator {
105 if format == "" {
106 format = "%d"
107 } else if strings.Count(format, "%") != 1 {
108 panic("expected format with exactly 1 verb")
109 }
110 producer := func() DecorFunc {
111 switch unit.(type) {
112 case SizeB1024:
110113 return func(s Statistics) string {
111114 return fmt.Sprintf(format, SizeB1024(s.Total))
112115 }
113 case UnitKB:
116 case SizeB1000:
114117 return func(s Statistics) string {
115118 return fmt.Sprintf(format, SizeB1000(s.Total))
116119 }
120123 }
121124 }
122125 }
123 return Any(producer(unit, format), wcc...)
126 return Any(producer(), wcc...)
124127 }
125128
126129 // CurrentNoUnit is a wrapper around Current with no unit param.
129132 }
130133
131134 // CurrentKibiByte is a wrapper around Current with predefined unit
132 // UnitKiB (bytes/1024).
135 // as SizeB1024(0).
133136 func CurrentKibiByte(format string, wcc ...WC) Decorator {
134 return Current(UnitKiB, format, wcc...)
137 return Current(SizeB1024(0), format, wcc...)
135138 }
136139
137140 // CurrentKiloByte is a wrapper around Current with predefined unit
138 // UnitKB (bytes/1000).
141 // as SizeB1000(0).
139142 func CurrentKiloByte(format string, wcc ...WC) Decorator {
140 return Current(UnitKB, format, wcc...)
143 return Current(SizeB1000(0), format, wcc...)
141144 }
142145
143146 // Current decorator with dynamic unit measure adjustment.
144147 //
145 // `unit` one of [0|UnitKiB|UnitKB] zero for no unit
148 // `unit` one of [0|SizeB1024(0)|SizeB1000(0)]
146149 //
147150 // `format` printf compatible verb for Current
148151 //
149152 // `wcc` optional WC config
150153 //
151 // format example if unit=UnitKiB:
152 //
154 // format example if unit=SizeB1024(0):
155 //
156 // format="%d" output: "12MiB"
157 // format="% d" output: "12 MiB"
153158 // format="%.1f" output: "12.0MiB"
154159 // format="% .1f" output: "12.0 MiB"
155 // format="%d" output: "12MiB"
156 // format="% d" output: "12 MiB"
157 func Current(unit int, format string, wcc ...WC) Decorator {
158 producer := func(unit int, format string) DecorFunc {
159 if format == "" {
160 format = "%d"
161 } else if strings.Count(format, "%") != 1 {
162 panic("expected format with exactly 1 verb")
163 }
164
165 switch unit {
166 case UnitKiB:
160 // format="%f" output: "12.000000MiB"
161 // format="% f" output: "12.000000 MiB"
162 func Current(unit interface{}, format string, wcc ...WC) Decorator {
163 if format == "" {
164 format = "%d"
165 } else if strings.Count(format, "%") != 1 {
166 panic("expected format with exactly 1 verb")
167 }
168 producer := func() DecorFunc {
169 switch unit.(type) {
170 case SizeB1024:
167171 return func(s Statistics) string {
168172 return fmt.Sprintf(format, SizeB1024(s.Current))
169173 }
170 case UnitKB:
174 case SizeB1000:
171175 return func(s Statistics) string {
172176 return fmt.Sprintf(format, SizeB1000(s.Current))
173177 }
177181 }
178182 }
179183 }
180 return Any(producer(unit, format), wcc...)
184 return Any(producer(), wcc...)
181185 }
182186
183187 // InvertedCurrentNoUnit is a wrapper around InvertedCurrent with no unit param.
186190 }
187191
188192 // InvertedCurrentKibiByte is a wrapper around InvertedCurrent with predefined unit
189 // UnitKiB (bytes/1024).
193 // as SizeB1024(0).
190194 func InvertedCurrentKibiByte(format string, wcc ...WC) Decorator {
191 return InvertedCurrent(UnitKiB, format, wcc...)
195 return InvertedCurrent(SizeB1024(0), format, wcc...)
192196 }
193197
194198 // InvertedCurrentKiloByte is a wrapper around InvertedCurrent with predefined unit
195 // UnitKB (bytes/1000).
199 // as SizeB1000(0).
196200 func InvertedCurrentKiloByte(format string, wcc ...WC) Decorator {
197 return InvertedCurrent(UnitKB, format, wcc...)
201 return InvertedCurrent(SizeB1000(0), format, wcc...)
198202 }
199203
200204 // InvertedCurrent decorator with dynamic unit measure adjustment.
201205 //
202 // `unit` one of [0|UnitKiB|UnitKB] zero for no unit
206 // `unit` one of [0|SizeB1024(0)|SizeB1000(0)]
203207 //
204208 // `format` printf compatible verb for InvertedCurrent
205209 //
206210 // `wcc` optional WC config
207211 //
208 // format example if unit=UnitKiB:
209 //
212 // format example if unit=SizeB1024(0):
213 //
214 // format="%d" output: "12MiB"
215 // format="% d" output: "12 MiB"
210216 // format="%.1f" output: "12.0MiB"
211217 // format="% .1f" output: "12.0 MiB"
212 // format="%d" output: "12MiB"
213 // format="% d" output: "12 MiB"
214 func InvertedCurrent(unit int, format string, wcc ...WC) Decorator {
215 producer := func(unit int, format string) DecorFunc {
216 if format == "" {
217 format = "%d"
218 } else if strings.Count(format, "%") != 1 {
219 panic("expected format with exactly 1 verb")
220 }
221
222 switch unit {
223 case UnitKiB:
218 // format="%f" output: "12.000000MiB"
219 // format="% f" output: "12.000000 MiB"
220 func InvertedCurrent(unit interface{}, format string, wcc ...WC) Decorator {
221 if format == "" {
222 format = "%d"
223 } else if strings.Count(format, "%") != 1 {
224 panic("expected format with exactly 1 verb")
225 }
226 producer := func() DecorFunc {
227 switch unit.(type) {
228 case SizeB1024:
224229 return func(s Statistics) string {
225230 return fmt.Sprintf(format, SizeB1024(s.Total-s.Current))
226231 }
227 case UnitKB:
232 case SizeB1000:
228233 return func(s Statistics) string {
229234 return fmt.Sprintf(format, SizeB1000(s.Total-s.Current))
230235 }
234239 }
235240 }
236241 }
237 return Any(producer(unit, format), wcc...)
238 }
242 return Any(producer(), wcc...)
243 }