Merge pull request #6262 from cli/fix-subcommand-stdout

Ensure that subprocesses connect to the original `os.Stdout`
This commit is contained in:
Mislav Marohnić 2022-09-15 11:07:22 +02:00 committed by GitHub
commit 0ecd424901
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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