input

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: MIT Imports: 3 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 input holds the terminal input primitives: the raw key sequences the TUI recognizes, a raw-bytes-to-semantic-event key map, and the grapheme-aware single-line/multi-line text editor used by the prompt and by form fields.

Go port of Ruby's lib/term_form/text.rb (Text + TextEditor), the key-byte table from lib/term_form/event.rb, and lib/tui/text_input.rb (which is only a compatibility alias in Ruby and needs no separate Go type).

This package deliberately does NOT decode terminal input: no raw-mode CSI parser, no escape-versus-alt timing heuristic, no bracketed-paste framing, no UTF-8 continuation buffer. Bubble Tea owns all of that. What is left — and what Bubble Tea does not provide — is the editing model: grapheme-indexed cursor motion, word kills, and paste sanitizing. KeyBytes names the sequences the editor recognizes, so a framework key message can be mapped onto them.

Index

Constants

View Source
const (
	CtrlA = "\x01"
	CtrlB = "\x02"
	CtrlD = "\x04"
	CtrlE = "\x05"
	CtrlF = "\x06"
	CtrlH = "\x08"
	CtrlK = "\x0b"
	CtrlU = "\x15"
	CtrlW = "\x17"
)

Control bytes the editor understands.

Variables

View Source
var KeyBytes = map[string]string{
	"tab": "\t", "shift_tab": "\x1b[Z", "return": "\r", "escape": "\x1b",
	"up": "\x1b[A", "down": "\x1b[B", "left": "\x1b[D", "right": "\x1b[C",
	"home": "\x1b[H", "end": "\x1b[F", "delete": "\x1b[3~", "backspace": "\x7f",
}

KeyBytes maps a named key to the byte sequence a terminal sends for it.

Functions

func CellSlice

func CellSlice(value string, start, width int) string

CellSlice slices a plain string by terminal cells. Partial wide graphemes become spaces, preserving the columns of content on either side.

func CellWidth

func CellWidth(value string) int

CellWidth is the display width of a plain string in terminal cells.

func Graphemes

func Graphemes(value string) []string

Graphemes splits a value into grapheme clusters — the units the cursor moves over, never bytes or codepoints.

func Normalize

func Normalize(value string) string

Normalize returns valid UTF-8, dropping invalid bytes. Field text is user input rather than transcript output, so an undecodable byte is discarded rather than replaced with a visible marker.

func Printable

func Printable(key string) bool

Printable reports whether every grapheme in key is printable, the test that separates typed text from control sequences.

Types

type Editor

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

Editor is mutable editing state. Cursor offsets count grapheme clusters.

func New

func New(text string, opts Options) *Editor

New builds an editor holding text, with the cursor at the end.

func (*Editor) Clear

func (e *Editor) Clear()

Clear empties the buffer.

func (*Editor) Cursor

func (e *Editor) Cursor() int

func (*Editor) Empty

func (e *Editor) Empty() bool

func (*Editor) HandleKey

func (e *Editor) HandleKey(key string) Result

HandleKey applies one raw key sequence.

func (*Editor) Insert

func (e *Editor) Insert(raw string) Result

Insert types text at the cursor. It reports None when the sanitized input is empty, so a host can distinguish "nothing happened" from an edit.

func (*Editor) Length

func (e *Editor) Length() int

func (*Editor) Replace

func (e *Editor) Replace(raw string)

Replace swaps the whole buffer and parks the cursor at the end.

func (*Editor) SetCursor

func (e *Editor) SetCursor(position int)

SetCursor moves the cursor, clamped into the text.

func (*Editor) String

func (e *Editor) String() string

func (*Editor) Text

func (e *Editor) Text() string

type Options

type Options struct {
	Multiline bool
	// NoKillToEnd reserves ctrl-k for the host instead of "kill to end of line".
	NoKillToEnd bool
}

Options configures an editor.

type Result

type Result string

Result reports what a key did. An unhandled key returns None so the caller can try another binding.

const (
	None    Result = ""
	Handled Result = "handled"
	Changed Result = "changed"
)

Jump to

Keyboard shortcuts

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