OnCompleteMeta
Vladimir Bauer
2 years ago
| 2 | 2 | var ( |
| 3 | 3 | _ Decorator = onCompleteWrapper{} |
| 4 | 4 | _ Wrapper = onCompleteWrapper{} |
| 5 | _ Decorator = onCompleteMetaWrapper{} | |
| 6 | _ Wrapper = onCompleteMetaWrapper{} | |
| 5 | 7 | ) |
| 6 | 8 | |
| 7 | 9 | // OnComplete wrap decorator. |
| 31 | 33 | func (d onCompleteWrapper) Unwrap() Decorator { |
| 32 | 34 | return d.Decorator |
| 33 | 35 | } |
| 36 | ||
| 37 | // OnCompleteMeta wrap decorator. | |
| 38 | // Provided fn is supposed to wrap output of given decorator | |
| 39 | // with meta information like ANSI escape codes for example. | |
| 40 | // Primary usage intention is to set SGR display attributes. | |
| 41 | // | |
| 42 | // `decorator` Decorator to wrap | |
| 43 | // `fn` func to apply meta information | |
| 44 | func OnCompleteMeta(decorator Decorator, fn func(string) string) Decorator { | |
| 45 | if decorator == nil { | |
| 46 | return nil | |
| 47 | } | |
| 48 | return onCompleteMetaWrapper{decorator, fn} | |
| 49 | } | |
| 50 | ||
| 51 | type onCompleteMetaWrapper struct { | |
| 52 | Decorator | |
| 53 | fn func(string) string | |
| 54 | } | |
| 55 | ||
| 56 | func (d onCompleteMetaWrapper) Decor(s Statistics) (string, int) { | |
| 57 | if s.Completed { | |
| 58 | str, width := d.Decorator.Decor(s) | |
| 59 | return d.fn(str), width | |
| 60 | } | |
| 61 | return d.Decorator.Decor(s) | |
| 62 | } | |
| 63 | ||
| 64 | func (d onCompleteMetaWrapper) Unwrap() Decorator { | |
| 65 | return d.Decorator | |
| 66 | } | |