| 23 | 23 |
// DecoratorFunc is a function that can be prepended and appended to the progress bar
|
| 24 | 24 |
type DecoratorFunc func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string
|
| 25 | 25 |
|
| 26 | |
// PrependName prepends name argument to the bar.
|
| 27 | |
// The conf argument defines the formatting properties
|
| 28 | |
func (b *Bar) PrependName(name string, minWidth int, conf byte) *Bar {
|
|
26 |
func Name(name string, minWidth int, conf byte) DecoratorFunc {
|
| 29 | 27 |
format := "%%"
|
| 30 | 28 |
if (conf & DidentRight) != 0 {
|
| 31 | 29 |
format += "-"
|
| 32 | 30 |
}
|
| 33 | 31 |
format += "%ds"
|
| 34 | |
b.PrependFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
|
|
32 |
return func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
|
| 35 | 33 |
if (conf & DwidthSync) != 0 {
|
| 36 | 34 |
myWidth <- utf8.RuneCountInString(name)
|
| 37 | 35 |
max := <-maxWidth
|
|
| 41 | 39 |
return fmt.Sprintf(fmt.Sprintf(format, max), name)
|
| 42 | 40 |
}
|
| 43 | 41 |
return fmt.Sprintf(fmt.Sprintf(format, minWidth), name)
|
| 44 | |
})
|
| 45 | |
return b
|
|
42 |
}
|
| 46 | 43 |
}
|
| 47 | 44 |
|
| 48 | |
func (b *Bar) PrependCounters(pairFormat string, unit Units, minWidth int, conf byte) *Bar {
|
|
45 |
func Counters(pairFormat string, unit Units, minWidth int, conf byte) DecoratorFunc {
|
| 49 | 46 |
format := "%%"
|
| 50 | 47 |
if (conf & DidentRight) != 0 {
|
| 51 | 48 |
format += "-"
|
| 52 | 49 |
}
|
| 53 | 50 |
format += "%ds"
|
| 54 | |
b.PrependFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
|
|
51 |
return func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
|
| 55 | 52 |
current := Format(s.Current).To(unit)
|
| 56 | 53 |
total := Format(s.Total).To(unit)
|
| 57 | 54 |
str := fmt.Sprintf(pairFormat, current, total)
|
|
| 64 | 61 |
return fmt.Sprintf(fmt.Sprintf(format, max), str)
|
| 65 | 62 |
}
|
| 66 | 63 |
return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
|
| 67 | |
})
|
| 68 | |
return b
|
|
64 |
}
|
| 69 | 65 |
}
|
| 70 | 66 |
|
| 71 | |
func (b *Bar) PrependETA(minWidth int, conf byte) *Bar {
|
|
67 |
func ETA(minWidth int, conf byte) DecoratorFunc {
|
| 72 | 68 |
format := "%%"
|
| 73 | 69 |
if (conf & DidentRight) != 0 {
|
| 74 | 70 |
format += "-"
|
| 75 | 71 |
}
|
| 76 | 72 |
format += "%ds"
|
| 77 | |
b.PrependFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
|
|
73 |
return func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
|
| 78 | 74 |
str := fmt.Sprint(time.Duration(s.Eta().Seconds()) * time.Second)
|
| 79 | 75 |
if (conf & DwidthSync) != 0 {
|
| 80 | 76 |
myWidth <- utf8.RuneCountInString(str)
|
|
| 85 | 81 |
return fmt.Sprintf(fmt.Sprintf(format, max), str)
|
| 86 | 82 |
}
|
| 87 | 83 |
return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
|
| 88 | |
})
|
| 89 | |
return b
|
|
84 |
}
|
| 90 | 85 |
}
|
| 91 | 86 |
|
| 92 | |
func (b *Bar) AppendETA(minWidth int, conf byte) *Bar {
|
|
87 |
func (b *Bar) Elapsed(minWidth int, conf byte) DecoratorFunc {
|
| 93 | 88 |
format := "%%"
|
| 94 | 89 |
if (conf & DidentRight) != 0 {
|
| 95 | 90 |
format += "-"
|
| 96 | 91 |
}
|
| 97 | 92 |
format += "%ds"
|
| 98 | |
b.AppendFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
|
| 99 | |
str := fmt.Sprint(time.Duration(s.Eta().Seconds()) * time.Second)
|
| 100 | |
if (conf & DwidthSync) != 0 {
|
| 101 | |
myWidth <- utf8.RuneCountInString(str)
|
| 102 | |
max := <-maxWidth
|
| 103 | |
if (conf & DextraSpace) != 0 {
|
| 104 | |
max++
|
| 105 | |
}
|
| 106 | |
return fmt.Sprintf(fmt.Sprintf(format, max), str)
|
| 107 | |
}
|
| 108 | |
return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
|
| 109 | |
})
|
| 110 | |
return b
|
| 111 | |
}
|
| 112 | |
|
| 113 | |
func (b *Bar) PrependElapsed(minWidth int, conf byte) *Bar {
|
| 114 | |
format := "%%"
|
| 115 | |
if (conf & DidentRight) != 0 {
|
| 116 | |
format += "-"
|
| 117 | |
}
|
| 118 | |
format += "%ds"
|
| 119 | |
b.PrependFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
|
|
93 |
return func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
|
| 120 | 94 |
str := fmt.Sprint(time.Duration(s.TimeElapsed.Seconds()) * time.Second)
|
| 121 | 95 |
if (conf & DwidthSync) != 0 {
|
| 122 | 96 |
myWidth <- utf8.RuneCountInString(str)
|
|
| 127 | 101 |
return fmt.Sprintf(fmt.Sprintf(format, max), str)
|
| 128 | 102 |
}
|
| 129 | 103 |
return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
|
| 130 | |
})
|
| 131 | |
return b
|
|
104 |
}
|
| 132 | 105 |
}
|
| 133 | 106 |
|
| 134 | |
func (b *Bar) AppendElapsed(minWidth int, conf byte) *Bar {
|
|
107 |
func Percentage(minWidth int, conf byte) DecoratorFunc {
|
| 135 | 108 |
format := "%%"
|
| 136 | 109 |
if (conf & DidentRight) != 0 {
|
| 137 | 110 |
format += "-"
|
| 138 | 111 |
}
|
| 139 | 112 |
format += "%ds"
|
| 140 | |
b.AppendFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
|
| 141 | |
str := fmt.Sprint(time.Duration(s.TimeElapsed.Seconds()) * time.Second)
|
| 142 | |
if (conf & DwidthSync) != 0 {
|
| 143 | |
myWidth <- utf8.RuneCountInString(str)
|
| 144 | |
max := <-maxWidth
|
| 145 | |
if (conf & DextraSpace) != 0 {
|
| 146 | |
max++
|
| 147 | |
}
|
| 148 | |
return fmt.Sprintf(fmt.Sprintf(format, max), str)
|
| 149 | |
}
|
| 150 | |
return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
|
| 151 | |
})
|
| 152 | |
return b
|
| 153 | |
}
|
| 154 | |
|
| 155 | |
func (b *Bar) AppendPercentage(minWidth int, conf byte) *Bar {
|
| 156 | |
format := "%%"
|
| 157 | |
if (conf & DidentRight) != 0 {
|
| 158 | |
format += "-"
|
| 159 | |
}
|
| 160 | |
format += "%ds"
|
| 161 | |
b.AppendFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
|
|
113 |
return func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
|
| 162 | 114 |
str := fmt.Sprintf("%d %%", percentage(s.Total, s.Current, 100))
|
| 163 | 115 |
if (conf & DwidthSync) != 0 {
|
| 164 | 116 |
myWidth <- utf8.RuneCountInString(str)
|
|
| 169 | 121 |
return fmt.Sprintf(fmt.Sprintf(format, max), str)
|
| 170 | 122 |
}
|
| 171 | 123 |
return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
|
| 172 | |
})
|
| 173 | |
return b
|
|
124 |
}
|
| 174 | 125 |
}
|
| 175 | |
|
| 176 | |
func (b *Bar) PrependPercentage(minWidth int, conf byte) *Bar {
|
| 177 | |
format := "%%"
|
| 178 | |
if (conf & DidentRight) != 0 {
|
| 179 | |
format += "-"
|
| 180 | |
}
|
| 181 | |
format += "%ds"
|
| 182 | |
b.PrependFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
|
| 183 | |
str := fmt.Sprintf("%d %%", percentage(s.Total, s.Current, 100))
|
| 184 | |
if (conf & DwidthSync) != 0 {
|
| 185 | |
myWidth <- utf8.RuneCountInString(str)
|
| 186 | |
max := <-maxWidth
|
| 187 | |
if (conf & DextraSpace) != 0 {
|
| 188 | |
max++
|
| 189 | |
}
|
| 190 | |
return fmt.Sprintf(fmt.Sprintf(format, max), str)
|
| 191 | |
}
|
| 192 | |
return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
|
| 193 | |
})
|
| 194 | |
return b
|
| 195 | |
}
|