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