Skip to content

Commit

Permalink
fix: the interactive confirm answers should match the confirm/reject …
Browse files Browse the repository at this point in the history
…text

Signed-off-by: Luis Davim <luis.davim@sendoso.com>
  • Loading branch information
luisdavim committed Jul 24, 2022
1 parent 8acc75a commit ba28126
Showing 1 changed file with 17 additions and 9 deletions.
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

0 comments on commit ba28126

Please sign in to comment.