6 | 6 |
"fmt"
|
7 | 7 |
"io"
|
8 | 8 |
"regexp"
|
|
9 |
"strings"
|
9 | 10 |
)
|
10 | 11 |
|
11 | 12 |
// Color colorizes your strings using the default settings.
|
|
24 | 25 |
// If you want to customize any of this behavior, use the Colorize struct.
|
25 | 26 |
func Color(v string) string {
|
26 | 27 |
return def.Color(v)
|
|
28 |
}
|
|
29 |
|
|
30 |
// ColorPrefix returns the color sequence that prefixes the given text.
|
|
31 |
//
|
|
32 |
// This is useful when wrapping text if you want to inherit the color
|
|
33 |
// of the wrapped text. For example, "[green]foo" will return "[green]".
|
|
34 |
// If there is no color sequence, then this will return "".
|
|
35 |
func ColorPrefix(v string) string {
|
|
36 |
return def.ColorPrefix(v)
|
27 | 37 |
}
|
28 | 38 |
|
29 | 39 |
// Colorize colorizes your strings, giving you the ability to customize
|
|
87 | 97 |
}
|
88 | 98 |
|
89 | 99 |
return result.String()
|
|
100 |
}
|
|
101 |
|
|
102 |
// ColorPrefix returns the first color sequence that exists in this string.
|
|
103 |
//
|
|
104 |
// For example: "[green]foo" would return "[green]". If no color sequence
|
|
105 |
// exists, then "" is returned. This is especially useful when wrapping
|
|
106 |
// colored texts to inherit the color of the wrapped text.
|
|
107 |
func (c *Colorize) ColorPrefix(v string) string {
|
|
108 |
return prefixRe.FindString(strings.TrimSpace(v))
|
90 | 109 |
}
|
91 | 110 |
|
92 | 111 |
// DefaultColors are the default colors used when colorizing.
|
|
158 | 177 |
}
|
159 | 178 |
|
160 | 179 |
var def Colorize
|
161 | |
var parseRe = regexp.MustCompile(`(?i)\[[a-z0-9_-]+\]`)
|
|
180 |
var parseReRaw = `\[[a-z0-9_-]+\]`
|
|
181 |
var parseRe = regexp.MustCompile(`(?i)` + parseReRaw)
|
|
182 |
var prefixRe = regexp.MustCompile(`^(?i)(` + parseReRaw + `)+`)
|
162 | 183 |
|
163 | 184 |
// Print is a convenience wrapper for fmt.Print with support for color codes.
|
164 | 185 |
//
|