Skip to content

Commit

Permalink
win: Respect WT_SESSION variable. (#706)
Browse files Browse the repository at this point in the history
This is in accordance to [this comment](microsoft/terminal#1040 (comment)). As discussed further in the issue, in an ideal world, we wouldn't check "user agents" of terminals one-by-one, but instead would do proper feature probing like described [here](https://github.com/termstandard/colors).

But anyway: This is a good first step to enhance Windows support.

Co-authored-by: Ankit <oss@ankit.pl>
  • Loading branch information
beatbrot and ankitpokhrel committed Mar 16, 2024
1 parent 170c129 commit df2a86c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pkg/tui/helper.go
Expand Up @@ -64,13 +64,14 @@ func getActionModal() *primitive.ActionModal {
SetTextColor(tcell.ColorDefault)
}

// IsDumbTerminal checks TERM environment variable and returns true if it is set to dumb.
// IsDumbTerminal checks TERM/WT_SESSION environment variable and returns true if they indicate a dumb terminal.
//
// Dumb terminal indicates terminal with limited capability. It may not provide support
// for special character sequences, e.g., no handling of ANSI escape sequences.
func IsDumbTerminal() bool {
term := strings.ToLower(os.Getenv("TERM"))
return term == "" || term == "dumb"
_, wtSession := os.LookupEnv("WT_SESSION")
return !wtSession && (term == "" || term == "dumb")
}

// IsNotTTY returns true if the stdout file descriptor is not a TTY.
Expand Down
34 changes: 34 additions & 0 deletions pkg/tui/helper_test.go
@@ -1,6 +1,7 @@
package tui

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -46,6 +47,39 @@ func TestColumnPadding(t *testing.T) {
}
}

func TestIsDumbTerminal(t *testing.T) {
// Store initial values & cleanup
t.Setenv("TERM", "")
t.Setenv("WT_SESSION", "")

empty := ""
foo := "foo"
setTermEnv(&empty, nil)
assert.True(t, IsDumbTerminal())

setTermEnv(nil, nil)
assert.True(t, IsDumbTerminal())

setTermEnv(&foo, nil)
assert.False(t, IsDumbTerminal())

setTermEnv(nil, &foo)
assert.False(t, IsDumbTerminal())
}

func setTermEnv(term *string, wtSession *string) {
if term != nil {
_ = os.Setenv("TERM", *term)
} else {
_ = os.Unsetenv("TERM")
}
if wtSession != nil {
_ = os.Setenv("WT_SESSION", *wtSession)
} else {
_ = os.Unsetenv("WT_SESSION")
}
}

func TestSplitText(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit df2a86c

Please sign in to comment.