Categorygithub.com/cppforlife/color
modulepackage
0.2.0
Repository: https://github.com/cppforlife/color.git
Documentation: pkg.go.dev

# README

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

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

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

# Functions

Black is an convenient helper function to print with black foreground.
BlackString is an convenient helper function to return a string with black foreground.
Blue is an convenient helper function to print with blue foreground.
BlueString is an convenient helper function to return a string with blue foreground.
Cyan is an convenient helper function to print with cyan foreground.
CyanString is an convenient helper function to return a string with cyan foreground.
Green is an convenient helper function to print with green foreground.
GreenString is an convenient helper function to return a string with green foreground.
Magenta is an convenient helper function to print with magenta foreground.
MagentaString is an convenient helper function to return a string with magenta foreground.
New returns a newly created color object.
Red is an convenient helper function to print with red foreground.
RedString is an convenient helper function to return a string with red foreground.
Set sets the given parameters immediately.
Unset resets all escape attributes and clears the output.
White is an convenient helper function to print with white foreground.
WhiteString is an convenient helper function to return a string with white foreground.
Yellow is an convenient helper function to print with yellow foreground.
YellowString is an convenient helper function to return a string with yellow foreground.

# Constants

Background text colors.
Background text colors.
Background text colors.
Background text colors.
Background Hi-Intensity text colors.
Background Hi-Intensity text colors.
Background Hi-Intensity text colors.
Background Hi-Intensity text colors.
Background Hi-Intensity text colors.
Background Hi-Intensity text colors.
Background Hi-Intensity text colors.
Background Hi-Intensity text colors.
Background text colors.
Background text colors.
Background text colors.
Background text colors.
Base attributes.
Base attributes.
Base attributes.
Base attributes.
Base attributes.
Base attributes.
Foreground text colors.
Foreground text colors.
Foreground text colors.
Foreground text colors.
Foreground Hi-Intensity text colors.
Foreground Hi-Intensity text colors.
Foreground Hi-Intensity text colors.
Foreground Hi-Intensity text colors.
Foreground Hi-Intensity text colors.
Foreground Hi-Intensity text colors.
Foreground Hi-Intensity text colors.
Foreground Hi-Intensity text colors.
Foreground text colors.
Foreground text colors.
Foreground text colors.
Foreground text colors.
Base attributes.
Base attributes.
Base attributes.
Base attributes.

# Variables

NoColor defines if the output is colorized or not.
Output defines the standard output of the print functions.

# Structs

Color defines a custom color object which is defined by SGR parameters.

# Type aliases

Attribute defines a single SGR Code.