MCPcopy Index your code
hub / github.com/cloudfoundry/cli / DisplayTextMenu

Method DisplayTextMenu

util/ui/prompt.go:83–124  ·  view source on GitHub ↗

DisplayTextMenu lets the user choose from a list of options, either by name or by number.

(choices []string, promptTemplate string, templateValues ...map[string]interface{})

Source from the content-addressed store, hash-verified

81// DisplayTextMenu lets the user choose from a list of options, either by name
82// or by number.
83func (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.
127func (ui *UI) DisplayTextPrompt(template string, templateValues ...map[string]interface{}) (string, error) {

Callers

nothing calls this directly

Calls 10

DisplayTextMethod · 0.95
DisplayNewlineMethod · 0.95
TranslateTextMethod · 0.95
isInterruptFunction · 0.85
containsFunction · 0.70
NewInteractionMethod · 0.65
SetInMethod · 0.65
SetOutMethod · 0.65
ResolveMethod · 0.65
ExitMethod · 0.65

Tested by

no test coverage detected