Properly detect mintty output redirection

mintty on windows always uses pipes to connect stdout between processes
and for the terminal output. The previous implementation always assumed
that stdout connected to a pipe means that stdout is displayed on a
mintty terminal. However, this detection breaks when using pipes to
connect processes and for powershell which uses pipes when redirecting
to a file.

Now the pipe filename is queried and matched against the pattern used by
msys / cygwin when connected to the terminal. In all other cases assume
that a pipe is just a regular pipe.
This commit is contained in:
Michael Eischer
2021-03-07 23:17:11 +01:00
parent 5e6af77b7a
commit 80564a9bc9
3 changed files with 35 additions and 5 deletions

View File

@@ -4,6 +4,7 @@ package termstatus
import (
"io"
"strings"
"syscall"
"unsafe"
@@ -80,6 +81,22 @@ func isPipe(fd uintptr) bool {
return err == nil && typ == windows.FILE_TYPE_PIPE
}
func getFileNameByHandle(fd uintptr) (string, error) {
type FILE_NAME_INFO struct {
FileNameLength int32
FileName [windows.MAX_LONG_PATH]uint16
}
var fi FILE_NAME_INFO
err := windows.GetFileInformationByHandleEx(windows.Handle(fd), windows.FileNameInfo, (*byte)(unsafe.Pointer(&fi)), uint32(unsafe.Sizeof(fi)))
if err != nil {
return "", err
}
filename := syscall.UTF16ToString(fi.FileName[:])
return filename, nil
}
// CanUpdateStatus returns true if status lines can be printed, the process
// output is not redirected to a file or pipe.
func CanUpdateStatus(fd uintptr) bool {
@@ -88,11 +105,23 @@ func CanUpdateStatus(fd uintptr) bool {
return true
}
// check that the output file type is a pipe (0x0003)
// pipes require special handling
if !isPipe(fd) {
return false
}
// assume we're running in mintty/cygwin
return true
fn, err := getFileNameByHandle(fd)
if err != nil {
return false
}
// inspired by https://github.com/RyanGlScott/mintty/blob/master/src/System/Console/MinTTY/Win32.hsc
// terminal: \msys-dd50a72ab4668b33-pty0-to-master
// pipe to cat: \msys-dd50a72ab4668b33-13244-pipe-0x16
if (strings.HasPrefix(fn, "\\cygwin-") || strings.HasPrefix(fn, "\\msys-")) &&
strings.Contains(fn, "-pty") && strings.HasSuffix(fn, "-master") {
return true
}
return false
}