term.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. //go:build aix || darwin || dragonfly || freebsd || (linux && !appengine) || netbsd || openbsd || os400 || solaris
  2. package readline
  3. import (
  4. "syscall"
  5. )
  6. type Termios syscall.Termios
  7. func SetRawMode(fd uintptr) (*Termios, error) {
  8. termios, err := getTermios(fd)
  9. if err != nil {
  10. return nil, err
  11. }
  12. newTermios := *termios
  13. newTermios.Iflag &^= syscall.IGNBRK | syscall.BRKINT | syscall.PARMRK | syscall.ISTRIP | syscall.INLCR | syscall.IGNCR | syscall.ICRNL | syscall.IXON
  14. newTermios.Lflag &^= syscall.ECHO | syscall.ECHONL | syscall.ICANON | syscall.ISIG | syscall.IEXTEN
  15. newTermios.Cflag &^= syscall.CSIZE | syscall.PARENB
  16. newTermios.Cflag |= syscall.CS8
  17. newTermios.Cc[syscall.VMIN] = 1
  18. newTermios.Cc[syscall.VTIME] = 0
  19. return termios, setTermios(fd, &newTermios)
  20. }
  21. func UnsetRawMode(fd uintptr, termios any) error {
  22. t := termios.(*Termios)
  23. return setTermios(fd, t)
  24. }
  25. // IsTerminal returns true if the given file descriptor is a terminal.
  26. func IsTerminal(fd uintptr) bool {
  27. _, err := getTermios(fd)
  28. return err == nil
  29. }