tty_unix.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || aix || zos
  2. // +build darwin dragonfly freebsd linux netbsd openbsd solaris aix zos
  3. package tea
  4. import (
  5. "fmt"
  6. "os"
  7. "os/signal"
  8. "syscall"
  9. "github.com/charmbracelet/x/term"
  10. )
  11. func (p *Program) initInput() (err error) {
  12. // Check if input is a terminal
  13. if f, ok := p.input.(term.File); ok && term.IsTerminal(f.Fd()) {
  14. p.ttyInput = f
  15. p.previousTtyInputState, err = term.MakeRaw(p.ttyInput.Fd())
  16. if err != nil {
  17. return fmt.Errorf("error entering raw mode: %w", err)
  18. }
  19. }
  20. if f, ok := p.output.(term.File); ok && term.IsTerminal(f.Fd()) {
  21. p.ttyOutput = f
  22. }
  23. return nil
  24. }
  25. func openInputTTY() (*os.File, error) {
  26. f, err := os.Open("/dev/tty")
  27. if err != nil {
  28. return nil, fmt.Errorf("could not open a new TTY: %w", err)
  29. }
  30. return f, nil
  31. }
  32. const suspendSupported = true
  33. // Send SIGTSTP to the entire process group.
  34. func suspendProcess() {
  35. c := make(chan os.Signal, 1)
  36. signal.Notify(c, syscall.SIGCONT)
  37. _ = syscall.Kill(0, syscall.SIGTSTP)
  38. // blocks until a CONT happens...
  39. <-c
  40. }