Skip to content

Commit

Permalink
Update dependencies, use term instead of Syscall
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryk-dk committed May 8, 2022
1 parent 90c02fa commit e1c53e3
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 35 deletions.
8 changes: 6 additions & 2 deletions v3/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ require (
github.com/mattn/go-colorable v0.1.8
github.com/mattn/go-isatty v0.0.12
github.com/mattn/go-runewidth v0.0.12
golang.org/x/term v0.0.0-20220411215600-e5f449aeb171
)

require (
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 // indirect
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 // indirect
)

go 1.12
go 1.18
6 changes: 4 additions & 2 deletions v3/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 h1:F5Gozwx4I1xtr/sr/8CFbb57iKi3297KFs0QDbGN60A=
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 h1:nonptSpoQ4vQjyraW20DXPAglgQfVnM9ZC6MmNLMR60=
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20220411215600-e5f449aeb171 h1:EH1Deb8WZJ0xc0WK//leUHXcX9aLE5SymusoTmMZye8=
golang.org/x/term v0.0.0-20220411215600-e5f449aeb171/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
2 changes: 1 addition & 1 deletion v3/termutil/term_appengine.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build appengine
//go:build appengine

package termutil

Expand Down
3 changes: 1 addition & 2 deletions v3/termutil/term_bsd.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// +build darwin freebsd netbsd openbsd dragonfly
// +build !appengine
//go:build (darwin || freebsd || netbsd || openbsd || dragonfly) && !appengine

package termutil

Expand Down
3 changes: 1 addition & 2 deletions v3/termutil/term_linux.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// +build linux
// +build !appengine
//go:build linux && !appengine

package termutil

Expand Down
3 changes: 1 addition & 2 deletions v3/termutil/term_nix.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// +build linux darwin freebsd netbsd openbsd dragonfly
// +build !appengine
//go:build (linux || darwin || freebsd || netbsd || openbsd || dragonfly) && !appengine

package termutil

Expand Down
3 changes: 1 addition & 2 deletions v3/termutil/term_solaris.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// +build solaris
// +build !appengine
//go:build solaris && !appengine

package termutil

Expand Down
2 changes: 1 addition & 1 deletion v3/termutil/term_win.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build windows
//go:build windows

package termutil

Expand Down
34 changes: 13 additions & 21 deletions v3/termutil/term_x.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// +build linux darwin freebsd netbsd openbsd solaris dragonfly
// +build !appengine
//go:build (linux || darwin || freebsd || netbsd || openbsd || solaris || dragonfly) && !appengine

package termutil

Expand All @@ -8,6 +7,8 @@ import (
"os"
"syscall"
"unsafe"

"golang.org/x/term"
)

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

type window struct {
Expand Down Expand Up @@ -53,30 +55,20 @@ func TerminalSize() (rows, cols int, err error) {
return int(w.Row), int(w.Col), nil
}

var oldState syscall.Termios

func lockEcho() (err error) {
func lockEcho() error {
fd := tty.Fd()
if _, _, e := syscall.Syscall6(sysIoctl, fd, ioctlReadTermios, uintptr(unsafe.Pointer(&oldState)), 0, 0, 0); e != 0 {
err = fmt.Errorf("Can't get terminal settings: %v", e)
return
}

newState := oldState
newState.Lflag &^= syscall.ECHO
newState.Lflag |= syscall.ICANON | syscall.ISIG
newState.Iflag |= syscall.ICRNL
if _, _, e := syscall.Syscall6(sysIoctl, fd, ioctlWriteTermios, uintptr(unsafe.Pointer(&newState)), 0, 0, 0); e != 0 {
err = fmt.Errorf("Can't set terminal settings: %v", e)
return
var err error
if termState, err = term.MakeRaw(int(fd)); err != nil {
return fmt.Errorf("error when puts the terminal connected to the given file descriptor: %v", err)
}
return
return nil
}

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

0 comments on commit e1c53e3

Please sign in to comment.