Detects whether a terminal supports colors. Credits github.com/efekarakus/termcolor.
Find a file
numbered.dev ab4cecbd3a
All checks were successful
Go / build (push) Successful in 48s
Update LICENSE
2025-12-25 13:32:59 +00:00
.forgejo/workflows Update .forgejo/workflows/go.yml 2025-12-12 20:19:11 +00:00
.gitignore Return LevelNone if no color env set 2019-11-10 17:48:41 -08:00
flag.go Add tests against chalk 2019-11-23 16:01:00 -08:00
go.mod Change module path to code.rebxd.com/Rebxd/termcolor 2025-12-12 19:38:28 +01:00
go.sum Change module path to code.rebxd.com/Rebxd/termcolor 2025-12-12 19:38:28 +01:00
LICENSE Update LICENSE 2025-12-25 13:32:59 +00:00
README.md Update README.md 2025-12-08 06:57:04 +01:00
termcolor.go Return true if support level is higher than compared 2019-11-24 08:07:09 -08:00
termcolor_test.go Return true if support level is higher than compared 2019-11-24 08:07:09 -08:00
version.go Update build constraint for version.go 2025-11-30 15:33:57 +01:00
version_windows.go Rename function windowsLevel to lookupWindows for fixing it 2025-11-30 15:52:23 +01:00

Termcolor GoDoc Actions Status

Detects what level of color support your terminal has. This package is heavily inspired by chalk's support-color module.

termcolor

Install

go get code.rebxd.com/Rebxd/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:
		// wrap text with 24 bit color https://en.wikipedia.org/wiki/ANSI_escape_code#24-bit
		fmt.Fprint(os.Stderr, "\x1b[38;2;25;255;203mSuccess!\n\x1b[0m")
	case termcolor.Level256:
		// wrap text with 8 bit color https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
		fmt.Fprint(os.Stderr, "\x1b[38;5;118mSuccess!\n\x1b[0m")
	case termcolor.LevelBasic:
		// wrap text with 3/4 bit color https://en.wikipedia.org/wiki/ANSI_escape_code#3/4_bit
		fmt.Fprint(os.Stderr, "\x1b[92mSuccess!\n\x1b[0m")
	default:
		// no color, return text as is.
		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.