mirror of https://github.com/mickael-menu/zk
Improve confirm prompt using third-party library
parent
5ef45020f0
commit
16fdcfe751
@ -1,83 +1,16 @@
|
||||
package term
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/mickael-menu/zk/core/style"
|
||||
survey "github.com/AlecAivazis/survey/v2"
|
||||
)
|
||||
|
||||
// PromptOpt holds metadata about a possible prompt response.
|
||||
type PromptOpt struct {
|
||||
// Default value for the response.
|
||||
Label string
|
||||
// Short description explaining this response.
|
||||
Description string
|
||||
// All recognized values for this response.
|
||||
AllowedResponses []string
|
||||
}
|
||||
|
||||
// Prompt displays a message and waits for the user to input one of the
|
||||
// available options.
|
||||
// Returns the selected option index.
|
||||
func (t *Terminal) Prompt(msg string, defaultOpt int, options []PromptOpt) int {
|
||||
responses := ""
|
||||
for i, opt := range options {
|
||||
if i == len(options)-1 {
|
||||
responses += " or "
|
||||
} else if i > 0 {
|
||||
responses += ", "
|
||||
}
|
||||
responses += opt.Label
|
||||
}
|
||||
|
||||
printHelp := func() {
|
||||
fmt.Println("\nExpected responses:")
|
||||
for _, opt := range options {
|
||||
fmt.Printf(" %v\t%v\n", opt.Label, opt.Description)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
for {
|
||||
fmt.Printf("%s\n%s > ", msg, responses)
|
||||
|
||||
if !t.IsInteractive() {
|
||||
fmt.Println(options[defaultOpt].AllowedResponses[0])
|
||||
return defaultOpt
|
||||
}
|
||||
|
||||
var response string
|
||||
_, err := fmt.Scan(&response)
|
||||
if err != nil {
|
||||
return defaultOpt
|
||||
}
|
||||
response = strings.ToLower(response)
|
||||
|
||||
for i, opt := range options {
|
||||
for _, allowedResp := range opt.AllowedResponses {
|
||||
if response == strings.ToLower(allowedResp) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printHelp()
|
||||
}
|
||||
}
|
||||
|
||||
// Confirm is a shortcut to prompt a yes/no question to the user.
|
||||
func (t *Terminal) Confirm(msg string, yesDescription string, noDescription string) bool {
|
||||
return t.Prompt(msg, 1, []PromptOpt{
|
||||
{
|
||||
Label: t.MustStyle("y", style.RuleEmphasis) + "es",
|
||||
Description: yesDescription,
|
||||
AllowedResponses: []string{"yes", "y", "ok"},
|
||||
},
|
||||
{
|
||||
Label: t.MustStyle("n", style.RuleEmphasis) + "o",
|
||||
Description: noDescription,
|
||||
AllowedResponses: []string{"no", "n"},
|
||||
},
|
||||
}) == 0
|
||||
func (t *Terminal) Confirm(msg string) bool {
|
||||
confirmed := false
|
||||
prompt := &survey.Confirm{
|
||||
Message: msg,
|
||||
Default: true,
|
||||
}
|
||||
survey.AskOne(prompt, &confirmed)
|
||||
return confirmed
|
||||
}
|
||||
|
Loading…
Reference in New Issue