Refactoring decorators godoc
Vladimir Bauer
8 years ago
| 45 | 45 | // DecoratorFunc is a function that can be prepended and appended to the progress bar |
| 46 | 46 | type DecoratorFunc func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string |
| 47 | 47 | |
| 48 | // OnComplete wraps provided decorator `fn` with on complete event `message`. | |
| 49 | // If you set `DwidthSync` bit in `conf` param, `minWidth` is ignored. | |
| 50 | // `DwidthSync` is effective with multiple bars only, if set decorator will participate | |
| 51 | // in width synchronization process with other decorators in the same column group. | |
| 48 | // OnComplete returns decorator, which wraps provided `fn` decorator, with sole | |
| 49 | // purpose to display final on cemplete message. | |
| 50 | // | |
| 51 | // `fn` DecoratorFunc to wrap | |
| 52 | // | |
| 53 | // `minWidth` minimum width to apply, if `DwidthSync` bit is not set | |
| 54 | // | |
| 55 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] | |
| 52 | 56 | func OnComplete(fn DecoratorFunc, message string, minWidth, conf int) DecoratorFunc { |
| 53 | 57 | msgDecorator := StaticName(message, minWidth, conf) |
| 54 | 58 | return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string { |
| 59 | 63 | } |
| 60 | 64 | } |
| 61 | 65 | |
| 62 | // StaticName is a simple name/message decorator. | |
| 63 | // If you set `DwidthSync` bit in `conf` param, `minWidth` is ignored. | |
| 64 | // `DwidthSync` is effective with multiple bars only, if set decorator will participate | |
| 65 | // in width synchronization process with other decorators in the same column group. | |
| 66 | // StaticName returns static name/message decorator. | |
| 67 | // | |
| 68 | // `name` string to display | |
| 69 | // | |
| 70 | // `minWidth` minimum width to apply, if `DwidthSync` bit is not set | |
| 71 | // | |
| 72 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] | |
| 66 | 73 | func StaticName(name string, minWidth, conf int) DecoratorFunc { |
| 67 | 74 | nameFn := func(*Statistics) string { |
| 68 | 75 | return name |
| 70 | 77 | return DynamicName(nameFn, minWidth, conf) |
| 71 | 78 | } |
| 72 | 79 | |
| 73 | // DynamicName is a name/message decorator, with ability to change message via provided `messageFn`. | |
| 74 | // If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored. | |
| 75 | // `DwidthSync` is effective with multiple bars only, if set decorator will participate | |
| 76 | // in width synchronization process with other decorators in the same column group. | |
| 80 | // DynamicName returns dynamic name/message decorator. | |
| 81 | // | |
| 82 | // `messageFn` callback function to get dynamic string message | |
| 83 | // | |
| 84 | // `minWidth` minimum width to apply, if `DwidthSync` bit is not set | |
| 85 | // | |
| 86 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] | |
| 77 | 87 | func DynamicName(messageFn func(*Statistics) string, minWidth, conf int) DecoratorFunc { |
| 78 | 88 | format := "%%" |
| 79 | 89 | if (conf & DidentRight) != 0 { |
| 96 | 106 | |
| 97 | 107 | // CountersNoUnit returns raw counters decorator |
| 98 | 108 | // |
| 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. | |
| 109 | // `pairFormat` printf compatible verbs for current and total, like "%f" or "%d" | |
| 110 | // | |
| 111 | // `minWidth` minimum width to apply, if `DwidthSync` bit is not set | |
| 102 | 112 | // |
| 103 | 113 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] |
| 104 | 114 | func CountersNoUnit(pairFormat string, minWidth, conf int) DecoratorFunc { |
| 107 | 117 | |
| 108 | 118 | // CountersKibiByte returns human friendly byte counters decorator, where counters unit is multiple by 1024. |
| 109 | 119 | // |
| 110 | // `pairFormat` printf compatible verbs for current and total, like "%f" or "%d". | |
| 111 | // | |
| 112 | // `minWidth` minimum width to apply, if `DwidthSync` bit is not set. | |
| 120 | // `pairFormat` printf compatible verbs for current and total, like "%f" or "%d" | |
| 121 | // | |
| 122 | // `minWidth` minimum width to apply, if `DwidthSync` bit is not set | |
| 113 | 123 | // |
| 114 | 124 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] |
| 115 | 125 | // |
| 122 | 132 | |
| 123 | 133 | // CountersKiloByte returns human friendly byte counters decorator, where counters unit is multiple by 1000. |
| 124 | 134 | // |
| 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. | |
| 135 | // `pairFormat` printf compatible verbs for current and total, like "%f" or "%d" | |
| 136 | // | |
| 137 | // `minWidth` minimum width to apply, if `DwidthSync` bit is not set | |
| 128 | 138 | // |
| 129 | 139 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] |
| 130 | 140 | // |
| 163 | 173 | } |
| 164 | 174 | } |
| 165 | 175 | |
| 166 | // ETA provides exponential-weighted-moving-average ETA decorator. | |
| 167 | // If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored. | |
| 168 | // `DwidthSync` is effective with multiple bars only, if set decorator will participate | |
| 169 | // in width synchronization process with other decorators in the same column group. | |
| 176 | // ETA returns exponential-weighted-moving-average ETA decorator. | |
| 177 | // | |
| 178 | // `minWidth` minimum width to apply, if `DwidthSync` bit is not set | |
| 179 | // | |
| 180 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] | |
| 170 | 181 | func ETA(minWidth, conf int) DecoratorFunc { |
| 171 | 182 | format := "%%" |
| 172 | 183 | if (conf & DidentRight) != 0 { |
| 187 | 198 | } |
| 188 | 199 | } |
| 189 | 200 | |
| 190 | // Elapsed provides elapsed time decorator. | |
| 191 | // If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored. | |
| 192 | // `DwidthSync` is effective with multiple bars only, if set decorator will participate | |
| 193 | // in width synchronization process with other decorators in the same column group. | |
| 201 | // Elapsed returns elapsed time decorator. | |
| 202 | // | |
| 203 | // `minWidth` minimum width to apply, if `DwidthSync` bit is not set | |
| 204 | // | |
| 205 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] | |
| 194 | 206 | func Elapsed(minWidth, conf int) DecoratorFunc { |
| 195 | 207 | format := "%%" |
| 196 | 208 | if (conf & DidentRight) != 0 { |
| 211 | 223 | } |
| 212 | 224 | } |
| 213 | 225 | |
| 214 | // Percentage provides percentage decorator. | |
| 215 | // If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored. | |
| 216 | // `DwidthSync` is effective with multiple bars only, if set decorator will participate | |
| 217 | // in width synchronization process with other decorators in the same column group. | |
| 226 | // Percentage returns percentage decorator. | |
| 227 | // | |
| 228 | // `minWidth` minimum width to apply, if `DwidthSync` bit is not set | |
| 229 | // | |
| 230 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] | |
| 218 | 231 | func Percentage(minWidth, conf int) DecoratorFunc { |
| 219 | 232 | format := "%%" |
| 220 | 233 | if (conf & DidentRight) != 0 { |
| 235 | 248 | } |
| 236 | 249 | } |
| 237 | 250 | |
| 251 | // CalcPercentage is a helper function, to calculate percentage. | |
| 238 | 252 | func CalcPercentage(total, current, width int64) (perc int64) { |
| 239 | 253 | if total <= 0 { |
| 240 | 254 | return 0 |