Merge branch 'master' into bdwyertech

This commit is contained in:
James Bowes 2020-09-28 09:37:06 -03:00 committed by GitHub
commit fd5822cb85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 8 deletions

View file

@ -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("")

View file

@ -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 = "↑"

View file

@ -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()

View file

@ -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
}