Skip to content

Commit

Permalink
move sectret to textinput
Browse files Browse the repository at this point in the history
  • Loading branch information
snakeice authored and MarkusZoppelt committed Apr 10, 2023
1 parent 9017305 commit 4167d09
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 144 deletions.
130 changes: 0 additions & 130 deletions interactive_secretinput_printer.go

This file was deleted.

14 changes: 0 additions & 14 deletions interactive_secretinput_printer_test.go

This file was deleted.

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 4167d09

Please sign in to comment.