Giter Site home page Giter Site logo

termcolor's Introduction

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.

termcolor's People

Contributors

efekarakus avatar

Stargazers

 avatar jack_tower avatar Alan Fregtman avatar Ishan Goel avatar igor avatar zbv avatar Uttara Sridhar avatar Toby Padilla avatar Doru Carastan avatar Alessio Biancalana avatar Christian Muehlhaeuser avatar  avatar  avatar Henry Wallace avatar Koichi Shiraishi avatar Yenlin Chen avatar  avatar Lubomir Anastasov avatar n0nz avatar Vincent Derelle avatar Montana Flynn avatar  avatar Sho Mizutani avatar David Killmon avatar Nghia Truong avatar Jehiah Czebotar avatar

Watchers

James Cloos avatar  avatar  avatar

termcolor's Issues

go get termcolor fails with powershell, respctively windows env

Both command line and powershell (windows 10) do not allow me to go get the package.

go get github.com/efekarakus/termcolor
# github.com/efekarakus/termcolor
..\..\go\pkg\mod\github.com\efekarakus\[email protected]\termcolor.go:80:21: undefined: lookupWindows

on Linux/bash all works fine

I prepared me a make file, only windows fails:

make
GOARCH=amd64 GOOS=darwin go build -o ../bin/macos/turn ./src/turn.go
GOARCH=amd64 GOOS=linux go build -o ../bin/linux/turn ./src/turn.go
GOARCH=amd64 GOOS=windows go build -o ../bin/windows/turn.exe ./src/turn.go
# github.com/efekarakus/termcolor
../../go/src/github.com/efekarakus/termcolor/termcolor.go:80:21: undefined: lookupWindows
make: *** [Makefile:6: build] Fehler 2

seems lookupWindows() in version_windows.go but there it does not work properly, when I try "cross compiling". The "problem is, that the identifier in version.go (non-windows) and version_windows.go (windows terminal cmd and powershell) are NOT the same.

In version_windows.go it is called "windowsLevel" but it is nowhere called within termcolor. I corrected that, all compiles find then, colors are correctly determinated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.