Skip to content

Commit

Permalink
used syscall
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryk-dk committed May 8, 2022
1 parent f435c2c commit 684eb1b
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions v3/termutil/term_x.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"os"
"syscall"
"unsafe"

"golang.org/x/term"
)

var (
Expand All @@ -17,7 +15,7 @@ var (
unlockSignals = []os.Signal{
os.Interrupt, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGKILL,
}
termState *term.State
oldState syscall.Termios
)

type window struct {
Expand Down Expand Up @@ -58,16 +56,23 @@ func TerminalSize() (rows, cols int, err error) {
func lockEcho() error {
fd := tty.Fd()

var err error
if termState, err = term.MakeRaw(int(fd)); err != nil {
if _, _, err := syscall.Syscall(sysIoctl, fd, ioctlReadTermios, uintptr(unsafe.Pointer(&oldState))); err != 0 {
return fmt.Errorf("error when puts the terminal connected to the given file descriptor: %v", err)
}

newState := oldState
newState.Lflag &^= syscall.ECHO
newState.Lflag |= syscall.ICANON | syscall.ISIG
newState.Iflag |= syscall.ICRNL
if _, _, e := syscall.Syscall(sysIoctl, fd, ioctlWriteTermios, uintptr(unsafe.Pointer(&newState))); e != 0 {
return fmt.Errorf("error update terminal settings: %v", e)
}
return nil
}

func unlockEcho() error {
fd := tty.Fd()
if err := term.Restore(int(fd), termState); err != nil {
if _, _, err := syscall.Syscall(sysIoctl, fd, ioctlWriteTermios, uintptr(unsafe.Pointer(&oldState))); err != 0 {
return fmt.Errorf("error restores the terminal connected to the given file descriptor: %w", err)
}
return nil
Expand Down

0 comments on commit 684eb1b

Please sign in to comment.