Help us shape the future of Go! Take the 2026 Go Developer Survey

terminal

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2026 License: BSD-3-Clause Imports: 10 Imported by: 0

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

Constants

This section is empty.

Variables

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

func New(in, out *os.File) *Terminal

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

func (t *Terminal) GetC(timeoutMs int) int

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

func (t *Terminal) GetScreenSize() (rows, cols int)

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

func (t *Terminal) MoveCursorColumn(col int)

MoveCursorColumn implements reline.IO.

func (*Terminal) Readline

func (t *Terminal) Readline(o Options) (string, error)

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.

func (*Terminal) UngetC

func (t *Terminal) UngetC(c int)

UngetC implements reline.IO: c is the next byte GetC returns.

func (*Terminal) Write

func (t *Terminal) Write(s string)

Write implements reline.IO.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL