Categorygithub.com/elk-language/go-prompt
modulepackage
1.1.5
Repository: https://github.com/elk-language/go-prompt.git
Documentation: pkg.go.dev

# README

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

License

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

Original Author

Masashi Shibata

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

Set a custom completer.
No description provided by the author
No description provided by the author
DeleteBeforeChar Go to Backspace.
DeleteChar Delete character under the cursor.
No description provided by the author
FilterContains checks whether the completion.Text contains sub.
FilterFuzzy checks whether the completion.Text fuzzy matches sub.
FilterHasPrefix checks whether the string completions.Text begins with sub.
FilterHasSuffix checks whether the completion.Text ends with sub.
GetKey returns Key correspond to input byte codes.
GoLeftChar Backward one character.
GoLineBeginning Go to the beginning of the line.
GoLineEnd Go to the End of the line.
GoRightChar Forward one character.
Input get the input data from the user and return it.
New returns a Prompt with powerful auto-completion.
NewBuffer is constructor of Buffer struct.
NewCompletionManager returns an initialized CompletionManager object.
NewDocument return the new empty document.
Create a new EagerLexer.
NewHistory returns new history object.
Build a new Renderer.
Create a new SimpleToken.
NewStderrWriter returns Writer object to write to stderr.
NewStdinReader returns Reader object to read from stdin.
NewStdoutWriter returns Writer object to write to stdout.
NoopCompleter implements a Completer function that always returns no suggestions.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
WithASCIICodeBind to set a custom key bind.
WithBreakLineCallback to run a callback at every break line.
WithCompleter is an option that sets a custom Completer object.
WithCompletionOnDown allows for Down arrow key to trigger completion.
WithCompletionWordSeparator can be used to set word separators.
WithDescriptionBGColor to change a background color of description text in drop down suggestions.
WithDescriptionTextColor to change a background color of description text in drop down suggestions.
WithExecuteOnEnterCallback can be used to set a custom callback function that determines whether an Enter key should trigger the Executor or add a newline to the user input buffer.
WithExitChecker set an exit function which checks if go-prompt exits its Run loop.
WithHistory to set history expressed by string array.
WithIndentSize is an option that sets the amount of spaces that constitute a single indentation level.
WithInitialText can be used to set the initial buffer text.
WithInputBGColor to change a color of background which is input by user.
WithInputTextColor to change a color of text which is input by user.
WithKeyBind to set a custom key bind.
WithKeyBindMode set a key bind mode.
WithLexer set lexer function and enable it.
WithMaxSuggestion specify the max number of displayed suggestions.
WithPrefix can be used to set a prefix string for the prompt.
WithPrefixBackgroundColor to change a background color of prefix string.
WithPrefixCallback can be used to change the prefix dynamically by a callback function.
WithPrefixTextColor change a text color of prefix string.
WithReader can be used to set a custom Reader object.
WithScrollbarBGColor to change a background color of scrollbar.
WithScrollbarThumbColor to change a thumb color on scrollbar.
WithSelectedDescriptionBGColor to change a background color of description which is selected inside suggestions drop down box.
WithSelectedDescriptionTextColor to change a text color of description which is selected inside suggestions drop down box.
WithSelectedSuggestionBGColor to change a background color for completed text which is selected inside suggestions drop down box.
WithSelectedSuggestionTextColor to change a text color for completed text which is selected inside suggestions drop down box.
WithShowCompletionAtStart to set completion window is open at start.
WithSuggestionBGColor change a background color in drop down suggestions.
WithSuggestionTextColor to change a text color in drop down suggestions.
WithTitle can be used to set the title displayed at the header bar of the terminal.
WithWriter can be used to set a custom Writer object.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
Matches any key.
No description provided by the author
No description provided by the author
Black represents a black.
Blue represents a blue.
No description provided by the author
Brown represents a brown.
CommonKeyBind is a mode without any keyboard shortcut.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Special.
Cyan represents a cyan.
DarkBlue represents a dark blue.
DarkGray represents a dark gray.
DarkGreen represents a dark green.
DarkRed represents a dark red.
DefaultColor represents a default color.
No description provided by the author
Default column count of the terminal.
Default row count of the terminal.
No description provided by the author
DisplayBlink set blink (less than 150 per minute).
DisplayBold set bold or increases intensity.
DisplayCrossedOut set characters legible, but marked for deletion.
DisplayDefaultFont set primary(default) font.
DisplayInvisible set invisible.
DisplayItalic set italic.
DisplayLowIntensity decreases intensity.
DisplayRapidBlink set blink (more than 150 per minute).
DisplayReset reset all display attributes.
DisplayReverse swap foreground and background colors.
DisplayUnderline set underline.
No description provided by the author
EmacsKeyBind is a mode to use emacs-like keyboard shortcut.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Fuchsia represents a fuchsia.
Green represents a green.
No description provided by the author
Key which is ignored.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
LightGray represents a light gray.
Key is not defined.
No description provided by the author
No description provided by the author
Purple represents a purple.
Red represents a red.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Aliases.
Turquoise represents a turquoise.
No description provided by the author
No description provided by the author
White represents a white.
No description provided by the author
Yellow represents a yellow.

# Variables

ASCIISequences holds mappings of the key and byte array.
NewStandardOutputWriter returns Writer object to write to stdout.

# Structs

ASCIICode is the type contains Key and it's ascii byte array.
ASCIICodeBind represents which []byte should do what operation.
Buffer emulates the console buffer.
CompletionManager manages which suggestion is now selected.
Document has text displayed in terminal and cursor position.
EagerLexer is a wrapper around LexerFunc that transforms an eager lexer which produces an array with all tokens at once into a streaming lexer compatible with go-prompt.
History stores the texts that are entered.
KeyBind represents which key should do what operation.
Position stores the coordinates of a p.
PosixReader is a Reader implementation for the POSIX environment.
PosixWriter is a Writer implementation for POSIX environment.
Prompt is a core struct of go-prompt.
Takes care of the rendering process.
SimpleToken as the default implementation of Token.
Suggest represents a single suggestion in the auto-complete box.
UserInput is the struct that contains the user input context.
VT100Writer generates VT100 escape sequences.
WinSize represents the width and height of terminal.

# Interfaces

Lexer is a streaming lexer that takes in a piece of text and streams tokens with the Next() method.
Reader is an interface to abstract input layer.
Token is a single unit of text returned by a Lexer.
Writer is an interface to abstract the output layer.

# Type aliases

Color represents color on terminal.
Completer is a function that returns a slice of suggestions for the given Document.
Constructor option for CompletionManager.
DisplayAttribute represents display attributes like Blinking, Bold, Italic and so on.
ExecuteOnEnterCallback is a function that receives user input after Enter has been pressed and determines whether the input should be executed.
Executor is called when the user inputs a line of text.
ExitChecker is called after user input to check if prompt must stop and exit go-prompt Run loop.
Filter is the type to filter the prompt.Suggestion array.
Key is the type express the key inserted from user.
KeyBindFunc receives buffer and processed it.
KeyBindMode to switch a key binding flexibly.
LexerFunc is a function implementing a simple lexer that receives a string and returns a complete slice of Tokens.
Option is the type to replace default parameters.
Callback function that returns a prompt prefix.
No description provided by the author