Convert Figma logo to code with AI

c-bata logogo-prompt

Building powerful interactive prompts in Go, inspired by python-prompt-toolkit.

5,267
346
5,267
113

Top Related Projects

Readline is a pure go(golang) implementation for GNU-Readline kind library

Interactive prompt for command-line applications

4,082

A golang library for building interactive and accessible prompts with full support for windows and posix terminals.

1,676

Library for creating interactive cli applications.

1,043

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

  1. 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)
}
  1. 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{}
}
  1. 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:

  1. Install the library:

    go get github.com/c-bata/go-prompt
    
  2. Import the library in your Go file:

    import "github.com/c-bata/go-prompt"
    
  3. 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()
    
  4. 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.

4,082

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.

1,676

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.

1,043

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 Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

go-prompt

Go Report Card Software License GoDoc tests

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

Features

Powerful auto-completion

demo

(This is a GIF animation of kube-prompt.)

Flexible options

go-prompt provides many options. Please check option section of GoDoc for more details.

options

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.

keyboard shortcuts

Key BindingDescription
Ctrl + AGo to the beginning of the line (Home)
Ctrl + EGo to the end of the line (End)
Ctrl + PPrevious command (Up arrow)
Ctrl + NNext command (Down arrow)
Ctrl + FForward one character
Ctrl + BBackward one character
Ctrl + DDelete character under the cursor
Ctrl + HDelete character before the cursor (Backspace)
Ctrl + WCut the word before the cursor to the clipboard
Ctrl + KCut the line after the cursor to the clipboard
Ctrl + UCut the line before the cursor to the clipboard
Ctrl + LClear the screen

History

You can use Up arrow and Down arrow to walk through the history of commands executed.

History

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.