我正在使用promptUi
创建一个选择列表。现在,我想在选择后提示“是”或“否”问题:
bold := color.New(color.Bold).SprintFunc()
cellTemplate := &promptui.SelectTemplates{
Label: "{{ . }}",
Active: "\U000027A4 {{ .| bold }}",
Inactive: " {{ . | faint }}",
Help: util.Faint("[Use arrow keys]"),
}
cellPrompt := promptui.Select{
Label: util.YellowBold("?") + " Select an environment to be installed",
Items: getCreateEnvironmentList(),
Templates: cellTemplate,
}
_, value, err := cellPrompt.Run()
if err != nil {
return fmt.Errorf("Failed to select: %v", err)
}
switch value {
case constants.CELLERY_CREATE_LOCAL:
{
// Prompt yes or no
}
case constants.CELLERY_CREATE_GCP:
{
// Prompt yes or no
}
default:
{
Back()
}
}
是否有类似的方式可以优雅地提示您?
答案 0 :(得分:5)
尝试以下func yesNo() bool
:
package main
import (
"fmt"
"log"
"github.com/manifoldco/promptui"
)
func main() {
fmt.Println(yesNo())
fmt.Println(yesNo())
}
func yesNo() bool {
prompt := promptui.Select{
Label: "Select[Yes/No]",
Items: []string{"Yes", "No"},
}
_, result, err := prompt.Run()
if err != nil {
log.Fatalf("Prompt failed %v\n", err)
}
return result == "Yes"
}
输出:
? Select[Yes/No]:
▸ Yes
No
✔ Yes
true
✔ No
false