Documentation
¶
Overview ¶
Package textinput provides a text input component for Bubble Tea applications.
Index ¶
- Constants
- Variables
- func Blink() tea.Msg
- func Paste() tea.Msg
- type CursorModedeprecated
- type EchoMode
- type KeyMap
- type Model
- func (m *Model) AvailableSuggestions() []string
- func (m *Model) Blur()
- func (m *Model) CurrentSuggestion() string
- func (m *Model) CurrentSuggestionIndex() int
- func (m *Model) CursorEnd()
- func (m Model) CursorMode() CursorModedeprecated
- func (m *Model) CursorStart()
- func (m *Model) Focus() tea.Cmd
- func (m Model) Focused() bool
- func (m *Model) MatchedSuggestions() []string
- func (m Model) Position() int
- func (m *Model) Reset()
- func (m *Model) SetCursor(pos int)
- func (m *Model) SetCursorMode(mode CursorMode) tea.Cmddeprecated
- func (m *Model) SetSuggestions(suggestions []string)
- func (m *Model) SetValue(s string)
- func (m Model) Update(msg tea.Msg) (Model, tea.Cmd)
- func (m Model) Value() string
- func (m Model) View() string
- type ValidateFunc
Examples ¶
Constants ¶
const ( // Deprecated: use [cursor.CursorBlink]. CursorBlink = CursorMode(cursor.CursorBlink) // Deprecated: use [cursor.CursorStatic]. CursorStatic = CursorMode(cursor.CursorStatic) // Deprecated: use [cursor.CursorHide]. CursorHide = CursorMode(cursor.CursorHide) )
Variables ¶
var DefaultKeyMap = KeyMap{ CharacterForward: key.NewBinding(key.WithKeys("right", "ctrl+f")), CharacterBackward: key.NewBinding(key.WithKeys("left", "ctrl+b")), WordForward: key.NewBinding(key.WithKeys("alt+right", "ctrl+right", "alt+f")), WordBackward: key.NewBinding(key.WithKeys("alt+left", "ctrl+left", "alt+b")), DeleteWordBackward: key.NewBinding(key.WithKeys("alt+backspace", "ctrl+w")), DeleteWordForward: key.NewBinding(key.WithKeys("alt+delete", "alt+d")), DeleteAfterCursor: key.NewBinding(key.WithKeys("ctrl+k")), DeleteBeforeCursor: key.NewBinding(key.WithKeys("ctrl+u")), DeleteCharacterBackward: key.NewBinding(key.WithKeys("backspace", "ctrl+h")), DeleteCharacterForward: key.NewBinding(key.WithKeys("delete", "ctrl+d")), LineStart: key.NewBinding(key.WithKeys("home", "ctrl+a")), LineEnd: key.NewBinding(key.WithKeys("end", "ctrl+e")), Paste: key.NewBinding(key.WithKeys("ctrl+v")), AcceptSuggestion: key.NewBinding(key.WithKeys("tab")), NextSuggestion: key.NewBinding(key.WithKeys("down", "ctrl+n")), PrevSuggestion: key.NewBinding(key.WithKeys("up", "ctrl+p")), }
DefaultKeyMap is the default set of key bindings for navigating and acting upon the textinput.
var NewModel = New
NewModel creates a new model with default settings.
Deprecated: Use New instead.
Functions ¶
Types ¶
type CursorMode
deprecated
added in
v0.8.0
type CursorMode int
Deprecated: use cursor.Mode.
func (CursorMode) String ¶ added in v0.8.0
func (c CursorMode) String() string
type EchoMode ¶ added in v0.7.1
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 ¶ added in v0.15.0
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.
type Model ¶
type Model struct {
Err error
// General settings.
Prompt string
Placeholder string
EchoMode EchoMode
EchoCharacter rune
Cursor cursor.Model
// Deprecated: use [cursor.BlinkSpeed] instead.
BlinkSpeed time.Duration
// Styles. These will be applied as inline styles.
//
// For an introduction to styling with Lip Gloss see:
// https://github.com/charmbracelet/lipgloss
PromptStyle lipgloss.Style
TextStyle lipgloss.Style
PlaceholderStyle lipgloss.Style
CompletionStyle lipgloss.Style
// Deprecated: use Cursor.Style instead.
CursorStyle lipgloss.Style
// CharLimit is the maximum amount of characters this input element will
// accept. If 0 or less, there's no limit.
CharLimit int
// Width is the maximum number of characters that can be displayed at once.
// It essentially treats the text field like a horizontally scrolling
// viewport. If 0 or less this setting is ignored.
Width 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 (*Model) AvailableSuggestions ¶ added in v0.17.0
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 ¶ added in v0.17.0
CurrentSuggestion returns the currently selected suggestion.
func (*Model) CurrentSuggestionIndex ¶ added in v0.21.0
CurrentSuggestionIndex returns the currently selected suggestion index.
func (*Model) CursorEnd ¶
func (m *Model) CursorEnd()
CursorEnd moves the cursor to the end of the input field.
func (Model) CursorMode
deprecated
added in
v0.8.0
func (m Model) CursorMode() CursorMode
Deprecated: use cursor.Mode.
func (*Model) CursorStart ¶
func (m *Model) CursorStart()
CursorStart moves the cursor to the start of the input field.
func (*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) MatchedSuggestions ¶ added in v0.21.0
MatchedSuggestions returns the list of matched suggestions.
func (*Model) Reset ¶
func (m *Model) Reset()
Reset sets the input to its default state with no input.
func (*Model) SetCursor ¶
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) SetCursorMode
deprecated
added in
v0.8.0
func (m *Model) SetCursorMode(mode CursorMode) tea.Cmd
Deprecated: use cursor.SetMode().
func (*Model) SetSuggestions ¶ added in v0.17.0
SetSuggestions sets the suggestions for the input.
type ValidateFunc ¶ added in v0.12.0
ValidateFunc is a function that returns an error if the input is invalid.
Example ¶
creditCardNumber := New()
creditCardNumber.Placeholder = "4505 **** **** 1234"
creditCardNumber.Focus()
creditCardNumber.CharLimit = 20
creditCardNumber.Width = 30
creditCardNumber.Prompt = ""
// This anonymous function is a valid function for ValidateFunc.
creditCardNumber.Validate = func(s string) error {
// Credit Card Number should a string less than 20 digits
// It should include 16 integers and 3 spaces
if len(s) > 16+3 {
return fmt.Errorf("CCN is too long")
}
if len(s) == 0 || len(s)%5 != 0 && (s[len(s)-1] < '0' || s[len(s)-1] > '9') {
return fmt.Errorf("CCN is invalid")
}
// The last digit should be a number unless it is a multiple of 4 in which
// case it should be a space
if len(s)%5 == 0 && s[len(s)-1] != ' ' {
return fmt.Errorf("CCN must separate groups with spaces")
}
// The remaining digits should be integers
c := strings.ReplaceAll(s, " ", "")
_, err := strconv.ParseInt(c, 10, 64)
return err
}