...
1# color [![](https://github.com/fatih/color/workflows/build/badge.svg)](https://github.com/fatih/color/actions) [![PkgGoDev](https://pkg.go.dev/badge/github.com/fatih/color)](https://pkg.go.dev/github.com/fatih/color)
2
3Color lets you use colorized outputs in terms of [ANSI Escape
4Codes](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors) in Go (Golang). It
5has support for Windows too! The API can be used in several ways, pick one that
6suits you.
7
8![Color](https://user-images.githubusercontent.com/438920/96832689-03b3e000-13f4-11eb-9803-46f4c4de3406.jpg)
9
10## Install
11
12```bash
13go get github.com/fatih/color
14```
15
16## Examples
17
18### Standard colors
19
20```go
21// Print with default helper functions
22color.Cyan("Prints text in cyan.")
23
24// A newline will be appended automatically
25color.Blue("Prints %s in blue.", "text")
26
27// These are using the default foreground colors
28color.Red("We have red")
29color.Magenta("And many others ..")
30
31```
32
33### Mix and reuse colors
34
35```go
36// Create a new color object
37c := color.New(color.FgCyan).Add(color.Underline)
38c.Println("Prints cyan text with an underline.")
39
40// Or just add them to New()
41d := color.New(color.FgCyan, color.Bold)
42d.Printf("This prints bold cyan %s\n", "too!.")
43
44// Mix up foreground and background colors, create new mixes!
45red := color.New(color.FgRed)
46
47boldRed := red.Add(color.Bold)
48boldRed.Println("This will print text in bold red.")
49
50whiteBackground := red.Add(color.BgWhite)
51whiteBackground.Println("Red text with white background.")
52```
53
54### Use your own output (io.Writer)
55
56```go
57// Use your own io.Writer output
58color.New(color.FgBlue).Fprintln(myWriter, "blue color!")
59
60blue := color.New(color.FgBlue)
61blue.Fprint(writer, "This will print text in blue.")
62```
63
64### Custom print functions (PrintFunc)
65
66```go
67// Create a custom print function for convenience
68red := color.New(color.FgRed).PrintfFunc()
69red("Warning")
70red("Error: %s", err)
71
72// Mix up multiple attributes
73notice := color.New(color.Bold, color.FgGreen).PrintlnFunc()
74notice("Don't forget this...")
75```
76
77### Custom fprint functions (FprintFunc)
78
79```go
80blue := color.New(color.FgBlue).FprintfFunc()
81blue(myWriter, "important notice: %s", stars)
82
83// Mix up with multiple attributes
84success := color.New(color.Bold, color.FgGreen).FprintlnFunc()
85success(myWriter, "Don't forget this...")
86```
87
88### Insert into noncolor strings (SprintFunc)
89
90```go
91// Create SprintXxx functions to mix strings with other non-colorized strings:
92yellow := color.New(color.FgYellow).SprintFunc()
93red := color.New(color.FgRed).SprintFunc()
94fmt.Printf("This is a %s and this is %s.\n", yellow("warning"), red("error"))
95
96info := color.New(color.FgWhite, color.BgGreen).SprintFunc()
97fmt.Printf("This %s rocks!\n", info("package"))
98
99// Use helper functions
100fmt.Println("This", color.RedString("warning"), "should be not neglected.")
101fmt.Printf("%v %v\n", color.GreenString("Info:"), "an important message.")
102
103// Windows supported too! Just don't forget to change the output to color.Output
104fmt.Fprintf(color.Output, "Windows support: %s", color.GreenString("PASS"))
105```
106
107### Plug into existing code
108
109```go
110// Use handy standard colors
111color.Set(color.FgYellow)
112
113fmt.Println("Existing text will now be in yellow")
114fmt.Printf("This one %s\n", "too")
115
116color.Unset() // Don't forget to unset
117
118// You can mix up parameters
119color.Set(color.FgMagenta, color.Bold)
120defer color.Unset() // Use it in your function
121
122fmt.Println("All text will now be bold magenta.")
123```
124
125### Disable/Enable color
126
127There might be a case where you want to explicitly disable/enable color output. the
128`go-isatty` package will automatically disable color output for non-tty output streams
129(for example if the output were piped directly to `less`).
130
131The `color` package also disables color output if the [`NO_COLOR`](https://no-color.org) environment
132variable is set to a non-empty string.
133
134`Color` has support to disable/enable colors programmatically both globally and
135for single color definitions. For example suppose you have a CLI app and a
136`-no-color` bool flag. You can easily disable the color output with:
137
138```go
139var flagNoColor = flag.Bool("no-color", false, "Disable color output")
140
141if *flagNoColor {
142 color.NoColor = true // disables colorized output
143}
144```
145
146It also has support for single color definitions (local). You can
147disable/enable color output on the fly:
148
149```go
150c := color.New(color.FgCyan)
151c.Println("Prints cyan text")
152
153c.DisableColor()
154c.Println("This is printed without any color")
155
156c.EnableColor()
157c.Println("This prints again cyan...")
158```
159
160## GitHub Actions
161
162To output color in GitHub Actions (or other CI systems that support ANSI colors), make sure to set `color.NoColor = false` so that it bypasses the check for non-tty output streams.
163
164## Todo
165
166* Save/Return previous values
167* Evaluate fmt.Formatter interface
168
169## Credits
170
171* [Fatih Arslan](https://github.com/fatih)
172* Windows support via @mattn: [colorable](https://github.com/mattn/go-colorable)
173
174## License
175
176The MIT License (MIT) - see [`LICENSE.md`](https://github.com/fatih/color/blob/master/LICENSE.md) for more details
View as plain text