Codebase list golang-github-vbauerster-mpb / 407252a
check for Write error Vladimir Bauer 4 years ago
2 changed file(s) with 33 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
3131 }
3232
3333 type bFiller struct {
34 rev bool
3435 components [components]*component
3536 tip struct {
3637 count uint
3738 onComplete *component
3839 frames []*component
3940 }
40 flush func(dst io.Writer, filling, padding [][]byte)
4141 }
4242
4343 type component struct {
112112 }
113113
114114 func (s *barStyle) Build() BarFiller {
115 bf := new(bFiller)
116 if s.rev {
117 bf.flush = func(dst io.Writer, filling, padding [][]byte) {
118 flush(dst, padding, filling)
119 }
120 } else {
121 bf.flush = flush
122 }
115 bf := &bFiller{rev: s.rev}
123116 bf.components[iLbound] = &component{
124117 width: runewidth.StringWidth(stripansi.Strip(s.lbound)),
125118 bytes: []byte(s.lbound),
163156 return
164157 }
165158
166 w.Write(s.components[iLbound].bytes)
167 defer w.Write(s.components[iRbound].bytes)
159 ow := optimisticWriter(w)
160 ow(s.components[iLbound].bytes)
161 defer ow(s.components[iRbound].bytes)
168162
169163 if width == 0 {
170164 return
235229 }
236230 }
237231
238 s.flush(w, filling, padding)
239 }
240
241 func flush(dst io.Writer, filling, padding [][]byte) {
232 if s.rev {
233 flush(ow, padding, filling)
234 } else {
235 flush(ow, filling, padding)
236 }
237 }
238
239 func flush(ow func([]byte), filling, padding [][]byte) {
242240 for i := len(filling) - 1; i >= 0; i-- {
243 dst.Write(filling[i])
241 ow(filling[i])
244242 }
245243 for i := 0; i < len(padding); i++ {
246 dst.Write(padding[i])
247 }
248 }
244 ow(padding[i])
245 }
246 }
247
248 func optimisticWriter(w io.Writer) func([]byte) {
249 return func(p []byte) {
250 _, err := w.Write(p)
251 if err != nil {
252 panic(err)
253 }
254 }
255 }
7272 return
7373 }
7474
75 var err error
7576 rest := width - frameWidth
7677 switch s.position {
7778 case positionLeft:
78 io.WriteString(w, frame+strings.Repeat(" ", rest))
79 _, err = io.WriteString(w, frame+strings.Repeat(" ", rest))
7980 case positionRight:
80 io.WriteString(w, strings.Repeat(" ", rest)+frame)
81 _, err = io.WriteString(w, strings.Repeat(" ", rest)+frame)
8182 default:
8283 str := strings.Repeat(" ", rest/2) + frame + strings.Repeat(" ", rest/2+rest%2)
83 io.WriteString(w, str)
84 _, err = io.WriteString(w, str)
85 }
86 if err != nil {
87 panic(err)
8488 }
8589 s.count++
8690 }