Refactoring: decorators minWidth to width
Vladimir Bauer
8 years ago
| 50 | 50 | // |
| 51 | 51 | // `fn` DecoratorFunc to wrap |
| 52 | 52 | // |
| 53 | // `minWidth` minimum width to apply, if `DwidthSync` bit is not set | |
| 54 | // | |
| 55 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] | |
| 56 | func OnComplete(fn DecoratorFunc, message string, minWidth, conf int) DecoratorFunc { | |
| 57 | msgDecorator := StaticName(message, minWidth, conf) | |
| 53 | // `width` width to apply, if `DwidthSync` bit is not set | |
| 54 | // | |
| 55 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] | |
| 56 | func OnComplete(fn DecoratorFunc, message string, width, conf int) DecoratorFunc { | |
| 57 | msgDecorator := StaticName(message, width, conf) | |
| 58 | 58 | return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string { |
| 59 | 59 | if s.Completed { |
| 60 | 60 | return msgDecorator(s, widthAccumulator, widthDistributor) |
| 67 | 67 | // |
| 68 | 68 | // `name` string to display |
| 69 | 69 | // |
| 70 | // `minWidth` minimum width to apply, if `DwidthSync` bit is not set | |
| 71 | // | |
| 72 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] | |
| 73 | func StaticName(name string, minWidth, conf int) DecoratorFunc { | |
| 70 | // `width` width to apply, if `DwidthSync` bit is not set | |
| 71 | // | |
| 72 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] | |
| 73 | func StaticName(name string, width, conf int) DecoratorFunc { | |
| 74 | 74 | nameFn := func(*Statistics) string { |
| 75 | 75 | return name |
| 76 | 76 | } |
| 77 | return DynamicName(nameFn, minWidth, conf) | |
| 77 | return DynamicName(nameFn, width, conf) | |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | // DynamicName returns dynamic name/message decorator. |
| 81 | 81 | // |
| 82 | 82 | // `messageFn` callback function to get dynamic string message |
| 83 | 83 | // |
| 84 | // `minWidth` minimum width to apply, if `DwidthSync` bit is not set | |
| 85 | // | |
| 86 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] | |
| 87 | func DynamicName(messageFn func(*Statistics) string, minWidth, conf int) DecoratorFunc { | |
| 84 | // `width` width to apply, if `DwidthSync` bit is not set | |
| 85 | // | |
| 86 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] | |
| 87 | func DynamicName(messageFn func(*Statistics) string, width, conf int) DecoratorFunc { | |
| 88 | 88 | format := "%%" |
| 89 | 89 | if (conf & DidentRight) != 0 { |
| 90 | 90 | format += "-" |
| 100 | 100 | } |
| 101 | 101 | return fmt.Sprintf(fmt.Sprintf(format, max), name) |
| 102 | 102 | } |
| 103 | return fmt.Sprintf(fmt.Sprintf(format, minWidth), name) | |
| 103 | return fmt.Sprintf(fmt.Sprintf(format, width), name) | |
| 104 | 104 | } |
| 105 | 105 | } |
| 106 | 106 | |
| 108 | 108 | // |
| 109 | 109 | // `pairFormat` printf compatible verbs for current and total, like "%f" or "%d" |
| 110 | 110 | // |
| 111 | // `minWidth` minimum width to apply, if `DwidthSync` bit is not set | |
| 112 | // | |
| 113 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] | |
| 114 | func CountersNoUnit(pairFormat string, minWidth, conf int) DecoratorFunc { | |
| 115 | return counters(pairFormat, 0, minWidth, conf) | |
| 111 | // `width` width to apply, if `DwidthSync` bit is not set | |
| 112 | // | |
| 113 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] | |
| 114 | func CountersNoUnit(pairFormat string, width, conf int) DecoratorFunc { | |
| 115 | return counters(pairFormat, 0, width, conf) | |
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | // CountersKibiByte returns human friendly byte counters decorator, where counters unit is multiple by 1024. |
| 119 | 119 | // |
| 120 | 120 | // `pairFormat` printf compatible verbs for current and total, like "%f" or "%d" |
| 121 | 121 | // |
| 122 | // `minWidth` minimum width to apply, if `DwidthSync` bit is not set | |
| 122 | // `width` width to apply, if `DwidthSync` bit is not set | |
| 123 | 123 | // |
| 124 | 124 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] |
| 125 | 125 | // |
| 126 | 126 | // pairFormat example: |
| 127 | 127 | // |
| 128 | 128 | // "%.1f / %.1f" = "1.0MiB / 12.0MiB" or "% .1f / % .1f" = "1.0 MiB / 12.0 MiB" |
| 129 | func CountersKibiByte(pairFormat string, minWidth, conf int) DecoratorFunc { | |
| 130 | return counters(pairFormat, unitKiB, minWidth, conf) | |
| 129 | func CountersKibiByte(pairFormat string, width, conf int) DecoratorFunc { | |
| 130 | return counters(pairFormat, unitKiB, width, conf) | |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | // CountersKiloByte returns human friendly byte counters decorator, where counters unit is multiple by 1000. |
| 134 | 134 | // |
| 135 | 135 | // `pairFormat` printf compatible verbs for current and total, like "%f" or "%d" |
| 136 | 136 | // |
| 137 | // `minWidth` minimum width to apply, if `DwidthSync` bit is not set | |
| 137 | // `width` width to apply, if `DwidthSync` bit is not set | |
| 138 | 138 | // |
| 139 | 139 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] |
| 140 | 140 | // |
| 141 | 141 | // pairFormat example: |
| 142 | 142 | // |
| 143 | 143 | // "%.1f / %.1f" = "1.0MB / 12.0MB" or "% .1f / % .1f" = "1.0 MB / 12.0 MB" |
| 144 | func CountersKiloByte(pairFormat string, minWidth, conf int) DecoratorFunc { | |
| 145 | return counters(pairFormat, unitKB, minWidth, conf) | |
| 146 | } | |
| 147 | ||
| 148 | func counters(pairFormat string, unit counterUnit, minWidth, conf int) DecoratorFunc { | |
| 144 | func CountersKiloByte(pairFormat string, width, conf int) DecoratorFunc { | |
| 145 | return counters(pairFormat, unitKB, width, conf) | |
| 146 | } | |
| 147 | ||
| 148 | func counters(pairFormat string, unit counterUnit, width, conf int) DecoratorFunc { | |
| 149 | 149 | format := "%%" |
| 150 | 150 | if (conf & DidentRight) != 0 { |
| 151 | 151 | format += "-" |
| 169 | 169 | } |
| 170 | 170 | return fmt.Sprintf(fmt.Sprintf(format, max), str) |
| 171 | 171 | } |
| 172 | return fmt.Sprintf(fmt.Sprintf(format, minWidth), str) | |
| 172 | return fmt.Sprintf(fmt.Sprintf(format, width), str) | |
| 173 | 173 | } |
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | // ETA returns exponential-weighted-moving-average ETA decorator. |
| 177 | 177 | // |
| 178 | // `minWidth` minimum width to apply, if `DwidthSync` bit is not set | |
| 179 | // | |
| 180 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] | |
| 181 | func ETA(minWidth, conf int) DecoratorFunc { | |
| 178 | // `width` width to apply, if `DwidthSync` bit is not set | |
| 179 | // | |
| 180 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] | |
| 181 | func ETA(width, conf int) DecoratorFunc { | |
| 182 | 182 | format := "%%" |
| 183 | 183 | if (conf & DidentRight) != 0 { |
| 184 | 184 | format += "-" |
| 194 | 194 | } |
| 195 | 195 | return fmt.Sprintf(fmt.Sprintf(format, max), str) |
| 196 | 196 | } |
| 197 | return fmt.Sprintf(fmt.Sprintf(format, minWidth), str) | |
| 197 | return fmt.Sprintf(fmt.Sprintf(format, width), str) | |
| 198 | 198 | } |
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | // Elapsed returns elapsed time decorator. |
| 202 | 202 | // |
| 203 | // `minWidth` minimum width to apply, if `DwidthSync` bit is not set | |
| 204 | // | |
| 205 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] | |
| 206 | func Elapsed(minWidth, conf int) DecoratorFunc { | |
| 203 | // `width` width to apply, if `DwidthSync` bit is not set | |
| 204 | // | |
| 205 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] | |
| 206 | func Elapsed(width, conf int) DecoratorFunc { | |
| 207 | 207 | format := "%%" |
| 208 | 208 | if (conf & DidentRight) != 0 { |
| 209 | 209 | format += "-" |
| 219 | 219 | } |
| 220 | 220 | return fmt.Sprintf(fmt.Sprintf(format, max), str) |
| 221 | 221 | } |
| 222 | return fmt.Sprintf(fmt.Sprintf(format, minWidth), str) | |
| 222 | return fmt.Sprintf(fmt.Sprintf(format, width), str) | |
| 223 | 223 | } |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | // Percentage returns percentage decorator. |
| 227 | 227 | // |
| 228 | // `minWidth` minimum width to apply, if `DwidthSync` bit is not set | |
| 229 | // | |
| 230 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] | |
| 231 | func Percentage(minWidth, conf int) DecoratorFunc { | |
| 228 | // `width` width to apply, if `DwidthSync` bit is not set | |
| 229 | // | |
| 230 | // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace] | |
| 231 | func Percentage(width, conf int) DecoratorFunc { | |
| 232 | 232 | format := "%%" |
| 233 | 233 | if (conf & DidentRight) != 0 { |
| 234 | 234 | format += "-" |
| 244 | 244 | } |
| 245 | 245 | return fmt.Sprintf(fmt.Sprintf(format, max), str) |
| 246 | 246 | } |
| 247 | return fmt.Sprintf(fmt.Sprintf(format, minWidth), str) | |
| 247 | return fmt.Sprintf(fmt.Sprintf(format, width), str) | |
| 248 | 248 | } |
| 249 | 249 | } |
| 250 | 250 | |