textarea

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2024 License: MIT Imports: 16 Imported by: 286

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 Blink() tea.Msg

Blink returns the blink command for the cursor.

func DefaultStyles

func DefaultStyles() (Style, Style)

DefaultStyles returns the default styles for focused and blurred states for the textarea.

func Paste

func Paste() tea.Msg

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

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 Model

type Model struct {
	Err error

	// 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

	// MaxWidth is the maximum width of the text area in columns. If 0 or less,
	// there's no limit.
	MaxWidth int
	// contains filtered or unexported fields
}

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

func New

func New() Model

New creates a new model with default settings.

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) 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) CursorStart

func (m *Model) CursorStart()

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

func (*Model) CursorUp

func (m *Model) CursorUp()

CursorUp moves the cursor up by one line.

func (*Model) Focus

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

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) Focused

func (m Model) Focused() bool

Focused returns the focus state on the model.

func (Model) Height

func (m Model) Height() int

Height returns the current height of the textarea.

func (*Model) InsertRune

func (m *Model) InsertRune(r rune)

InsertRune inserts a rune at the cursor position.

func (*Model) InsertString

func (m *Model) InsertString(s string)

InsertString inserts a string at the cursor position.

func (*Model) Length

func (m *Model) Length() int

Length returns the number of characters currently in the text input.

func (Model) Line

func (m Model) Line() int

Line returns the line position.

func (*Model) LineCount added in v0.14.0

func (m *Model) LineCount() int

LineCount returns the number of lines that are currently in the text input.

func (Model) LineInfo

func (m Model) LineInfo() 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

func (m *Model) SetCursor(col 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) SetHeight

func (m *Model) SetHeight(h int)

SetHeight sets the height of the textarea.

func (*Model) SetPromptFunc added in v0.14.0

func (m *Model) SetPromptFunc(promptWidth int, fn func(lineIdx int) string)

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) 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 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.

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 text area in its current state.

func (Model) Width

func (m Model) Width() int

Width returns the width of the textarea.

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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