Documentation ¶
Overview ¶
Package color is a lightweight terminal color library. It is only compatible with terminals which support ANSI escape codes.
The API exposes two programming styles. You can use functions that print the color escape sequences directly to the output stream:
color.Stdout(color.Red) fmt.Println("Red text") color.Stdout(color.Reset)
Or, you can retrieve the color escape sequence by directly looking up a color.Code in a color.Profile:
fmt.Printf("%sRed text%s\n", color.StdoutProfile[color.Red], color.StdoutProfile[color.Reset])
Text destined for stderr should use color.Stderr and color.StderrProfile instead.
When the output stream is not a character device, or the TERM environment variable indicates an unsupported terminal type, functions will print nothing or return a nil byte slice.
While we link the main CockroachDB binary against libncurses or libterminfo, which bundle information about nearly every terminal ever produced, handling colors for non-ANSI terminals is no small task. See, for example, the difference between the setaf (set ANSI foreground) and setf (set foreground) capabilities. Since non-ANSI terminals are few and far between, it's not worth the trouble.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Profile256 = Profile{ Black: []byte("\033[38;5;0m"), Red: []byte("\033[38;5;160m"), Green: []byte("\033[38;5;2m"), Yellow: []byte("\033[38;5;214m"), Blue: []byte("\033[38;5;4m"), Magenta: []byte("\033[38;5;5m"), Cyan: []byte("\033[38;5;33m"), White: []byte("\033[38;5;315"), Gray: []byte("\033[38;5;246m"), Reset: []byte("\033[0m"), }
Profile256 is suitable for terminals with 256-color support.
var Profile8 = Profile{ Black: []byte("\033[0;30;49m"), Red: []byte("\033[0;31;49m"), Green: []byte("\033[0;32;49m"), Yellow: []byte("\033[0;33;49m"), Blue: []byte("\033[0;34;49m"), Magenta: []byte("\033[0;35;49m"), Cyan: []byte("\033[0;36;49m"), White: []byte("\033[0;37;49m"), Gray: []byte("\033[2;37;49m"), Reset: []byte("\033[0m"), }
Profile8 is suitable for terminals with 8-color support.
var StderrProfile = detectProfileOrPanic(os.Stderr)
StderrProfile is the Profile to use for stderr.
var StdoutProfile = detectProfileOrPanic(os.Stdout)
StdoutProfile is the Profile to use for stdout.
Functions ¶
Types ¶
type Code ¶
type Code int
Code represents a terminal color code.
func PickArbitraryColor ¶
PickArbitraryColor maps a uint32 to an arbitrary color code (excluding Reset) in a deterministic fashion.
type Profile ¶
Profile defines escape sequences which provide color in terminals. Some terminals support 8 colors, some 256, others none at all.
func DetectProfile ¶
DetectProfile configures a profile suitable for the given file output.
func (Profile) BackgroundColorSequence ¶
BackgroundColorSequence returns the corresponding color in the profile as a background color escape sequence.