ζ₯ζ¬θͺ | Π ΡΡΡΠΊΠΈΠΉ | δΈζ | νκ΅μ΄ | EspaΓ±ol | FranΓ§ais
prompt is a simple terminal prompt library for Go that provides powerful interactive command-line interfaces. This library is designed as a replacement for the unmaintained c-bata/go-prompt library, addressing critical issues while adding enhanced functionality and better cross-platform support.
- π₯οΈ Cross-Platform Support - Works seamlessly on Linux, macOS, and Windows
- π Auto-Completion - Tab completion with fuzzy matching and customizable suggestions
- π Command History - Navigation with arrow keys, persistent history, and reverse search (Ctrl+R)
- β¨οΈ Key Bindings - Comprehensive shortcuts including Emacs-style navigation
- π Color Themes - Built-in color schemes and customizable theming
- π Multi-line Input - Support for multi-line input with proper cursor navigation
- π§ Simple API - Clean, modern API design with functional options pattern
go get github.com/nao1215/prompt
- Go Version: 1.24 or later
- Operating Systems:
- Linux
- macOS
- Windows
package main
import (
"errors"
"fmt"
"log"
"github.com/nao1215/prompt"
)
func main() {
p, err := prompt.New("$ ")
if err != nil {
log.Fatal(err)
}
defer p.Close()
for {
input, err := p.Run()
if err != nil {
if errors.Is(err, prompt.ErrEOF) {
fmt.Println("Goodbye!")
break
}
log.Printf("Error: %v\n", err)
continue
}
if input == "exit" {
break
}
fmt.Printf("You entered: %s\n", input)
}
}
package main
import (
"errors"
"log"
"github.com/nao1215/prompt"
)
func completer(d prompt.Document) []prompt.Suggestion {
return []prompt.Suggestion{
{Text: "help", Description: "Show help message"},
{Text: "users", Description: "List all users"},
{Text: "groups", Description: "List all groups"},
{Text: "exit", Description: "Exit the program"},
}
}
func main() {
p, err := prompt.New("myapp> ",
prompt.WithCompleter(completer),
prompt.WithColorScheme(prompt.ThemeNightOwl),
)
if err != nil {
log.Fatal(err)
}
defer p.Close()
for {
input, err := p.Run()
if err != nil {
if errors.Is(err, prompt.ErrEOF) {
break
}
continue
}
if input == "exit" {
break
}
// Handle commands...
}
}
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/nao1215/prompt"
)
func main() {
// Create prompt with history and timeout
p, err := prompt.New(">>> ",
prompt.WithMemoryHistory(100),
prompt.WithColorScheme(prompt.ThemeDracula),
)
if err != nil {
log.Fatal(err)
}
defer p.Close()
// Use context for timeout support
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
input, err := p.RunWithContext(ctx)
if err == context.DeadlineExceeded {
fmt.Println("Timeout reached")
return
}
fmt.Printf("Input: %s\n", input)
}
package main
import (
"errors"
"fmt"
"log"
"strings"
"github.com/nao1215/prompt"
)
func sqlCompleter(d prompt.Document) []prompt.Suggestion {
keywords := []string{
"SELECT", "FROM", "WHERE", "INSERT", "UPDATE",
"DELETE", "CREATE TABLE", "DROP TABLE",
}
suggestions := []prompt.Suggestion{}
input := strings.ToUpper(d.GetWordBeforeCursor())
for _, keyword := range keywords {
if strings.HasPrefix(keyword, input) {
suggestions = append(suggestions, prompt.Suggestion{
Text: keyword,
Description: "SQL keyword",
})
}
}
return suggestions
}
func main() {
p, err := prompt.New("sql> ",
prompt.WithCompleter(sqlCompleter),
prompt.WithMemoryHistory(50),
)
if err != nil {
log.Fatal(err)
}
defer p.Close()
for {
query, err := p.Run()
if err != nil {
if errors.Is(err, prompt.ErrEOF) {
break
}
continue
}
if query == "exit" || query == "quit" {
break
}
if strings.TrimSpace(query) != "" {
fmt.Printf("Executing: %s\n", query)
// Execute SQL query here...
}
}
}
// Create a fuzzy completer for commands
commands := []string{
"git status", "git commit", "git push", "git pull",
"docker run", "docker build", "docker ps",
"kubectl get", "kubectl apply", "kubectl delete",
}
fuzzyCompleter := prompt.NewFuzzyCompleter(commands)
p, err := prompt.New("$ ",
prompt.WithCompleter(fuzzyCompleter),
)
keyMap := prompt.NewDefaultKeyMap()
// Add Ctrl+L to clear the line
keyMap.Bind('\x0C', prompt.ActionDeleteLine)
p, err := prompt.New("$ ",
prompt.WithKeyMap(keyMap),
)
historyConfig := &prompt.HistoryConfig{
Enabled: true,
MaxEntries: 1000,
File: "/home/user/.myapp_history",
MaxFileSize: 1024 * 1024, // 1MB
MaxBackups: 3,
}
p, err := prompt.New("$ ",
prompt.WithHistory(historyConfig),
)
The library supports comprehensive key bindings out of the box:
Key | Action |
---|---|
Enter | Submit input |
Ctrl+C | Cancel and return ErrInterrupted |
Ctrl+D | EOF when buffer is empty |
β/β | Navigate history (or lines in multi-line mode) |
β/β | Move cursor |
Ctrl+A / Home | Move to beginning of line |
Ctrl+E / End | Move to end of line |
Ctrl+K | Delete from cursor to end of line |
Ctrl+U | Delete entire line |
Ctrl+W | Delete word backwards |
Ctrl+R | Reverse history search |
Tab | Auto-completion |
Backspace | Delete character backwards |
Delete | Delete character forwards |
Ctrl+β/β | Move by word boundaries |
Built-in color themes:
// Available themes
prompt.ThemeDefault
prompt.ThemeDracula
prompt.ThemeNightOwl
prompt.ThemeMonokai
prompt.ThemeSolarizedDark
prompt.ThemeSolarizedLight
// Usage
p, err := prompt.New("$ ",
prompt.WithColorScheme(prompt.ThemeDracula),
)
See the example directory for complete working examples:
- Basic Usage - Simple prompt with basic functionality
- Auto-completion - Tab completion with suggestions
- Command History - History navigation and persistence
- Multi-line Input - Multi-line editing support
- Interactive Shell - File explorer shell example
- Do NOT share prompt instances across goroutines
- Do NOT call methods concurrently on the same prompt instance
- Do NOT call
Close()
whileRun()
is active in another goroutine - Use separate prompt instances for concurrent operations if needed
The library provides specific error types:
prompt.ErrEOF
: User pressed Ctrl+D with empty bufferprompt.ErrInterrupted
: User pressed Ctrl+Ccontext.DeadlineExceeded
: Timeout reached (when using context)context.Canceled
: Context was cancelled
Contributions are welcome! Please see the Contributing Guide for more details.
- Go 1.24 or later
- golangci-lint for code quality
- Cross-platform testing on Linux, macOS, and Windows
If you find this project useful, please consider:
- β Giving it a star on GitHub - it helps others discover the project
- π Becoming a sponsor - your support keeps the project alive and motivates continued development
Your support, whether through stars, sponsorships, or contributions, is what drives this project forward. Thank you!
This project is licensed under the MIT License - see the LICENSE file for details.