From c8289ba21d358e0f74131eb656791a53ae0ca63b Mon Sep 17 00:00:00 2001 From: Joseph Cumines Date: Fri, 11 Aug 2023 21:39:53 +1000 Subject: [PATCH 1/3] Fix getOriginalTermios error not propagated when called multiple times --- filter.go | 6 ++++-- key.go | 1 + term/raw.go | 4 ++-- term/term.go | 23 +++++++++++++++++------ 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/filter.go b/filter.go index 61c17ed7..205c437f 100644 --- a/filter.go +++ b/filter.go @@ -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) } diff --git a/key.go b/key.go index 3d3f5fcc..4429d8d3 100644 --- a/key.go +++ b/key.go @@ -4,6 +4,7 @@ package prompt // Key is the type express the key inserted from user. +// //go:generate stringer -type=Key type Key int diff --git a/term/raw.go b/term/raw.go index e2a4a7b8..b46d8ab8 100644 --- a/term/raw.go +++ b/term/raw.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows package term @@ -6,7 +7,6 @@ import ( "syscall" "github.com/pkg/term/termios" - "golang.org/x/sys/unix" ) // SetRaw put terminal into a raw mode @@ -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) } diff --git a/term/term.go b/term/term.go index 9c8e2e6b..9d3f41ab 100644 --- a/term/term.go +++ b/term/term.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows package term @@ -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. @@ -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) } From 2cc56e195bbbffd46792f642d40f4e2dcc3f83fa Mon Sep 17 00:00:00 2001 From: Joseph Cumines Date: Mon, 14 Aug 2023 08:34:37 +1000 Subject: [PATCH 2/3] prompt: Fix staticcheck lint issue fbshipit-source-id: 84e7177 --- bisect/bisect_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bisect/bisect_test.go b/bisect/bisect_test.go index 332921bf..c4db1bc4 100644 --- a/bisect/bisect_test.go +++ b/bisect/bisect_test.go @@ -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() From e1dbe10bc53b917b16d09f44aabf02813293387d Mon Sep 17 00:00:00 2001 From: Joseph Cumines Date: Tue, 15 Aug 2023 09:46:49 +1000 Subject: [PATCH 3/3] prompt: Fix term restore by closing fd only AFTER restoring fbshipit-source-id: 68880c5 --- reader_posix.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/reader_posix.go b/reader_posix.go index cb209b25..85a62a63 100644 --- a/reader_posix.go +++ b/reader_posix.go @@ -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.