summaryrefslogtreecommitdiffstats
path: root/decor/meta.go
blob: 0045a31f4b90f82fca08a4feb78b74432fedaa03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package decor

var (
	_ Decorator = metaWrapper{}
	_ Wrapper   = metaWrapper{}
)

// Meta wrap decorator.
// Provided fn is supposed to wrap output of given decorator
// with meta information like ANSI escape codes for example.
// Primary usage intention is to set SGR display attributes.
//
//	`decorator` Decorator to wrap
//	`fn` func to apply meta information
func Meta(decorator Decorator, fn func(string) string) Decorator {
	if decorator == nil {
		return nil
	}
	return metaWrapper{decorator, fn}
}

type metaWrapper struct {
	Decorator
	fn func(string) string
}

func (d metaWrapper) Decor(s Statistics) (string, int) {
	str, width := d.Decorator.Decor(s)
	return d.fn(str), width
}

func (d metaWrapper) Unwrap() Decorator {
	return d.Decorator
}