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.
This commit is contained in:
Mislav Marohnić 2022-09-14 16:47:42 +02:00
parent e2e8d697db
commit 58ef50ae1f

View file

@ -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(),