Skip to content

Commit

Permalink
Merge pull request #458 from MarkusZoppelt/add-feedback-password-prom…
Browse files Browse the repository at this point in the history
…pter
  • Loading branch information
MarvinJWendt committed Apr 15, 2023
2 parents 1ce176a + 9d14585 commit 1b1a776
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
30 changes: 30 additions & 0 deletions _examples/interactive_textinput/password/ci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"os"
"time"

"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"
)

// ------ Automation for CI ------
// You can ignore this function, it is used to automatically run the demo and generate the example animation in our CI system.
func init() {
if os.Getenv("CI") == "true" {
go func() {
time.Sleep(time.Second)
input := "Hello, World!"
for _, r := range input {
if r == '\n' {
keyboard.SimulateKeyPress(keys.Enter)
} else {
keyboard.SimulateKeyPress(r)
}
time.Sleep(time.Millisecond * 250)
}

keyboard.SimulateKeyPress(keys.Enter)
}()
}
}
10 changes: 10 additions & 0 deletions _examples/interactive_textinput/password/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import "github.com/pterm/pterm"

func main() {
result, _ := pterm.DefaultInteractiveTextInput.WithMask("*").Show("Enter your password")

logger := pterm.DefaultLogger
logger.Info("Password received", logger.Args("password", result))
}
14 changes: 14 additions & 0 deletions interactive_textinput_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var (
DefaultInteractiveTextInput = InteractiveTextInputPrinter{
DefaultText: "Input text",
TextStyle: &ThemeDefault.PrimaryStyle,
Mask: "",
}
)

Expand All @@ -23,6 +24,7 @@ type InteractiveTextInputPrinter struct {
TextStyle *Style
DefaultText string
MultiLine bool
Mask string

input []string
cursorXPos int
Expand All @@ -48,6 +50,12 @@ func (p InteractiveTextInputPrinter) WithMultiLine(multiLine ...bool) *Interacti
return &p
}

// WithMask sets the mask.
func (p InteractiveTextInputPrinter) WithMask(mask string) *InteractiveTextInputPrinter {
p.Mask = mask
return &p
}

// Show shows the interactive select menu and returns the selected entry.
func (p InteractiveTextInputPrinter) Show(text ...string) (string, error) {
// should be the first defer statement to make sure it is executed last
Expand Down Expand Up @@ -200,13 +208,19 @@ func (p InteractiveTextInputPrinter) updateArea(area *AreaPrinter) string {
p.cursorYPos = 0
}
areaText := p.text

for i, s := range p.input {
if i < len(p.input)-1 {
areaText += s + "\n"
} else {
areaText += s
}
}

if p.Mask != "" {
areaText = p.text + strings.Repeat(p.Mask, internal.GetStringMaxWidth(areaText)-internal.GetStringMaxWidth(p.text))
}

if p.cursorXPos+internal.GetStringMaxWidth(p.input[p.cursorYPos]) < 1 {
p.cursorXPos = -internal.GetStringMaxWidth(p.input[p.cursorYPos])
}
Expand Down
13 changes: 13 additions & 0 deletions interactive_textinput_printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package pterm_test
import (
"testing"

"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"
"github.com/MarvinJWendt/testza"

"github.com/pterm/pterm"
Expand All @@ -28,3 +30,14 @@ func TestInteractiveTextInputPrinter_WithTextStyle(t *testing.T) {
p := pterm.DefaultInteractiveTextInput.WithTextStyle(style)
testza.AssertEqual(t, p.TextStyle, style)
}

func TestInteractiveTextInputPrinter_WithMask(t *testing.T) {
go func() {
keyboard.SimulateKeyPress('a')
keyboard.SimulateKeyPress('b')
keyboard.SimulateKeyPress('c')
keyboard.SimulateKeyPress(keys.Enter)
}()
result, _ := pterm.DefaultInteractiveTextInput.WithMask("*").Show()
testza.AssertEqual(t, result, "abc")
}

0 comments on commit 1b1a776

Please sign in to comment.