bar filler: prefer meta with explicit string type
Vladimir Bauer
2 years ago
| 23 | 23 | type BarStyleComposer interface { |
| 24 | 24 | BarFillerBuilder |
| 25 | 25 | Lbound(string) BarStyleComposer |
| 26 | LboundMeta(func(...interface{}) string) BarStyleComposer | |
| 26 | LboundMeta(func(string) string) BarStyleComposer | |
| 27 | 27 | Rbound(string) BarStyleComposer |
| 28 | RboundMeta(func(...interface{}) string) BarStyleComposer | |
| 28 | RboundMeta(func(string) string) BarStyleComposer | |
| 29 | 29 | Filler(string) BarStyleComposer |
| 30 | FillerMeta(func(...interface{}) string) BarStyleComposer | |
| 30 | FillerMeta(func(string) string) BarStyleComposer | |
| 31 | 31 | Refiller(string) BarStyleComposer |
| 32 | RefillerMeta(func(...interface{}) string) BarStyleComposer | |
| 32 | RefillerMeta(func(string) string) BarStyleComposer | |
| 33 | 33 | Padding(string) BarStyleComposer |
| 34 | PaddingMeta(func(...interface{}) string) BarStyleComposer | |
| 34 | PaddingMeta(func(string) string) BarStyleComposer | |
| 35 | 35 | Tip(frames ...string) BarStyleComposer |
| 36 | TipMeta(func(...interface{}) string) BarStyleComposer | |
| 36 | TipMeta(func(string) string) BarStyleComposer | |
| 37 | 37 | TipOnComplete() BarStyleComposer |
| 38 | 38 | Reverse() BarStyleComposer |
| 39 | 39 | } |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | type flushSection struct { |
| 47 | meta func(io.Writer, ...interface{}) error | |
| 47 | meta func(io.Writer, []byte) error | |
| 48 | 48 | bytes []byte |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | type bFiller struct { |
| 52 | 52 | components [components]component |
| 53 | meta [components]func(io.Writer, ...interface{}) error | |
| 53 | meta [components]func(io.Writer, []byte) error | |
| 54 | 54 | flush func(w io.Writer, sections ...flushSection) error |
| 55 | 55 | tipOnComplete bool |
| 56 | 56 | tip struct { |
| 61 | 61 | |
| 62 | 62 | type barStyle struct { |
| 63 | 63 | style [components]string |
| 64 | metaFuncs [components]func(io.Writer, ...interface{}) error | |
| 64 | metaFuncs [components]func(io.Writer, []byte) error | |
| 65 | 65 | tipFrames []string |
| 66 | 66 | tipOnComplete bool |
| 67 | 67 | rev bool |
| 85 | 85 | return s |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | func (s *barStyle) LboundMeta(fn func(...interface{}) string) BarStyleComposer { | |
| 88 | func (s *barStyle) LboundMeta(fn func(string) string) BarStyleComposer { | |
| 89 | 89 | s.metaFuncs[iLbound] = makeMetaFunc(fn) |
| 90 | 90 | return s |
| 91 | 91 | } |
| 95 | 95 | return s |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | func (s *barStyle) RboundMeta(fn func(...interface{}) string) BarStyleComposer { | |
| 98 | func (s *barStyle) RboundMeta(fn func(string) string) BarStyleComposer { | |
| 99 | 99 | s.metaFuncs[iRbound] = makeMetaFunc(fn) |
| 100 | 100 | return s |
| 101 | 101 | } |
| 105 | 105 | return s |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | func (s *barStyle) FillerMeta(fn func(...interface{}) string) BarStyleComposer { | |
| 108 | func (s *barStyle) FillerMeta(fn func(string) string) BarStyleComposer { | |
| 109 | 109 | s.metaFuncs[iFiller] = makeMetaFunc(fn) |
| 110 | 110 | return s |
| 111 | 111 | } |
| 115 | 115 | return s |
| 116 | 116 | } |
| 117 | 117 | |
| 118 | func (s *barStyle) RefillerMeta(fn func(...interface{}) string) BarStyleComposer { | |
| 118 | func (s *barStyle) RefillerMeta(fn func(string) string) BarStyleComposer { | |
| 119 | 119 | s.metaFuncs[iRefiller] = makeMetaFunc(fn) |
| 120 | 120 | return s |
| 121 | 121 | } |
| 125 | 125 | return s |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | func (s *barStyle) PaddingMeta(fn func(...interface{}) string) BarStyleComposer { | |
| 128 | func (s *barStyle) PaddingMeta(fn func(string) string) BarStyleComposer { | |
| 129 | 129 | s.metaFuncs[iPadding] = makeMetaFunc(fn) |
| 130 | 130 | return s |
| 131 | 131 | } |
| 137 | 137 | return s |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | func (s *barStyle) TipMeta(fn func(...interface{}) string) BarStyleComposer { | |
| 140 | func (s *barStyle) TipMeta(fn func(string) string) BarStyleComposer { | |
| 141 | 141 | s.metaFuncs[iTip] = makeMetaFunc(fn) |
| 142 | 142 | return s |
| 143 | 143 | } |
| 276 | 276 | return s.meta[iRbound](w, s.components[iRbound].bytes) |
| 277 | 277 | } |
| 278 | 278 | |
| 279 | func makeMetaFunc(fn func(...interface{}) string) func(io.Writer, ...interface{}) error { | |
| 280 | return func(w io.Writer, a ...interface{}) (err error) { | |
| 281 | for i := 0; i < len(a) && err == nil; i++ { | |
| 282 | _, err = io.WriteString(w, fn(string(a[i].([]byte)))) | |
| 283 | } | |
| 279 | func makeMetaFunc(fn func(string) string) func(io.Writer, []byte) error { | |
| 280 | return func(w io.Writer, p []byte) (err error) { | |
| 281 | _, err = io.WriteString(w, fn(string(p))) | |
| 284 | 282 | return err |
| 285 | 283 | } |
| 286 | 284 | } |
| 287 | 285 | |
| 288 | func defaultMeta(w io.Writer, a ...interface{}) (err error) { | |
| 289 | for i := 0; i < len(a) && err == nil; i++ { | |
| 290 | _, err = w.Write(a[i].([]byte)) | |
| 291 | } | |
| 286 | func defaultMeta(w io.Writer, p []byte) (err error) { | |
| 287 | _, err = w.Write(p) | |
| 292 | 288 | return err |
| 293 | 289 | } |