• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..20-Jan-2019-

.travis.ymlH A D20-Jan-201948 65

Gopkg.lockH A D20-Jan-2019725 2822

Gopkg.tomlH A D20-Jan-2019686 3126

LICENSE.mdH A D20-Jan-20191.1 KiB2116

README.mdH A D20-Jan-20195.1 KiB186124

color.goH A D20-Jan-201917.9 KiB604340

color_test.goH A D20-Jan-20198.3 KiB343270

doc.goH A D20-Jan-20194.3 KiB1341

README.md

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