diff --git a/src/pkg/term/term.go b/src/pkg/term/term.go index 43bbdc6b6..e0e1b319e 100644 --- a/src/pkg/term/term.go +++ b/src/pkg/term/term.go @@ -22,6 +22,17 @@ import ( "golang.org/x/sys/unix" ) +type Option func(*unix.Termios) + +func ChangeState(oldState *unix.Termios, options ...Option) *unix.Termios { + newState := *oldState + for _, option := range options { + option(&newState) + } + + return &newState +} + func GetState(file *os.File) (*unix.Termios, error) { fileFD := file.Fd() fileFDInt := int(fileFD) @@ -36,3 +47,41 @@ func IsTerminal(file *os.File) bool { return true } + +func Restore(file *os.File, oldState *unix.Termios) error { + fileFD := file.Fd() + fileFDInt := int(fileFD) + err := unix.IoctlSetTermios(fileFDInt, unix.TCSETS, oldState) + return err +} + +func SetState(file *os.File, state *unix.Termios) error { + fileFD := file.Fd() + fileFDInt := int(fileFD) + err := unix.IoctlSetTermios(fileFDInt, unix.TCSETS, state) + return err +} + +func WithVMIN(vmin uint8) Option { + return func(state *unix.Termios) { + state.Cc[unix.VMIN] = vmin + } +} + +func WithVTIME(vtime uint8) Option { + return func(state *unix.Termios) { + state.Cc[unix.VTIME] = vtime + } +} + +func WithoutECHO() Option { + return func(state *unix.Termios) { + state.Lflag &^= unix.ECHO + } +} + +func WithoutICANON() Option { + return func(state *unix.Termios) { + state.Lflag &^= unix.ICANON + } +}