console.go 787 B

1234567891011121314151617181920212223242526272829303132333435
  1. // +build windows
  2. package windowsconsole
  3. import (
  4. "os"
  5. "github.com/Azure/go-ansiterm/winterm"
  6. )
  7. // GetHandleInfo returns file descriptor and bool indicating whether the file is a console.
  8. func GetHandleInfo(in interface{}) (uintptr, bool) {
  9. switch t := in.(type) {
  10. case *ansiReader:
  11. return t.Fd(), true
  12. case *ansiWriter:
  13. return t.Fd(), true
  14. }
  15. var inFd uintptr
  16. var isTerminal bool
  17. if file, ok := in.(*os.File); ok {
  18. inFd = file.Fd()
  19. isTerminal = IsConsole(inFd)
  20. }
  21. return inFd, isTerminal
  22. }
  23. // IsConsole returns true if the given file descriptor is a Windows Console.
  24. // The code assumes that GetConsoleMode will return an error for file descriptors that are not a console.
  25. func IsConsole(fd uintptr) bool {
  26. _, e := winterm.GetConsoleMode(fd)
  27. return e == nil
  28. }