Skip to content

AutocompleteInput

KeyAction
TabAccept the highlighted suggestion
EnterAccept suggestion (if dropdown open), otherwise submit
EscClose dropdown; if already closed, fire onCancel
/ Navigate suggestions (or history when dropdown is closed)
/ Move cursor
Home / Ctrl+AJump to start of input
End / Ctrl+EJump to end of input
Ctrl+UClear the entire line
Ctrl+WDelete word backward
Backspace / DeleteDelete character

Three built-in provider constructors cover the common cases. All accept a fixed []Suggestion slice and return a SuggestionProvider func.

// Filter by prefix of the current token
provider := components.PrefixMatcher(suggestions)
// Filter by fuzzy substring match
provider := components.FuzzyMatcher(suggestions)
// Always return the full list (useful for short static lists)
provider := components.StaticSuggestions(suggestions)
// Combine multiple providers
provider := components.ChainedProvider(fields, operators, values)

The SuggestionProvider signature is func(text string, cursorPos int) []Suggestion, so you can supply a dynamic one (e.g., querying a DB) directly.

When a suggestion is accepted, InsertText replaces the current token — the word ending at the cursor, split on spaces, (, and ). Set InsertText when the display text differs from what should be typed (e.g., a human-readable label vs. a field name).

Use GetPreferredHeight() when laying out the widget inside a tview.Flex — it returns the exact row count needed for the input box plus the current dropdown height so the surrounding layout does not clip suggestions.

AutocompleteInput preview

AutocompleteInput is an input field with a suggestion dropdown. The SuggestionProvider is called on every keystroke; the dropdown appears when it returns at least one result. Tab or Enter accepts the highlighted suggestion, replacing the current token (word ending at cursor, split on spaces and parentheses). Up/Down navigate the list; Esc closes it without accepting. Up/Down also navigate history when the dropdown is closed.

Constructor func NewAutocompleteInput() *AutocompleteInput

Usage

suggestions := []components.Suggestion{
{Text: "status", Category: "Field"},
{Text: "service", Category: "Field"},
{Text: "started", Category: "Value"},
}
input := components.NewAutocompleteInput().
SetPrompt("> ").
SetPlaceholder("Type to search...").
SetMaxSuggestions(8).
SetSuggestionProvider(components.FuzzyMatcher(suggestions))
input.SetOnSubmit(func(text string) {
fmt.Println("submitted:", text)
})
fmt.Println(input != nil)

Methods

Method Signature Description
Clear func (ai *AutocompleteInput) Clear() *AutocompleteInput Clear clears the input text.
GetPreferredHeight func (ai *AutocompleteInput) GetPreferredHeight() int GetPreferredHeight returns the height needed to display the input and suggestions.
GetSelectedSuggestionIndex func (ai *AutocompleteInput) GetSelectedSuggestionIndex() int GetSelectedSuggestionIndex returns the currently selected suggestion index.
GetSuggestions func (ai *AutocompleteInput) GetSuggestions() []Suggestion GetSuggestions returns the current filtered suggestions.
GetText func (ai *AutocompleteInput) GetText() string GetText returns the current input text.
IsSuggestionsVisible func (ai *AutocompleteInput) IsSuggestionsVisible() bool IsSuggestionsVisible returns whether suggestions are currently displayed.
SetHistoryProvider func (ai *AutocompleteInput) SetHistoryProvider(fn HistoryProvider) *AutocompleteInput SetHistoryProvider sets a function to provide history entries. The function receives direction (-1 for previous, +1 for next) and returns the entry.
SetMaxSuggestions func (ai *AutocompleteInput) SetMaxSuggestions(max int) *AutocompleteInput SetMaxSuggestions sets the maximum number of suggestions to display.
SetOnCancel func (ai *AutocompleteInput) SetOnCancel(fn func()) *AutocompleteInput SetOnCancel sets the cancel callback (Esc pressed).
SetOnChange func (ai *AutocompleteInput) SetOnChange(fn func(text string)) *AutocompleteInput SetOnChange sets the change callback (input changed).
SetOnSelect func (ai *AutocompleteInput) SetOnSelect(fn func(suggestion Suggestion)) *AutocompleteInput SetOnSelect sets the selection callback for when a suggestion is chosen.
SetOnSubmit func (ai *AutocompleteInput) SetOnSubmit(fn func(text string)) *AutocompleteInput SetOnSubmit sets the submit callback (Enter pressed).
SetPlaceholder func (ai *AutocompleteInput) SetPlaceholder(placeholder string) *AutocompleteInput SetPlaceholder sets the placeholder text shown when input is empty.
SetPrompt func (ai *AutocompleteInput) SetPrompt(prompt string) *AutocompleteInput SetPrompt sets the prompt displayed before the input.
SetSuggestionProvider func (ai *AutocompleteInput) SetSuggestionProvider(fn SuggestionProvider) *AutocompleteInput SetSuggestionProvider sets a function to provide suggestions.
SetText func (ai *AutocompleteInput) SetText(text string) *AutocompleteInput SetText sets the input text.
SetTitle func (ai *AutocompleteInput) SetTitle(title string) *AutocompleteInput SetTitle sets the title displayed in the border.

Types

type HistoryProvider HistoryProvider is a function that returns history entries. Direction: -1 for previous, +1 for next. Returns the history entry string, or empty string if none.
type Suggestion struct Suggestion represents an autocomplete suggestion.
FieldTypeDescription
Text string Display text
InsertText string Text to insert when selected (defaults to Text if empty)
Description string Optional description
Category string Category for grouping (e.g., "Field", "Operator", "Value")
Data any Optional user data
type SuggestionProvider SuggestionProvider is a function that returns suggestions based on input and cursor position.