Codebase list golang-github-vbauerster-mpb / 68a4154
Expose WCs BuildFormat and FormatMsg methods Vladimir Bauer 8 years ago
2 changed file(s) with 27 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
8989 format string
9090 }
9191
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 {
9495 if (wc.C & DSyncWidth) != 0 {
9596 widthAccumulator <- utf8.RuneCountInString(msg)
9697 max := <-widthDistributor
100101 if (wc.C & DextraSpace) != 0 {
101102 max++
102103 }
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() {
112111 wc.format = "%%"
113112 if (wc.C & DidentRight) != 0 {
114113 wc.format += "-"
115114 }
116115 wc.format += "%ds"
117 return wc.format
118116 }
119117
120118 // Global convenience shortcuts
166164 if len(wc) > 0 {
167165 wc0 = wc[0]
168166 }
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)
171170 })
172171 }
173172
211210 if len(wc) > 0 {
212211 wc0 = wc[0]
213212 }
213 wc0.BuildFormat()
214214 return DecoratorFunc(func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
215215 var str string
216216 switch unit {
221221 default:
222222 str = fmt.Sprintf(pairFormat, s.Current, s.Total)
223223 }
224 return wc0.formatMsg(str, widthAccumulator, widthDistributor)
224 return wc0.FormatMsg(str, widthAccumulator, widthDistributor)
225225 })
226226 }
227227
235235 if len(wc) > 0 {
236236 wc0 = wc[0]
237237 }
238 wc0.BuildFormat()
238239 return DecoratorFunc(func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
239240 var str string
240241 hours := int64((s.TimeElapsed / time.Hour) % 60)
251252 case ET_STYLE_MMSS:
252253 str = fmt.Sprintf("%02d:%02d", minutes, seconds)
253254 }
254 return wc0.formatMsg(str, widthAccumulator, widthDistributor)
255 return wc0.FormatMsg(str, widthAccumulator, widthDistributor)
255256 })
256257 }
257258
263264 if len(wc) > 0 {
264265 wc0 = wc[0]
265266 }
267 wc0.BuildFormat()
266268 return DecoratorFunc(func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
267269 str := fmt.Sprintf("%d %%", CalcPercentage(s.Total, s.Current, 100))
268 return wc0.formatMsg(str, widthAccumulator, widthDistributor)
270 return wc0.FormatMsg(str, widthAccumulator, widthDistributor)
269271 })
270272 }
271273
322324 if len(wc) > 0 {
323325 wc0 = wc[0]
324326 }
327 wc0.BuildFormat()
325328 return DecoratorFunc(func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
326329 var str string
327330 speed := float64(s.Current) / s.TimeElapsed.Seconds()
337340 default:
338341 str = fmt.Sprintf(unitFormat, speed)
339342 }
340 return wc0.formatMsg(str, widthAccumulator, widthDistributor)
341 })
342 }
343 return wc0.FormatMsg(str, widthAccumulator, widthDistributor)
344 })
345 }
2525 if len(wc) > 0 {
2626 wc0 = wc[0]
2727 }
28 wc0.BuildFormat()
2829 if age == .0 {
2930 age = ewma.AVG_METRIC_AGE
3031 }
5354
5455 func (s *ewmaETA) Decor(st *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
5556 if st.Completed && s.onComplete != nil {
56 return s.onComplete.wc.formatMsg(s.onComplete.msg, widthAccumulator, widthDistributor)
57 return s.onComplete.wc.FormatMsg(s.onComplete.msg, widthAccumulator, widthDistributor)
5758 }
5859
5960 var str string
7374 str = fmt.Sprintf("%02d:%02d", minutes, seconds)
7475 }
7576
76 return s.wc.formatMsg(str, widthAccumulator, widthDistributor)
77 return s.wc.FormatMsg(str, widthAccumulator, widthDistributor)
7778 }
7879
7980 func (s *ewmaETA) NextAmount(n int) {
8889 if len(wc) > 0 {
8990 wc0 = wc[0]
9091 }
92 wc0.BuildFormat()
9193 s.onComplete = &struct {
9294 msg string
9395 wc WC