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

lineedit

package
v0.21.0 Latest Latest
Warning

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

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

Documentation

Overview

Package lineedit is wctr's interactive line editor, as a pure model: key events in, the line, the cursor, candidate listings and submitted lines out. It has no terminal, DOM or widget type in it, so every front end — the playground's xterm.js page, a go-widgets terminal widget — drives the same implementation.

It provides: editing (insert, Backspace, Delete, Left/Right, Home/End, Ctrl-A/E/B/F, Ctrl-U/K/W, Ctrl-L), history (Up/Down, Ctrl-P/N, bounded, exportable for persistence), reverse incremental search (Ctrl-R), and Tab completion through cobra's __complete (see Completer): a unique match is inserted with a trailing space (unless the directive says NoSpace), several are completed to their longest common prefix, a second Tab lists them in columns, or as "value -- description" lines when cobra gives descriptions, asking "Display all N possibilities? (y or n)" past a threshold. Completion understands the line's quoting.

ParseKeys turns terminal input (what xterm.js's onData delivers) into keys; Renderer turns successive States into ANSI output for a terminal. A widget can ignore both and use Key and State directly.

Index

Constants

View Source
const MaxLine = 4096

MaxLine bounds the line the splitter accepts.

Variables

View Source
var ErrUnterminated = errors.New("unterminated quote or escape")

ErrUnterminated is returned for an open quote or a trailing backslash.

Functions

func Format

func Format(cs []cli.Candidate, cols int) []string

Format lays candidates out for a terminal cols wide: one aligned "value -- description" line each when any has a description (as zsh and fish show them), columns filled top to bottom otherwise (as bash).

func Quote

func Quote(w string, quote byte, closed bool) string

Quote renders a word for insertion: inside the given open quote (0 for none), so that Split reads it back as w; closed adds the closing quote.

func Split

func Split(line string) ([]string, error)

Split returns the words of line.

Types

type Completer

type Completer func(ctx context.Context, words []string, current string) (cands []cli.Candidate, noSpace bool)

Completer lists the candidates for the word being typed: words are the complete words before it, current is its (unquoted) prefix. noSpace asks that a unique match get no trailing space.

func WctrCompleter

func WctrCompleter(o cli.Options, builtins []cli.Candidate) Completer

WctrCompleter completes a line whose first word is "wctr" through the command tree o describes (cobra's __complete, in-process), and the first word itself from builtins (which should include "wctr").

type Editor

type Editor struct {
	// contains filtered or unexported fields
}

Editor is the line editor.

func New

func New(o Options) *Editor

New returns an editor.

func (*Editor) History

func (e *Editor) History() []string

History returns the history, oldest first (to persist it).

func (*Editor) Key

func (e *Editor) Key(ctx context.Context, k Key) Event

Key applies one key; ctx bounds a completion.

func (*Editor) Reset

func (e *Editor) Reset()

Reset clears the line (a new prompt).

func (*Editor) SetCols

func (e *Editor) SetCols(n int)

SetCols sets the display width for listings.

func (*Editor) SetHistory

func (e *Editor) SetHistory(h []string)

SetHistory replaces the history (a restored one), bounded. A restored history is untrusted (browser storage, a file): an entry that a user could not have typed — invalid UTF-8 or a non-printable rune, which the renderer would otherwise write to the terminal as is — is dropped.

func (*Editor) State

func (e *Editor) State() State

State returns what to display.

type Event

type Event struct {
	// Submit is set when Enter ended a line; Line is that line.
	Submit bool
	Line   string
	// Print are lines to show above the prompt (a candidate listing, a
	// question); the line is then shown again below them.
	Print []string
	// Interrupt is Ctrl-C on an empty or abandoned line ("^C").
	Interrupt bool
	// Clear is Ctrl-L: clear the screen, keep the line.
	Clear bool
	// EOF is Ctrl-D on an empty line.
	EOF bool
	// Bell is a key that did nothing (no completion, end of history).
	Bell bool
}

Event is what a key did besides editing.

type Key

type Key struct {
	Code KeyCode
	Rune rune
}

Key is one key press.

func ParseKeys

func ParseKeys(data string) []Key

ParseKeys turns terminal input (a typed key, an escape sequence, or a paste) into keys. Unknown sequences become KeyUnknown, never text.

func R

func R(r rune) Key

R is a character key.

type KeyCode

type KeyCode int

KeyCode names a non-character key.

const (
	KeyRune KeyCode = iota // a printable character, in Key.Rune
	KeyEnter
	KeyBackspace
	KeyDelete
	KeyLeft
	KeyRight
	KeyHome
	KeyEnd
	KeyUp
	KeyDown
	KeyTab
	KeyEscape
	KeyCtrlA
	KeyCtrlB
	KeyCtrlC
	KeyCtrlD
	KeyCtrlE
	KeyCtrlF
	KeyCtrlG
	KeyCtrlK
	KeyCtrlL
	KeyCtrlN
	KeyCtrlP
	KeyCtrlR
	KeyCtrlU
	KeyCtrlW
	KeyUnknown
)

type Options

type Options struct {
	// Prompt is shown before the line (default "$ ").
	Prompt string
	// Complete answers Tab; nil disables completion.
	Complete Completer
	// HistorySize bounds the history (default 500).
	HistorySize int
	// Cols is the display width used for listings (default 80); SetCols
	// changes it.
	Cols int
}

Options configure an Editor.

type Partial

type Partial struct {
	// Words are the complete words before the one being typed.
	Words []string
	// Current is the word being typed, unquoted ("" for a new word).
	Current string
	// Start is the byte offset in the line where the current word begins.
	Start int
	// Quote is the quote the current word is still inside (' or "), or 0.
	Quote byte
}

Partial is the line before the cursor, split for completion.

func SplitPartial

func SplitPartial(prefix string) Partial

SplitPartial splits a line prefix the way Split does, except that the last word may be unterminated (an open quote, a trailing backslash).

type Renderer

type Renderer struct {
	// Cols is the terminal width.
	Cols int
	// PromptStyle wraps the prompt (default bold cyan); "" for none.
	PromptStyle string
	// contains filtered or unexported fields
}

Renderer turns States into ANSI output for a terminal that does not wrap lines on its own behalf (xterm.js does: the renderer accounts for it). It assumes one column per rune.

func (*Renderer) Forget

func (r *Renderer) Forget()

Forget drops what was rendered (the screen was cleared).

func (*Renderer) Leave

func (r *Renderer) Leave(s State) string

Leave moves the cursor below the input (after Enter, Ctrl-C) and forgets it: the next Render starts a fresh prompt.

func (*Renderer) Print

func (r *Renderer) Print(lines []string, s State) string

Print returns the output that shows lines above the input, then the input again below them.

func (*Renderer) Render

func (r *Renderer) Render(s State) string

Render returns the output that replaces what was last rendered with s.

type State

type State struct {
	Prompt string
	Line   string
	// Cursor is the cursor's position in Line, in runes.
	Cursor int
}

State is what to display.

Jump to

Keyboard shortcuts

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