Codebase list golang-github-vbauerster-mpb / 89321e7
OnAbortMeta 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 = onAbortWrapper{}
44 _ Wrapper = onAbortWrapper{}
5 _ Decorator = onAbortMetaWrapper{}
6 _ Wrapper = onAbortMetaWrapper{}
57 )
68
79 // OnAbort wrap decorator.
3234 func (d onAbortWrapper) Unwrap() Decorator {
3335 return d.Decorator
3436 }
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 }