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

..03-May-2022-

.github/workflows/H07-Jan-2020-

vendor/H03-May-2022-

LICENSE.mdH A D07-Jan-20201.1 KiB

README.mdH A D07-Jan-20204.8 KiB

color.goH A D07-Jan-202017.9 KiB

color_test.goH A D07-Jan-20208.3 KiB

doc.goH A D07-Jan-20204.3 KiB

go.modH A D07-Jan-2020126

go.sumH A D07-Jan-2020753

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