Codebase list golang-github-vbauerster-mpb / e62fdc9
Total, Current and InvertedCurrent decorators Vladimir Bauer 5 years ago
1 changed file(s) with 199 addition(s) and 23 deletion(s). Raw diff Collapse all Expand all
11
22 import (
33 "fmt"
4 "strings"
45 )
56
67 const (
3031 //
3132 // `unit` one of [0|UnitKiB|UnitKB] zero for no unit
3233 //
33 // `pairFmt` printf compatible verbs for current and total, like "%f" or "%d"
34 // `pairFmt` printf compatible verbs for current and total pair
3435 //
3536 // `wcc` optional WC config
3637 //
4243 // pairFmt="% d / % d" output: "1 MB / 12 MB"
4344 //
4445 func Counters(unit int, pairFmt string, wcc ...WC) Decorator {
45 return Any(chooseSizeProducer(unit, pairFmt), wcc...)
46 }
47
48 func chooseSizeProducer(unit int, format string) DecorFunc {
49 if format == "" {
50 format = "%d / %d"
51 }
52 switch unit {
53 case UnitKiB:
54 return func(s Statistics) string {
55 return fmt.Sprintf(format, SizeB1024(s.Current), SizeB1024(s.Total))
56 }
57 case UnitKB:
58 return func(s Statistics) string {
59 return fmt.Sprintf(format, SizeB1000(s.Current), SizeB1000(s.Total))
60 }
61 default:
62 return func(s Statistics) string {
63 return fmt.Sprintf(format, s.Current, s.Total)
64 }
65 }
66 }
46 producer := func(unit int, pairFmt string) DecorFunc {
47 if pairFmt == "" {
48 pairFmt = "%d / %d"
49 } else if strings.Count(pairFmt, "%") != 2 {
50 panic("expected pairFmt with exactly 2 verbs")
51 }
52 switch unit {
53 case UnitKiB:
54 return func(s Statistics) string {
55 return fmt.Sprintf(pairFmt, SizeB1024(s.Current), SizeB1024(s.Total))
56 }
57 case UnitKB:
58 return func(s Statistics) string {
59 return fmt.Sprintf(pairFmt, SizeB1000(s.Current), SizeB1000(s.Total))
60 }
61 default:
62 return func(s Statistics) string {
63 return fmt.Sprintf(pairFmt, s.Current, s.Total)
64 }
65 }
66 }
67 return Any(producer(unit, pairFmt), wcc...)
68 }
69
70 // TotalNoUnit is a wrapper around Total with no unit param.
71 func TotalNoUnit(format string, wcc ...WC) Decorator {
72 return Total(0, format, wcc...)
73 }
74
75 // TotalKibiByte is a wrapper around Total with predefined unit
76 // UnitKiB (bytes/1024).
77 func TotalKibiByte(format string, wcc ...WC) Decorator {
78 return Total(UnitKiB, format, wcc...)
79 }
80
81 // TotalKiloByte is a wrapper around Total with predefined unit
82 // UnitKB (bytes/1000).
83 func TotalKiloByte(format string, wcc ...WC) Decorator {
84 return Total(UnitKB, format, wcc...)
85 }
86
87 // Total decorator with dynamic unit measure adjustment.
88 //
89 // `unit` one of [0|UnitKiB|UnitKB] zero for no unit
90 //
91 // `format` printf compatible verb for Total
92 //
93 // `wcc` optional WC config
94 //
95 // format example if unit=UnitKiB:
96 //
97 // format="%.1f" output: "12.0MiB"
98 // format="% .1f" output: "12.0 MiB"
99 // format="%d" output: "12MiB"
100 // format="% d" output: "12 MiB"
101 //
102 func Total(unit int, format string, wcc ...WC) Decorator {
103 producer := func(unit int, format string) DecorFunc {
104 if format == "" {
105 format = "%d"
106 } else if strings.Count(format, "%") != 1 {
107 panic("expected format with exactly 1 verb")
108 }
109
110 switch unit {
111 case UnitKiB:
112 return func(s Statistics) string {
113 return fmt.Sprintf(format, SizeB1024(s.Total))
114 }
115 case UnitKB:
116 return func(s Statistics) string {
117 return fmt.Sprintf(format, SizeB1000(s.Total))
118 }
119 default:
120 return func(s Statistics) string {
121 return fmt.Sprintf(format, s.Total)
122 }
123 }
124 }
125 return Any(producer(unit, format), wcc...)
126 }
127
128 // CurrentNoUnit is a wrapper around Current with no unit param.
129 func CurrentNoUnit(format string, wcc ...WC) Decorator {
130 return Current(0, format, wcc...)
131 }
132
133 // CurrentKibiByte is a wrapper around Current with predefined unit
134 // UnitKiB (bytes/1024).
135 func CurrentKibiByte(format string, wcc ...WC) Decorator {
136 return Current(UnitKiB, format, wcc...)
137 }
138
139 // CurrentKiloByte is a wrapper around Current with predefined unit
140 // UnitKB (bytes/1000).
141 func CurrentKiloByte(format string, wcc ...WC) Decorator {
142 return Current(UnitKB, format, wcc...)
143 }
144
145 // Current decorator with dynamic unit measure adjustment.
146 //
147 // `unit` one of [0|UnitKiB|UnitKB] zero for no unit
148 //
149 // `format` printf compatible verb for Current
150 //
151 // `wcc` optional WC config
152 //
153 // format example if unit=UnitKiB:
154 //
155 // format="%.1f" output: "12.0MiB"
156 // format="% .1f" output: "12.0 MiB"
157 // format="%d" output: "12MiB"
158 // format="% d" output: "12 MiB"
159 //
160 func Current(unit int, format string, wcc ...WC) Decorator {
161 producer := func(unit int, format string) DecorFunc {
162 if format == "" {
163 format = "%d"
164 } else if strings.Count(format, "%") != 1 {
165 panic("expected format with exactly 1 verb")
166 }
167
168 switch unit {
169 case UnitKiB:
170 return func(s Statistics) string {
171 return fmt.Sprintf(format, SizeB1024(s.Current))
172 }
173 case UnitKB:
174 return func(s Statistics) string {
175 return fmt.Sprintf(format, SizeB1000(s.Current))
176 }
177 default:
178 return func(s Statistics) string {
179 return fmt.Sprintf(format, s.Current)
180 }
181 }
182 }
183 return Any(producer(unit, format), wcc...)
184 }
185
186 // InvertedCurrentNoUnit is a wrapper around InvertedCurrent with no unit param.
187 func InvertedCurrentNoUnit(format string, wcc ...WC) Decorator {
188 return InvertedCurrent(0, format, wcc...)
189 }
190
191 // InvertedCurrentKibiByte is a wrapper around InvertedCurrent with predefined unit
192 // UnitKiB (bytes/1024).
193 func InvertedCurrentKibiByte(format string, wcc ...WC) Decorator {
194 return InvertedCurrent(UnitKiB, format, wcc...)
195 }
196
197 // InvertedCurrentKiloByte is a wrapper around InvertedCurrent with predefined unit
198 // UnitKB (bytes/1000).
199 func InvertedCurrentKiloByte(format string, wcc ...WC) Decorator {
200 return InvertedCurrent(UnitKB, format, wcc...)
201 }
202
203 // InvertedCurrent decorator with dynamic unit measure adjustment.
204 //
205 // `unit` one of [0|UnitKiB|UnitKB] zero for no unit
206 //
207 // `format` printf compatible verb for InvertedCurrent
208 //
209 // `wcc` optional WC config
210 //
211 // format example if unit=UnitKiB:
212 //
213 // format="%.1f" output: "12.0MiB"
214 // format="% .1f" output: "12.0 MiB"
215 // format="%d" output: "12MiB"
216 // format="% d" output: "12 MiB"
217 //
218 func InvertedCurrent(unit int, format string, wcc ...WC) Decorator {
219 producer := func(unit int, format string) DecorFunc {
220 if format == "" {
221 format = "%d"
222 } else if strings.Count(format, "%") != 1 {
223 panic("expected format with exactly 1 verb")
224 }
225
226 switch unit {
227 case UnitKiB:
228 return func(s Statistics) string {
229 return fmt.Sprintf(format, SizeB1024(s.Total-s.Current))
230 }
231 case UnitKB:
232 return func(s Statistics) string {
233 return fmt.Sprintf(format, SizeB1000(s.Total-s.Current))
234 }
235 default:
236 return func(s Statistics) string {
237 return fmt.Sprintf(format, s.Total-s.Current)
238 }
239 }
240 }
241 return Any(producer(unit, format), wcc...)
242 }