Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(textinput): added optional mask for prompting for secrets/passwords/tokens #458

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions _examples/interactive_textinput/password/ci.go
@@ -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
@@ -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
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
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")
}