1
0
mirror of https://github.com/fatih/color.git synced 2024-11-24 08:02:14 +02:00
Color package for Go (golang)
Go to file
2014-12-27 13:18:19 -08:00
.travis.yml Update to Go version 1.3 2014-06-24 11:56:10 -07:00
color_test.go Fix Readme.md, add colorstring helper functions 2014-02-24 10:01:46 -08:00
color.go Fix Readme.md, add colorstring helper functions 2014-02-24 10:01:46 -08:00
doc.go Add documentation about Sprint functions. 2014-02-18 03:26:43 -08:00
LICENSE.md Improve README and add examples to test. 2014-02-17 01:40:02 -08:00
README.md Update README.md 2014-12-27 13:18:19 -08:00

Color GoDoc Build Status

Color lets you use colorized outputs in terms of ANSI Escape Codes. The API can be used in several ways, pick one that suits you.

Color

Install

go get github.com/fatih/color

Examples

Standard colors

// 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

// 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.")

Custom print functions (PrintFunc)

// 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...")

Insert into noncolor strings (SprintFunc)

// 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.Printf("This", color.RedString("warning"), "should be not neglected.")
fmt.Printf(color.GreenString("Info:"), "an important message." )

Plug into existing code

// 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.")

Todo

  • Save/Return previous values
  • Add Windows support
  • Evaluate fmt.Formatter interface

Credits

License

The MIT License (MIT) - see LICENSE.md for more details