lineedit

package
v0.151.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: MIT, BSD-3-Clause Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package lineedit is a vendored copy of golang.org/x/term's Terminal line editor (v0.42.0), patched to be East-Asian-wide-rune aware. Upstream assumes every rune occupies exactly one terminal cell, so its cursor math corrupted lines containing 한글/CJK/wide-emoji (residue on delete, misplaced cursor). Unlike bubbletea's inline renderer, this editor leaves the HARDWARE cursor at the logical caret position, so terminal IME preedit composition renders in the right place — the reason we keep an x/term-style editor for the REPL.

Changes from upstream are marked "wide-rune:" and live in visualLength, moveCursorToPos, setLine, and eraseNPreviousChars. Everything else is byte-for-byte upstream; keep it that way to ease future resyncs.

Index

Constants

This section is empty.

Variables

View Source
var ErrPasteIndicator = pasteIndicatorError{}

ErrPasteIndicator may be returned from ReadLine as the error, in addition to valid line data. It indicates that bracketed paste mode is enabled and that the returned line consists only of pasted data. Programs may wish to interpret pasted data more literally than typed data.

Functions

This section is empty.

Types

type EscapeCodes

type EscapeCodes struct {
	// Foreground colors
	Black, Red, Green, Yellow, Blue, Magenta, Cyan, White []byte

	// Reset all attributes
	Reset []byte
}

EscapeCodes contains escape sequences that can be written to the terminal in order to achieve different styles of text.

type History

type History interface {
	// Add will be called by [Terminal.ReadLine] to add
	// a new, most recent entry to the history.
	// It is allowed to drop any entry, including
	// the entry being added (e.g., if it's deemed an invalid entry),
	// the least-recent entry (e.g., to keep the history bounded),
	// or any other entry.
	Add(entry string)

	// Len returns the number of entries in the history.
	Len() int

	// At returns an entry from the history.
	// Index 0 is the most-recently added entry and
	// index Len()-1 is the least-recently added entry.
	// If index is < 0 or >= Len(), it panics.
	At(idx int) string
}

A History provides a (possibly bounded) queue of input lines read by Terminal.ReadLine.

type Terminal

type Terminal struct {
	// AutoCompleteCallback, if non-null, is called for each keypress with
	// the full input line and the current position of the cursor (in
	// bytes, as an index into |line|). If it returns ok=false, the key
	// press is processed normally. Otherwise it returns a replacement line
	// and the new cursor position.
	//
	// This will be disabled during ReadPassword.
	AutoCompleteCallback func(line string, pos int, key rune) (newLine string, newPos int, ok bool)

	// Escape contains a pointer to the escape codes for this terminal.
	// It's always a valid pointer, although the escape codes themselves
	// may be empty if the terminal doesn't support them.
	Escape *EscapeCodes

	// History records and retrieves lines of input read by [ReadLine] which
	// a user can retrieve and navigate using the up and down arrow keys.
	//
	// It is not safe to call ReadLine concurrently with any methods on History.
	//
	// [NewTerminal] sets this to a default implementation that records the
	// last 100 lines of input.
	History History
	// contains filtered or unexported fields
}

Terminal contains the state for running a VT100 terminal that is capable of reading lines of input.

func NewTerminal

func NewTerminal(c io.ReadWriter, prompt string) *Terminal

NewTerminal runs a VT100 terminal on the given ReadWriter. If the ReadWriter is a local terminal, that terminal must first have been put into raw mode. prompt is a string that is written at the start of each input line (i.e. "> ").

func (*Terminal) ReadLine

func (t *Terminal) ReadLine() (line string, err error)

ReadLine returns a line of input from the terminal.

func (*Terminal) ReadPassword

func (t *Terminal) ReadPassword(prompt string) (line string, err error)

ReadPassword temporarily changes the prompt and reads a password, without echo, from the terminal.

The AutoCompleteCallback is disabled during this call.

func (*Terminal) SetBracketedPasteMode

func (t *Terminal) SetBracketedPasteMode(on bool)

SetBracketedPasteMode requests that the terminal bracket paste operations with markers. Not all terminals support this but, if it is supported, then enabling this mode will stop any autocomplete callback from running due to pastes. Additionally, any lines that are completely pasted will be returned from ReadLine with the error set to ErrPasteIndicator.

func (*Terminal) SetPrompt

func (t *Terminal) SetPrompt(prompt string)

SetPrompt sets the prompt to be used when reading subsequent lines.

func (*Terminal) SetSize

func (t *Terminal) SetSize(width, height int) error

func (*Terminal) Write

func (t *Terminal) Write(buf []byte) (n int, err error)

Jump to

Keyboard shortcuts

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