tty_unix.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. "fmt"
  6. "os"
  7. "github.com/containerd/console"
  8. )
  9. func (p *Program) initInput() error {
  10. // If input's a file, use console to manage it
  11. if f, ok := p.input.(*os.File); ok {
  12. c, err := console.ConsoleFromFile(f)
  13. if err != nil {
  14. return nil //nolint:nilerr // ignore error, this was just a test
  15. }
  16. p.console = c
  17. }
  18. return nil
  19. }
  20. // On unix systems, RestoreInput closes any TTYs we opened for input. Note that
  21. // we don't do this on Windows as it causes the prompt to not be drawn until
  22. // the terminal receives a keypress rather than appearing promptly after the
  23. // program exits.
  24. func (p *Program) restoreInput() error {
  25. if p.console != nil {
  26. if err := p.console.Reset(); err != nil {
  27. return fmt.Errorf("error restoring console: %w", err)
  28. }
  29. }
  30. return nil
  31. }
  32. func openInputTTY() (*os.File, error) {
  33. f, err := os.Open("/dev/tty")
  34. if err != nil {
  35. return nil, fmt.Errorf("could not open a new TTY: %w", err)
  36. }
  37. return f, nil
  38. }