DisplayTextMenu lets the user choose from a list of options, either by name or by number.
(choices []string, promptTemplate string, templateValues ...map[string]interface{})
| 81 | // DisplayTextMenu lets the user choose from a list of options, either by name |
| 82 | // or by number. |
| 83 | func (ui *UI) DisplayTextMenu(choices []string, promptTemplate string, templateValues ...map[string]interface{}) (string, error) { |
| 84 | for i, c := range choices { |
| 85 | t := fmt.Sprintf("%d. %s", i+1, c) |
| 86 | ui.DisplayText(t) |
| 87 | } |
| 88 | ui.DisplayNewline() |
| 89 | |
| 90 | translatedPrompt := ui.TranslateText(promptTemplate, templateValues...) |
| 91 | |
| 92 | interactivePrompt := ui.Interactor.NewInteraction(translatedPrompt) |
| 93 | |
| 94 | interactivePrompt.SetIn(ui.In) |
| 95 | interactivePrompt.SetOut(ui.OutForInteraction) |
| 96 | |
| 97 | value := "enter to skip" |
| 98 | err := interactivePrompt.Resolve(&value) |
| 99 | |
| 100 | if isInterrupt(err) { |
| 101 | ui.Exiter.Exit(sigIntExitCode) |
| 102 | } |
| 103 | |
| 104 | if err != nil { |
| 105 | return "", err |
| 106 | } |
| 107 | |
| 108 | if value == "enter to skip" { |
| 109 | return "", nil |
| 110 | } |
| 111 | |
| 112 | i, err := strconv.Atoi(value) |
| 113 | if err != nil { |
| 114 | if contains(choices, value) { |
| 115 | return value, nil |
| 116 | } |
| 117 | return "", InvalidChoiceError{Choice: value} |
| 118 | } |
| 119 | |
| 120 | if i > len(choices) || i <= 0 { |
| 121 | return "", ErrInvalidIndex |
| 122 | } |
| 123 | return choices[i-1], nil |
| 124 | } |
| 125 | |
| 126 | // DisplayTextPrompt outputs the prompt and waits for user input. |
| 127 | func (ui *UI) DisplayTextPrompt(template string, templateValues ...map[string]interface{}) (string, error) { |
nothing calls this directly
no test coverage detected