package colorprofileimport ()constdumbTerm = "dumb"// 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 gest 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.func ( io.Writer, []string) Profile { , := .(term.File) := newEnviron() := isTTYForced() || ( && term.IsTerminal(.Fd())) , := .lookup("TERM") := ! || == dumbTerm := colorProfile(, )if == TrueColor || envNoColor() {// We already know we have TrueColor, or NO_COLOR is set.return }if && ! { := Terminfo() := tmux()// Color profile is the maximum of env, terminfo, and tmux.returnmax(, max(, )) }return}// Env returns the color profile based on the terminal 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 gest 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.func ( []string) ( Profile) {returncolorProfile(true, newEnviron())}func ( bool, environ) ( Profile) { , := .lookup("TERM") := (! && runtime.GOOS != "windows") || == dumbTerm := envColorProfile()if ! || {// Check if the output is a terminal. // Treat dumb terminals as NoTTY = NoTTY } else { = }ifenvNoColor() && {if > ASCII { = ASCII }return//nolint:nakedret }ifcliColorForced() {if < ANSI { = ANSI }if > { = }return//nolint:nakedret }ifcliColor() {if && ! && < ANSI { = ANSI } }return}// envNoColor returns true if the environment variables explicitly disable color output// by setting NO_COLOR (https://no-color.org/).func ( environ) bool { , := strconv.ParseBool(.get("NO_COLOR"))return}func ( environ) bool { , := strconv.ParseBool(.get("CLICOLOR"))return}func ( environ) bool { , := strconv.ParseBool(.get("CLICOLOR_FORCE"))return}func ( environ) bool { , := strconv.ParseBool(.get("TTY_FORCE"))return}func ( environ) bool { := strings.ToLower(.get("COLORTERM"))return == "truecolor" || == "24bit" || == "yes" || == "true"}// envColorProfile returns infers the color profile from the environment.func ( environ) ( Profile) { , := .lookup("TERM")if ! || len() == 0 || == dumbTerm { = NoTTYifruntime.GOOS == "windows" {// Use Windows API to detect color profile. Windows Terminal and // cmd.exe don't define $TERM.if , := windowsColorProfile(); { = } } } else { = ANSI }switch {casestrings.Contains(, "alacritty"),strings.Contains(, "contour"),strings.Contains(, "foot"),strings.Contains(, "ghostty"),strings.Contains(, "kitty"),strings.Contains(, "rio"),strings.Contains(, "st"),strings.Contains(, "wezterm"):returnTrueColorcasestrings.HasPrefix(, "tmux"), strings.HasPrefix(, "screen"):if < ANSI256 { = ANSI256 }casestrings.HasPrefix(, "xterm"):if < ANSI { = ANSI } }iflen(["WT_SESSION"]) > 0 {// Windows Terminal supports TrueColorreturnTrueColor }if , := strconv.ParseBool(.get("GOOGLE_CLOUD_SHELL")); {returnTrueColor }// GNU Screen doesn't support TrueColor // Tmux doesn't support $COLORTERMifcolorTerm() && !strings.HasPrefix(, "screen") && !strings.HasPrefix(, "tmux") {returnTrueColor }ifstrings.HasSuffix(, "256color") && < ANSI256 { = ANSI256 }// Direct color terminals support true colors.ifstrings.HasSuffix(, "direct") {returnTrueColor }return//nolint:nakedret}// 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.func ( string) ( Profile) {iflen() == 0 || == "dumb" {returnNoTTY } = ANSI , := terminfo.Load()if != nil {return } := .ExtBoolCapsShort()if , := ["Tc"]; {returnTrueColor }if , := ["RGB"]; {returnTrueColor }return}// 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.func ( []string) Profile {returntmux(newEnviron())}// tmux returns the color profile based on the tmux environment variables.func ( environ) ( Profile) {if , := .lookup("TMUX"); ! || len() == 0 {// Not in tmuxreturnNoTTY }// Check if tmux has either Tc or RGB capabilities. Otherwise, return // ANSI256. = ANSI256 := exec.CommandContext(context.Background(), "tmux", "info") , := .Output()if != nil {return }for := rangebytes.SplitSeq(, []byte("\n")) {if (bytes.Contains(, []byte("Tc")) || bytes.Contains(, []byte("RGB"))) &&bytes.Contains(, []byte("true")) {returnTrueColor } }return}// environ is a map of environment variables.typeenvironmap[string]string// newEnviron returns a new environment map from a slice of environment// variables.func ( []string) environ { := make(map[string]string, len())for , := range { := strings.SplitN(, "=", 2)varstringiflen() == 2 { = [1] } [[0]] = }return}// lookup returns the value of an environment variable and a boolean indicating// if it exists.func ( environ) ( string) (string, bool) { , := []return , }// get returns the value of an environment variable and empty string if it// doesn't exist.func ( environ) ( string) string { , := .lookup()return}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)