2017-04-17 18:08:24 -04:00
|
|
|
// +build windows
|
|
|
|
|
2020-05-09 11:28:17 -04:00
|
|
|
package windowsconsole
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
2020-05-09 11:28:17 -04:00
|
|
|
"golang.org/x/sys/windows"
|
2017-04-17 18:08:24 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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()
|
2020-05-09 11:28:17 -04:00
|
|
|
isTerminal = isConsole(inFd)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
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.
|
2021-06-10 08:43:07 -04:00
|
|
|
// Deprecated: use golang.org/x/sys/windows.GetConsoleMode() or golang.org/x/term.IsTerminal()
|
2020-05-09 11:28:17 -04:00
|
|
|
var IsConsole = isConsole
|
|
|
|
|
|
|
|
func isConsole(fd uintptr) bool {
|
|
|
|
var mode uint32
|
|
|
|
err := windows.GetConsoleMode(windows.Handle(fd), &mode)
|
|
|
|
return err == nil
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|