Codebase list golang-github-vbauerster-mpb / 45ef877
default formats "% d" for unit and "%d" for no unit Vladimir Bauer 3 years ago
2 changed file(s) with 57 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
3737 // pairFmt="%f / %f" output: "1.000000MB / 12.000000MB"
3838 // pairFmt="% f / % f" output: "1.000000 MB / 12.000000 MB"
3939 func Counters(unit interface{}, pairFmt string, wcc ...WC) Decorator {
40 if pairFmt == "" {
41 pairFmt = "% d / % d"
42 }
43 producer := func() DecorFunc {
44 switch unit.(type) {
45 case SizeB1024:
40 producer := func() DecorFunc {
41 switch unit.(type) {
42 case SizeB1024:
43 if pairFmt == "" {
44 pairFmt = "% d / % d"
45 }
4646 return func(s Statistics) string {
4747 return fmt.Sprintf(pairFmt, SizeB1024(s.Current), SizeB1024(s.Total))
4848 }
4949 case SizeB1000:
50 if pairFmt == "" {
51 pairFmt = "% d / % d"
52 }
5053 return func(s Statistics) string {
5154 return fmt.Sprintf(pairFmt, SizeB1000(s.Current), SizeB1000(s.Total))
5255 }
5356 default:
57 if pairFmt == "" {
58 pairFmt = "%d / %d"
59 }
5460 return func(s Statistics) string {
5561 return fmt.Sprintf(pairFmt, s.Current, s.Total)
5662 }
9399 // format="%f" output: "12.000000MiB"
94100 // format="% f" output: "12.000000 MiB"
95101 func Total(unit interface{}, format string, wcc ...WC) Decorator {
96 if format == "" {
97 format = "% d"
98 }
99 producer := func() DecorFunc {
100 switch unit.(type) {
101 case SizeB1024:
102 producer := func() DecorFunc {
103 switch unit.(type) {
104 case SizeB1024:
105 if format == "" {
106 format = "% d"
107 }
102108 return func(s Statistics) string {
103109 return fmt.Sprintf(format, SizeB1024(s.Total))
104110 }
105111 case SizeB1000:
112 if format == "" {
113 format = "% d"
114 }
106115 return func(s Statistics) string {
107116 return fmt.Sprintf(format, SizeB1000(s.Total))
108117 }
109118 default:
119 if format == "" {
120 format = "%d"
121 }
110122 return func(s Statistics) string {
111123 return fmt.Sprintf(format, s.Total)
112124 }
149161 // format="%f" output: "12.000000MiB"
150162 // format="% f" output: "12.000000 MiB"
151163 func Current(unit interface{}, format string, wcc ...WC) Decorator {
152 if format == "" {
153 format = "% d"
154 }
155 producer := func() DecorFunc {
156 switch unit.(type) {
157 case SizeB1024:
164 producer := func() DecorFunc {
165 switch unit.(type) {
166 case SizeB1024:
167 if format == "" {
168 format = "% d"
169 }
158170 return func(s Statistics) string {
159171 return fmt.Sprintf(format, SizeB1024(s.Current))
160172 }
161173 case SizeB1000:
174 if format == "" {
175 format = "% d"
176 }
162177 return func(s Statistics) string {
163178 return fmt.Sprintf(format, SizeB1000(s.Current))
164179 }
165180 default:
181 if format == "" {
182 format = "%d"
183 }
166184 return func(s Statistics) string {
167185 return fmt.Sprintf(format, s.Current)
168186 }
205223 // format="%f" output: "12.000000MiB"
206224 // format="% f" output: "12.000000 MiB"
207225 func InvertedCurrent(unit interface{}, format string, wcc ...WC) Decorator {
208 if format == "" {
209 format = "% d"
210 }
211 producer := func() DecorFunc {
212 switch unit.(type) {
213 case SizeB1024:
226 producer := func() DecorFunc {
227 switch unit.(type) {
228 case SizeB1024:
229 if format == "" {
230 format = "% d"
231 }
214232 return func(s Statistics) string {
215233 return fmt.Sprintf(format, SizeB1024(s.Total-s.Current))
216234 }
217235 case SizeB1000:
236 if format == "" {
237 format = "% d"
238 }
218239 return func(s Statistics) string {
219240 return fmt.Sprintf(format, SizeB1000(s.Total-s.Current))
220241 }
221242 default:
243 if format == "" {
244 format = "%d"
245 }
222246 return func(s Statistics) string {
223247 return fmt.Sprintf(format, s.Total-s.Current)
224248 }
6666 // unit=SizeB1000(0), format="%.1f" output: "1.0MB/s"
6767 // unit=SizeB1000(0), format="% .1f" output: "1.0 MB/s"
6868 func MovingAverageSpeed(unit interface{}, format string, average ewma.MovingAverage, wcc ...WC) Decorator {
69 if format == "" {
70 format = "% d"
71 }
7269 d := &movingAverageSpeed{
7370 WC: initWC(wcc...),
7471 average: average,
127124 // unit=SizeB1000(0), format="%.1f" output: "1.0MB/s"
128125 // unit=SizeB1000(0), format="% .1f" output: "1.0 MB/s"
129126 func NewAverageSpeed(unit interface{}, format string, startTime time.Time, wcc ...WC) Decorator {
130 if format == "" {
131 format = "% d"
132 }
133127 d := &averageSpeed{
134128 WC: initWC(wcc...),
135129 startTime: startTime,
160154 func chooseSpeedProducer(unit interface{}, format string) func(float64) string {
161155 switch unit.(type) {
162156 case SizeB1024:
157 if format == "" {
158 format = "% d"
159 }
163160 return func(speed float64) string {
164161 return fmt.Sprintf(format, FmtAsSpeed(SizeB1024(math.Round(speed))))
165162 }
166163 case SizeB1000:
164 if format == "" {
165 format = "% d"
166 }
167167 return func(speed float64) string {
168168 return fmt.Sprintf(format, FmtAsSpeed(SizeB1000(math.Round(speed))))
169169 }
170170 default:
171 if format == "" {
172 format = "%f"
173 }
171174 return func(speed float64) string {
172175 return fmt.Sprintf(format, speed)
173176 }