skip nil decorators
Vladimir Bauer
4 years ago
| 9 | 9 | |
| 10 | 10 | // BarOption is a func option to alter default behavior of a bar. |
| 11 | 11 | type BarOption func(*bState) |
| 12 | ||
| 13 | func skipNil(decorators []decor.Decorator) (filtered []decor.Decorator) { | |
| 14 | for _, d := range decorators { | |
| 15 | if d != nil { | |
| 16 | filtered = append(filtered, d) | |
| 17 | } | |
| 18 | } | |
| 19 | return | |
| 20 | } | |
| 12 | 21 | |
| 13 | 22 | func (s *bState) addDecorators(dest *[]decor.Decorator, decorators ...decor.Decorator) { |
| 14 | 23 | type mergeWrapper interface { |
| 25 | 34 | // AppendDecorators let you inject decorators to the bar's right side. |
| 26 | 35 | func AppendDecorators(decorators ...decor.Decorator) BarOption { |
| 27 | 36 | return func(s *bState) { |
| 28 | s.addDecorators(&s.aDecorators, decorators...) | |
| 37 | s.addDecorators(&s.aDecorators, skipNil(decorators)...) | |
| 29 | 38 | } |
| 30 | 39 | } |
| 31 | 40 | |
| 32 | 41 | // PrependDecorators let you inject decorators to the bar's left side. |
| 33 | 42 | func PrependDecorators(decorators ...decor.Decorator) BarOption { |
| 34 | 43 | return func(s *bState) { |
| 35 | s.addDecorators(&s.pDecorators, decorators...) | |
| 44 | s.addDecorators(&s.pDecorators, skipNil(decorators)...) | |
| 36 | 45 | } |
| 37 | 46 | } |
| 38 | 47 | |