Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getOriginalTermios error not propagated when called multiple times #16

Merged
merged 3 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bisect/bisect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ func TestBisectRight(t *testing.T) {
}

func BenchmarkRight(b *testing.B) {
rand.Seed(0)
r := rand.New(rand.NewSource(0))

for _, l := range []int{10, 1e2, 1e3, 1e4} {
x := rand.Perm(l)
insertion := rand.Int()
x := r.Perm(l)
insertion := r.Int()

b.Run(fmt.Sprintf("arrayLength=%d", l), func(b *testing.B) {
b.ResetTimer()
Expand Down
6 changes: 4 additions & 2 deletions filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ func FilterContains(completions []Suggest, sub string, ignoreCase bool) []Sugges

// FilterFuzzy checks whether the completion.Text fuzzy matches sub.
// Fuzzy searching for "dog" is equivalent to "*d*o*g*". This search term
// would match, for example, "Good food is gone"
// ^ ^ ^
// would match, for example:
//
// "Good food is gone"
// ^ ^ ^
func FilterFuzzy(completions []Suggest, sub string, ignoreCase bool) []Suggest {
return filterSuggestions(completions, sub, ignoreCase, fuzzyMatch)
}
Expand Down
1 change: 1 addition & 0 deletions key.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions reader_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ func (t *PosixReader) Open() error {

// Close should be called after stopping input
func (t *PosixReader) Close() error {
if err := syscall.Close(t.fd); err != nil {
return err
}
if err := term.Restore(); err != nil {
_ = syscall.Close(t.fd)
return err
}
return nil
return syscall.Close(t.fd)
}

// Read returns byte array.
Expand Down
4 changes: 2 additions & 2 deletions term/raw.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

package term
Expand All @@ -6,7 +7,6 @@ import (
"syscall"

"github.com/pkg/term/termios"
"golang.org/x/sys/unix"
)

// SetRaw put terminal into a raw mode
Expand All @@ -25,5 +25,5 @@ func SetRaw(fd int) error {
n.Cc[syscall.VMIN] = 1
n.Cc[syscall.VTIME] = 0

return termios.Tcsetattr(uintptr(fd), termios.TCSANOW, (*unix.Termios)(&n))
return termios.Tcsetattr(uintptr(fd), termios.TCSANOW, n)
}
23 changes: 17 additions & 6 deletions term/term.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

package term
Expand All @@ -10,18 +11,28 @@ import (
)

var (
saveTermios *unix.Termios
saveTermios unix.Termios
saveTermiosErr error
saveTermiosFD int
saveTermiosOnce sync.Once
)

func getOriginalTermios(fd int) (unix.Termios, error) {
var err error
func getOriginalTermios(fd int) (*unix.Termios, error) {
saveTermiosOnce.Do(func() {
saveTermiosFD = fd
saveTermios, err = termios.Tcgetattr(uintptr(fd))
var v *unix.Termios
v, saveTermiosErr = termios.Tcgetattr(uintptr(fd))
if saveTermiosErr == nil {
// save a copy
saveTermios = *v
}
})
return *saveTermios, err
if saveTermiosErr != nil {
return nil, saveTermiosErr
}
// return a copy
v := saveTermios
return &v, nil
}

// Restore terminal's mode.
Expand All @@ -30,5 +41,5 @@ func Restore() error {
if err != nil {
return err
}
return termios.Tcsetattr(uintptr(saveTermiosFD), termios.TCSANOW, &o)
return termios.Tcsetattr(uintptr(saveTermiosFD), termios.TCSANOW, o)
}
Loading