From d840af0272ab334deb28873853465e195d68b848 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Wed, 5 Jul 2023 12:43:35 -0400 Subject: [PATCH] fix: detect windows terminal environment variable And add test to look for WT_SESSION --- termenv_test.go | 9 +++++++++ termenv_unix.go | 29 +++++------------------------ 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/termenv_test.go b/termenv_test.go index 8d9e2bc..e4762fb 100644 --- a/termenv_test.go +++ b/termenv_test.go @@ -438,3 +438,12 @@ func TestWithTTY(t *testing.T) { } } } + +func TestWindowsTerminal(t *testing.T) { + t.Setenv("TERM", "xterm-256color") + t.Setenv("WT_SESSION", "1") + o := NewOutput(ioutil.Discard, WithTTY(true)) + if cp := o.ColorProfile(); cp != TrueColor { + t.Fatalf("expected color profile to be %d, got %d", TrueColor, cp) + } +} diff --git a/termenv_unix.go b/termenv_unix.go index c598185..1462c64 100644 --- a/termenv_unix.go +++ b/termenv_unix.go @@ -4,11 +4,8 @@ package termenv import ( - "errors" "fmt" "io" - "io/ioutil" - "os" "strconv" "strings" "time" @@ -60,7 +57,11 @@ func (o *Output) ColorProfile() Profile { } if strings.Contains(term, "256color") { - return checkWSLinWindowsTerminal(term, "/proc/sys/kernel/osrelease") + // Check if we're running Windows Terminal + if o.environ.Getenv("WT_SESSION") != "" { + return TrueColor + } + return ANSI256 } if strings.Contains(term, "color") { return ANSI @@ -72,26 +73,6 @@ func (o *Output) ColorProfile() Profile { return Ascii } -func checkWSLinWindowsTerminal(term, filename string) Profile { - // Check for WSL - wsl, err := ioutil.ReadFile(filename) - // Additional check for Mac OS as it doesn't have "/proc/" folder - if err != nil && !errors.Is(err, os.ErrNotExist) { - return ANSI256 - } - if string(wsl) != "" { - // 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 - wslLower := strings.ToLower(string(wsl)) - // Also check if the terminal used is Windows Terminal - if strings.Contains(wslLower, "microsoft") && os.Getenv("WT_SESSION") != "" { - return TrueColor - } - } - return ANSI256 -} - func (o Output) foregroundColor() Color { s, err := o.termStatusReport(10) if err == nil {