Codebase list golang-github-vbauerster-mpb / 75a6b2e
OnCompleteMeta Vladimir Bauer 2 years ago
1 changed file(s) with 33 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
22 var (
33 _ Decorator = onCompleteWrapper{}
44 _ Wrapper = onCompleteWrapper{}
5 _ Decorator = onCompleteMetaWrapper{}
6 _ Wrapper = onCompleteMetaWrapper{}
57 )
68
79 // OnComplete wrap decorator.
3133 func (d onCompleteWrapper) Unwrap() Decorator {
3234 return d.Decorator
3335 }
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 }