tty_windows.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //go:build windows
  2. // +build windows
  3. package tea
  4. import (
  5. "os"
  6. "github.com/containerd/console"
  7. )
  8. func (p *Program) initInput() error {
  9. // If input's a file, use console to manage it
  10. if f, ok := p.input.(*os.File); ok {
  11. // Save a reference to the current stdin then replace stdin with our
  12. // input. We do this so we can hand input off to containerd/console to
  13. // set raw mode, and do it in this fashion because the method
  14. // console.ConsoleFromFile isn't supported on Windows.
  15. p.windowsStdin = os.Stdin
  16. os.Stdin = f
  17. // Note: this will panic if it fails.
  18. c := console.Current()
  19. p.console = c
  20. }
  21. return nil
  22. }
  23. // restoreInput restores stdout in the event that we placed it aside to handle
  24. // input with CONIN$, above.
  25. func (p *Program) restoreInput() error {
  26. if p.windowsStdin != nil {
  27. os.Stdin = p.windowsStdin
  28. }
  29. return nil
  30. }
  31. // Open the Windows equivalent of a TTY.
  32. func openInputTTY() (*os.File, error) {
  33. f, err := os.OpenFile("CONIN$", os.O_RDWR, 0644)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return f, nil
  38. }