MCPcopy
hub / github.com/cheat/cheat / Prompt

Function Prompt

internal/installer/prompt.go:12–52  ·  view source on GitHub ↗

Prompt prompts the user for a answer

(prompt string, def bool)

Source from the content-addressed store, hash-verified

10
11// Prompt prompts the user for a answer
12func Prompt(prompt string, def bool) (bool, error) {
13
14 // display the prompt
15 fmt.Printf("%s: ", prompt)
16
17 // read one byte at a time until newline to avoid buffering past the
18 // end of the current line, which would consume input intended for
19 // subsequent Prompt calls on the same stdin
20 var line []byte
21 buf := make([]byte, 1)
22 for {
23 n, err := os.Stdin.Read(buf)
24 if n > 0 {
25 if buf[0] == '\n' {
26 break
27 }
28 if buf[0] != '\r' {
29 line = append(line, buf[0])
30 }
31 }
32 if err != nil {
33 if len(line) > 0 {
34 break
35 }
36 return false, fmt.Errorf("failed to prompt: %v", err)
37 }
38 }
39
40 // normalize the answer
41 ans := strings.ToLower(strings.TrimSpace(string(line)))
42
43 // return the appropriate response
44 switch ans {
45 case "y":
46 return true, nil
47 case "":
48 return def, nil
49 default:
50 return false, nil
51 }
52}

Callers 4

runFunction · 0.92
TestPromptFunction · 0.85
TestPromptErrorFunction · 0.85
RunFunction · 0.85

Calls

no outgoing calls

Tested by 2

TestPromptFunction · 0.68
TestPromptErrorFunction · 0.68