Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CompletionFunc ¶
CompletionFunc is used to find the completions of the word delineated by [wordStart,wordEnd) within text. The full text is provided so that context-sensitive completion can be performed (e.g. a keyword might only be valid in certain contexts). The list of completions should be returned in priority order.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option defines the interface for Prompt options.
func WithCompleter ¶
func WithCompleter(fn CompletionFunc) Option
WithCompleter allows configuring a callback that will be invoked when text is entered or deleted in order to determine completions of the word at the current position.
func WithHistory ¶
func WithInput ¶
WithInput allows configuring the input reader for a Prompt. This option is primarily useful for tests.
func WithInputFinished ¶
WithInputFinished allows configuring a callback that will be invoked when enter is pressed to determine if the input is considered complete or not. If the input is not complete, a newline is instead inserted into the input.
func WithOutput ¶
WithOutput allows configuring the output writer for a Prompt. This option is primarily useful for tests.
type Prompt ¶
type Prompt struct {
// contains filtered or unexported fields
}
Prompt contains the state for reading single or multi-line input from a terminal. Similar to readline, libedit, and other CLI line reading libraries, Prompt provides support for basic editing functionality such as cursor movement, deletion, a kill ring, and history.
Prompt supports a common subset of the universe of key input sequences which are used by ~75% of the terminals in the terminfo database, including most modern terminals. Prompt itself does not use terminfo. Additionally, Prompt requires that the terminal handle a minimal set of ANSI escape sequences for rendering text:
- cursor-up: ESC[A
- cursor-down: ESC[B
- cursor-right: ESC[C
- cursor-left: ESC[D
- cursor-home: ESC[H
- erase-line-to-right: ESC[K
- erase-screen: ESC[2J
Prompt eschews using more advanced terminal operations such as insert/delete character and insert mode. This decision results in Prompt having to re-render more lines of text on editing operations, yet for line editing the difference usually amounts to sending a few hundred bytes to the terminal (for a long line). On modern hardware and networks, this amount of data is trivial. The benefit of eschewing more advanced terminal operations is that the same rendering output is used for all terminals as opposed to the libedit/readline approach which requires intimate knowledge of the terminal capabilities (via terminfo) and which can sometimes go horribly wrong resulting in corruption of the rendered text.
func New ¶
New creates a new Prompt using the supplied options. If no options are specified, the Prompt uses os.Stdin and os.Stdout for input and output.