Are you happy with your logging solution? Would you help us out by taking a 30-second survey? Click here

color

Color package for Go (golang)

Subscribe to updates I use color


Statistics on color

Number of watchers on Github 2188
Number of open issues 5
Average time to close an issue about 1 month
Main language Go
Average time to merge a PR about 14 hours
Open pull requests 10+
Closed pull requests 5+
Last commit over 1 year ago
Repo Created over 5 years ago
Repo Last Updated over 1 year ago
Size 1.2 MB
Homepage http://godoc.org/...
Organization / Authorfatih
Latest Releasev1.6.0
Contributors11
Page Updated
Do you use color? Leave a review!
View open issues (5)
View color activity
View on github
Fresh, new opensource launches 🚀🚀🚀
Trendy new open source projects in your inbox! View examples

Subscribe to our mailing list

Evaluating color for your project? Score Explanation
Commits Score (?)
Issues & PR Score (?)

Color GoDoc Build Status

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

Install

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

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

Use your own output (io.Writer)

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

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

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)

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

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

There might be a case where you want to explicitly disable/enable color output. the go-isatty package will automatically disable color output for non-tty output streams (for example if the output were piped directly to less)

Color has support to disable/enable colors both globally and for single color definitions. 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 // disables colorized output
}

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

color open pull requests (View All Pulls)
  • Adding escape function.
  • Suggest using method value instead of Print*Func
  • Add option to escape color attributes for Zsh/Bash prompt
  • Use color cache map
  • Add Fprintf as requested by Francisco
  • Add direct Sprintf support
  • Expose colorable Stderr to use as writer
  • vendor: switch to dep and update dependencies
  • Add a palette for cycling through a set of colors
  • Implement fmt.Formatter for external formatting fatih/color#70.
color questions on Stackoverflow (View All Questions)
  • Cannot change ImageView color on API 16
  • How to use multi color in richtextbox WPF
  • How to give Color changing animation to a custom shape?
  • Replace the specific RGB color with another image using PHP
  • Possible to incorporate color meta tag into semi-transparent background?
  • Change background color of a word in richtextbox
  • python xlwt set color as hex
  • How to change a color of table data in according to date
  • NVD3 Legend Color Guide Rendering Issue - No color appearing in color guide in tooltip within Internet Explorer / Chrome
  • Overlap a transparent foreground of a Relative layout to make text views color gray transparent inside Relative layout
  • How to dynamically change item text color in gridlist with custom adapter?
  • Setting RGBA channels of Color using a Converter
  • Interfacing Color Sensor TCS3200 With TI CC2650
  • how to change the background color of bootstrap navbar
  • imagemagick: wrong color during convertation of png file
  • iOS - Smooth Color Change Transition/Animation
  • Setting color in D3 based on data like a boss
  • Color bar missing in ggplot legend, Windows Remote Desktop
  • continue color scale is not working in ggplot2_2.0.0 in windows remote desktop
  • Background color crash iOS app
  • Color elements given an array of colors
  • How to automatically transfer color rows another sheet in execl?
  • Uipageviewcontroller pager dot color - change diff color for all pages
  • What is the system color of a NSTextField's text?
  • Custom Progress Indicator - Animate subviews color overlay left to right
  • Set border and background color of textView
  • How to change Status Bar text color in iOS 9
  • UIWebView black color at bottom increases with Animation?
  • manipulating luma in YUV color space
  • BufferedImage doesn't change color - Java
color list of languages used
color latest release notes
v1.6.0 v1.6.0
  • Changed vendoring to use dep
  • Updated dependencies to the latest version. Checkout go Gopkg.lock to see the exact versions.
  • Couple of small cosmetic changes in README
v1.5.0 v1.5.0
  • Added new High Intensity Color helper functions gh-68
v1.4.1 v1.4.1

Update github.com/mattn/go-isatty

Other projects in Go