diff --git a/pkg/iostreams/iostreams.go b/pkg/iostreams/iostreams.go index 7844910ac..0caa677df 100644 --- a/pkg/iostreams/iostreams.go +++ b/pkg/iostreams/iostreams.go @@ -292,6 +292,7 @@ func (s *IOStreams) StartAlternateScreenBuffer() { func (s *IOStreams) StopAlternateScreenBuffer() { if s.alternateScreenBufferActive { fmt.Fprint(s.Out, "\x1b[?1049l") + s.alternateScreenBufferActive = false } } @@ -403,10 +404,10 @@ func System() *IOStreams { stdoutIsTTY := isTerminal(os.Stdout) stderrIsTTY := isTerminal(os.Stderr) - assumeTrueColor := false + isVirtualTerminal := false if stdoutIsTTY { if err := enableVirtualTerminalProcessing(os.Stdout); err == nil { - assumeTrueColor = true + isVirtualTerminal = true } } @@ -416,8 +417,8 @@ func System() *IOStreams { Out: colorable.NewColorable(os.Stdout), ErrOut: colorable.NewColorable(os.Stderr), colorEnabled: EnvColorForced() || (!EnvColorDisabled() && stdoutIsTTY), - is256enabled: assumeTrueColor || Is256ColorSupported(), - hasTrueColor: assumeTrueColor || IsTrueColorSupported(), + is256enabled: isVirtualTerminal || Is256ColorSupported(), + hasTrueColor: isVirtualTerminal || IsTrueColorSupported(), pagerCommand: os.Getenv("PAGER"), ttySize: ttySize, } @@ -426,7 +427,7 @@ func System() *IOStreams { io.progressIndicatorEnabled = true } - if stdoutIsTTY && assumeTrueColor { + if stdoutIsTTY && isVirtualTerminal { io.alternateScreenBufferEnabled = true } diff --git a/pkg/iostreams/iostreams_test.go b/pkg/iostreams/iostreams_test.go index fc4ead08b..6b3aa5026 100644 --- a/pkg/iostreams/iostreams_test.go +++ b/pkg/iostreams/iostreams_test.go @@ -2,6 +2,7 @@ package iostreams import ( "errors" + "fmt" "testing" ) @@ -66,3 +67,20 @@ func TestIOStreams_ForceTerminal(t *testing.T) { }) } } + +func TestStopAlternateScreenBuffer(t *testing.T) { + ios, _, stdout, _ := Test() + ios.SetAlternateScreenBufferEnabled(true) + + ios.StartAlternateScreenBuffer() + fmt.Fprint(ios.Out, "test") + ios.StopAlternateScreenBuffer() + + // Stopping a subsequent time should no-op. + ios.StopAlternateScreenBuffer() + + const want = "\x1b[?1049htest\x1b[?1049l" + if got := stdout.String(); got != want { + t.Errorf("after IOStreams.StopAlternateScreenBuffer() got %q, want %q", got, want) + } +}