2023-10-28 03:01:48 +00:00
|
|
|
//go:build aix || darwin || dragonfly || freebsd || (linux && !appengine) || netbsd || openbsd || os400 || solaris
|
|
|
|
|
2023-10-25 23:41:18 +00:00
|
|
|
package readline
|
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Termios syscall.Termios
|
|
|
|
|
2024-05-22 16:00:38 +00:00
|
|
|
func SetRawMode(fd uintptr) (*Termios, error) {
|
2023-10-25 23:41:18 +00:00
|
|
|
termios, err := getTermios(fd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
newTermios := *termios
|
|
|
|
newTermios.Iflag &^= syscall.IGNBRK | syscall.BRKINT | syscall.PARMRK | syscall.ISTRIP | syscall.INLCR | syscall.IGNCR | syscall.ICRNL | syscall.IXON
|
|
|
|
newTermios.Lflag &^= syscall.ECHO | syscall.ECHONL | syscall.ICANON | syscall.ISIG | syscall.IEXTEN
|
|
|
|
newTermios.Cflag &^= syscall.CSIZE | syscall.PARENB
|
|
|
|
newTermios.Cflag |= syscall.CS8
|
|
|
|
newTermios.Cc[syscall.VMIN] = 1
|
|
|
|
newTermios.Cc[syscall.VTIME] = 0
|
|
|
|
|
|
|
|
return termios, setTermios(fd, &newTermios)
|
|
|
|
}
|
|
|
|
|
2024-05-22 16:00:38 +00:00
|
|
|
func UnsetRawMode(fd uintptr, termios any) error {
|
2024-02-15 05:28:35 +00:00
|
|
|
t := termios.(*Termios)
|
|
|
|
return setTermios(fd, t)
|
2023-10-25 23:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsTerminal returns true if the given file descriptor is a terminal.
|
2024-05-22 16:00:38 +00:00
|
|
|
func IsTerminal(fd uintptr) bool {
|
2023-10-28 03:01:48 +00:00
|
|
|
_, err := getTermios(fd)
|
|
|
|
return err == nil
|
2023-10-25 23:41:18 +00:00
|
|
|
}
|