go-prompt
Building powerful interactive prompts in Go, inspired by python-prompt-toolkit.
Top Related Projects
Readline is a pure go(golang) implementation for GNU-Readline kind library
Interactive prompt for command-line applications
A golang library for building interactive and accessible prompts with full support for windows and posix terminals.
Library for creating interactive cli applications.
Pure Go line editor with history, inspired by linenoise
Quick Overview
go-prompt is a powerful library for building interactive command-line applications in Go. It provides features like auto-completion, syntax highlighting, and a user-friendly prompt interface, making it easier for developers to create sophisticated CLI tools with a great user experience.
Pros
- Easy-to-use API for creating interactive prompts
- Built-in support for auto-completion and syntax highlighting
- Customizable appearance and behavior
- Cross-platform compatibility (Windows, macOS, Linux)
Cons
- Limited documentation and examples for advanced use cases
- May have performance issues with very large datasets for auto-completion
- Requires manual handling of command execution logic
- Not suitable for simple, non-interactive CLI applications
Code Examples
- Basic prompt usage:
package main
import (
"fmt"
"github.com/c-bata/go-prompt"
)
func completer(d prompt.Document) []prompt.Suggest {
s := []prompt.Suggest{
{Text: "hello", Description: "Print hello"},
{Text: "world", Description: "Print world"},
{Text: "exit", Description: "Exit the program"},
}
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
}
func main() {
p := prompt.New(
executor,
completer,
prompt.OptionPrefix(">>> "),
prompt.OptionTitle("Simple CLI"),
)
p.Run()
}
func executor(in string) {
fmt.Println("You typed:", in)
}
- Custom completer with dynamic suggestions:
func dynamicCompleter(d prompt.Document) []prompt.Suggest {
args := strings.Split(d.TextBeforeCursor(), " ")
if len(args) <= 1 {
return []prompt.Suggest{
{Text: "users", Description: "User management"},
{Text: "config", Description: "Configuration options"},
}
}
switch args[0] {
case "users":
return []prompt.Suggest{
{Text: "list", Description: "List all users"},
{Text: "add", Description: "Add a new user"},
{Text: "remove", Description: "Remove a user"},
}
case "config":
return []prompt.Suggest{
{Text: "show", Description: "Show current configuration"},
{Text: "set", Description: "Set a configuration option"},
}
}
return []prompt.Suggest{}
}
- Customizing prompt appearance:
p := prompt.New(
executor,
completer,
prompt.OptionPrefix("🚀 "),
prompt.OptionTitle("My Awesome CLI"),
prompt.OptionPrefixTextColor(prompt.Yellow),
prompt.OptionPreviewSuggestionTextColor(prompt.Blue),
prompt.OptionSelectedSuggestionBGColor(prompt.LightGray),
prompt.OptionSuggestionBGColor(prompt.DarkGray),
)
Getting Started
To use go-prompt in your Go project, follow these steps:
-
Install the library:
go get github.com/c-bata/go-prompt
-
Import the library in your Go file:
import "github.com/c-bata/go-prompt"
-
Create a basic prompt with a completer and executor function:
func completer(d prompt.Document) []prompt.Suggest { // Implement your suggestion logic here } func executor(in string) { // Implement your command execution logic here } p := prompt.New(executor, completer) p.Run()
-
Customize the prompt as needed using the various OptionXXX functions provided by the library.
Competitor Comparisons
Readline is a pure go(golang) implementation for GNU-Readline kind library
Pros of readline
- More mature and widely used in Go projects
- Supports multi-line editing and history navigation
- Lightweight with minimal dependencies
Cons of readline
- Less feature-rich compared to go-prompt
- Limited customization options for prompt appearance
- Lacks built-in support for syntax highlighting
Code Comparison
readline:
rl, err := readline.New("> ")
if err != nil {
panic(err)
}
defer rl.Close()
go-prompt:
p := prompt.New(
executor,
completer,
prompt.OptionPrefix(">>> "),
)
p.Run()
Key Differences
- readline focuses on providing a simple, lightweight readline implementation
- go-prompt offers more advanced features like syntax highlighting and custom completions
- readline uses a more traditional approach, while go-prompt provides a higher-level abstraction
Use Cases
- readline: Ideal for simple CLI applications or when lightweight dependencies are crucial
- go-prompt: Better suited for complex interactive prompts with rich features and customization
Community and Maintenance
- readline: Larger user base and longer history in the Go ecosystem
- go-prompt: Active development with frequent updates and feature additions
Performance
- readline: Generally faster due to its simplicity and focus on core functionality
- go-prompt: May have slightly higher overhead due to additional features
Interactive prompt for command-line applications
Pros of promptui
- More comprehensive input options, including select lists and confirmation prompts
- Built-in validation and transformation of user input
- Simpler API for basic use cases
Cons of promptui
- Less flexible for complex, multi-line prompts
- Fewer customization options for prompt appearance
- No built-in support for auto-completion
Code Comparison
promptui example:
prompt := promptui.Select{
Label: "Select Day",
Items: []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"},
}
_, result, err := prompt.Run()
go-prompt example:
p := prompt.New(
executor,
completer,
prompt.OptionPrefix(">>> "),
prompt.OptionTitle("My CLI"),
)
p.Run()
Summary
promptui offers a more user-friendly API for common prompt types, making it easier to create simple CLI interfaces quickly. It includes built-in validation and transformation features, which can save development time.
go-prompt, on the other hand, provides more flexibility for complex prompts and offers powerful auto-completion capabilities. It's better suited for creating interactive shells or command-line applications that require advanced features.
The choice between these libraries depends on the specific requirements of your project. If you need a quick and easy way to add basic prompts to your CLI, promptui might be the better choice. For more complex, interactive CLI applications, especially those requiring auto-completion, go-prompt could be more suitable.
A golang library for building interactive and accessible prompts with full support for windows and posix terminals.
Pros of survey
- More comprehensive set of question types (e.g., multi-select, password input)
- Better support for complex survey-style interactions
- Easier to create multi-step questionnaires
Cons of survey
- Less suitable for creating interactive command-line interfaces
- May have a steeper learning curve for simple prompts
- Lacks some advanced features like syntax highlighting
Code Comparison
survey:
prompt := &survey.Input{
Message: "Enter your name:",
}
survey.AskOne(prompt, &name)
go-prompt:
name := prompt.Input("Enter your name: ", completer)
Summary
survey is better suited for creating complex questionnaires and surveys, offering a wide range of question types. It excels in multi-step interactions but may be overkill for simple prompts. go-prompt, on the other hand, is more focused on creating interactive command-line interfaces with features like auto-completion and syntax highlighting. It's generally easier to use for basic prompts but lacks some of the advanced survey capabilities of survey.
Choose survey for comprehensive questionnaires and survey-style applications, while go-prompt is ideal for interactive CLI tools with simpler prompt requirements.
Library for creating interactive cli applications.
Pros of ishell
- More feature-rich, offering built-in command history, custom prompts, and multiline input
- Provides a higher-level abstraction for building interactive shells
- Includes support for subcommands and nested command structures
Cons of ishell
- Less focused on auto-completion functionality compared to go-prompt
- May have a steeper learning curve due to its more comprehensive feature set
- Potentially higher memory footprint due to additional features
Code Comparison
ishell:
shell := ishell.New()
shell.AddCmd(&ishell.Cmd{
Name: "greet",
Help: "greet user",
Func: func(c *ishell.Context) {
c.Println("Hello, world!")
},
})
shell.Run()
go-prompt:
p := prompt.New(
executor,
completer,
prompt.OptionPrefix(">>> "),
)
p.Run()
The ishell example demonstrates its command-based structure, while go-prompt showcases its simpler setup focused on completion and execution functions.
Pure Go line editor with history, inspired by linenoise
Pros of liner
- Simpler and more lightweight, focusing on basic line editing functionality
- Better suited for simpler CLI applications with minimal prompt requirements
- Easier to integrate into existing projects due to its simplicity
Cons of liner
- Limited features compared to go-prompt, lacking advanced completion and suggestion capabilities
- Less customizable and flexible for complex prompt scenarios
- May require more manual implementation for advanced features
Code Comparison
liner:
line, err := liner.NewLiner()
defer line.Close()
line.SetCtrlCAborts(true)
input, err := line.Prompt("Enter command: ")
go-prompt:
p := prompt.New(
executor,
completer,
prompt.OptionPrefix(">>> "),
prompt.OptionTitle("My CLI"),
)
p.Run()
Summary
liner is a simpler, lightweight option for basic line editing in CLI applications. It's easier to integrate but lacks advanced features. go-prompt offers more robust functionality, including sophisticated completion and suggestion capabilities, making it better suited for complex CLI tools. The choice between the two depends on the specific requirements of your project and the level of prompt functionality needed.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
go-prompt
A library for building powerful interactive prompts inspired by python-prompt-toolkit, making it easier to build cross-platform command line tools using Go.
package main
import (
"fmt"
"github.com/c-bata/go-prompt"
)
func completer(d prompt.Document) []prompt.Suggest {
s := []prompt.Suggest{
{Text: "users", Description: "Store the username and age"},
{Text: "articles", Description: "Store the article text posted by user"},
{Text: "comments", Description: "Store the text commented to articles"},
}
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
}
func main() {
fmt.Println("Please select table.")
t := prompt.Input("> ", completer)
fmt.Println("You selected " + t)
}
Projects using go-prompt
- c-bata/kube-prompt : An interactive kubernetes client featuring auto-complete written in Go.
- rancher/cli : The Rancher Command Line Interface (CLI)is a unified tool to manage your Rancher server
- kubicorn/kubicorn : Simple, cloud native infrastructure for Kubernetes.
- cch123/asm-cli : Interactive shell of assembly language(X86/X64) based on unicorn and rasm2
- ktr0731/evans : more expressive universal gRPC client
- CrushedPixel/moshpit: A Command-line tool for datamoshing.
- last-ent/testy-go: Testy Go: A tool for easy testing!
- tiagorlampert/CHAOS: a PoC that allow generate payloads and control remote operating systems.
- abs-lang/abs: ABS is a scripting language that works best on terminal. It tries to combine the elegance of languages such as Python, or Ruby, to the convenience of Bash.
- takashabe/btcli: btcli is a CLI client for the Bigtable. Has many read options and auto-completion.
- ysn2233/kafka-prompt: An interactive kafka-prompt(kafka-shell) built on existing kafka command client
- fishi0x01/vsh: HashiCorp Vault interactive shell
- mstrYoda/docker-shell: A simple interactive prompt for docker
- c-bata/gh-prompt: An interactive GitHub CLI featuring auto-complete.
- docker-slim/docker-slim: Don't change anything in your Docker container image and minify it by up to 30x (and for compiled languages even more) making it secure too! (free and open source)
- rueyaa332266/ezcron: Ezcron is a CLI tool, helping you deal with cron expression easier.
- qingstor/qsctl: Advanced command line tool for QingStor Object Storage.
- segmentio/topicctl: Tool for declarative management of Kafka topics
- chriswalz/bit: Bit is a modern Git CLI
- (If you create a CLI utility using go-prompt and want your own project to be listed here, please submit a GitHub issue.)
Features
Powerful auto-completion
(This is a GIF animation of kube-prompt.)
Flexible options
go-prompt provides many options. Please check option section of GoDoc for more details.
Keyboard Shortcuts
Emacs-like keyboard shortcuts are available by default (these also are the default shortcuts in Bash shell). You can customize and expand these shortcuts.
Key Binding | Description |
---|---|
Ctrl + A | Go to the beginning of the line (Home) |
Ctrl + E | Go to the end of the line (End) |
Ctrl + P | Previous command (Up arrow) |
Ctrl + N | Next command (Down arrow) |
Ctrl + F | Forward one character |
Ctrl + B | Backward one character |
Ctrl + D | Delete character under the cursor |
Ctrl + H | Delete character before the cursor (Backspace) |
Ctrl + W | Cut the word before the cursor to the clipboard |
Ctrl + K | Cut the line after the cursor to the clipboard |
Ctrl + U | Cut the line before the cursor to the clipboard |
Ctrl + L | Clear the screen |
History
You can use Up arrow and Down arrow to walk through the history of commands executed.
Multiple platform support
We have confirmed go-prompt works fine in the following terminals:
- iTerm2 (macOS)
- Terminal.app (macOS)
- Command Prompt (Windows)
- gnome-terminal (Ubuntu)
Links
Author
Masashi Shibata
License
This software is licensed under the MIT license, see LICENSE for more information.
Top Related Projects
Readline is a pure go(golang) implementation for GNU-Readline kind library
Interactive prompt for command-line applications
A golang library for building interactive and accessible prompts with full support for windows and posix terminals.
Library for creating interactive cli applications.
Pure Go line editor with history, inspired by linenoise
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot