From 58ef50ae1fff9e224249f7a80a628445ee0c38b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 14 Sep 2022 16:47:42 +0200 Subject: [PATCH] Ensure that subprocesses connect to the original `os.Stdout` On non-Windows platforms, this avoids wrapping `IOStreams.Out` in a `fdWriter` and thus causing subprocesses to lose connection to the terminal that gh is connected to. --- pkg/iostreams/iostreams.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/pkg/iostreams/iostreams.go b/pkg/iostreams/iostreams.go index cbf35d7c1..1a6399ae8 100644 --- a/pkg/iostreams/iostreams.go +++ b/pkg/iostreams/iostreams.go @@ -430,19 +430,28 @@ func System() *IOStreams { stdoutIsTTY := isTerminal(os.Stdout) stderrIsTTY := isTerminal(os.Stderr) + var stdout fileWriter = os.Stdout isVirtualTerminal := false if stdoutIsTTY { + // enables ANSI escape sequences on modern Windows if err := enableVirtualTerminalProcessing(os.Stdout.Fd()); err == nil { isVirtualTerminal = true + } else { + // as a fallback, translate ANSI escape sequences to Windows console syscalls + colorableStdout := colorable.NewColorable(os.Stdout) + if colorableStdout != os.Stdout { + // ensure that the file descriptor of the original stdout is preserved + stdout = &fdWriter{ + fd: os.Stdout.Fd(), + Writer: colorableStdout, + } + } } } io := &IOStreams{ - In: os.Stdin, - Out: &fdWriter{ - fd: os.Stdout.Fd(), - Writer: colorable.NewColorable(os.Stdout), - }, + In: os.Stdin, + Out: stdout, ErrOut: colorable.NewColorable(os.Stderr), colorEnabled: EnvColorForced() || (!EnvColorDisabled() && stdoutIsTTY), is256enabled: isVirtualTerminal || Is256ColorSupported(),