From eb4300894800652860cb8bd9b70573fac3d31365 Mon Sep 17 00:00:00 2001 From: Mateusz Drewniak Date: Sat, 1 Jul 2023 23:54:20 +0200 Subject: [PATCH] Ignore unused control characters --- completion.go | 5 ++--- prompt.go | 7 +++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/completion.go b/completion.go index 0405b90d..0ae50f21 100644 --- a/completion.go +++ b/completion.go @@ -42,15 +42,14 @@ type CompletionManager struct { // GetSelectedSuggestion returns the selected item. func (c *CompletionManager) GetSelectedSuggestion() (s Suggest, ok bool) { - if c.selected == -1 { + if c.selected == -1 || c.selected >= len(c.tmp) { return Suggest{}, false } else if c.selected < -1 { debug.Assert(false, "must not reach here") c.selected = -1 return Suggest{}, false - } else if c.selected >= len(c.tmp) { - return Suggest{}, false } + return c.tmp[c.selected], true } diff --git a/prompt.go b/prompt.go index 3c9c4460..5427917d 100644 --- a/prompt.go +++ b/prompt.go @@ -4,6 +4,8 @@ import ( "bytes" "os" "time" + "unicode" + "unicode/utf8" "github.com/c-bata/go-prompt/internal/debug" ) @@ -157,6 +159,11 @@ func (p *Prompt) feed(b []byte) (shouldExit bool, exec *Exec) { if p.handleASCIICodeBinding(b) { return } + char, _ := utf8.DecodeRune(b) + if unicode.IsControl(char) { + return + } + p.buf.InsertText(string(b), false, true) }