Skip to content

Commit

Permalink
fix: detect windows terminal environment variable
Browse files Browse the repository at this point in the history
And add test to look for WT_SESSION
  • Loading branch information
aymanbagabas committed Jul 5, 2023
1 parent fe6db9e commit d840af0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
9 changes: 9 additions & 0 deletions termenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
29 changes: 5 additions & 24 deletions termenv_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
package termenv

import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit d840af0

Please sign in to comment.