Color
![Build Status](http://img.shields.io/travis/fatih/color.svg?style=flat-square)
Color lets you use colorized outputs in terms of ANSI Escape
Codes in Go (Golang). It
has support for Windows too! The API can be used in several ways, pick one that
suits you.
![Color](http://i.imgur.com/c1JI0lA.png)
Install
go get github.com/fatih/color
Examples
Standard colors
color.Cyan("Prints text in cyan.")
color.Blue("Prints %s in blue.", "text")
color.Red("We have red")
color.Magenta("And many others ..")
Mix and reuse colors
c := color.New(color.FgCyan).Add(color.Underline)
c.Println("Prints cyan text with an underline.")
d := color.New(color.FgCyan, color.Bold)
d.Printf("This prints bold cyan %s\n", "too!.")
red := color.New(color.FgRed)
boldRed := red.Add(color.Bold)
boldRed.Println("This will print text in bold red.")
whiteBackground := red.Add(color.BgWhite)
whiteBackground.Println("Red text with white background.")
Custom print functions (PrintFunc)
red := color.New(color.FgRed).PrintfFunc()
red("Warning")
red("Error: %s", err)
notice := color.New(color.Bold, color.FgGreen).PrintlnFunc()
notice("Don't forget this...")
Insert into noncolor strings (SprintFunc)
yellow := color.New(color.FgYellow).SprintFunc()
red := color.New(color.FgRed).SprintFunc()
fmt.Printf("This is a %s and this is %s.\n", yellow("warning"), red("error"))
info := color.New(color.FgWhite, color.BgGreen).SprintFunc()
fmt.Printf("This %s rocks!\n", info("package"))
fmt.Printf("This", color.RedString("warning"), "should be not neglected.")
fmt.Printf(color.GreenString("Info:"), "an important message." )
fmt.Fprintf(color.Output, "Windows support: %s", color.GreenString("PASS"))
Plug into existing code
color.Set(color.FgYellow)
fmt.Println("Existing text will now be in yellow")
fmt.Printf("This one %s\n", "too")
color.Unset()
color.Set(color.FgMagenta, color.Bold)
defer color.Unset()
fmt.Println("All text will now be bold magenta.")
Disable color
There might be a case where you want to disable color output (for example to
pipe the standard output of your app to somewhere else). Color
has support to
disable colors both globally and for single color definition. For example
suppose you have a CLI app and a --no-color
bool flag. You can easily disable
the color output with:
var flagNoColor = flag.Bool("no-color", false, "Disable color output")
if *flagNoColor {
color.NoColor = true
}
It also has support for single color definitions (local). You can
disable/enable color output on the fly:
c := color.New(color.FgCyan)
c.Println("Prints cyan text")
c.DisableColor()
c.Println("This is printed without any color")
c.EnableColor()
c.Println("This prints again cyan...")
Todo
- Save/Return previous values
- Evaluate fmt.Formatter interface
Credits
License
The MIT License (MIT) - see LICENSE.md
for more details