package termenvimport ()// ColorLevel is the color level supported by a terminal.typeColorLeveluint8const (TermColorNoneColorLevel = iota// not support colorTermColor16// 16(4bit) ANSI color supportedTermColor256// 256(8bit) color supportedTermColorTrue// support TRUE(RGB) color)// String returns the string name of the color level.func ( ColorLevel) () string {switch {caseTermColor16:return"ansiColor"caseTermColor256:return"256color"caseTermColorTrue:return"trueColor"default:return"none" }}// NoColor returns true if the NO_COLOR environment variable is set.func () bool { returnnoColor }// TermColorLevel returns the color support level for the current terminal.func () ColorLevel { returncolorLevel }// IsSupportColor returns true if the terminal supports color.func () bool { returncolorLevel > TermColorNone }// IsSupport256Color returns true if the terminal supports 256 colors.func () bool { returncolorLevel >= TermColor256 }// IsSupportTrueColor returns true if the terminal supports true color.func () bool { returncolorLevel == TermColorTrue }//// ---------------- Force set color support ----------------//var backLevel ColorLevel// SetColorLevel value force.func ( ColorLevel) {// backup old valuebackLevel = colorLevel// force set color levelcolorLevel = supportColor = > TermColorNonenoColor = !supportColor}// DisableColor in the current terminalfunc () {// backup old valuebackLevel = colorLevel// force disable colornoColor = truesupportColor = falsecolorLevel = TermColorNone}// ForceEnableColor flags value. TIP: use for unit testing.//// Usage://// ccolor.ForceEnableColor()// defer ccolor.RevertColorSupport()func () {// backup old valuebackLevel = colorLevel// force enables colornoColor = falsesupportColor = truecolorLevel = TermColor256// return colorLevel}// RevertColorSupport flags to init value.func () {// revert color flags varcolorLevel = backLevelsupportColor = backLevel > TermColorNonenoColor = os.Getenv("NO_COLOR") == ""}/************************************************************* * helper methods for detect color supports *************************************************************/// DetectColorLevel for current env//// NOTICE: The method will detect terminal info each time.//// if only want to get current color level, please direct call IsSupportColor() or TermColorLevel()func () ColorLevel { , := detectTermColorLevel()return}// on TERM=screen: not support true-colorconst noTrueColorTerm = "screen"// detect terminal color support level//// refer https://github.com/Delta456/box-cli-makerfunc detectTermColorLevel() ( ColorLevel, bool) { := runtime.GOOS == "windows" := os.Getenv("TERM")if != noTrueColorTerm {// On JetBrains Terminal // - TERM value not set, but support true-color // env: // TERMINAL_EMULATOR=JetBrains-JediTerm := os.Getenv("TERMINAL_EMULATOR")if == "JetBrains-JediTerm" {debugf("True Color support on JetBrains-JediTerm, is win: %v", )returnTermColorTrue, false } } = detectColorLevelFromEnv(, )// fallback: simple detect by TERM value string.if == TermColorNone {debugf("level=none - fallback check special term color support") , = detectSpecialTermColor()debugf("color level by detectSpecialTermColor: %s", .String()) } else {debugf("color level by detectColorLevelFromEnv: %s", .String()) }return}// detectColorFromEnv returns the color level COLORTERM, FORCE_COLOR,// TERM_PROGRAM, or determined from the TERM environment variable.//// refer the github.com/xo/terminfo.ColorLevelFromEnv()// https://en.wikipedia.org/wiki/Terminfofunc detectColorLevelFromEnv( string, bool) ColorLevel {if == noTrueColorTerm { // on TERM=screen: not support true-colorreturnTermColor256 }// check for overriding environment variables , , := os.Getenv("COLORTERM"), os.Getenv("TERM_PROGRAM"), os.Getenv("FORCE_COLOR")switch {casestrings.Contains(, "truecolor") || strings.Contains(, "24bit"):returnTermColorTruecase != "" || != "":returnTermColor16case == "Apple_Terminal":returnTermColor256case == "Terminus" || == "Hyper":returnTermColorTruecase == "iTerm.app":// check iTerm version := os.Getenv("TERM_PROGRAM_VERSION")if != "" { , := strconv.Atoi(strings.Split(, ".")[0])if != nil {setLastErr(errors.New("invalid TERM_PROGRAM_VERSION=" + ))returnTermColor256// return TermColorNone }if == 3 {returnTermColorTrue } }returnTermColor256 }// otherwise determine from TERM's max_colors capability // if !isWin && termVal != "" { // debugf("TERM=%s - TODO check color level by load terminfo file", termVal) // return TermColor16 // }// no TERM env value. default return none levelreturnTermColorNone}
The pages are generated with Goldsv0.8.4. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.