|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- # Color [![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/fatih/color) [![Build Status](http://img.shields.io/travis/fatih/color.svg?style=flat-square)](https://travis-ci.org/fatih/color)
-
-
-
- Color lets you use colorized outputs in terms of [ANSI Escape
- Codes](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors) 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
-
- ```bash
- go get github.com/fatih/color
- ```
-
- Note that the `vendor` folder is here for stability. Remove the folder if you
- already have the dependencies in your GOPATH.
-
- ## Examples
-
- ### Standard colors
-
- ```go
- // Print with default helper functions
- color.Cyan("Prints text in cyan.")
-
- // A newline will be appended automatically
- color.Blue("Prints %s in blue.", "text")
-
- // These are using the default foreground colors
- color.Red("We have red")
- color.Magenta("And many others ..")
-
- ```
-
- ### Mix and reuse colors
-
- ```go
- // Create a new color object
- c := color.New(color.FgCyan).Add(color.Underline)
- c.Println("Prints cyan text with an underline.")
-
- // Or just add them to New()
- d := color.New(color.FgCyan, color.Bold)
- d.Printf("This prints bold cyan %s\n", "too!.")
-
- // Mix up foreground and background colors, create new mixes!
- 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.")
- ```
-
- ### Use your own output (io.Writer)
-
- ```go
- // Use your own io.Writer output
- color.New(color.FgBlue).Fprintln(myWriter, "blue color!")
-
- blue := color.New(color.FgBlue)
- blue.Fprint(writer, "This will print text in blue.")
- ```
-
- ### Custom print functions (PrintFunc)
-
- ```go
- // Create a custom print function for convenience
- red := color.New(color.FgRed).PrintfFunc()
- red("Warning")
- red("Error: %s", err)
-
- // Mix up multiple attributes
- notice := color.New(color.Bold, color.FgGreen).PrintlnFunc()
- notice("Don't forget this...")
- ```
-
- ### Custom fprint functions (FprintFunc)
-
- ```go
- blue := color.New(FgBlue).FprintfFunc()
- blue(myWriter, "important notice: %s", stars)
-
- // Mix up with multiple attributes
- success := color.New(color.Bold, color.FgGreen).FprintlnFunc()
- success(myWriter, "Don't forget this...")
- ```
-
- ### Insert into noncolor strings (SprintFunc)
-
- ```go
- // Create SprintXxx functions to mix strings with other non-colorized strings:
- 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"))
-
- // Use helper functions
- fmt.Println("This", color.RedString("warning"), "should be not neglected.")
- fmt.Printf("%v %v\n", color.GreenString("Info:"), "an important message.")
-
- // Windows supported too! Just don't forget to change the output to color.Output
- fmt.Fprintf(color.Output, "Windows support: %s", color.GreenString("PASS"))
- ```
-
- ### Plug into existing code
-
- ```go
- // Use handy standard colors
- color.Set(color.FgYellow)
-
- fmt.Println("Existing text will now be in yellow")
- fmt.Printf("This one %s\n", "too")
-
- color.Unset() // Don't forget to unset
-
- // You can mix up parameters
- color.Set(color.FgMagenta, color.Bold)
- defer color.Unset() // Use it in your function
-
- 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:
-
- ```go
-
- var flagNoColor = flag.Bool("no-color", false, "Disable color output")
-
- if *flagNoColor {
- color.NoColor = true // disables colorized output
- }
- ```
-
- It also has support for single color definitions (local). You can
- disable/enable color output on the fly:
-
- ```go
- 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
-
- * [Fatih Arslan](https://github.com/fatih)
- * Windows support via @mattn: [colorable](https://github.com/mattn/go-colorable)
-
- ## License
-
- The MIT License (MIT) - see [`LICENSE.md`](https://github.com/fatih/color/blob/master/LICENSE.md) for more details
-
|