mirror of
https://github.com/restic/restic.git
synced 2025-12-03 23:51:51 +00:00
Merge pull request #3325 from MichaelEischer/fix-mintty-output
Fix windows terminal output for mintty
This commit is contained in:
@@ -67,7 +67,7 @@ func New(wr io.Writer, errWriter io.Writer, disableStatus bool) *Terminal {
|
||||
return t
|
||||
}
|
||||
|
||||
if d, ok := wr.(fder); ok && canUpdateStatus(d.Fd()) {
|
||||
if d, ok := wr.(fder); ok && CanUpdateStatus(d.Fd()) {
|
||||
// only use the fancy status code when we're running on a real terminal.
|
||||
t.canUpdateStatus = true
|
||||
t.fd = d.Fd()
|
||||
|
||||
@@ -20,9 +20,9 @@ func moveCursorUp(wr io.Writer, fd uintptr) func(io.Writer, uintptr, int) {
|
||||
return posixMoveCursorUp
|
||||
}
|
||||
|
||||
// canUpdateStatus returns true if status lines can be printed, the process
|
||||
// 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 {
|
||||
func CanUpdateStatus(fd uintptr) bool {
|
||||
if !terminal.IsTerminal(int(fd)) {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ package termstatus
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strings"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
@@ -80,19 +81,47 @@ func isPipe(fd uintptr) bool {
|
||||
return err == nil && typ == windows.FILE_TYPE_PIPE
|
||||
}
|
||||
|
||||
// canUpdateStatus returns true if status lines can be printed, the process
|
||||
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 {
|
||||
func CanUpdateStatus(fd uintptr) bool {
|
||||
// easy case, the terminal is cmd or psh, without redirection
|
||||
if isWindowsTerminal(fd) {
|
||||
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
|
||||
}
|
||||
|
||||
30
internal/ui/termstatus/terminal_windows_test.go
Normal file
30
internal/ui/termstatus/terminal_windows_test.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package termstatus
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
|
||||
rtest "github.com/restic/restic/internal/test"
|
||||
)
|
||||
|
||||
func TestIsMinTTY(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
path string
|
||||
result bool
|
||||
}{
|
||||
{`\\.\pipe\msys-dd50a72ab4668b33-pty0-to-master`, true},
|
||||
{`\\.\pipe\msys-dd50a72ab4668b33-13244-pipe-0x16`, false},
|
||||
} {
|
||||
filename, err := syscall.UTF16FromString(test.path)
|
||||
rtest.OK(t, err)
|
||||
handle, err := windows.CreateNamedPipe(&filename[0], windows.PIPE_ACCESS_DUPLEX,
|
||||
windows.PIPE_TYPE_BYTE, 1, 1024, 1024, 0, nil)
|
||||
rtest.OK(t, err)
|
||||
defer windows.CloseHandle(handle)
|
||||
|
||||
rtest.Assert(t, CanUpdateStatus(uintptr(handle)) == test.result,
|
||||
"expected CanUpdateStatus(%v) == %v", test.path, test.result)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user