Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color detection error in WSL in Windows Terminal #124

Open
meowgorithm opened this issue Mar 10, 2023 · 2 comments
Open

Color detection error in WSL in Windows Terminal #124

meowgorithm opened this issue Mar 10, 2023 · 2 comments

Comments

@meowgorithm
Copy link
Collaborator

meowgorithm commented Mar 10, 2023

This could be user error, but I'm noticing that, while termenv will correctly detect TrueColor in Windows Terminal, it incorrectly detects ANSI256, in WSL.

Given this code...

package main

import (
        "fmt"

        "github.com/muesli/termenv"
)

func main() {
        p := func() string {
                switch termenv.ColorProfile() {
                case termenv.TrueColor:
                        return "TrueColor"
                case termenv.ANSI256:
                        return "ANSI256"
                case termenv.ANSI:
                        return "ANSI"
                case termenv.Ascii:
                        return "Ascii"
                default:
                        return "(Undetected)"
                }
        }()

        fmt.Printf("Termenv detected profile: %s\n", p)
}

...I get the following output. The first line shows example.exe running in PowerShell and then in WSL.

Screenshot 2023-03-10 105403

@meowgorithm meowgorithm changed the title Inconsistent Detection in WSL in Windows Terminal Color detection error in WSL in Windows Terminal Mar 10, 2023
@muesli
Copy link
Owner

muesli commented Mar 13, 2023

I'll see if we can reliably detect WSL inside Windows Terminal and add a special case for its TrueColor support.

@Delta456
Copy link

Delta456 commented Jul 1, 2023

To know if WSL is being used inside Windows Terminal. One can do:

// Detect WSL as it has True Color support
	wsl, err := ioutil.ReadFile("/proc/sys/kernel/osrelease")
	// Additional check needed for Mac Os as it doesn't have "/proc/" folder
	if err != nil && !errors.Is(err, os.ErrNotExist) {
		log.Fatal(err)
	}
	// Lowercasing every content inside "/proc/sys/kernal/osrelease"
	// because it gives "Microsoft" for WSL and "microsoft" for WSL 2
	// so no need of checking twice
	if string(wsl) != "" {
		wslLower := strings.ToLower(string(wsl))
               // Also check if the terminal used is Windows Terminal
		if strings.Contains(wslLower, "Microsoft") && os.GetEnv("WT_SESSION") != 0 {
			return terminfo.ColorLevelMillions
		}
	}

@muesli I can work on this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants