Colour
Colour lets you use colourized 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.
Install
go get github.com/felix/colour
Examples
Standard colours
colour.Cyan("Prints text in cyan.")
colour.Blue("Prints %s in blue.", "text")
colour.Red("We have red")
colour.Magenta("And many others ..")
Mix and reuse colours
c := colour.New(colour.FgCyan).Add(colour.Underline)
c.Println("Prints cyan text with an underline.")
d := colour.New(colour.FgCyan, colour.Bold)
d.Printf("This prints bold cyan %s\n", "too!.")
red := colour.New(colour.FgRed)
boldRed := red.Add(colour.Bold)
boldRed.Println("This will print text in bold red.")
whiteBackground := red.Add(colour.BgWhite)
whiteBackground.Println("Red text with white background.")
Use your own output (io.Writer)
colour.New(colour.FgBlue).Fprintln(myWriter, "blue colour!")
blue := colour.New(colour.FgBlue)
blue.Fprint(writer, "This will print text in blue.")
Custom print functions (PrintFunc)
red := colour.New(colour.FgRed).PrintfFunc()
red("Warning")
red("Error: %s", err)
notice := colour.New(colour.Bold, colour.FgGreen).PrintlnFunc()
notice("Don't forget this...")
Custom fprint functions (FprintFunc)
blue := colour.New(FgBlue).FprintfFunc()
blue(myWriter, "important notice: %s", stars)
success := colour.New(colour.Bold, colour.FgGreen).FprintlnFunc()
success(myWriter, "Don't forget this...")
Insert into noncolour strings (SprintFunc)
yellow := colour.New(colour.FgYellow).SprintFunc()
red := colour.New(colour.FgRed).SprintFunc()
fmt.Printf("This is a %s and this is %s.\n", yellow("warning"), red("error"))
info := colour.New(colour.FgWhite, colour.BgGreen).SprintFunc()
fmt.Printf("This %s rocks!\n", info("package"))
fmt.Println("This", colour.RedString("warning"), "should be not neglected.")
fmt.Printf("%v %v\n", colour.GreenString("Info:"), "an important message.")
fmt.Fprintf(colour.Output, "Windows support: %s", colour.GreenString("PASS"))
Plug into existing code
colour.Set(colour.FgYellow)
fmt.Println("Existing text will now be in yellow")
fmt.Printf("This one %s\n", "too")
colour.Unset()
colour.Set(colour.FgMagenta, colour.Bold)
defer colour.Unset()
fmt.Println("All text will now be bold magenta.")
Disable/Enable colour
There might be a case where you want to explicitly disable/enable colour output.
the go-isatty package will automatically disable colour output for non-tty
output streams (for example if the output were piped directly to less)
Colour has support to disable/enable colours both globally and for single colour
definitions. For example suppose you have a CLI app and a --no-colour bool
flag. You can easily disable the colour output with:
var flagNoColour = flag.Bool("no-colour", false, "Disable colour output")
if *flagNoColour {
colour.NoColour = true
}
It also has support for single colour definitions (local). You can
disable/enable colour output on the fly:
c := colour.New(colour.FgCyan)
c.Println("Prints cyan text")
c.DisableColour()
c.Println("This is printed without any colour")
c.EnableColour()
c.Println("This prints again cyan...")
License
The MIT License (MIT).