Effortlessly build beautiful command-line apps 🪄 Try the demo
go-clack/prompts
is an opinionated, pre-styled wrapper around go-clack/core
.
- 💎 Beautiful, minimal UI
- ✅ Simple API
- 🧱 Comes with
Text
,Confirm
,Select
,MultiSelect
,Spinner
, and more.
To start using go-clack/prompts
, follow these steps:
Add the go-clack/prompts
package to your Go project:
go get github.com/orochaa/go-clack/prompts
To create and run a simple text prompt, you can use the following code:
// main.go
package main
import (
"github.com/orochaa/go-clack/prompts"
)
func main() {
name, err := prompts.Text(prompts.TextParams{
Message: "What's your name?",
})
prompts.ExitOnError(err)
fmt.Printf("Hello, %s!\n", name)
}
Compile and run your application:
go run main.go
The Intro
and Outro
functions will print a message to begin or end a prompt session, respectively.
prompts.Intro("create-my-app")
// Do stuff
prompts.Outro("You're all set!")
An error
is returned when a user cancels a prompt with CTRL + C
.
You should handle this situation for each prompt, optionally providing a nice cancellation message with the Cancel
utility.
value, err := prompts.Text(/* TODO */)
if (err != nil) {
prompts.Cancel("Operation cancelled.")
os.Exit(0)
}
// Do stuff with `value`
Or just exiting using the ExitOnError
utility.
value, err := prompts.Text(/* TODO */)
prompts.ExitOnError(err)
// Do stuff with `value`
The Text
component accepts a single line of text.
meaning, err := prompts.Text(prompts.TextParams{
Message: "What is the meaning of life?",
Placeholder: "Not sure",
InitialValue: "42",
Validate: func(value string) error {
if value.length === 0 {
return errors.New("Value is required!")
}
return nil
},
})
The Password
component accepts a password input, masking the characters.
password, err := prompts.Password(prompts.PasswordParams{
Message: "Enter your password:",
Validate: func(value string) error {
if value.length < 8 {
return errors.New("Password must be at least 8 characters long!")
}
return nil
},
})
The Path
component accepts a file or directory path.
path, err := prompts.Path(prompts.PathParams{
Message: "Enter the file path:",
Validate: func(value string) error {
if !fileExists(value) {
return errors.New("File does not exist!")
}
return nil
},
})
The Confirm
component accepts a yes or no answer. The result is a boolean value of true
or false
.
confirmed, err := prompts.Confirm(prompts.ConfirmParams{
Message: "Are you sure?",
InitialValue: true,
})
The Select
component allows the user to choose a single option from a list.
project, err := prompts.Select(prompts.SelectParams{
Message: "Pick a project type:",
Options: []*prompts.SelectOption[string]{
{Label: "TypeScript", Value: "ts"},
{Label: "JavaScript", Value: "js"},
{Label: "CoffeeScript", Value: "coffee", Hint: "oh no"},
},
})
The MultiSelect
component allows the user to choose multiple options from a list.
additionalTools, err := prompts.MultiSelect(prompts.MultiSelectParams[string]{
Message: "Select additional tools:",
Options: []*prompts.MultiSelectOption[string]{
{Value: "eslint", Label: "ESLint", Hint: "recommended"},
{Value: "prettier", Label: "Prettier"},
{Value: "gh-action", Label: "GitHub Action"},
},
})
The GroupMultiSelect
component allows the user to choose multiple options from grouped lists.
groupChoices, err := prompts.GroupMultiSelect(prompts.GroupMultiSelectParams[string]{
Message: "Select additional tools:",
Options: map[string][]prompts.MultiSelectOption[string]{
"Group 1": {
{Label: "Option 1", Value: "1"},
{Label: "Option 2", Value: "2"},
},
"Group 2": {
{Label: "Option 3", Value: "3"},
{Label: "Option 4", Value: "4"},
},
},
})
The SelectPath
component allows the user to select a file/folder on a tree based select with free navigation by arrow keys.
selectedPath, err := prompts.SelectPath(prompts.SelectPathParams{
Message: "Select a path:",
})
The MultiSelectPath
component allows the user to select multiple files/folders on a tree based select with free navigation by arrow keys.
selectedPaths, err := prompts.MultiSelectPath(prompts.MultiSelectPathParams{
Message: "Select paths",
})
The SelectKey
component allows the user to choose a option associated to a key.
selectedKey, err := prompts.SelectKey(prompts.SelectKeyParams{
Message: "Select a key",
Options: []prompts.SelectKeyOption[string]{
{Key: "f", Label: "Foo"},
{Key: "b", Label: "Bar"},
{Key: "Enter", Label: "Baz"},
},
})
The spinner component surfaces a pending action, such as a long-running download or dependency installation.
s := prompts.Spinner()
s.Start("Installing via npm")
// Do installation here
s.Stop("Installed via npm")
Execute multiple tasks in spinners.
prompts.Tasks(
[]prompts.Task{
{
Title: "Installing via npm",
Task: func(message func(msg string)) (string, error) {
// Do installation here
return "Installed via npm", nil
}
},
},
prompts.SpinnerOptions{}
)
prompts.Info("Info!")
prompts.Success("Success!")
prompts.Step("Step!")
prompts.Warn("Warn!")
prompts.Error("Error!")
prompts.Message("Hello, World", prompts.MessageOptions{
FirstLine: prompts.MessageLineOptions{
Start: picocolors.Cyan("~"),
},
Default: prompts.MessageLineOptions{
Start: picocolors.Gray(symbols.BAR),
},
})