From 289685b65456eba670d973e87802443f7cf8e95a Mon Sep 17 00:00:00 2001 From: micnncim Date: Wed, 5 Feb 2020 01:56:16 +0900 Subject: [PATCH 1/3] Add Ctrl-H support to delete input text --- cursor.go | 2 +- keycodes.go | 3 +++ select.go | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/cursor.go b/cursor.go index a233ba5..f329df1 100644 --- a/cursor.go +++ b/cursor.go @@ -195,7 +195,7 @@ func (c *Cursor) Listen(line []rune, pos int, key rune) ([]rune, int, bool) { case 0: // empty case KeyEnter: return []rune(c.Get()), c.Position, false - case KeyBackspace: + case KeyBackspace, KeyCtrlH: if c.erase { c.erase = false c.Replace("") diff --git a/keycodes.go b/keycodes.go index e084565..2c625c3 100644 --- a/keycodes.go +++ b/keycodes.go @@ -13,6 +13,9 @@ var ( // KeyBackspace is the default key for deleting input text. KeyBackspace rune = readline.CharBackspace + // KeyCtrlH is the key for deleting input text. + KeyCtrlH rune = readline.CharCtrlH + // KeyPrev is the default key to go up during selection. KeyPrev rune = readline.CharPrev KeyPrevDisplay = "↑" diff --git a/select.go b/select.go index 15dfc41..19b9e0c 100644 --- a/select.go +++ b/select.go @@ -273,7 +273,7 @@ func (s *Select) innerRun(cursorPos, scroll int, top rune) (int, string, error) } else { searchMode = true } - case key == KeyBackspace: + case key == KeyBackspace || key == KeyCtrlH: if !canSearch || !searchMode { break } From c2f558aa8ab5c302c5761930793834f36b10eb09 Mon Sep 17 00:00:00 2001 From: jamesdobson Date: Sat, 18 Apr 2020 13:48:07 -0400 Subject: [PATCH 2/3] Fixed echo of cursor after input is finished. (fixes #117) Based on: https://github.com/manifoldco/promptui/pull/118 by github.com/richardmcsong, taking only the changes required to fix issue #117. --- cursor.go | 10 +++++++++- prompt.go | 4 ++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/cursor.go b/cursor.go index a233ba5..5113bda 100644 --- a/cursor.go +++ b/cursor.go @@ -1,6 +1,9 @@ package promptui -import "fmt" +import ( + "fmt" + "strings" +) // Pointer is A specific type that translates a given set of runes into a given // set of runes pointed at by the cursor. @@ -144,6 +147,11 @@ func (c *Cursor) Get() string { return string(c.input) } +// GetMask returns a mask string with length equal to the input +func (c *Cursor) GetMask(mask rune) string { + return strings.Repeat(string(mask), len(c.input)) +} + // Replace replaces the previous input with whatever is specified, and moves the // cursor to the end position func (c *Cursor) Replace(input string) { diff --git a/prompt.go b/prompt.go index 97ff373..0ec64b0 100644 --- a/prompt.go +++ b/prompt.go @@ -218,9 +218,9 @@ func (p *Prompt) Run() (string, error) { return "", err } - echo := cur.Format() + echo := cur.Get() if p.Mask != 0 { - echo = cur.FormatMask(p.Mask) + echo = cur.GetMask(p.Mask) } prompt := render(p.Templates.success, p.Label) From 361491009c119a9063b88de9581d5ebc5cb440c4 Mon Sep 17 00:00:00 2001 From: Yaroslav Skopets Date: Thu, 30 Apr 2020 01:09:02 +0200 Subject: [PATCH 3/3] prompt: add `HideEntered` flag --- prompt.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/prompt.go b/prompt.go index 97ff373..85fe4ed 100644 --- a/prompt.go +++ b/prompt.go @@ -33,6 +33,9 @@ type Prompt struct { // allows hiding private information like passwords. Mask rune + // HideEntered sets whether to hide the text after the user has pressed enter. + HideEntered bool + // Templates can be used to customize the prompt output. If nil is passed, the // default templates are used. See the PromptTemplates docs for more info. Templates *PromptTemplates @@ -234,9 +237,14 @@ func (p *Prompt) Run() (string, error) { } } - sb.Reset() - sb.Write(prompt) - sb.Flush() + if p.HideEntered { + clearScreen(sb) + } else { + sb.Reset() + sb.Write(prompt) + sb.Flush() + } + rl.Write([]byte(showCursor)) rl.Close()