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

feat: detect windows terminal #141

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions termenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package termenv

import (
"bytes"
"errors"
"fmt"
"image/color"
"io"
"io/ioutil"
"os"
"strings"
Expand Down Expand Up @@ -160,6 +162,39 @@ func TestANSIProfile(t *testing.T) {
}
}

func TestWSLDetectioninWindowsTerminal(t *testing.T) {
file, err := os.Open("/proc/sys/kernel/osrelease")
defer file.Close()
if err != nil && !errors.Is(err, os.ErrNotExist) {
t.Error("Error reading file proc/sys/kernel/osrelease")
}

tmpFile, err := os.CreateTemp("", "osrelease")
defer tmpFile.Close()
if err != nil {
t.Error("Error making tmp file")
}

io.Copy(tmpFile, file)
_, err = io.WriteString(tmpFile, "6.2.6-76060206-microsoft")
if err != nil {
t.Error("Error writing to tmp file")
}

content, err := os.ReadFile(tmpFile.Name())
Delta456 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
t.Error("Error reading tmp file")
}

if os.Getenv("WT_SESSION") == "" {
os.Setenv("WT_SESSION", "a22023a86b26f5b301335de43994ddc3")
}
p := ColorProfile()
if p != TrueColor && !strings.Contains(strings.ToLower(string(content)), "mircosoft") && os.Getenv("WT_SESSION") == "" {
t.Errorf("Expected %d, got %d", TrueColor, p)
}
}

func TestANSI256Profile(t *testing.T) {
p := ANSI256

Expand Down
19 changes: 19 additions & 0 deletions termenv_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
package termenv

import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -57,6 +60,22 @@ func (o *Output) ColorProfile() Profile {
}

if strings.Contains(term, "256color") {
// Check for WSL
wsl, err := ioutil.ReadFile("/proc/sys/kernel/osrelease")
// 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
}
if strings.Contains(term, "color") {
Expand Down