Codebase list golang-github-vbauerster-mpb / eca79bf
Refactoring Counters to counters Vladimir Bauer 8 years ago
2 changed file(s) with 31 addition(s) and 35 deletion(s). Raw diff Collapse all Expand all
2323
2424 const (
2525 _ = iota
26 // Unit_KiB Kibibyte = 1024 b
27 Unit_KiB
28 // Unit_kB Kilobyte = 1000 b
29 Unit_kB
26 unitKiB
27 unitKB
3028 )
3129
32 type Unit uint
30 type counterUnit uint
3331
3432 type CounterKiB int64
3533
9595 }
9696
9797 // CountersNoUnit returns raw counters decorator
98 // If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored.
99 // `DwidthSync` is effective with multiple bars only, if set decorator will participate
100 // in width synchronization process with other decorators in the same column group.
98 //
99 // `pairFormat` printf compatible verbs for current and total, like "%f" or "%d".
100 //
101 // `minWidth` minimum width to apply, if `DwidthSync` bit is not set.
102 //
103 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
101104 func CountersNoUnit(pairFormat string, minWidth, conf int) DecoratorFunc {
102 return Counters(pairFormat, 0, minWidth, conf)
105 return counters(pairFormat, 0, minWidth, conf)
103106 }
104107
105108 // CountersKibiByte returns human friendly byte counters decorator, where counters unit is multiple by 1024.
112115 //
113116 // pairFormat example:
114117 //
115 // `"%.1f / %.1f" = "1.0MiB / 12.0MiB"` or `"% .1f / % .1f" = "1.0 MiB / 12.0 MiB"`.
118 // "%.1f / %.1f" = "1.0MiB / 12.0MiB" or "% .1f / % .1f" = "1.0 MiB / 12.0 MiB"
116119 func CountersKibiByte(pairFormat string, minWidth, conf int) DecoratorFunc {
117 return Counters(pairFormat, Unit_KiB, minWidth, conf)
118 }
119
120 // CountersKiloByte returns human friendly byte counters decorator, where
121 // counters unit is multiple by 1000.
122 // `pairFormat` must contain two printf compatible verbs, like "%f" or "%d".
123 // First verb substituted with Current, second one with Total.
124 // Example: `"%.1f / %.1f" = "1.0MB / 12.0MB"` or `"% .1f / % .1f" = "1.0 MB / 12.0 MB"`.
125 // If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored.
126 // `DwidthSync` is effective with multiple bars only, if set decorator will participate
127 // in width synchronization process with other decorators in the same column group.
120 return counters(pairFormat, unitKiB, minWidth, conf)
121 }
122
123 // CountersKiloByte returns human friendly byte counters decorator, where counters unit is multiple by 1000.
124 //
125 // `pairFormat` printf compatible verbs for current and total, like "%f" or "%d".
126 //
127 // `minWidth` minimum width to apply, if `DwidthSync` bit is not set.
128 //
129 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
130 //
131 // pairFormat example:
132 //
133 // "%.1f / %.1f" = "1.0MB / 12.0MB" or "% .1f / % .1f" = "1.0 MB / 12.0 MB"
128134 func CountersKiloByte(pairFormat string, minWidth, conf int) DecoratorFunc {
129 return Counters(pairFormat, Unit_kB, minWidth, conf)
130 }
131
132 // Counters provides basic counters decorator.
133 // `pairFormat` must contain two printf compatible verbs, like "%f" or "%d".
134 // First verb substituted with Current, second one with Total.
135 // Example: `"%.1f / %.1f" = "1.0MiB / 12.0MiB"` or `"% .1f / % .1f" = "1.0 MiB / 12.0 MiB"`.
136 // `unit` is one of decor.Unit_KiB/decor.Unit_kB or just zero if you need raw unitless numbers.
137 // If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored.
138 // `DwidthSync` is effective with multiple bars only, if set decorator will participate
139 // in width synchronization process with other decorators in the same column group.
140 func Counters(pairFormat string, unit Unit, minWidth, conf int) DecoratorFunc {
135 return counters(pairFormat, unitKB, minWidth, conf)
136 }
137
138 func counters(pairFormat string, unit counterUnit, minWidth, conf int) DecoratorFunc {
141139 format := "%%"
142140 if (conf & DidentRight) != 0 {
143141 format += "-"
146144 return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
147145 var str string
148146 switch unit {
149 case Unit_KiB:
147 case unitKiB:
150148 str = fmt.Sprintf(pairFormat, CounterKiB(s.Current), CounterKiB(s.Total))
151 case Unit_kB:
149 case unitKB:
152150 str = fmt.Sprintf(pairFormat, CounterKB(s.Current), CounterKB(s.Total))
153151 default:
154152 str = fmt.Sprintf(pairFormat, s.Current, s.Total)