2017-04-17 18:08:24 -04:00
|
|
|
// +build windows
|
|
|
|
|
2020-04-16 05:23:37 -04:00
|
|
|
package windowsconsole // import "github.com/moby/term/windows"
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/Azure/go-ansiterm/winterm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetHandleInfo returns file descriptor and bool indicating whether the file is a console.
|
|
|
|
func GetHandleInfo(in interface{}) (uintptr, bool) {
|
|
|
|
switch t := in.(type) {
|
|
|
|
case *ansiReader:
|
|
|
|
return t.Fd(), true
|
|
|
|
case *ansiWriter:
|
|
|
|
return t.Fd(), true
|
|
|
|
}
|
|
|
|
|
|
|
|
var inFd uintptr
|
|
|
|
var isTerminal bool
|
|
|
|
|
|
|
|
if file, ok := in.(*os.File); ok {
|
|
|
|
inFd = file.Fd()
|
|
|
|
isTerminal = IsConsole(inFd)
|
|
|
|
}
|
|
|
|
return inFd, isTerminal
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsConsole returns true if the given file descriptor is a Windows Console.
|
|
|
|
// The code assumes that GetConsoleMode will return an error for file descriptors that are not a console.
|
|
|
|
func IsConsole(fd uintptr) bool {
|
|
|
|
_, e := winterm.GetConsoleMode(fd)
|
|
|
|
return e == nil
|
|
|
|
}
|