Termcolor
Detects what level of color support your terminal has.
This package is heavily inspired by chalk's support-color module.
Install
go get github.com/efekarakus/termcolor
Examples
Colorize output by finding out which level of color your terminal support:
func main() {
switch l := termcolor.SupportLevel(os.Stderr); l {
case termcolor.Level16M:
fmt.Fprint(os.Stderr, "\x1b[38;2;25;255;203mSuccess!\n\x1b[0m")
case termcolor.Level256:
fmt.Fprint(os.Stderr, "\x1b[38;5;118mSuccess!\n\x1b[0m")
case termcolor.LevelBasic:
fmt.Fprint(os.Stderr, "\x1b[92mSuccess!\n\x1b[0m")
default:
fmt.Fprint(os.Stderr, "Success!\n")
}
}
Alternatively, you can use:
if termcolor.Supports16M(os.Stderr) {}
if termcolor.Supports256(os.Stderr) {}
if termcolor.SupportsBasic(os.Stderr) {}
if termcolor.SupportsNone(os.Stderr) {}
Priorities
The same environment variable and flag priorities as chalk's supports-color module is applied.
It obeys the --color
and --no-color
CLI flags.
For situations where using --color
is not possible, use the environment variable FORCE_COLOR=1
(level 1), FORCE_COLOR=2
(level 2), or FORCE_COLOR=3
(level 3) to forcefully enable color, or FORCE_COLOR=0
to forcefully disable. The use of FORCE_COLOR
overrides all other color support checks.
Explicit 256/Truecolor mode can be enabled using the --color=256
and --color=16m
flags, respectively.
Credits
License
The MIT License (MIT) - see LICENSE for more details.