tty_unix.go 999 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || aix
  2. // +build darwin dragonfly freebsd linux netbsd openbsd solaris aix
  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. c, err := console.ConsoleFromFile(f)
  12. if err != nil {
  13. return nil //nolint:nilerr // ignore error, this was just a test
  14. }
  15. p.console = c
  16. }
  17. return nil
  18. }
  19. // On unix systems, RestoreInput closes any TTYs we opened for input. Note that
  20. // we don't do this on Windows as it causes the prompt to not be drawn until
  21. // the terminal receives a keypress rather than appearing promptly after the
  22. // program exits.
  23. func (p *Program) restoreInput() error {
  24. if p.console != nil {
  25. return p.console.Reset()
  26. }
  27. return nil
  28. }
  29. func openInputTTY() (*os.File, error) {
  30. f, err := os.Open("/dev/tty")
  31. if err != nil {
  32. return nil, err
  33. }
  34. return f, nil
  35. }