Skip to content

Commit

Permalink
Merge pull request #382 from luisdavim/confirm_answers
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed Jul 26, 2022
2 parents 8acc75a + 561cda9 commit c358754
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
6 changes: 5 additions & 1 deletion .golangci.yml
Expand Up @@ -88,5 +88,9 @@ issues:
- linters:
- gocritic
text: "unnecessaryDefer:"
- linters:
- gocritic
text: "preferDecodeRune:"
service:
golangci-lint-version: 1.31.x # use the fixed version to not introduce new linters unexpectedly
golangci-lint-version: 1.31.x # use the fixed version to not introduce new linters unexpectedly

26 changes: 17 additions & 9 deletions interactive_confirm_printer.go
Expand Up @@ -3,6 +3,7 @@ package pterm
import (
"fmt"
"os"
"strings"

"atomicgo.dev/cursor"
"atomicgo.dev/keyboard"
Expand Down Expand Up @@ -98,23 +99,24 @@ func (p InteractiveConfirmPrinter) Show(text ...string) (bool, error) {
}

p.TextStyle.Print(text[0] + " " + p.getSuffix() + ": ")
y, n := p.getShortHandles()

err := keyboard.Listen(func(keyInfo keys.Key) (stop bool, err error) {
key := keyInfo.Code
char := keyInfo.String()
char := strings.ToLower(keyInfo.String())
if err != nil {
return false, fmt.Errorf("failed to get key: %w", err)
}

switch key {
case keys.RuneKey:
switch char {
case "y", "Y":
case y:
p.ConfirmStyle.Print(p.ConfirmText)
Println()
result = true
return true, nil
case "n", "N":
case n:
p.RejectStyle.Print(p.RejectText)
Println()
result = false
Expand All @@ -139,15 +141,21 @@ func (p InteractiveConfirmPrinter) Show(text ...string) (bool, error) {
return result, err
}

// getShortHandles returns the short hand answers for the confirmation prompt
func (p InteractiveConfirmPrinter) getShortHandles() (string, string) {
y := strings.ToLower(string([]rune(p.ConfirmText)[0]))
n := strings.ToLower(string([]rune(p.RejectText)[0]))

return y, n
}

// getSuffix returns the confirmation prompt suffix
func (p InteractiveConfirmPrinter) getSuffix() string {
var y string
var n string
y, n := p.getShortHandles()
if p.DefaultValue {
y = "Y"
n = "n"
y = strings.ToUpper(y)
} else {
y = "y"
n = "N"
n = strings.ToUpper(n)
}

return p.SuffixStyle.Sprintf("[%s/%s]", y, n)
Expand Down
39 changes: 39 additions & 0 deletions interactive_confirm_printer_test.go
Expand Up @@ -76,6 +76,45 @@ func TestInteractiveConfirmPrinter_WithRejectText(t *testing.T) {
testza.AssertEqual(t, p.RejectText, "reject")
}

func TestInteractiveConfirmPrinter_CustomAnswers(t *testing.T) {
p := pterm.DefaultInteractiveConfirm.WithRejectText("reject").WithConfirmText("accept")
tests := []struct {
name string
key rune
expected bool
}{
{
name: "Accept_upper_case",
key: 'A',
expected: true,
},
{
name: "Accept_lower",
key: 'a',
expected: true,
},
{
name: "Reject_upper_case",
key: 'R',
expected: false,
},
{
name: "Reject_lower_case",
key: 'r',
expected: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
go func() {
keyboard.SimulateKeyPress(tc.key)
}()
result, _ := p.Show()
testza.AssertEqual(t, result, tc.expected)
})
}
}

func TestInteractiveConfirmPrinter_WithSuffixStyle(t *testing.T) {
style := pterm.NewStyle(pterm.FgRed)
p := pterm.DefaultInteractiveConfirm.WithSuffixStyle(style)
Expand Down

0 comments on commit c358754

Please sign in to comment.