Detects whether a terminal supports colors

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.

Owner
Efe Karakus
👋 Howdy!
Efe Karakus
Similar Resources

A simple logging interface that supports cross-platform color and concurrency.

A simple logging interface that supports cross-platform color and concurrency.

WLog Package wlog creates simple to use UI structure. The UI is used to simply print to the screen. There a wrappers that will wrap each other to crea

Sep 26, 2022

Query, update and convert data structures from the command line. Comparable to jq/yq but supports JSON, TOML, YAML, XML and CSV with zero runtime dependencies.

Query, update and convert data structures from the command line. Comparable to jq/yq but supports JSON, TOML, YAML, XML and CSV with zero runtime dependencies.

dasel Dasel (short for data-selector) allows you to query and modify data structures using selector strings. Comparable to jq / yq, but supports JSON,

Jan 2, 2023

A flexible commandline tool for template rendering. Supports lots of local and remote datasources.

A flexible commandline tool for template rendering. Supports lots of local and remote datasources.

Read the docs at docs.gomplate.ca, chat with developers and community in the #gomplate channel on Gophers Slack gomplate is a template renderer which

Jan 5, 2023

Command-line tool to customize the official Spotify client. Supports Windows, MacOS and Linux.

Command-line tool to customize the official Spotify client. Supports Windows, MacOS and Linux.

Command-line tool to customize the official Spotify client. Supports Windows, MacOS and Linux. Features Change colors whole UI Inject CSS for advanced

Jan 2, 2023

Speaker command reads aloud the text message. It supports multilingual voice reading

speaker - Read the text aloud speaker command reads aloud the text message. It supports multilingual voice reading. If you want the time signal, the s

Aug 22, 2022

Terminal client for SimpleNote

GoNote - Terminal client for SimpleNote GoNote is a simple utility for managing notes in your SimpleNote account. It allows basic operations like crea

Nov 22, 2022

gomerge is a tool to quickly bulk merge several pull requests from your terminal.

gomerge is a tool to quickly bulk merge several pull requests from your terminal.

Gomerge is a tool to quickly enable you to bulk merge Github pull requests from your terminal. The intention of this tool is to simplfy, and eventually automate the merging of github pull requests. This tool should be able to run on most systems.

Dec 28, 2022

`tmax` is a powerful tool to help you get terminal cmd directly.

`tmax`  is a powerful tool to help you get terminal cmd directly.

The positioning of tmax is a command line tool with a little artificial intelligence. If you frequently deal with the terminal daily, tmax will greatly improve your work efficiency.

Oct 15, 2022

YouTube client on your terminal

MeowTube YouTube client on your terminal Table of Contents About Getting Started Usage Contributing About MeowTube is a CLI (Command Line Interface) t

Jul 29, 2022
Comments
  • Supports* functions should return true if the level supported is higher

    Supports* functions should return true if the level supported is higher

    Task

    The Supports256, SupportsBasic, SupportsNone functions don't return true if the terminal supports more colors. For example, we'd expect for SupportsBasic and SupportsNone to be true if Supports256 is true.

    Originally posted in a comment on reddit

  • Update version_windows.go

    Update version_windows.go

    tiny problem with the go-file in the termcolor package determinating the available colors on windows. Apparently lookupWindows() is missnamed in version_windows.go The "problem is, that the identifier in version.go (non-windows) and version_windows.go (windows terminal cmd and powershell) are NOT the same.

    Please see also the open issue

  • go get termcolor fails with powershell, respctively windows env

    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.

Chalk is a Go Package which can be used for making terminal output more vibrant with text colors, text styles and background colors.
Chalk is a Go Package which can be used for making terminal output more vibrant with text colors, text styles and background colors.

Chalk Chalk is a Go Package which can be used for making terminal output more vibrant with text colors, text styles and background colors. Documentati

Oct 29, 2022
Golang package with functionality to add colors to your logs to the terminal.
Golang package with functionality to add colors to your logs to the terminal.

colrz It's a set of funcs and constants to provide basic colors to your terminal app. How to use Get it go get github.com/unnamedxaer/colrz Use it pac

Sep 15, 2022
Light weight Terminal User Interface (TUI) to pick material colors written by Go.
Light weight Terminal User Interface (TUI) to pick material colors written by Go.

mcpick Light weight Terminal User Interface (TUI) to pick material colors. You do NOT need to take your hands off the keyboard to pick colors. Getting

Dec 27, 2022
A command-line tool that uses a fake AI neural network to tell whether or not Ben is sus

Is ben sus? Let's use an AI neural network, trained by his own tweets to figure out. A command-line tool that uses a fake AI neural network to tell wh

Jan 7, 2022
Gowget - A simple wget tool built using golang to download files from the internet whether it is zip

gowget wget built using golang About: A simple wget tool built using golang to d

May 21, 2022
CLI hex dumper with colors
CLI hex dumper with colors

heksa Hex dumper with colors Features ANSI colors for different byte groups such as Printable: A-Z, a-z, 0-9 Spaces: space, tab, new line Special: 0x0

Sep 27, 2022
Donald Knuth's Algorithm 7.2.2.1M for covering with multiplicities and colors via dancing links

Covering with multiplicities and colors via Dancing Links Go implementation of Donald Knuth's Algorithm 7.2.2.1M for covering with multiplicities and

Dec 14, 2022
A Go package for converting RGB and other color formats/colorspaces into DMC thread colors (DMC color name and floss number)

go-c2dmc A Go package for converting RGB and other color formats/colorspaces into DMC thread colors (DMC color name and floss number). Implemented as

Jul 25, 2022
Go-colorful: A library for playing with colors in golang
Go-colorful: A library for playing with colors in golang

go-colorful A library for playing with colors in Go. Supports Go 1.13 onwards. Why? I love games. I make games. I love detail and I get lost in detail

Dec 30, 2022
Stonks is a terminal based stock visualizer and tracker that displays realtime stocks in graph format in a terminal.
Stonks is a terminal based stock visualizer and tracker that displays realtime stocks in graph format in a terminal.

Stonks is a terminal based stock visualizer and tracker. Installation Requirements: golang >= 1.13 Manual Clone the repo Run make && make install Pack

Dec 16, 2022