| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package windowsconsole |
| 5 |
|
| 6 |
import ( |
| 7 |
"os" |
| 8 |
|
| 9 |
"golang.org/x/sys/windows" |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
func GetHandleInfo(in interface{}) (uintptr, bool) { |
| 14 |
switch t := in.(type) { |
| 15 |
case *ansiReader: |
| 16 |
return t.Fd(), true |
| 17 |
case *ansiWriter: |
| 18 |
return t.Fd(), true |
| 19 |
} |
| 20 |
|
| 21 |
var inFd uintptr |
| 22 |
var isTerminal bool |
| 23 |
|
| 24 |
if file, ok := in.(*os.File); ok { |
| 25 |
inFd = file.Fd() |
| 26 |
isTerminal = isConsole(inFd) |
| 27 |
} |
| 28 |
return inFd, isTerminal |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
func IsConsole(fd uintptr) bool { |
| 36 |
return isConsole(fd) |
| 37 |
} |
| 38 |
|
| 39 |
func isConsole(fd uintptr) bool { |
| 40 |
var mode uint32 |
| 41 |
err := windows.GetConsoleMode(windows.Handle(fd), &mode) |
| 42 |
return err == nil |
| 43 |
} |