Skip to content

Commit

Permalink
fix(tea): set TERM environment variable when we have a PTY attached
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Jan 16, 2025
1 parent 02b9cd9 commit 7afbce9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
8 changes: 8 additions & 0 deletions bubbletea/tea_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ import (
)

func makeOpts(s ssh.Session) []tea.ProgramOption {
pty, _, ok := s.Pty()
envs := s.Environ()
if ok {
envs = append(envs, "TERM="+pty.Term)
}
//nolint:godox
// TODO: Support Windows PTYs
return []tea.ProgramOption{
tea.WithInput(s),
tea.WithOutput(s),
tea.WithEnvironment(envs),
}
}
25 changes: 22 additions & 3 deletions bubbletea/tea_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,38 @@ import (

func makeOpts(s ssh.Session) []tea.ProgramOption {
pty, _, ok := s.Pty()
if !ok || s.EmulatedPty() {
envs := append(s.Environ(), "TERM="+pty.Term)
envs := s.Environ()

if !ok {
return []tea.ProgramOption{
tea.WithInput(s),
tea.WithOutput(s),
tea.WithEnvironment(envs),
}
}

// Make sure we have $TERM in the environment when we have a PTY session.
envs = append(envs, "TERM="+pty.Term)
if s.EmulatedPty() {
return []tea.ProgramOption{
tea.WithInput(s),
tea.WithOutput(s),
// Force color profile to be set based on environment variables. We
// do this because we don't have a real PTY attached, hence
// [ssh.Session.EmulatedPty]. This is sort of a hack, but it's the
// best we can do ;)
tea.WithColorProfile(colorprofile.Env(envs)),
tea.WithEnvironment(envs),
}
}

//nolint:godox
// TODO: Add $SSH_PTY and other environment variables to the environment
// when we have a real PTY attached.

return []tea.ProgramOption{
tea.WithInput(pty.Slave),
tea.WithOutput(pty.Slave),
tea.WithEnvironment(s.Environ()),
tea.WithEnvironment(envs),
}
}

0 comments on commit 7afbce9

Please sign in to comment.