2022-03-08 10:45:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
|
|
|
type tty struct {
|
|
|
|
fd int
|
|
|
|
termios *unix.Termios
|
|
|
|
}
|
|
|
|
|
2024-01-21 14:26:00 +00:00
|
|
|
func newTTY(fd uintptr) *tty {
|
|
|
|
termios, _ := unix.IoctlGetTermios(int(fd), unix.TCGETS)
|
|
|
|
return &tty{int(fd), termios}
|
|
|
|
}
|
|
|
|
|
2022-03-08 10:45:56 +00:00
|
|
|
func (t *tty) EchoOff() {
|
2024-01-21 14:26:00 +00:00
|
|
|
if t.termios == nil {
|
2022-03-08 10:45:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-21 14:26:00 +00:00
|
|
|
newState := *t.termios
|
2022-03-08 10:45:56 +00:00
|
|
|
newState.Lflag &^= unix.ECHO
|
|
|
|
newState.Lflag |= unix.ICANON | unix.ISIG
|
|
|
|
newState.Iflag |= unix.ICRNL
|
|
|
|
unix.IoctlSetTermios(t.fd, unix.TCSETS, &newState)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tty) Restore() {
|
2024-01-21 14:26:00 +00:00
|
|
|
if t.termios == nil {
|
|
|
|
return
|
2022-03-08 10:45:56 +00:00
|
|
|
}
|
2024-01-21 14:26:00 +00:00
|
|
|
|
|
|
|
unix.IoctlSetTermios(t.fd, unix.TCSETS, t.termios)
|
2022-03-08 10:45:56 +00:00
|
|
|
}
|