Skip to content

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

License

Notifications You must be signed in to change notification settings

elk-language/go-prompt

 
 

Repository files navigation

go-prompt

Go Report Card Software License GoDoc tests

This is a fork of c-bata/go-prompt. It's a great library but it's been abandoned for quite a while. This project aims to continue its development.

The library has been rewritten in many aspects, fixing existing bugs and adding new essential functionality.

Most notable changes include:

  • Support for custom syntax highlighting with a lexer
  • Multiline editing
  • A scrolling buffer is used for displaying the current content which makes it possible to edit text of arbitrary length (only the visible part of the text is rendered)
  • Support for automatic indentation when pressing Enter and the input is incomplete or for executing the input when it is complete. This is determined by a custom callback function.

I highly encourage you to see the changelog which fully documents the changes that have been made.


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/elk-language/go-prompt"
	pstrings "github.com/elk-language/go-prompt/strings"
)

func completer(d prompt.Document) ([]prompt.Suggest, pstrings.RuneNumber, pstrings.RuneNumber) {
	endIndex := d.CurrentRuneIndex()
	w := d.GetWordBeforeCursor()
	startIndex := endIndex - pstrings.RuneCount(w)

	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, w, true), startIndex, endIndex
}

func main() {
	fmt.Println("Please select table.")
	t := prompt.Input(
		prompt.WithPrefix("> "),
		prompt.WithCompleter(completer),
	)
	fmt.Println("You selected " + t)
}

Features

Automatic indentation with a custom callback

automatic indentation

Multiline editing with scrolling

multiline editing

Custom syntax highlighting

syntax highlighting

Powerful auto-completion

autocompletion

(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 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.

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

License

This software is licensed under the MIT license, see LICENSE for more information.

Original Author

Masashi Shibata

Packages

 
 
 

Languages

  • Go 99.2%
  • Other 0.8%