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