term.go 1.02 KB
Newer Older
mashun1's avatar
v1  
mashun1 committed
1
2
3
4
5
6
7
8
9
10
//go:build aix || darwin || dragonfly || freebsd || (linux && !appengine) || netbsd || openbsd || os400 || solaris

package readline

import (
	"syscall"
)

type Termios syscall.Termios

xuxzh1's avatar
init  
xuxzh1 committed
11
func SetRawMode(fd uintptr) (*Termios, error) {
mashun1's avatar
v1  
mashun1 committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
	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)
}

xuxzh1's avatar
init  
xuxzh1 committed
28
func UnsetRawMode(fd uintptr, termios any) error {
mashun1's avatar
v1  
mashun1 committed
29
30
31
32
33
	t := termios.(*Termios)
	return setTermios(fd, t)
}

// IsTerminal returns true if the given file descriptor is a terminal.
xuxzh1's avatar
init  
xuxzh1 committed
34
func IsTerminal(fd uintptr) bool {
mashun1's avatar
v1  
mashun1 committed
35
36
37
	_, err := getTermios(fd)
	return err == nil
}