editline

package
v0.0.0-...-486954f Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2023 License: Apache-2.0 Imports: 21 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultKeyMap = KeyMap{
	KeyMap: textarea.DefaultKeyMap,

	AlwaysNewline:   key.NewBinding(key.WithKeys("ctrl+o"), key.WithHelp("C-o", "force newline")),
	AlwaysComplete:  key.NewBinding(key.WithKeys("alt+enter", "alt+\r"), key.WithHelp("M-⤶/M-C-m", "force complete")),
	AutoComplete:    key.NewBinding(key.WithKeys("tab"), key.WithHelp("tab", "try autocomplete")),
	Interrupt:       key.NewBinding(key.WithKeys("ctrl+c"), key.WithHelp("C-c", "clear/cancel")),
	SignalQuit:      key.NewBinding(key.WithKeys(`ctrl+\`)),
	SignalTTYStop:   key.NewBinding(key.WithKeys("ctrl+z")),
	Refresh:         key.NewBinding(key.WithKeys("ctrl+l"), key.WithHelp("C-l", "refresh display")),
	EndOfInput:      key.NewBinding(key.WithKeys("ctrl+d"), key.WithHelp("C-d", "erase/stop")),
	AbortSearch:     key.NewBinding(key.WithKeys("ctrl+g"), key.WithDisabled()),
	SearchBackward:  key.NewBinding(key.WithKeys("ctrl+r"), key.WithHelp("C-r", "search hist"), key.WithDisabled()),
	HistoryPrevious: key.NewBinding(key.WithKeys("alt+p"), key.WithHelp("M-p", "prev history entry"), key.WithDisabled()),
	HistoryNext:     key.NewBinding(key.WithKeys("alt+n"), key.WithHelp("M-n", "next history entry"), key.WithDisabled()),
	HideShowPrompt:  key.NewBinding(key.WithKeys("alt+."), key.WithHelp("M-.", "hide/show prompt")),
	MoreHelp:        key.NewBinding(key.WithKeys("alt+?"), key.WithHelp("M-?", "toggle key help")),
	ReflowLine:      key.NewBinding(key.WithKeys("alt+q"), key.WithHelp("M-q", "reflow line")),
	ReflowAll:       key.NewBinding(key.WithKeys("alt+Q", "alt+`"), key.WithHelp("M-S-q/M-`", "reflow all")),
	Debug:           key.NewBinding(key.WithKeys("ctrl+_", "ctrl+@"), key.WithHelp("C-_/C-@", "debug mode"), key.WithDisabled()),
	ExternalEdit:    key.NewBinding(key.WithKeys("alt+f2", "alt+2"), key.WithHelp("M-2/M-F2", "external edit")),
}

DefaultKeyMap is the default set of key bindings.

View Source
var ErrInterrupted = errors.New("interrupted")

ErrInterrupted is returned when the input is terminated with Ctrl+C.

Functions

func DefaultReflow

func DefaultReflow(
	allText bool, currentText string, targetWidth int,
) (changed bool, newText, info string)

DefaultReflow is the default/initial value of Reflow.

func DefaultStyles

func DefaultStyles() (Style, Style)

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

func InputComplete

func InputComplete() tea.Msg

InputComplete generates an InputCompleteMsg.

Types

type AutoCompleteFn

type AutoCompleteFn func(entireInput [][]rune, line, col int) (msg string, comp Completions)

AutoCompleteFn is called upon the user pressing the autocomplete key. The callback is provided the text of the input and the position of the cursor in the input. The returned msg is printed above the input box.

type Candidate

type Candidate interface {
	// Replacement is the string to replace.
	Replacement() string

	// MoveRight returns the number of times the cursor
	// should be moved to the right to arrive at the
	// end of the word being replaced by the completion.
	//
	// For example, if the input is this:
	//
	//       alice
	//        ^
	//
	// where the cursor is on the 2nd character, and
	// the completion is able to replace the entire word,
	// MoveRight should return 4.
	MoveRight() int

	// DeleteLeft returns the total number of characters
	// being replaced by the completion, including the
	// characters to the right of the cursor (as returned by MoveRight).
	//
	// For example, if the input is this:
	//
	//       alice
	//        ^
	//
	// where the cursor is on the 2nd character, and
	// the completion is able to replace the entire word,
	// DeleteLeft should return 5.
	DeleteLeft() int
}

Candidate is the type of one completion candidate.

type Completions

type Completions interface {
	// Values is the set of all completion values.
	complete.Values

	// Candidate converts a complete.Entry to a Candidate.
	Candidate(e complete.Entry) Candidate
}

Completions is the return value of AutoCompleteFn.

func SimpleWordsCompletion

func SimpleWordsCompletion(words []string, category string, cursor, start, end int) Completions

SimpleWordsCompletion turns an array of simple strings into a Completions interface suitable to return from an AutoCompleteFn. The start/end positions refer to the word start and end positions on the current line.

func SingleWordCompletion

func SingleWordCompletion(word string, cursor, start, end int) Completions

SingleWordCompletion turns a simple string into a Completions interface suitable to return from an AutoCompleteFn. The start/end positions refer to the word start and end positions on the current line.

type InputCompleteMsg

type InputCompleteMsg struct{}

InputCompleteMsg is generated by the editor when the input is complete.

type KeyMap

type KeyMap struct {
	textarea.KeyMap

	EndOfInput      key.Binding
	Interrupt       key.Binding
	AutoComplete    key.Binding
	SignalQuit      key.Binding
	SignalTTYStop   key.Binding
	Refresh         key.Binding
	AbortSearch     key.Binding
	SearchBackward  key.Binding
	HistoryPrevious key.Binding
	HistoryNext     key.Binding
	Debug           key.Binding
	HideShowPrompt  key.Binding
	AlwaysNewline   key.Binding
	AlwaysComplete  key.Binding
	MoreHelp        key.Binding
	ReflowLine      key.Binding
	ReflowAll       key.Binding
	ExternalEdit    key.Binding
}

KeyMap is the key bindings for actions within the editor.

type Model

type Model struct {
	// Err is the final state at the end of input.
	// Likely io.EOF or ErrInterrupted.
	Err error

	// KeyMap is the key bindings to use.
	KeyMap KeyMap

	// Styling. FocusedStyle and BlurredStyle are used to style the textarea in
	// focused and blurred states.
	// Only takes effect at Reset() or Focus().
	FocusedStyle Style
	BlurredStyle Style

	// Placeholder is displayed when the editor is still empty.
	// Only takes effect at Reset() or Focus().
	Placeholder string

	// CheckInputComplete is called when the Enter key is pressed.  It
	// determines whether a newline character should be added to the
	// input (callback returns false) or whether the input should
	// terminate altogether (callback returns true). The callback is
	// provided the text of the input and the line number at which the
	// cursor is currently positioned as argument.
	//
	// The default behavior if CheckInputComplete is nil is to terminate
	// the input when enter is pressed.
	CheckInputComplete func(entireInput [][]rune, line, col int) bool

	// AutoComplete is the AutoCompleteFn to use.
	AutoComplete AutoCompleteFn

	// CharLimit is the maximum size of the input in characters.
	// Set to zero or less for no limit.
	CharLimit int

	// MaxHeight is the maximum height of the input in lines.
	// Set to zero or less for no limit.
	MaxHeight int

	// MaxWidth is the maximum width of the input in characters.
	// Set to zero or less for no limit.
	MaxWidth int

	// CursorMode determines how the cursor is displayed.
	CursorMode cursor.Mode

	// MaxHistorySize is the maximum number of entries in the history.
	// Set to zero for no limit.
	MaxHistorySize int

	// DedupHistory if true avoids adding a history entry
	// if it is equal to the last one added.
	DedupHistory bool

	// DeleteCharIfNotEOF, if true, causes the EndOfInput key binding
	// to be translated to delete-character-forward when it is not
	// entered at the beginning of a line.
	// Meant for use when the EndOfInput key binding is Ctrl+D, which
	// is the standard character deletion in textarea/libedit.
	// This can be set to false if the EndOfInput binding is fully
	// separate from DeleteCharacterForward.
	DeleteCharIfNotEOF bool

	// Prompt is the prompt displayed before entry lines.
	// Only takes effect at Reset().
	Prompt string

	// NextPrompt, if defined is the prompt displayed before entry lines
	// after the first one.
	// Only takes effect at Reset().
	NextPrompt string

	// Reflow, if defined, is used for the reflowing commands (M-q/M-Q).
	// The info returned value, if any, is displayed as an informational
	// message above the editor.
	Reflow func(all bool, currentText string, targetWidth int) (changed bool, newText, info string)

	// SearchPrompt is the prompt displayed before the history search pattern.
	SearchPrompt string
	// SearchPromptNotFound is the prompt displayed before the history search pattern,
	// when no match is found.
	SearchPromptNotFound string
	// SearchPromptInvalid is the prompt displayed before the history search pattern,
	// when the pattern is invalid.
	SearchPromptInvalid string

	// CaseSensitiveSearch, if enabled, makes history search case-sensitive.
	CaseSensitiveSearch bool

	// ShowLineNumbers if true shows line numbers at the beginning
	// of each input line.
	// Only takes effect at Reset() or Focus().
	ShowLineNumbers bool
	// contains filtered or unexported fields
}

Model represents a widget that supports multi-line entry with auto-growing of the text height.

func New

func New(width, height int) *Model

New creates a new Model.

func (*Model) AddHistoryEntry

func (m *Model) AddHistoryEntry(s string)

AddHistoryEntry adds an entry to the history navigation list.

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

func (m *Model) Debug() string

Debug returns debug details about the state of the model.

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 is displayed.

func (Model) FullHelp

func (m Model) FullHelp() [][]key.Binding

FullHelp is part of the help.KeyMap interface.

func (*Model) GetHistory

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

GetHistory retrieves all the entries in the history navigation list.

func (*Model) Init

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

Init is part of the tea.Model interface.

func (*Model) Reset

func (m *Model) Reset()

Reset sets the input to its default state with no input. The history is preserved.

func (*Model) SetDebugEnabled

func (m *Model) SetDebugEnabled(enable bool)

SetDebugEnabled enables/disables the debug mode binding. When disabling it, it also proactively disables debugging if currently enabled.

func (*Model) SetExternalEditorEnabled

func (m *Model) SetExternalEditorEnabled(enable bool, extension string)

SetExternalEditorEnabled enables using an external editor (via $EDITOR). The extension is added to the generated file name so that the editor can auto-select a language for syntax highlighting. If the extension is left empty, "txt" is assumed.

func (*Model) SetHistory

func (m *Model) SetHistory(h []string)

SetHistory sets the history navigation list all at once.

func (*Model) SetSize

func (m *Model) SetSize(width, height int)

SetSize changes the size of the editor. NB: if one of the dimensions is zero, the call is a no-op. NB: it only takes effect at the first next event processed.

func (Model) ShortHelp

func (m Model) ShortHelp() []key.Binding

ShortHelp is part of the help.KeyMap interface.

func (*Model) Update

func (m *Model) Update(imsg tea.Msg) (tea.Model, tea.Cmd)

Update is the Bubble Tea event handler. This is part of the tea.Model interface.

func (*Model) Value

func (m *Model) Value() string

Value retrieves the value of the text input.

func (Model) View

func (m Model) View() string

View renders the text area in its current state. This is part of the tea.Model interface.

type Style

type Style struct {
	Editor textarea.Style

	SearchInput struct {
		PromptStyle      lipgloss.Style
		TextStyle        lipgloss.Style
		BackgroundStyle  lipgloss.Style
		PlaceholderStyle lipgloss.Style
		CursorStyle      lipgloss.Style
	}
}

Style that will be applied to the editor.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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