| 30 | 30 |
type CounterKiB int64
|
| 31 | 31 |
|
| 32 | 32 |
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 |
}
|
| 44 | 44 |
}
|
| 45 | 45 |
|
| 46 | 46 |
var res, unit string
|
|
| 84 | 84 |
type CounterKB int64
|
| 85 | 85 |
|
| 86 | 86 |
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 |
}
|
| 98 | 98 |
}
|
| 99 | 99 |
|
| 100 | 100 |
var res, unit string
|
|
| 136 | 136 |
}
|
| 137 | 137 |
|
| 138 | 138 |
// 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...)
|
| 141 | 141 |
}
|
| 142 | 142 |
|
| 143 | 143 |
// CountersKibiByte is a wrapper around Counters with predefined unit
|
| 144 | 144 |
// 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...)
|
| 147 | 147 |
}
|
| 148 | 148 |
|
| 149 | 149 |
// CountersKiloByte is a wrapper around Counters with predefined unit
|
| 150 | 150 |
// 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...)
|
| 153 | 153 |
}
|
| 154 | 154 |
|
| 155 | 155 |
// Counters decorator with dynamic unit measure adjustment.
|
| 156 | 156 |
//
|
| 157 | 157 |
// `unit` one of [0|UnitKiB|UnitKB] zero for no unit
|
| 158 | 158 |
//
|
| 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"
|
| 160 | 160 |
//
|
| 161 | 161 |
// `wcc` optional WC config
|
| 162 | 162 |
//
|
| 163 | |
// pairFormat example if UnitKB is chosen:
|
|
163 |
// pairFmt example if UnitKB is chosen:
|
| 164 | 164 |
//
|
| 165 | 165 |
// "%.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 {
|
| 167 | 168 |
var wc WC
|
| 168 | 169 |
for _, widthConf := range wcc {
|
| 169 | 170 |
wc = widthConf
|
| 170 | 171 |
}
|
| 171 | 172 |
wc.Init()
|
| 172 | 173 |
d := &countersDecorator{
|
| 173 | |
WC: wc,
|
| 174 | |
unit: unit,
|
| 175 | |
pairFormat: pairFormat,
|
|
174 |
WC: wc,
|
|
175 |
unit: unit,
|
|
176 |
pairFmt: pairFmt,
|
| 176 | 177 |
}
|
| 177 | 178 |
return d
|
| 178 | 179 |
}
|
|
| 180 | 181 |
type countersDecorator struct {
|
| 181 | 182 |
WC
|
| 182 | 183 |
unit int
|
| 183 | |
pairFormat string
|
|
184 |
pairFmt string
|
| 184 | 185 |
completeMsg *string
|
| 185 | 186 |
}
|
| 186 | 187 |
|
|
| 192 | 193 |
var str string
|
| 193 | 194 |
switch d.unit {
|
| 194 | 195 |
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))
|
| 196 | 197 |
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)
|
| 200 | 201 |
}
|
| 201 | 202 |
|
| 202 | 203 |
return d.FormatMsg(str)
|