ultrago/cli/input.go

34 lines
718 B
Go
Raw Normal View History

2024-08-13 15:38:22 +00:00
package cli
import "github.com/erikgeiser/promptkit/textinput"
func WithPretty(inp *textinput.TextInput) {
const template = `
{{- Foreground "2" (Bold "?") }} {{ .Prompt }}
{{- Foreground "8" ">" }} {{ .Input }}
`
inp.Template = template
}
type InputOption func(*textinput.TextInput)
func WithDefault(defaultValue string) InputOption {
return func(inp *textinput.TextInput) {
inp.Placeholder = defaultValue
}
}
func WithValidate(validate func(string) error) InputOption {
return func(inp *textinput.TextInput) {
inp.Validate = validate
}
}
func Ask(prompt string, opts ...InputOption) (string, error) {
inp := textinput.New(prompt)
for _, opt := range opts {
opt(inp)
}
return inp.RunPrompt()
}