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

fix: the interactive confirm answers should match the confirm/reject text #382

Merged
merged 3 commits into from Jul 26, 2022
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
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