A simple Go package to make terminal input styled a little more like console input in Craft CMS commands.
go get github.com/pixelandtonic/prompt
To create a new default prompt:
p := prompt.NewPrompt()
You can also override the options for a prompt using:
p := prompt.NewWithOptions(opts)
To prompt a user to enter input, you simply .Ask
:
opts := &prompt.InputOptions{Default: "42"}
a, err := p.Ask("What is the answer to the Ultimate Question of Life, the Universe, and Everything", opts)
if err != nil {
log.Println(err)
}
fmt.Println("the user answered:", a)
Note: Notice how we did not add a space or ? at the end of the question. The default options when calling
.NewPrompt()
will always add a space and question mark at the end of the output question. If you want to override that pass options usingprompt.Options
If the user does not provide an input, the default value passed in the options will be returned.
You can also validate input with a simple func:
opts := &prompt.InputOptions{Default: "42", Validator: validateTheMeaning}
a, err := p.Ask("What is the answer to the Ultimate Question of Life, the Universe, and Everything", opts)
if err != nil {
log.Println(err)
}
fmt.Println("the user answered:", a)
The validate function is simple and takes a string and returns an error:
func validateTheMeaning(input string) error {
if input != "42" {
return errors.New("wrong, the answer is 42")
}
return nil
}
You can also use Confirm to always return a boolean which defaults to false
opts := &prompt.InputOptions{Default: "yes"}
confirm, err := p.Confirm("Do you confirm these changes", opts)
if err != nil {
log.Println(err)
}
fmt.Println("confirmed:", confirm)
You can also pass a validator to confirm, but this will default to checking if the input contains a y
.
You can also create a selection of items to choose from:
opts := &prompt.SelectOptions{Default: 1}
selected, index, err := p.Select("Select an speed", []string{"Ludicrous mode", "Normal mode"}, opts)
if err != nil {
log.Println(err)
}