Skip to content

Commit f30be82

Browse files
tautropflistefanhaller
authored andcommitted
Set the TERM env variable
This communicates to pagers that we're in a very simple terminal that they should not expect to have much capabilities. See #3419
1 parent e1c3ef6 commit f30be82

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

pkg/gui/pty.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/creack/pty"
1313
"github.com/jesseduffield/gocui"
1414
"github.com/jesseduffield/lazygit/pkg/utils"
15+
"github.com/samber/lo"
1516
)
1617

1718
func (gui *Gui) desiredPtySize() *pty.Winsize {
@@ -54,6 +55,13 @@ func (gui *Gui) newPtyTask(view *gocui.View, cmd *exec.Cmd, prefix string) error
5455

5556
cmdStr := strings.Join(cmd.Args, " ")
5657

58+
// This communicates to pagers that we're in a very simple
59+
// terminal that they should not expect to have much capabilities.
60+
// Moving the cursor, clearing the screen, or querying for colors are among such "advanced" capabilities.
61+
// Context: https://github.com/jesseduffield/lazygit/issues/3419
62+
cmd.Env = removeExistingTermEnvVars(cmd.Env)
63+
cmd.Env = append(cmd.Env, "TERM=dumb")
64+
5765
cmd.Env = append(cmd.Env, "GIT_PAGER="+pager)
5866

5967
manager := gui.getManager(view)
@@ -87,3 +95,20 @@ func (gui *Gui) newPtyTask(view *gocui.View, cmd *exec.Cmd, prefix string) error
8795

8896
return nil
8997
}
98+
99+
func removeExistingTermEnvVars(env []string) []string {
100+
return lo.Filter(env, func(envVar string, _ int) bool {
101+
return !isTermEnvVar(envVar)
102+
})
103+
}
104+
105+
// Terminals set a variety of different environment variables
106+
// to identify themselves to processes. This list should catch the most common among them.
107+
func isTermEnvVar(envVar string) bool {
108+
return strings.HasPrefix(envVar, "TERM=") ||
109+
strings.HasPrefix(envVar, "TERM_PROGRAM=") ||
110+
strings.HasPrefix(envVar, "TERM_PROGRAM_VERSION=") ||
111+
strings.HasPrefix(envVar, "TERMINAL_EMULATOR=") ||
112+
strings.HasPrefix(envVar, "TERMINAL_NAME=") ||
113+
strings.HasPrefix(envVar, "TERMINAL_VERSION_")
114+
}

0 commit comments

Comments
 (0)