textinput

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 13 Imported by: ? 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 textinput is a single-line text input widget built on top of the root tui package — the first package in this module that depends on tui rather than the other way around, forming a widget layer above the core (term/ansi/input) -> tui layering described in the README.

Unlike ansi.Style and layout.Box, Model here isn't an immutable value-builder: it's a small live component with both configuration (Placeholder, Width, ...) and state (the typed value, cursor position), so configuration is plain exported fields you set once after New, mirroring how a parent tui.Model holds its own fields — not the chainable pattern used by the purely-descriptive Style/Box types.

Example

A textinput.Model is a value: Update returns the edited copy.

package main

import (
	"fmt"

	"github.com/ows4444/tui"
	"github.com/ows4444/tui/textinput"
)

func main() {
	m := textinput.New()
	m.Focus()
	for _, r := range "héllo" {
		m, _ = m.Update(tui.Key{Type: tui.KeyRunes, Text: string([]rune{r})})
	}
	m, _ = m.Update(tui.Key{Type: tui.KeyBackspace})
	fmt.Println(m.Value(), m.Cursor())
}
Output:
héll 4

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyMap

type KeyMap struct {
	Left           keymap.Binding // move the cursor one character left
	Right          keymap.Binding // move the cursor one character right
	WordLeft       keymap.Binding // move the cursor one word left
	WordRight      keymap.Binding // move the cursor one word right
	Home           keymap.Binding // move the cursor to the start of the value
	End            keymap.Binding // move the cursor to the end of the value
	DeleteBack     keymap.Binding // delete the character before the cursor
	DeleteForward  keymap.Binding // delete the character at the cursor
	DeleteToStart  keymap.Binding // delete from the cursor to the start
	DeleteToEnd    keymap.Binding // delete from the cursor to the end
	DeleteWordBack keymap.Binding // delete the word before the cursor
	Undo           keymap.Binding // undo the last edit
	Redo           keymap.Binding // redo an undone edit
	Copy           keymap.Binding // copy the selection with OSC 52
	Cut            keymap.Binding // copy the selection with OSC 52 and delete it
	Paste          keymap.Binding // insert the text of the last Copy or Cut
}

KeyMap names the keys of each editing action of a Model.

func DefaultKeyMap

func DefaultKeyMap() KeyMap

DefaultKeyMap returns the keys a Model used before KeyMap existed.

type Model

type Model struct {
	Placeholder string
	Prompt      string
	Width       int // 0 = unlimited width, no horizontal scrolling
	CharLimit   int // 0 = unlimited length

	TextStyle        ansi.Style
	PlaceholderStyle ansi.Style
	CursorStyle      ansi.Style
	// SelectionStyle styles selected text. New sets a blue background; the
	// zero value falls back to reverse video.
	SelectionStyle ansi.Style

	// UndoLimit is the number of undo steps kept (see KeyMap.Undo): 0 means
	// 100 and a negative value turns undo off. A run of typed characters is
	// one step. SetValue and Reset forget the history.
	UndoLimit int

	// DisableCopy makes Copy and Cut do nothing, for fields whose value must
	// not reach the clipboard (passwordinput sets it).
	DisableCopy bool

	// Mouse, when true, makes Update handle tui.MouseEvent: a left click inside
	// Bounds moves the cursor to the clicked cell and a drag selects. Off (the
	// default) ignores the mouse.
	Mouse bool
	// Bounds is the screen rectangle where the app draws the field, prompt
	// included; clicks outside it do not start a selection. The app sets it
	// each frame it moves.
	Bounds hittest.Rect

	// Motion is the reduced-motion preference. The zero value is
	// motion.Normal, the behaviour before this field existed. With
	// motion.Reduced the cursor does not blink: it stays solid and Focus
	// schedules no tick.
	Motion motion.Preference

	// KeyMap holds the keys for the editing actions. New fills it with
	// DefaultKeyMap; a Model built as a struct literal with a zero KeyMap
	// behaves as if it held DefaultKeyMap. Typed characters are not actions:
	// they are always inserted.
	KeyMap KeyMap
	// contains filtered or unexported fields
}

Model is a single-line text input. Embed it as a field in a parent tui.Model, forward relevant Msgs to its Update, and render its View as part of the parent's own View.

func New

func New() Model

