Giter Club home page Giter Club logo

Comments (9)

Delta456 avatar Delta456 commented on September 13, 2024 2

@inhere Amazing! Would appreciate if you could also add my repo as reference in the REAMDE.md.

from color.

Delta456 avatar Delta456 commented on September 13, 2024 1

I found this from https://www.github.com/chalk/supports-color/tree/master/index.js

  // Windows 10 build 10586 is the first Windows release that supports 256 colors.
        // Windows 10 build 14931 is the first release that supports 16m/TrueColor

from color.

Delta456 avatar Delta456 commented on September 13, 2024

Please use this instead of hardcore logic 🙏

from color.

Delta456 avatar Delta456 commented on September 13, 2024

Also WSL has true color support.

from color.

Delta456 avatar Delta456 commented on September 13, 2024

Here's how I did it https://github.com/Delta456/box-cli-maker/blob/dev/util.go, see detectTerminalColor function

from color.

inhere avatar inhere commented on September 13, 2024

ok, thanks

from color.

inhere avatar inhere commented on September 13, 2024

hi @vincentbernat @Delta456

Thanks you.

I refer xo/terminfo.detectColorLevelFromEnv and https://github.com/Delta456/box-cli-maker, rewrite the detect method: detectTermColorLevel()

code please see https://github.com/gookit/color/blob/master/detect_env.go

from color.

Delta456 avatar Delta456 commented on September 13, 2024

Also I believe you could had imported my module and used it instead of making one.

from color.

inhere avatar inhere commented on September 13, 2024

hi @Delta456

Your module also has some problems, please see my detection code

color/detect_env.go

Lines 31 to 156 in 1459bf6

func detectTermColorLevel() (level terminfo.ColorLevel, needVTP bool) {
var err error
// on windows WSL:
// - runtime.GOOS == "Linux"
// - support true-color
// env:
// WSL_DISTRO_NAME=Debian
if val := os.Getenv("WSL_DISTRO_NAME"); val != "" {
// detect WSL as it has True Color support
if detectWSL() {
debugf("True Color support on WSL environment")
return terminfo.ColorLevelMillions, false
}
}
isWin := runtime.GOOS == "windows"
// TERM=screen
termVal := os.Getenv("TERM")
// on `screen` not support True-color
if termVal != "screen" {
// On JetBrains Terminal
// - support true-color
// TERMINAL_EMULATOR=JetBrains-JediTerm
val := os.Getenv("TERMINAL_EMULATOR")
if val == "JetBrains-JediTerm" {
debugf("True Color support on JetBrains-JediTerm, is win: %v", isWin)
return terminfo.ColorLevelMillions, isWin
}
}
// level, err = terminfo.ColorLevelFromEnv()
level, err = detectColorLevelFromEnv(termVal)
// fmt.Println(level.String(), err)
// debugf("color level by terminfo.ColorLevelFromEnv(): %s", level)
if err != nil {
// if on windows OS
if isWin {
debugf("fallback1 check special term color on windows")
level, needVTP = detectSpecialTermColor()
} else {
saveInternalError(err)
}
return
}
// enable VTP as it has True Color support
if level == terminfo.ColorLevelNone && isWin {
debugf("fallback2 check special term color on windows")
level, needVTP = detectSpecialTermColor()
}
return
}
// detectColorFromEnv returns the color level COLORTERM, FORCE_COLOR,
// TERM_PROGRAM, or determined from the TERM environment variable.
//
// refer the terminfo.ColorLevelFromEnv()
func detectColorLevelFromEnv(termVal string) (terminfo.ColorLevel, error) {
// on TERM=screen: not support true-color
// termVal := os.Getenv("TERM")
// check for overriding environment variables
colorTerm, termProg, forceColor := os.Getenv("COLORTERM"), os.Getenv("TERM_PROGRAM"), os.Getenv("FORCE_COLOR")
switch {
case strings.Contains(colorTerm, "truecolor") || strings.Contains(colorTerm, "24bit") || termProg == "Hyper":
if termVal == "screen" { // on TERM=screen: not support true-color
return terminfo.ColorLevelHundreds, nil
}
return terminfo.ColorLevelMillions, nil
case colorTerm != "" || forceColor != "":
return terminfo.ColorLevelBasic, nil
case termProg == "Apple_Terminal":
return terminfo.ColorLevelHundreds, nil
case termProg == "Terminus":
if termVal == "screen" { // on TERM=screen: not support true-color
return terminfo.ColorLevelHundreds, nil
}
return terminfo.ColorLevelMillions, nil
case termProg == "iTerm.app":
if termVal == "screen" { // on TERM=screen: not support true-color
return terminfo.ColorLevelHundreds, nil
}
// check version
ver := os.Getenv("TERM_PROGRAM_VERSION")
if ver == "" {
return terminfo.ColorLevelHundreds, nil
}
i, err := strconv.Atoi(strings.Split(ver, ".")[0])
if err != nil {
return terminfo.ColorLevelNone, terminfo.ErrInvalidTermProgramVersion
}
if i == 3 {
return terminfo.ColorLevelMillions, nil
}
return terminfo.ColorLevelHundreds, nil
}
// otherwise determine from TERM's max_colors capability
if termVal != "" {
ti, err := terminfo.Load(termVal)
if err != nil {
return terminfo.ColorLevelNone, err
}
// on TERM=screen:
// - support 256, not support true-color. test on macOS
if termVal == "screen" {
return terminfo.ColorLevelHundreds, nil
}
v, ok := ti.Nums[terminfo.MaxColors]
switch {
case !ok || v <= 16:
return terminfo.ColorLevelNone, nil
case ok && v >= 256:
return terminfo.ColorLevelHundreds, nil
}
return terminfo.ColorLevelBasic, nil
}
// Not TERM env value. default return none level
return terminfo.ColorLevelNone, nil
// return terminfo.ColorLevelBasic, nil
}

And my package as the base package, can not rely on your application package in reverse. :)

from color.

Related Issues (20)

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.