Documentation
¶
Overview ¶
Package textarea provides a multi-line text input component for Bubble Tea applications.
Index ¶
- Variables
- func Blink() tea.Msg
- func DefaultStyles() (Style, Style)
- func Paste() tea.Msg
- type KeyMap
- type LineInfo
- type MemoCache
- type Model
- func (m *Model) Blur()
- func (m Model) CalculateContentHeight() int
- func (m *Model) CursorDown()
- func (m *Model) CursorEnd()
- func (m Model) CursorIndex() int
- func (m *Model) CursorStart()
- func (m *Model) CursorUp()
- func (m *Model) Focus() tea.Cmd
- func (m Model) Focused() bool
- func (m Model) GetDynamicHeight() bool
- func (m Model) Height() int
- func (m *Model) InsertRune(r rune)
- func (m *Model) InsertString(s string)
- func (m *Model) Length() int
- func (m Model) Line() int
- func (m *Model) LineCount() int
- func (m Model) LineInfo() LineInfo
- func (m *Model) Reset()
- func (m *Model) SetCursor(col int)
- func (m *Model) SetHeight(h int)
- func (m *Model) SetHighlighter(h func(string, lipgloss.Style) string)
- func (m *Model) SetMaxVisualHeight(maxVisualHeight int)
- func (m *Model) SetPromptFunc(promptWidth int, fn func(lineIdx int) string)
- func (m *Model) SetValue(s string)
- func (m *Model) SetWidth(w int)
- func (m Model) Update(msg tea.Msg) (Model, tea.Cmd)
- func (m Model) Value() string
- func (m Model) View() string
- func (m Model) Width() int
- type Style
Constants ¶
This section is empty.
Variables ¶
var DefaultKeyMap = KeyMap{ CharacterForward: key.NewBinding(key.WithKeys("right", "ctrl+f"), key.WithHelp("right", "character forward")), CharacterBackward: key.NewBinding(key.WithKeys("left", "ctrl+b"), key.WithHelp("left", "character backward")), WordForward: key.NewBinding(key.WithKeys("alt+right", "alt+f"), key.WithHelp("alt+right", "word forward")), WordBackward: key.NewBinding(key.WithKeys("alt+left", "alt+b"), key.WithHelp("alt+left", "word backward")), LineNext: key.NewBinding(key.WithKeys("down", "ctrl+n"), key.WithHelp("down", "next line")), LinePrevious: key.NewBinding(key.WithKeys("up", "ctrl+p"), key.WithHelp("up", "previous line")), DeleteWordBackward: key.NewBinding(key.WithKeys("alt+backspace", "ctrl+w"), key.WithHelp("alt+backspace", "delete word backward")), DeleteWordForward: key.NewBinding(key.WithKeys("alt+delete", "alt+d"), key.WithHelp("alt+delete", "delete word forward")), DeleteAfterCursor: key.NewBinding(key.WithKeys("ctrl+k"), key.WithHelp("ctrl+k", "delete after cursor")), DeleteBeforeCursor: key.NewBinding(key.WithKeys("ctrl+u"), key.WithHelp("ctrl+u", "delete before cursor")), InsertNewline: key.NewBinding(key.WithKeys("enter", "ctrl+m"), key.WithHelp("enter", "insert newline")), DeleteCharacterBackward: key.NewBinding(key.WithKeys("backspace", "ctrl+h"), key.WithHelp("backspace", "delete character backward")), DeleteCharacterForward: key.NewBinding(key.WithKeys("delete", "ctrl+d"), key.WithHelp("delete", "delete character forward")), LineStart: key.NewBinding(key.WithKeys("home", "ctrl+a"), key.WithHelp("home", "line start")), LineEnd: key.NewBinding(key.WithKeys("end", "ctrl+e"), key.WithHelp("end", "line end")), Paste: key.NewBinding(key.WithKeys("ctrl+v"), key.WithHelp("ctrl+v", "paste")), InputBegin: key.NewBinding(key.WithKeys("alt+<", "ctrl+home"), key.WithHelp("alt+<", "input begin")), InputEnd: key.NewBinding(key.WithKeys("alt+>", "ctrl+end"), key.WithHelp("alt+>", "input end")), CapitalizeWordForward: key.NewBinding(key.WithKeys("alt+c"), key.WithHelp("alt+c", "capitalize word forward")), LowercaseWordForward: key.NewBinding(key.WithKeys("alt+l"), key.WithHelp("alt+l", "lowercase word forward")), UppercaseWordForward: key.NewBinding(key.WithKeys("alt+u"), key.WithHelp("alt+u", "uppercase word forward")), TransposeCharacterBackward: key.NewBinding(key.WithKeys("ctrl+t"), key.WithHelp("ctrl+t", "transpose character backward")), }
DefaultKeyMap is the default set of key bindings for navigating and acting upon the textarea.
Functions ¶
func DefaultStyles ¶
DefaultStyles returns the default styles for focused and blurred states for the textarea.
Types ¶
type KeyMap ¶
type KeyMap struct {
CharacterBackward key.Binding
CharacterForward key.Binding
DeleteAfterCursor key.Binding
DeleteBeforeCursor key.Binding
DeleteCharacterBackward key.Binding
DeleteCharacterForward key.Binding
DeleteWordBackward key.Binding
DeleteWordForward key.Binding
InsertNewline key.Binding
LineEnd key.Binding
LineNext key.Binding
LinePrevious key.Binding
LineStart key.Binding
Paste key.Binding
WordBackward key.Binding
WordForward key.Binding
InputBegin key.Binding
InputEnd key.Binding
UppercaseWordForward key.Binding
LowercaseWordForward key.Binding
CapitalizeWordForward key.Binding
TransposeCharacterBackward key.Binding
}
KeyMap is the key bindings for different actions within the textarea.
type LineInfo ¶
type LineInfo struct {
// Width is the number of columns in the line.
Width int
// CharWidth is the number of characters in the line to account for
// double-width runes.
CharWidth int
// Height is the number of rows in the line.
Height int
// StartColumn is the index of the first column of the line.
StartColumn int
// ColumnOffset is the number of columns that the cursor is offset from the
// start of the line.
ColumnOffset int
// RowOffset is the number of rows that the cursor is offset from the start
// of the line.
RowOffset int
// CharOffset is the number of characters that the cursor is offset
// from the start of the line. This will generally be equivalent to
// ColumnOffset, but will be different there are double-width runes before
// the cursor.
CharOffset int
}
LineInfo is a helper for keeping track of line information regarding soft-wrapped lines.
type MemoCache ¶
type MemoCache[T lineContent, V any] struct { // contains filtered or unexported fields }
func NewMemoCache ¶
type Model ¶
type Model struct {
Err error
// DynamicHeight, if enabled, causes the textarea height to automatically
// adjust based on the content, up to MaxVisualHeight.
// This field is automatically set based on MaxVisualHeight (> 0 enables it).
DynamicHeight bool
// Prompt is printed at the beginning of each line.
//
// When changing the value of Prompt after the model has been
// initialized, ensure that SetWidth() gets called afterwards.
//
// See also SetPromptFunc().
Prompt string
// Placeholder is the text displayed when the user
// hasn't entered anything yet.
Placeholder string
// ShowLineNumbers, if enabled, causes line numbers to be printed
// after the prompt.
ShowLineNumbers bool
// EndOfBufferCharacter is displayed at the end of the input.
EndOfBufferCharacter rune
// KeyMap encodes the keybindings recognized by the widget.
KeyMap KeyMap
// Styling. FocusedStyle and BlurredStyle are used to style the textarea in
// focused and blurred states.
FocusedStyle Style
BlurredStyle Style
// Cursor is the text area cursor.
Cursor cursor.Model
// CharLimit is the maximum number of characters this input element will
// accept. If 0 or less, there's no limit.
CharLimit int
// MaxHeight is the maximum height of the text area in rows. If 0 or less,
// there's no limit.
MaxHeight int
// MaxVisualHeight is the maximum visual (rendered) height of the text area in rows.
// When content exceeds this height, the textarea switches to viewport scrolling.
// If 0, dynamic height is disabled and the textarea uses a fixed height.
// If > 0, dynamic height is enabled and grows up to this limit.
MaxVisualHeight int
// MaxWidth is the maximum width of the text area in columns. If 0 or less,
// there's no limit.
MaxWidth int
// Highlighter applies syntax highlighting to the text.
// It takes the text content and the base style (context) to allow merging.
Highlighter func(text string, baseStyle lipgloss.Style) string
// contains filtered or unexported fields
}
Model is the Bubble Tea model for this text area element.
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) CalculateContentHeight ¶
CalculateContentHeight calculates the total height needed to display all content taking into account both hard line breaks (newlines) and soft line breaks (wrapping).
func (*Model) CursorDown ¶
func (m *Model) CursorDown()
CursorDown moves the cursor down by one line. Returns whether or not the cursor blink should be reset.
func (*Model) CursorEnd ¶
func (m *Model) CursorEnd()
CursorEnd moves the cursor to the end of the input field.
func (Model) CursorIndex ¶
CursorIndex returns the cursor position as a byte index in the string returned by Value().
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 hidden.
func (Model) GetDynamicHeight ¶
GetDynamicHeight returns whether dynamic height is currently enabled. Dynamic height is enabled when MaxVisualHeight > 0.
func (*Model) InsertRune ¶
InsertRune inserts a rune at the cursor position.
func (*Model) InsertString ¶
InsertString inserts a string at the cursor position.
func (*Model) LineCount ¶
LineCount returns the number of lines that are currently in the text input.
func (Model) LineInfo ¶
LineInfo returns the number of characters from the start of the (soft-wrapped) line and the (soft-wrapped) line width.
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) SetHighlighter ¶
SetHighlighter sets the function used to highlight text.
func (*Model) SetMaxVisualHeight ¶
SetMaxVisualHeight sets the maximum visual height of the textarea. If maxVisualHeight > 0, dynamic height is enabled and the textarea will grow up to this limit before switching to viewport scrolling. If maxVisualHeight = 0, dynamic height is disabled and textarea uses fixed height.
func (*Model) SetPromptFunc ¶
SetPromptFunc supersedes the Prompt field and sets a dynamic prompt instead. If the function returns a prompt that is shorter than the specified promptWidth, it will be padded to the left. If it returns a prompt that is longer, display artifacts may occur; the caller is responsible for computing an adequate promptWidth.
func (*Model) SetWidth ¶
SetWidth sets the width of the textarea to fit exactly within the given width. This means that the textarea will account for the width of the prompt and whether or not line numbers are being shown.
Ensure that SetWidth is called after setting the Prompt and ShowLineNumbers, It is important that the width of the textarea be exactly the given width and no more.
type Style ¶
type Style struct {
Base lipgloss.Style
CursorLine lipgloss.Style
CursorLineNumber lipgloss.Style
EndOfBuffer lipgloss.Style
LineNumber lipgloss.Style
Placeholder lipgloss.Style
Prompt lipgloss.Style
Text lipgloss.Style
}
Style that will be applied to the text area.
Style can be applied to focused and unfocused states to change the styles depending on the focus state.
For an introduction to styling with Lip Gloss see: https://github.com/charmbracelet/lipgloss