AutocompleteInput
Key bindings
Section titled “Key bindings”| Key | Action |
|---|---|
Tab | Accept the highlighted suggestion |
Enter | Accept suggestion (if dropdown open), otherwise submit |
Esc | Close dropdown; if already closed, fire onCancel |
↑ / ↓ | Navigate suggestions (or history when dropdown is closed) |
← / → | Move cursor |
Home / Ctrl+A | Jump to start of input |
End / Ctrl+E | Jump to end of input |
Ctrl+U | Clear the entire line |
Ctrl+W | Delete word backward |
Backspace / Delete | Delete character |
Suggestion providers
Section titled “Suggestion providers”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 tokenprovider := components.PrefixMatcher(suggestions)
// Filter by fuzzy substring matchprovider := components.FuzzyMatcher(suggestions)
// Always return the full list (useful for short static lists)provider := components.StaticSuggestions(suggestions)
// Combine multiple providersprovider := 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.
Suggestion token insertion
Section titled “Suggestion token insertion”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).
GetPreferredHeight
Section titled “GetPreferredHeight”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.
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. | Field | Type | Description |
|---|---|---|
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.