New returns a Model with sane default styling: a faint placeholder and a reverse-video block cursor (Program keeps the real terminal cursor hidden throughout, so the cursor is simulated by styling, not by positioning the terminal's own cursor).

func NewPath

func NewPath() Model

NewPath returns a Model configured for file-path entry: a "/path/to/file" placeholder, with New's default styling. Everything else is a plain Model.

func NewSearch

func NewSearch() Model

NewSearch returns a Model configured for search: a "/ " prompt and a "Search..." placeholder, with New's default styling. Everything else (Update, View, scrolling) is a plain Model.

func (Model) Bindings

func (m Model) Bindings() []keymap.Binding

Bindings returns the editing actions the Model currently honours, with descriptions, for help text. Typed characters are not listed.

func (*Model) Blur

func (m *Model) Blur()

Blur marks the input unfocused. Any blink tick already in flight from a prior Focus is a no-op once it arrives (see Update) and doesn't reschedule itself, so the blink loop stops on its own without needing to cancel the Cmd.

func (Model) Cursor

func (m Model) Cursor() int

Cursor returns the cursor's current position within Value as a grapheme cluster index (one cluster is one user-perceived character; for text without combining sequences this equals the rune index).

func (Model) CursorCell

func (m Model) CursorCell() (x, y int, ok bool)

CursorCell returns the display cell the cursor occupies in View: x columns from the left of the prompt, y always 0. ok is false while the field is not focused, so a parent can forward the result as its tui.CursorPlacer answer (after adding its own offset) and the terminal's real cursor follows the field, which an IME or a screen magnifier needs.

func (*Model) CursorEnd

func (m *Model) CursorEnd()

CursorEnd moves the cursor to the end of Value.

func (*Model) CursorStart

func (m *Model) CursorStart()

CursorStart moves the cursor to the beginning of Value.

func (*Model) Focus

func (m *Model) Focus() tui.Cmd

Focus marks the input focused and starts its cursor blinking. The returned Cmd must be run (returned onward, or dispatched) for the blink to actually animate.

func (Model) Focused

func (m Model) Focused() bool

Focused reports whether the input is currently focused.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

LayoutNode adapts the input to a layout.Node. Measure reports its natural size: the prompt, the value (or placeholder) and one cell for the cursor, on one row. Render fits the allotted Size by using the width left after the prompt as the visible text window (one cell is kept for the cursor), so a long value scrolls around the cursor as View does when Width is set. The Model is not changed.

Widgets that embed Model and hold a secret must override this too (see passwordinput and maskedinput), or the promoted method would draw the real value.

func (Model) Linearize

func (m Model) Linearize() string

Linearize renders the field as one plain-text line for accessible output (see tui.Linearizer): its label, "text field", ", focused" when it has focus, and its value in words, e.g. `Name, text field, focused, value: Ada`, or "empty" (with the placeholder) when nothing is typed. No cursor, prompt punctuation or styling.

Widgets that embed Model and hold a secret must override this (see passwordinput and maskedinput), or the promoted method would speak the real value.

func (*Model) Reset

func (m *Model) Reset()

Reset clears the value and moves the cursor to the start.

func (*Model) SetCursor

func (m *Model) SetCursor(pos int)

SetCursor moves the cursor to pos, clamped within Value.

func (Model) SetTheme

func (m Model) SetTheme(t theme.Theme) Model

SetTheme returns m with its text, placeholder and cursor styles taken from t: the text in t.Text, the placeholder faint in t.Muted, and the cursor in reverse video, which needs no colour to stay visible. It makes Model a tui.ThemeSetter, so a root model can forward the Program's theme (see tui.WithTheme). The style fields stay exported; set them after SetTheme to override.

func (*Model) SetValue

func (m *Model) SetValue(s string)

SetValue replaces the current value and moves the cursor to its end, truncating to CharLimit if set.

func (Model) Tokens

func (m Model) Tokens() theme.Tokens

Tokens returns the instance's colour overrides as set by WithTokens. Roles not overridden are nil: the widget's colours come from SetTheme (which honours theme.WithTokens(theme.ComponentTextInput, ...)) or its style fields.

func (Model) Update

func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)

Update handles a key press or a blink tick. It returns a concrete Model (not tui.Model) so callers can chain without a type assertion, the same way ansi.Style and layout.Box avoid forcing one.

func (Model) Value

func (m Model) Value() string

Value returns the current text.

func (Model) View

func (m Model) View() string

View renders the prompt, then the value with the cursor position highlighted via CursorStyle, or — when the value is empty — a cursor cell followed by the styled placeholder. When Width is set and the value is longer than it, the visible window scrolls to keep the cursor in view.

func (Model) WithTokens

func (m Model) WithTokens(tok theme.Tokens) Model

WithTokens returns m with tok as its per-instance colour override: Text recolours the text and Muted the placeholder. Nil fields leave the current styles alone, and a later SetTheme keeps the override applied.

Jump to

Keyboard shortcuts

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