diff --git a/cursor.go b/cursor.go index a233ba5..f2fa871 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) { @@ -195,7 +203,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 660c467..ef5cd6f 100644 --- a/keycodes.go +++ b/keycodes.go @@ -8,6 +8,9 @@ var ( // KeyEnter is the default key for submission/selection. KeyEnter rune = readline.CharEnter + // 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/prompt.go b/prompt.go index 97ff373..8e35123 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 @@ -218,9 +221,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) @@ -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() 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 }