Codebase list golang-github-vbauerster-mpb / ac5077b
decorators signature change Vladimir Bauer 9 years ago
1 changed file(s) with 177 addition(s) and 60 deletion(s). Raw diff Collapse all Expand all
11
22 import (
33 "fmt"
4 "strconv"
54 "time"
5 "unicode/utf8"
6 )
7
8 const (
9 // DidentRight specifies identation direction.
10 // | foo| b| Without DidentRight
11 // |foo |b | With DidentRight
12 DidentRight = 1 << iota
13
14 // DwidthSync will auto sync max width
15 DwidthSync
16
17 // DextraSpace adds extra space, makes sence with DwidthSync only.
18 // When DidentRight bit set, the space will be added to the right,
19 // otherwise to the left.
20 DextraSpace
621 )
722
823 type decoratorOperation uint
2237 f DecoratorFunc
2338 }
2439
25 func (b *Bar) PrependName(name string, padding int) *Bar {
26 layout := "%" + strconv.Itoa(padding) + "s"
27 b.PrependFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
28 return fmt.Sprintf(layout, name)
29 })
30 return b
31 }
32
33 func (b *Bar) PrependCounters(unit Units, padding int) *Bar {
34 layout := "%" + strconv.Itoa(padding) + "s"
40 func (b *Bar) PrependName(name string, minWidth int, conf byte) *Bar {
41 format := "%%"
42 if (conf & DidentRight) != 0 {
43 format += "-"
44 }
45 format += "%ds"
46 b.PrependFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
47 if (conf & DwidthSync) != 0 {
48 myWidth <- utf8.RuneCountInString(name)
49 max := <-maxWidth
50 if (conf & DextraSpace) != 0 {
51 max++
52 }
53 return fmt.Sprintf(fmt.Sprintf(format, max), name)
54 }
55 return fmt.Sprintf(fmt.Sprintf(format, minWidth), name)
56 })
57 return b
58 }
59
60 func (b *Bar) PrependCounters(unit Units, minWidth int, conf byte) *Bar {
61 format := "%%"
62 if (conf & DidentRight) != 0 {
63 format += "-"
64 }
65 format += "%ds"
3566 b.PrependFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
3667 current := Format(s.Current).To(unit)
3768 total := Format(s.Total).To(unit)
3869 str := fmt.Sprintf("%s / %s", current, total)
39 return fmt.Sprintf(layout, str)
40 })
41 return b
42 }
43
44 func (b *Bar) PrependETA(padding int) *Bar {
45 layout := "ETA%" + strconv.Itoa(padding) + "s"
46 b.PrependFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
47 return fmt.Sprintf(layout, time.Duration(s.Eta().Seconds())*time.Second)
48 })
49 return b
50 }
51
52 func (b *Bar) AppendETA(padding int) *Bar {
53 layout := "ETA %" + strconv.Itoa(padding) + "s"
70 if (conf & DwidthSync) != 0 {
71 myWidth <- utf8.RuneCountInString(str)
72 max := <-maxWidth
73 if (conf & DextraSpace) != 0 {
74 max++
75 }
76 return fmt.Sprintf(fmt.Sprintf(format, max), str)
77 }
78 return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
79 })
80 return b
81 }
82
83 func (b *Bar) PrependETA(minWidth int, conf byte) *Bar {
84 format := "%%"
85 if (conf & DidentRight) != 0 {
86 format += "-"
87 }
88 format += "%ds"
89 b.PrependFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
90 str := fmt.Sprint(time.Duration(s.Eta().Seconds()) * time.Second)
91 if (conf & DwidthSync) != 0 {
92 myWidth <- utf8.RuneCountInString(str)
93 max := <-maxWidth
94 if (conf & DextraSpace) != 0 {
95 max++
96 }
97 return fmt.Sprintf(fmt.Sprintf(format, max), str)
98 }
99 return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
100 })
101 return b
102 }
103
104 func (b *Bar) AppendETA(minWidth int, conf byte) *Bar {
105 format := "%%"
106 if (conf & DidentRight) != 0 {
107 format += "-"
108 }
109 format += "%ds"
54110 b.AppendFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
55 return fmt.Sprintf(layout, time.Duration(s.Eta().Seconds())*time.Second)
56 })
57 return b
58 }
59
60 func (b *Bar) PrependElapsed(padding int) *Bar {
61 layout := "%" + strconv.Itoa(padding) + "s"
62 b.PrependFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
63 return fmt.Sprintf(layout, time.Duration(s.TimeElapsed.Seconds())*time.Second)
64 })
65 return b
66 }
67
68 func (b *Bar) AppendElapsed() *Bar {
111 str := fmt.Sprint(time.Duration(s.Eta().Seconds()) * time.Second)
112 if (conf & DwidthSync) != 0 {
113 myWidth <- utf8.RuneCountInString(str)
114 max := <-maxWidth
115 if (conf & DextraSpace) != 0 {
116 max++
117 }
118 return fmt.Sprintf(fmt.Sprintf(format, max), str)
119 }
120 return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
121 })
122 return b
123 }
124
125 func (b *Bar) PrependElapsed(minWidth int, conf byte) *Bar {
126 format := "%%"
127 if (conf & DidentRight) != 0 {
128 format += "-"
129 }
130 format += "%ds"
131 b.PrependFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
132 str := fmt.Sprint(time.Duration(s.TimeElapsed.Seconds()) * time.Second)
133 if (conf & DwidthSync) != 0 {
134 myWidth <- utf8.RuneCountInString(str)
135 max := <-maxWidth
136 if (conf & DextraSpace) != 0 {
137 max++
138 }
139 return fmt.Sprintf(fmt.Sprintf(format, max), str)
140 }
141 return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
142 })
143 return b
144 }
145
146 func (b *Bar) AppendElapsed(minWidth int, conf byte) *Bar {
147 format := "%%"
148 if (conf & DidentRight) != 0 {
149 format += "-"
150 }
151 format += "%ds"
69152 b.AppendFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
70 return fmt.Sprint(time.Duration(s.TimeElapsed.Seconds()) * time.Second)
71 })
72 return b
73 }
74
75 func (b *Bar) AppendPercentage() *Bar {
153 str := fmt.Sprint(time.Duration(s.TimeElapsed.Seconds()) * time.Second)
154 if (conf & DwidthSync) != 0 {
155 myWidth <- utf8.RuneCountInString(str)
156 max := <-maxWidth
157 if (conf & DextraSpace) != 0 {
158 max++
159 }
160 return fmt.Sprintf(fmt.Sprintf(format, max), str)
161 }
162 return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
163 })
164 return b
165 }
166
167 func (b *Bar) AppendPercentage(minWidth int, conf byte) *Bar {
168 format := "%%"
169 if (conf & DidentRight) != 0 {
170 format += "-"
171 }
172 format += "%ds"
76173 b.AppendFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
77 completed := percentage(s.Total, s.Current, 100)
78 return fmt.Sprintf("%3d %%", completed)
79 })
80 return b
81 }
82
83 func (b *Bar) PrependPercentage(padding int) *Bar {
84 layout := "%" + strconv.Itoa(padding) + "d %%"
85 b.PrependFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
86 completed := percentage(s.Total, s.Current, 100)
87 return fmt.Sprintf(layout, completed)
88 })
89 return b
90 }
174 str := fmt.Sprintf("%d %%", percentage(s.Total, s.Current, 100))
175 if (conf & DwidthSync) != 0 {
176 myWidth <- utf8.RuneCountInString(str)
177 max := <-maxWidth
178 if (conf & DextraSpace) != 0 {
179 max++
180 }
181 return fmt.Sprintf(fmt.Sprintf(format, max), str)
182 }
183 return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
184 })
185 return b
186 }
187
188 func (b *Bar) PrependPercentage(minWidth int, conf byte) *Bar {
189 format := "%%"
190 if (conf & DidentRight) != 0 {
191 format += "-"
192 }
193 format += "%ds"
194 b.PrependFunc(func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
195 str := fmt.Sprintf("%d %%", percentage(s.Total, s.Current, 100))
196 if (conf & DwidthSync) != 0 {
197 myWidth <- utf8.RuneCountInString(str)
198 max := <-maxWidth
199 if (conf & DextraSpace) != 0 {
200 max++
201 }
202 return fmt.Sprintf(fmt.Sprintf(format, max), str)
203 }
204 return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
205 })
206 return b
207 }