Codebase list golang-github-vbauerster-mpb / 41b66b0
meta decorator Vladimir Bauer 2 years ago
1 changed file(s) with 34 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 package decor
1
2 var (
3 _ Decorator = metaWrapper{}
4 _ Wrapper = metaWrapper{}
5 )
6
7 // Meta wrap decorator.
8 // Provided fn is supposed to wrap output of given decorator
9 // with meta information like ANSI escape codes for example.
10 // Primary usage intention is to set SGR display attributes.
11 //
12 // `decorator` Decorator to wrap
13 // `fn` func to apply meta information
14 func Meta(decorator Decorator, fn func(string) string) Decorator {
15 if decorator == nil {
16 return nil
17 }
18 return metaWrapper{decorator, fn}
19 }
20
21 type metaWrapper struct {
22 Decorator
23 fn func(string) string
24 }
25
26 func (d metaWrapper) Decor(s Statistics) (string, int) {
27 str, width := d.Decorator.Decor(s)
28 return d.fn(str), width
29 }
30
31 func (d metaWrapper) Unwrap() Decorator {
32 return d.Decorator
33 }