textinput

package
v0.0.0-...-48f2f3c Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package textinput provides a text input component for Bubble Tea applications.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Blink() tea.Msg

Blink is a command used to initialize cursor blinking.

func Paste

func Paste() tea.Msg

Paste is a command for pasting from the clipboard into the text input.

Types

type EchoMode

type EchoMode int

EchoMode sets the input behavior of the text input field.

const (
	// EchoNormal displays text as is. This is the default behavior.
	EchoNormal EchoMode = iota

	// EchoPassword displays the EchoCharacter mask instead of actual
	// characters. This is commonly used for password fields.
	EchoPassword

	// EchoNone displays nothing as characters are entered. This is commonly
	// seen for password fields on the command line.
	EchoNone
)

type KeyMap

type KeyMap struct {
	CharacterForward        key.Binding
	CharacterBackward       key.Binding
	WordForward             key.Binding
	WordBackward            key.Binding
	DeleteWordBackward      key.Binding
	DeleteWordForward       key.Binding
	DeleteAfterCursor       key.Binding
	DeleteBeforeCursor      key.Binding
	DeleteCharacterBackward key.Binding
	DeleteCharacterForward  key.Binding
	LineStart               key.Binding
	LineEnd                 key.Binding
	Paste                   key.Binding
	AcceptSuggestion        key.Binding
	NextSuggestion          key.Binding
	PrevSuggestion          key.Binding
}

KeyMap is the key bindings for different actions within the textinput.

func DefaultKeyMap

func DefaultKeyMap() KeyMap

DefaultKeyMap is the default set of key bindings for navigating and acting upon the textinput.

type Model

type Model struct {
	Err error

	// General settings.
	Prompt        string
	Placeholder   string
	EchoMode      EchoMode
	EchoCharacter rune

	// Styling. FocusedStyle and BlurredStyle are used to style the textarea in
	// focused and blurred states.
	Styles textinput.Styles

	// CharLimit is the maximum amount of characters this input element will
	// accept. If 0 or less, there's no limit.
	CharLimit int

	// KeyMap encodes the keybindings recognized by the widget.
	KeyMap KeyMap

	// Validate is a function that checks whether or not the text within the
	// input is valid. If it is not valid, the `Err` field will be set to the
	// error returned by the function. If the function is not defined, all
	// input is considered valid.
	Validate ValidateFunc

	// Should the input suggest to complete
	ShowSuggestions bool
	// contains filtered or unexported fields
}

Model is the Bubble Tea model for this text input element.

func New

func New() Model

New creates a new model with default settings.

func (*Model) AvailableSuggestions

func (m *Model) AvailableSuggestions() []string

AvailableSuggestions returns the list of available suggestions.

func (*Model) Blur

func (m *Model) Blur()

Blur removes the focus state on the model. When the model is blurred it can not receive keyboard input and the cursor will be hidden.

func (*Model) CurrentSuggestion

func (m *Model) CurrentSuggestion() string

CurrentSuggestion returns the currently selected suggestion.

func (*Model) CurrentSuggestionIndex

func (m *Model) CurrentSuggestionIndex() int

CurrentSuggestionIndex returns the currently selected suggestion index.

func (Model) Cursor

func (m Model) Cursor() *tea.Cursor

Cursor returns a tea.Cursor for rendering a real cursor in a Bubble Tea program. This requires that [Model.VirtualCursor] is set to false.

Note that you will almost certainly also need to adjust the offset cursor position per the textarea's per the textarea's position in the terminal.

Example:

// In your top-level View function:
f := tea.NewFrame(m.textarea.View())
f.Cursor = m.textarea.Cursor()
f.Cursor.Position.X += offsetX
f.Cursor.Position.Y += offsetY

func (*Model) CursorEnd

func (m *Model) CursorEnd()

CursorEnd moves the cursor to the end of the input field.

func (*Model) CursorStart

func (m *Model) CursorStart()

CursorStart moves the cursor to the start of the input field.

func (*Model) Focus

func (m *Model) Focus()

Focus sets the focus state on the model. When the model is in focus it can receive keyboard input and the cursor will be shown.

func (Model) Focused

func (m Model) Focused() bool

Focused returns the focus state on the model.

func (*Model) MatchedSuggestions

func (m *Model) MatchedSuggestions() []string

MatchedSuggestions returns the list of matched suggestions.

func (Model) Position

func (m Model) Position() int

Position returns the cursor position.

func (*Model) Reset

func (m *Model) Reset()

Reset sets the input to its default state with no input.

func (*Model) SetCursor

func (m *Model) SetCursor(pos int)

SetCursor moves the cursor to the given position. If the position is out of bounds the cursor will be moved to the start or end accordingly.

func (*Model) SetSuggestions

func (m *Model) SetSuggestions(suggestions []string)

SetSuggestions sets the suggestions for the input.

func (*Model) SetValue

func (m *Model) SetValue(s string)

SetValue sets the value of the text input.

func (*Model) SetWidth

func (m *Model) SetWidth(w int)

SetWidth sets the width of the text input.

func (Model) Update

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

Update is the Bubble Tea update loop.

func (Model) Value

func (m Model) Value() string

Value returns the value of the text input.

func (Model) View

func (m Model) View() string

View renders the textinput in its current state.

func (Model) Width

func (m Model) Width() int

Width returns the width of the text input.

type ValidateFunc

type ValidateFunc func(string) error

ValidateFunc is a function that returns an error if the input is invalid.

Jump to

Keyboard shortcuts

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