Documentation
¶
Overview ¶
Package terminal runs the reline line editor on a real terminal: the host half of MRI's Reline (Reline::Core#readline and #readmultiline over Reline::ANSI) that the editing core leaves to its embedder.
A Terminal puts its input in raw mode for the time of one Readline, decodes the keys it reads with the editor's keymaps and the ANSI terminal bindings (reline.SetANSIKeyBindings), feeds them to a reline.LineEditor, and redraws the edited lines, with their prompts and the completion menu, after every key. Between two reads the terminal is left as it was found, so a program's output is written in the terminal's normal mode.
It is pure Go: raw mode and the window size come from golang.org/x/term, on Unix and on Windows (whose console then takes virtual-terminal input and output sequences, Windows 10 and later).
Index ¶
- Variables
- type Options
- type Terminal
- func (t *Terminal) ClearScreen()
- func (t *Terminal) EraseAfterCursor()
- func (t *Terminal) GetC(timeoutMs int) int
- func (t *Terminal) GetScreenSize() (rows, cols int)
- func (t *Terminal) MoveCursorColumn(col int)
- func (t *Terminal) Readline(o Options) (string, error)
- func (t *Terminal) UngetC(c int)
- func (t *Terminal) Write(s string)
Constants ¶
This section is empty.
Variables ¶
var ErrInterrupt = errors.New("interrupt")
ErrInterrupt is returned by Readline when Ctrl-C is typed: the read is abandoned, as MRI's Reline raises Interrupt.
Functions ¶
This section is empty.
Types ¶
type Options ¶
type Options struct {
// Prompt is shown before the first line.
Prompt string
// Terminate, when set, makes the read multiline (MRI
// Reline.readmultiline): Enter on the last line submits the buffer when
// Terminate accepts it — the buffer is given with a final newline — and
// starts a new line of the buffer otherwise.
Terminate func(buffer string) bool
// PromptProc gives each line of a multiline buffer its prompt (MRI
// Reline.prompt_proc); nil shows Prompt on every line.
PromptProc reline.PromptProc
// Complete computes the completion candidates for the word before the
// cursor (MRI Reline.completion_proc); nil disables completion.
Complete reline.CompletionProc
// AppendCharacter is added after a unique completion, typically a space
// (MRI Reline.completion_append_character).
AppendCharacter string
// AddHistory appends a submitted, non-empty buffer to the history (the
// add_hist argument of MRI's readline).
AddHistory bool
}
Options configure one Readline.
type Terminal ¶
type Terminal struct {
// Config holds the keymaps and the editing mode; New gives it the ANSI
// terminal bindings.
Config *reline.Config
// History is the history the editor navigates; New makes it unlimited.
History *reline.History
// contains filtered or unexported fields
}
Terminal is a reline.IO on a terminal. Its zero value is not usable; make one with New.
func New ¶
New returns a Terminal reading keys from in and drawing on out, which are normally os.Stdin and os.Stdout; in must be a terminal (term.IsTerminal).
func (*Terminal) ClearScreen ¶
func (t *Terminal) ClearScreen()
ClearScreen implements reline.IO (MRI Reline::ANSI#clear_screen): the screen is cleared and the next drawing starts at its top.
func (*Terminal) EraseAfterCursor ¶
func (t *Terminal) EraseAfterCursor()
EraseAfterCursor implements reline.IO.
func (*Terminal) GetC ¶
GetC implements reline.IO: the next input byte, a byte pushed back first; -1 when the input has ended or, with timeoutMs >= 0, when no byte comes within timeoutMs milliseconds.
func (*Terminal) GetScreenSize ¶
GetScreenSize implements reline.IO (MRI Reline::ANSI#get_screen_size): the terminal's size, else LINES and COLUMNS, else 24 by 80.
func (*Terminal) MoveCursorColumn ¶
MoveCursorColumn implements reline.IO.
func (*Terminal) Readline ¶
Readline reads one buffer: a line, or several with o.Terminate. It returns io.EOF when Ctrl-D is typed on an empty buffer or the input ends, ErrInterrupt on Ctrl-C, and an error when the terminal cannot be put in raw mode. The submitted buffer has no final newline.