module

Colorprofile

Profile is a color profile: NoTTY, Ascii, ANSI, ANSI256, or TrueColor.

Constants

Ascii = Profile::ASCII

Ascii is an alias for the ASCII profile for backwards compatibility.

DUMB_TERM = "dumb"
VERSION = "0.1.0"

Class methods

detect(output : IO, env : Array(String)) : Profile

Detect returns the color profile based on the terminal output, and environment variables. This respects NO_COLOR, CLICOLOR, and CLICOLOR_FORCE environment variables.

The rules as follows:

  • TERM=dumb is always treated as NoTTY unless CLICOLOR_FORCE=1 is set.
  • If COLORTERM=truecolor, and the profile is not NoTTY, it gets upgraded to TrueColor.
  • Using any 256 color terminal (e.g. TERM=xterm-256color) will set the profile to ANSI256.
  • Using any color terminal (e.g. TERM=xterm-color) will set the profile to ANSI.
  • Using CLICOLOR=1 without TERM defined should be treated as ANSI if the output is a terminal.
  • NO_COLOR takes precedence over CLICOLOR/CLICOLOR_FORCE, and will disable colors but not text decoration, i.e. bold, italic, faint, etc.

See https://no-color.org/ and https://bixense.com/clicolors/ for more information.

Source
env(env : Array(String)) : Profile

Env returns the color profile based on the terminal environment variables. This respects NO_COLOR, CLICOLOR, and CLICOLOR_FORCE environment variables.

Source
new_environ(environ : Array(String)) : Environ

new_environ returns a new environment map from a slice of environment variables.

Source
new_writer(io : IO, environ : Array(String)) : Writer

NewWriter creates a new color profile writer that downgrades color sequences based on the detected color profile.

If environ is nil, it will use environment variables from ENV.

It queries the given writer to determine if it supports ANSI escape codes. If it does, along with the given environment variables, it will determine the appropriate color profile to use for color formatting.

This respects the NO_COLOR, CLICOLOR, and CLICOLOR_FORCE environment variables.

Source
terminfo_profile(term : String) : Profile

Terminfo returns the color profile based on the terminal's terminfo database. This relies on the Tc and RGB capabilities to determine if the terminal supports TrueColor. If term is empty or "dumb", it returns NoTTY.

Source
tmux(env : Array(String)) : Profile

Tmux returns the color profile based on tmux info output. Tmux supports overriding the terminal's color capabilities, so this function will return the color profile based on the tmux configuration.

Source

Nested types