-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcolorgful.go
100 lines (85 loc) · 2.63 KB
/
colorgful.go
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package colorgful
import (
"regexp"
"strings"
"github.com/kovetskiy/lorg"
"github.com/reconquest/loreley"
)
var (
placeholderRegexp = regexp.MustCompile(`\${([^}]+)}`)
)
// Format parses specified formatting (loreley based) and return
// lorg.Formatter.
//
// Following functions are available:
//
// * {bg <color>}, {fg <color>}, {nobg}, {nofg}, {bold}, {nobold}, {reverse},
// {noreverse}, {reset}, {from <text> <bg>}, {to <bg> <text>}.
// * {onlevel <level> <style>} - match given level and insert given style.
// * {ontrace <style>} - alias for {onlevel "trace" <style>}.
// * {ondebug <style>} - alias for {onlevel "debug" <style>}.
// * {oninfo <style>} - alias for {onlevel "info" <style>}.
// * {onwarning <style>} - alias for {onlevel "warning" <style>}.
// * {onerror <style>} - alias for {onlevel "error" <style>}.
// * {onfatal <style>} - alias for {onlevel "fatal" <style>}.
// * {store} - store all previous style
// * {restore} - restore previous style, saved by {store}.
//
func Format(formatting string) (lorg.Formatter, error) {
formatting = placeholderRegexp.ReplaceAllString(
strings.Replace(formatting, `\`, `\\`, -1),
`{lorg "$1"}`,
)
var (
inserter = &inserter{}
restorer = &restorer{}
)
extensions := map[string]interface{}{
"lorg": inserter.insertLorg,
"onlevel": inserter.insertOnLevel,
"restore": inserter.insertRestore,
"store": inserter.insertStore,
"ontrace": func(value string) string {
return inserter.insertOnLevel("trace", value)
},
"ondebug": func(value string) string {
return inserter.insertOnLevel("debug", value)
},
"oninfo": func(value string) string {
return inserter.insertOnLevel("info", value)
},
"onwarning": func(value string) string {
return inserter.insertOnLevel("warning", value)
},
"onerror": func(value string) string {
return inserter.insertOnLevel("error", value)
},
"onfatal": func(value string) string {
return inserter.insertOnLevel("fatal", value)
},
}
style, err := loreley.Compile(
formatting,
extensions,
)
if err != nil {
return nil, err
}
inserter.Style = style
formatting, err = style.ExecuteToString(nil)
if err != nil {
return nil, err
}
format := lorg.NewFormat(formatting)
format.SetPlaceholder("onlevel", restorer.handleOnLevel)
format.SetPlaceholder("restore", restorer.handleRestore)
format.SetPlaceholder("store", restorer.handleStore)
return &formatter{
Format: format,
restorer: restorer,
}, nil
}
// FormatWithReset is same as Format, but appends {reset} to the end.
func FormatWithReset(formatting string) (lorg.Formatter, error) {
return Format(formatting + loreley.StyleReset)
}