decorators: more descriptive params
Vladimir Bauer
8 years ago
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | // DecoratorFunc is a function that can be prepended and appended to the progress bar |
| 47 | type DecoratorFunc func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string | |
| 47 | type DecoratorFunc func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string | |
| 48 | 48 | |
| 49 | 49 | // Name deprecated, use StaticName instead |
| 50 | 50 | func Name(name string, minWidth int, conf byte) DecoratorFunc { |
| 70 | 70 | format += "-" |
| 71 | 71 | } |
| 72 | 72 | format += "%ds" |
| 73 | return func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string { | |
| 73 | return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string { | |
| 74 | 74 | name := nameFn(s) |
| 75 | 75 | if (conf & DwidthSync) != 0 { |
| 76 | myWidth <- utf8.RuneCountInString(name) | |
| 77 | max := <-maxWidth | |
| 76 | widthAccumulator <- utf8.RuneCountInString(name) | |
| 77 | max := <-widthDistributor | |
| 78 | 78 | if (conf & DextraSpace) != 0 { |
| 79 | 79 | max++ |
| 80 | 80 | } |
| 112 | 112 | format += "-" |
| 113 | 113 | } |
| 114 | 114 | format += "%ds" |
| 115 | return func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string { | |
| 115 | return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string { | |
| 116 | 116 | var str string |
| 117 | 117 | switch unit { |
| 118 | 118 | case Unit_KiB: |
| 123 | 123 | str = fmt.Sprintf(pairFormat, s.Current, s.Total) |
| 124 | 124 | } |
| 125 | 125 | if (conf & DwidthSync) != 0 { |
| 126 | myWidth <- utf8.RuneCountInString(str) | |
| 127 | max := <-maxWidth | |
| 126 | widthAccumulator <- utf8.RuneCountInString(str) | |
| 127 | max := <-widthDistributor | |
| 128 | 128 | if (conf & DextraSpace) != 0 { |
| 129 | 129 | max++ |
| 130 | 130 | } |
| 143 | 143 | format += "-" |
| 144 | 144 | } |
| 145 | 145 | format += "%ds" |
| 146 | return func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string { | |
| 146 | return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string { | |
| 147 | 147 | str := fmt.Sprint(time.Duration(s.Eta().Seconds()) * time.Second) |
| 148 | 148 | if (conf & DwidthSync) != 0 { |
| 149 | myWidth <- utf8.RuneCountInString(str) | |
| 150 | max := <-maxWidth | |
| 149 | widthAccumulator <- utf8.RuneCountInString(str) | |
| 150 | max := <-widthDistributor | |
| 151 | 151 | if (conf & DextraSpace) != 0 { |
| 152 | 152 | max++ |
| 153 | 153 | } |
| 166 | 166 | format += "-" |
| 167 | 167 | } |
| 168 | 168 | format += "%ds" |
| 169 | return func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string { | |
| 169 | return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string { | |
| 170 | 170 | str := fmt.Sprint(time.Duration(s.TimeElapsed.Seconds()) * time.Second) |
| 171 | 171 | if (conf & DwidthSync) != 0 { |
| 172 | myWidth <- utf8.RuneCountInString(str) | |
| 173 | max := <-maxWidth | |
| 172 | widthAccumulator <- utf8.RuneCountInString(str) | |
| 173 | max := <-widthDistributor | |
| 174 | 174 | if (conf & DextraSpace) != 0 { |
| 175 | 175 | max++ |
| 176 | 176 | } |
| 189 | 189 | format += "-" |
| 190 | 190 | } |
| 191 | 191 | format += "%ds" |
| 192 | return func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string { | |
| 192 | return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string { | |
| 193 | 193 | str := fmt.Sprintf("%d %%", CalcPercentage(s.Total, s.Current, 100)) |
| 194 | 194 | if (conf & DwidthSync) != 0 { |
| 195 | myWidth <- utf8.RuneCountInString(str) | |
| 196 | max := <-maxWidth | |
| 195 | widthAccumulator <- utf8.RuneCountInString(str) | |
| 196 | max := <-widthDistributor | |
| 197 | 197 | if (conf & DextraSpace) != 0 { |
| 198 | 198 | max++ |
| 199 | 199 | } |