Color extends fmt.Printf
with verbs for producing colored output.
It has full terminfo support with github.com/nhooyr/terminfo!
note: things may change but it looks pretty stable. email me if you have any new ideas.
go get github.com/nhooyr/color
See godoc for more information.
// "panic:" with a red foreground then normal "foo".
color.Printf("%h[fgRed]panic:%r %s\n", "foo")
// "panic:" with a red background then normal "bar".
color.Printf("%h[bgRed]panic:%r %s\n", "bar")
// Bold "panic:" then normal "foo".
color.Printf("%h[bold]panic:%r %s\n", "foo")
// Underlined "panic:" with then normal "bar".
color.Printf("%h[underline]panic:%r %s\n", "bar")
// "panic:" using color 83 as the foreground then normal "foo".
color.Printf("%h[fg83]panic:%r %s\n", "foo")
// "panic:" using color 158 as the background then normal "bar".
color.Printf("%h[bg158]panic:%r %s\n", "bar")
// Bolded "panic:" with a green foreground then normal "foo".
color.Printf("%h[fgGreen+bold]panic:%r %s\n", "foo")
// Underlined "panic:" with a bright black background then normal "bar".
color.Printf("%h[bg8+underline]panic:%r %s\n", "bar")
// Prepare only processes the highlight verbs in the string,
// letting you print it repeatedly with performance.
panicFormat := color.Prepare("%h[fgRed+bold]panic:%r %s\n")
// Each will print "panic:" and some normal text after, but if standard output
// is a terminal, "panic:" will be printed in bold with a red foreground.
color.Printfp(panicFormat, "foo")
color.Printfp(panicFormat, "bar")
color.Printfp(panicFormat, "foo")
hello := color.Prepare("%h[fgBlue+bold]HELLO%r")
// Each will print "HELLO", but if standard output is a terminal,
// "HELLO" will be printed in bold with a blue foreground.
color.Println(hello)
color.Println(hello)
color.Println(hello)
A Printer
writes to an io.Writer
.
redFormat := color.Prepare("%h[fgRed]%s%r\n")
// If standard error is a terminal, this will print in color.
// Otherwise it will print a normal "bar".
p := color.New(os.Stderr, color.IsTerminal(os.Stderr))
p.Printfp(redFormat, "bar")
// "foo" with red foreground.
p = color.New(os.Stderr, true)
p.Printfp(redFormat, "foo")
// Normal "bar", the highlight verbs are ignored.
p = color.New(os.Stderr, false)
p.Printfp(redFormat, "bar")
redFormat := color.Prepare("%h[fgRed]%s%r\n")
// If os.Stderr is a terminal, this will print in color.
// Otherwise it will be a normal "foo".
log.Printfp(redFormat, "foo")
// Normal "bar", the highlight verbs are ignored.
log.SetColor(false)
log.Printfp(redFormat, "bar")
// "foo" with a red foreground.
log.SetColor(true)
log.Fatalfp(redFormat, "foo")
Add the following to after/syntax/go.vim
to highlight the highlight verbs within strings.
syn match goFormatSpecifier /%[-#0 +]*\%(\*\|\d\+\)\=\%(\.\%(\*\|\d\+\)\)*\%([vTtbcdoqxXUeEfgGspr]\|h\[[a-zA-Z+0-9]\+\]\)/ contained containedin=goString
- True color support
- Windows support
- Respect $TERM
- Seperate log package
- color.Format docs
- A better way to combine
color.Format
s - Logging/Printing/Format tests
- cleanup above tests