]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/github.com/mitchellh/colorstring/colorstring.go
Upgrade to 0.12
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / mitchellh / colorstring / colorstring.go
1 // colorstring provides functions for colorizing strings for terminal
2 // output.
3 package colorstring
4
5 import (
6 "bytes"
7 "fmt"
8 "io"
9 "regexp"
10 "strings"
11 )
12
13 // Color colorizes your strings using the default settings.
14 //
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.
18 //
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".
21 //
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
24 // terminal session.
25 //
26 // If you want to customize any of this behavior, use the Colorize struct.
27 func Color(v string) string {
28 return def.Color(v)
29 }
30
31 // ColorPrefix returns the color sequence that prefixes the given text.
32 //
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)
38 }
39
40 // Colorize colorizes your strings, giving you the ability to customize
41 // some of the colorization process.
42 //
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
50 // "1;34"
51 Colors map[string]string
52
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.
56 Disable bool
57
58 // Reset, if true, will reset the color after each colorization by
59 // adding a reset code at the end.
60 Reset bool
61 }
62
63 // Color colorizes a string according to the settings setup in the struct.
64 //
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 {
69 return v
70 }
71
72 result := new(bytes.Buffer)
73 colored := false
74 m := []int{0, 0}
75 for _, nm := range matches {
76 // Write the text in between this match and the last
77 result.WriteString(v[m[1]:nm[0]])
78 m = nm
79
80 var replace string
81 if code, ok := c.Colors[v[m[0]+1:m[1]-1]]; ok {
82 colored = true
83
84 if !c.Disable {
85 replace = fmt.Sprintf("\033[%sm", code)
86 }
87 } else {
88 replace = v[m[0]:m[1]]
89 }
90
91 result.WriteString(replace)
92 }
93 result.WriteString(v[m[1]:])
94
95 if colored && c.Reset && !c.Disable {
96 // Write the clear byte at the end
97 result.WriteString("\033[0m")
98 }
99
100 return result.String()
101 }
102
103 // ColorPrefix returns the first color sequence that exists in this string.
104 //
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))
110 }
111
112 // DefaultColors are the default colors used when colorizing.
113 //
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
117
118 func init() {
119 DefaultColors = map[string]string{
120 // Default foreground/background colors
121 "default": "39",
122 "_default_": "49",
123
124 // Foreground colors
125 "black": "30",
126 "red": "31",
127 "green": "32",
128 "yellow": "33",
129 "blue": "34",
130 "magenta": "35",
131 "cyan": "36",
132 "light_gray": "37",
133 "dark_gray": "90",
134 "light_red": "91",
135 "light_green": "92",
136 "light_yellow": "93",
137 "light_blue": "94",
138 "light_magenta": "95",
139 "light_cyan": "96",
140 "white": "97",
141
142 // Background colors
143 "_black_": "40",
144 "_red_": "41",
145 "_green_": "42",
146 "_yellow_": "43",
147 "_blue_": "44",
148 "_magenta_": "45",
149 "_cyan_": "46",
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",
158 "_white_": "107",
159
160 // Attributes
161 "bold": "1",
162 "dim": "2",
163 "underline": "4",
164 "blink_slow": "5",
165 "blink_fast": "6",
166 "invert": "7",
167 "hidden": "8",
168
169 // Reset to reset everything to their defaults
170 "reset": "0",
171 "reset_bold": "21",
172 }
173
174 def = Colorize{
175 Colors: DefaultColors,
176 Reset: true,
177 }
178 }
179
180 var def Colorize
181 var parseReRaw = `\[[a-z0-9_-]+\]`
182 var parseRe = regexp.MustCompile(`(?i)` + parseReRaw)
183 var prefixRe = regexp.MustCompile(`^(?i)(` + parseReRaw + `)+`)
184
185 // Print is a convenience wrapper for fmt.Print with support for color codes.
186 //
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))
193 }
194
195 // Println is a convenience wrapper for fmt.Println with support for color
196 // codes.
197 //
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))
204 }
205
206 // Printf is a convenience wrapper for fmt.Printf with support for color codes.
207 //
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...)
213 }
214
215 // Fprint is a convenience wrapper for fmt.Fprint with support for color codes.
216 //
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
220 // encountered.
221 func Fprint(w io.Writer, a string) (n int, err error) {
222 return fmt.Fprint(w, Color(a))
223 }
224
225 // Fprintln is a convenience wrapper for fmt.Fprintln with support for color
226 // codes.
227 //
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))
234 }
235
236 // Fprintf is a convenience wrapper for fmt.Fprintf with support for color
237 // codes.
238 //
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
241 // encountered.
242 func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
243 return fmt.Fprintf(w, Color(format), a...)
244 }