console.go 976 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //go:build windows
  2. // +build windows
  3. package windowsconsole
  4. import (
  5. "os"
  6. "golang.org/x/sys/windows"
  7. )
  8. // GetHandleInfo returns file descriptor and bool indicating whether the file is a console.
  9. func GetHandleInfo(in interface{}) (uintptr, bool) {
  10. switch t := in.(type) {
  11. case *ansiReader:
  12. return t.Fd(), true
  13. case *ansiWriter:
  14. return t.Fd(), true
  15. }
  16. var inFd uintptr
  17. var isTerminal bool
  18. if file, ok := in.(*os.File); ok {
  19. inFd = file.Fd()
  20. isTerminal = isConsole(inFd)
  21. }
  22. return inFd, isTerminal
  23. }
  24. // IsConsole returns true if the given file descriptor is a Windows Console.
  25. // The code assumes that GetConsoleMode will return an error for file descriptors that are not a console.
  26. //
  27. // Deprecated: use [windows.GetConsoleMode] or [golang.org/x/term.IsTerminal].
  28. func IsConsole(fd uintptr) bool {
  29. return isConsole(fd)
  30. }
  31. func isConsole(fd uintptr) bool {
  32. var mode uint32
  33. err := windows.GetConsoleMode(windows.Handle(fd), &mode)
  34. return err == nil
  35. }