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