| 89 | 89 |
format string
|
| 90 | 90 |
}
|
| 91 | 91 |
|
| 92 | |
func (wc WC) formatMsg(msg string, widthAccumulator chan<- int, widthDistributor <-chan int) string {
|
| 93 | |
format := wc.buildFormat()
|
|
92 |
// FormatMsg formats final message according to WC.W and WC.C.
|
|
93 |
// Should be called by any Decorator implementation.
|
|
94 |
func (wc WC) FormatMsg(msg string, widthAccumulator chan<- int, widthDistributor <-chan int) string {
|
| 94 | 95 |
if (wc.C & DSyncWidth) != 0 {
|
| 95 | 96 |
widthAccumulator <- utf8.RuneCountInString(msg)
|
| 96 | 97 |
max := <-widthDistributor
|
|
| 100 | 101 |
if (wc.C & DextraSpace) != 0 {
|
| 101 | 102 |
max++
|
| 102 | 103 |
}
|
| 103 | |
return fmt.Sprintf(fmt.Sprintf(format, max), msg)
|
| 104 | |
}
|
| 105 | |
return fmt.Sprintf(fmt.Sprintf(format, wc.W), msg)
|
| 106 | |
}
|
| 107 | |
|
| 108 | |
func (wc *WC) buildFormat() string {
|
| 109 | |
if wc.format != "" {
|
| 110 | |
return wc.format
|
| 111 | |
}
|
|
104 |
return fmt.Sprintf(fmt.Sprintf(wc.format, max), msg)
|
|
105 |
}
|
|
106 |
return fmt.Sprintf(fmt.Sprintf(wc.format, wc.W), msg)
|
|
107 |
}
|
|
108 |
|
|
109 |
// BuildFormat builds initial format according to WC.C
|
|
110 |
func (wc *WC) BuildFormat() {
|
| 112 | 111 |
wc.format = "%%"
|
| 113 | 112 |
if (wc.C & DidentRight) != 0 {
|
| 114 | 113 |
wc.format += "-"
|
| 115 | 114 |
}
|
| 116 | 115 |
wc.format += "%ds"
|
| 117 | |
return wc.format
|
| 118 | 116 |
}
|
| 119 | 117 |
|
| 120 | 118 |
// Global convenience shortcuts
|
|
| 166 | 164 |
if len(wc) > 0 {
|
| 167 | 165 |
wc0 = wc[0]
|
| 168 | 166 |
}
|
| 169 | |
return DecoratorFunc(func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
|
| 170 | |
return wc0.formatMsg(name, widthAccumulator, widthDistributor)
|
|
167 |
wc0.BuildFormat()
|
|
168 |
return DecoratorFunc(func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
|
|
169 |
return wc0.FormatMsg(name, widthAccumulator, widthDistributor)
|
| 171 | 170 |
})
|
| 172 | 171 |
}
|
| 173 | 172 |
|
|
| 211 | 210 |
if len(wc) > 0 {
|
| 212 | 211 |
wc0 = wc[0]
|
| 213 | 212 |
}
|
|
213 |
wc0.BuildFormat()
|
| 214 | 214 |
return DecoratorFunc(func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
|
| 215 | 215 |
var str string
|
| 216 | 216 |
switch unit {
|
|
| 221 | 221 |
default:
|
| 222 | 222 |
str = fmt.Sprintf(pairFormat, s.Current, s.Total)
|
| 223 | 223 |
}
|
| 224 | |
return wc0.formatMsg(str, widthAccumulator, widthDistributor)
|
|
224 |
return wc0.FormatMsg(str, widthAccumulator, widthDistributor)
|
| 225 | 225 |
})
|
| 226 | 226 |
}
|
| 227 | 227 |
|
|
| 235 | 235 |
if len(wc) > 0 {
|
| 236 | 236 |
wc0 = wc[0]
|
| 237 | 237 |
}
|
|
238 |
wc0.BuildFormat()
|
| 238 | 239 |
return DecoratorFunc(func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
|
| 239 | 240 |
var str string
|
| 240 | 241 |
hours := int64((s.TimeElapsed / time.Hour) % 60)
|
|
| 251 | 252 |
case ET_STYLE_MMSS:
|
| 252 | 253 |
str = fmt.Sprintf("%02d:%02d", minutes, seconds)
|
| 253 | 254 |
}
|
| 254 | |
return wc0.formatMsg(str, widthAccumulator, widthDistributor)
|
|
255 |
return wc0.FormatMsg(str, widthAccumulator, widthDistributor)
|
| 255 | 256 |
})
|
| 256 | 257 |
}
|
| 257 | 258 |
|
|
| 263 | 264 |
if len(wc) > 0 {
|
| 264 | 265 |
wc0 = wc[0]
|
| 265 | 266 |
}
|
|
267 |
wc0.BuildFormat()
|
| 266 | 268 |
return DecoratorFunc(func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
|
| 267 | 269 |
str := fmt.Sprintf("%d %%", CalcPercentage(s.Total, s.Current, 100))
|
| 268 | |
return wc0.formatMsg(str, widthAccumulator, widthDistributor)
|
|
270 |
return wc0.FormatMsg(str, widthAccumulator, widthDistributor)
|
| 269 | 271 |
})
|
| 270 | 272 |
}
|
| 271 | 273 |
|
|
| 322 | 324 |
if len(wc) > 0 {
|
| 323 | 325 |
wc0 = wc[0]
|
| 324 | 326 |
}
|
|
327 |
wc0.BuildFormat()
|
| 325 | 328 |
return DecoratorFunc(func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
|
| 326 | 329 |
var str string
|
| 327 | 330 |
speed := float64(s.Current) / s.TimeElapsed.Seconds()
|
|
| 337 | 340 |
default:
|
| 338 | 341 |
str = fmt.Sprintf(unitFormat, speed)
|
| 339 | 342 |
}
|
| 340 | |
return wc0.formatMsg(str, widthAccumulator, widthDistributor)
|
| 341 | |
})
|
| 342 | |
}
|
|
343 |
return wc0.FormatMsg(str, widthAccumulator, widthDistributor)
|
|
344 |
})
|
|
345 |
}
|