termcolor

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2019 License: MIT Imports: 5 Imported by: 4

README

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 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:
		// 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.

Documentation

Overview

Package termcolor detects what level of color support your terminal has.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Supports16M

func Supports16M(f FileDescriptor) bool

Supports16M returns true if the file descriptor can support true colors.

func Supports256

func Supports256(f FileDescriptor) bool

Supports256 returns true if the file descriptor can support 256 colors.

func SupportsBasic

func SupportsBasic(f FileDescriptor) bool

SupportsBasic returns true if the file descriptor can support the basic 16 colors.

func SupportsNone

func SupportsNone(f FileDescriptor) bool

SupportNone returns true if the file descriptor cannot support colors.

Types

type FileDescriptor

type FileDescriptor interface {
	Fd() uintptr
}

FileDescriptor is the interface that wraps the file descriptor method.

type Level

type Level int

Level represents the number of colors the terminal supports.

const (
	// LevelNone represents a terminal that does not support colors.
	LevelNone Level = iota
	// LevelBasic represents a terminal that can support the basic 16 colors.
	LevelBasic
	// Level256 represents a terminal that can support 256 colors.
	Level256
	// Level16M represents a terminal that can support "true colors".
	Level16M
)

Color levels that can be supported by a terminal. See https://en.wikipedia.org/wiki/ANSI_escape_code#Colors

func SupportLevel

func SupportLevel(f FileDescriptor) Level

SupportLevel returns the color level that's supported by the file descriptor. If the environment variables set no color, then returns LevelNone.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL