MCPcopy Create free account
hub / github.com/git-bug/git-bug / PromptChoice

Function PromptChoice

commands/input/prompt.go:136–159  ·  view source on GitHub ↗

PromptChoice is a prompt giving possible choices Return the index starting at zero of the choice selected.

(prompt string, choices []string)

Source from the content-addressed store, hash-verified

134// PromptChoice is a prompt giving possible choices
135// Return the index starting at zero of the choice selected.
136func PromptChoice(prompt string, choices []string) (int, error) {
137 for {
138 for i, choice := range choices {
139 _, _ = fmt.Fprintf(os.Stderr, "[%d]: %s\n", i+1, choice)
140 }
141 _, _ = fmt.Fprintf(os.Stderr, "%s: ", prompt)
142
143 line, err := bufio.NewReader(os.Stdin).ReadString('\n')
144 fmt.Println()
145 if err != nil {
146 return 0, err
147 }
148
149 line = strings.TrimSpace(line)
150
151 index, err := strconv.Atoi(line)
152 if err != nil || index < 1 || index > len(choices) {
153 _, _ = fmt.Fprintln(os.Stderr, "invalid input")
154 continue
155 }
156
157 return index - 1, nil
158 }
159}
160
161func PromptURLWithRemote(prompt, name string, validRemotes []string, validators ...PromptValidator) (string, error) {
162 if len(validRemotes) == 0 {

Callers 2

ConfigureMethod · 0.92

Calls 2

ReadStringMethod · 0.65
PrintlnMethod · 0.65

Tested by

no test coverage detected