Skip to content

Commit

Permalink
Merge branch 'master' into bdwyertech
Browse files Browse the repository at this point in the history
  • Loading branch information
jbowes authored Sep 28, 2020
2 parents 9d08362 + 0ce406e commit fd5822c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
12 changes: 10 additions & 2 deletions cursor.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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("")
Expand Down
3 changes: 3 additions & 0 deletions keycodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "↑"
Expand Down
18 changes: 13 additions & 5 deletions prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion select.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit fd5822c

Please sign in to comment.