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

Function PromptDefault

commands/input/prompt.go:57–90  ·  view source on GitHub ↗

PromptDefault is a simple text input with a default value.

(prompt, name, preValue string, validators ...PromptValidator)

Source from the content-addressed store, hash-verified

55
56// PromptDefault is a simple text input with a default value.
57func PromptDefault(prompt, name, preValue string, validators ...PromptValidator) (string, error) {
58loop:
59 for {
60 if preValue != "" {
61 _, _ = fmt.Fprintf(os.Stderr, "%s [%s]: ", prompt, preValue)
62 } else {
63 _, _ = fmt.Fprintf(os.Stderr, "%s: ", prompt)
64 }
65
66 line, err := bufio.NewReader(os.Stdin).ReadString('\n')
67 if err != nil {
68 return "", err
69 }
70
71 line = strings.TrimSpace(line)
72
73 if preValue != "" && line == "" {
74 line = preValue
75 }
76
77 for _, validator := range validators {
78 complaint, err := validator(name, line)
79 if err != nil {
80 return "", err
81 }
82 if complaint != "" {
83 _, _ = fmt.Fprintln(os.Stderr, complaint)
84 continue loop
85 }
86 }
87
88 return line, nil
89 }
90}
91
92// PromptPassword is a specialized text input that doesn't display the characters entered.
93func PromptPassword(prompt, name string, validators ...PromptValidator) (string, error) {

Callers 3

ConfigureMethod · 0.92
runUserNewFunction · 0.92
PromptFunction · 0.85

Calls 1

ReadStringMethod · 0.65

Tested by

no test coverage detected