1 // colorstring provides functions for colorizing strings for terminal
13 // Color colorizes your strings using the default settings.
15 // Strings given to Color should use the syntax `[color]` to specify the
16 // color for text following. For example: `[blue]Hello` will return "Hello"
17 // in blue. See DefaultColors for all the supported colors and attributes.
19 // If an unrecognized color is given, it is ignored and assumed to be part
20 // of the string. For example: `[hi]world` will result in "[hi]world".
22 // A color reset is appended to the end of every string. This will reset
23 // the color of following strings when you output this text to the same
26 // If you want to customize any of this behavior, use the Colorize struct.
27 func Color(v string) string {
31 // ColorPrefix returns the color sequence that prefixes the given text.
33 // This is useful when wrapping text if you want to inherit the color
34 // of the wrapped text. For example, "[green]foo" will return "[green]".
35 // If there is no color sequence, then this will return "".
36 func ColorPrefix(v string) string {
37 return def.ColorPrefix(v)
40 // Colorize colorizes your strings, giving you the ability to customize
41 // some of the colorization process.
43 // The options in Colorize can be set to customize colorization. If you're
44 // only interested in the defaults, just use the top Color function directly,
45 // which creates a default Colorize.
46 type Colorize struct {
47 // Colors maps a color string to the code for that color. The code
48 // is a string so that you can use more complex colors to set foreground,
49 // background, attributes, etc. For example, "boldblue" might be
51 Colors map[string]string
53 // If true, color attributes will be ignored. This is useful if you're
54 // outputting to a location that doesn't support colors and you just
55 // want the strings returned.
58 // Reset, if true, will reset the color after each colorization by
59 // adding a reset code at the end.
63 // Color colorizes a string according to the settings setup in the struct.
65 // For more details on the syntax, see the top-level Color function.
66 func (c *Colorize) Color(v string) string {
67 matches := parseRe.FindAllStringIndex(v, -1)
68 if len(matches) == 0 {
72 result := new(bytes.Buffer)
75 for _, nm := range matches {
76 // Write the text in between this match and the last
77 result.WriteString(v[m[1]:nm[0]])
81 if code, ok := c.Colors[v[m[0]+1:m[1]-1]]; ok {
85 replace = fmt.Sprintf("\033[%sm", code)
88 replace = v[m[0]:m[1]]
91 result.WriteString(replace)
93 result.WriteString(v[m[1]:])
95 if colored && c.Reset && !c.Disable {
96 // Write the clear byte at the end
97 result.WriteString("\033[0m")
100 return result.String()
103 // ColorPrefix returns the first color sequence that exists in this string.
105 // For example: "[green]foo" would return "[green]". If no color sequence
106 // exists, then "" is returned. This is especially useful when wrapping
107 // colored texts to inherit the color of the wrapped text.
108 func (c *Colorize) ColorPrefix(v string) string {
109 return prefixRe.FindString(strings.TrimSpace(v))
112 // DefaultColors are the default colors used when colorizing.
114 // If the color is surrounded in underscores, such as "_blue_", then that
115 // color will be used for the background color.
116 var DefaultColors map[string]string
119 DefaultColors = map[string]string{
120 // Default foreground/background colors
136 "light_yellow": "93",
138 "light_magenta": "95",
150 "_light_gray_": "47",
151 "_dark_gray_": "100",
152 "_light_red_": "101",
153 "_light_green_": "102",
154 "_light_yellow_": "103",
155 "_light_blue_": "104",
156 "_light_magenta_": "105",
157 "_light_cyan_": "106",
169 // Reset to reset everything to their defaults
175 Colors: DefaultColors,
181 var parseReRaw = `\[[a-z0-9_-]+\]`
182 var parseRe = regexp.MustCompile(`(?i)` + parseReRaw)
183 var prefixRe = regexp.MustCompile(`^(?i)(` + parseReRaw + `)+`)
185 // Print is a convenience wrapper for fmt.Print with support for color codes.
187 // Print formats using the default formats for its operands and writes to
188 // standard output with support for color codes. Spaces are added between
189 // operands when neither is a string. It returns the number of bytes written
190 // and any write error encountered.
191 func Print(a string) (n int, err error) {
192 return fmt.Print(Color(a))
195 // Println is a convenience wrapper for fmt.Println with support for color
198 // Println formats using the default formats for its operands and writes to
199 // standard output with support for color codes. Spaces are always added
200 // between operands and a newline is appended. It returns the number of bytes
201 // written and any write error encountered.
202 func Println(a string) (n int, err error) {
203 return fmt.Println(Color(a))
206 // Printf is a convenience wrapper for fmt.Printf with support for color codes.
208 // Printf formats according to a format specifier and writes to standard output
209 // with support for color codes. It returns the number of bytes written and any
210 // write error encountered.
211 func Printf(format string, a ...interface{}) (n int, err error) {
212 return fmt.Printf(Color(format), a...)
215 // Fprint is a convenience wrapper for fmt.Fprint with support for color codes.
217 // Fprint formats using the default formats for its operands and writes to w
218 // with support for color codes. Spaces are added between operands when neither
219 // is a string. It returns the number of bytes written and any write error
221 func Fprint(w io.Writer, a string) (n int, err error) {
222 return fmt.Fprint(w, Color(a))
225 // Fprintln is a convenience wrapper for fmt.Fprintln with support for color
228 // Fprintln formats using the default formats for its operands and writes to w
229 // with support for color codes. Spaces are always added between operands and a
230 // newline is appended. It returns the number of bytes written and any write
231 // error encountered.
232 func Fprintln(w io.Writer, a string) (n int, err error) {
233 return fmt.Fprintln(w, Color(a))
236 // Fprintf is a convenience wrapper for fmt.Fprintf with support for color
239 // Fprintf formats according to a format specifier and writes to w with support
240 // for color codes. It returns the number of bytes written and any write error
242 func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
243 return fmt.Fprintf(w, Color(format), a...)