Documentation
¶
Overview ¶
Package suggestions provides autocomplete functionality for TUI forms.
The main component is Manager, which handles filtering, navigation, and rendering of suggestions. It supports predefined filter functions for common use cases.
Example usage:
iconSuggestions := suggestions.NewManager(
[]string{"terminal-bash", "code", "play"},
3, // max visible
suggestions.StartsWithFilter,
)
// In your Update method:
iconSuggestions.Next() // Navigate to next suggestion
iconSuggestions.ApplySelected(&textInput) // Apply selection
// In your View method:
if iconSuggestions.ShouldShow(input.Value()) {
return iconSuggestions.Render()
}
Index ¶
- type FilterFunc
- type Manager
- func (sm *Manager) ApplySelected(input *textinput.Model)
- func (sm *Manager) GetSelected() string
- func (sm *Manager) GetVisible() []string
- func (sm *Manager) Next()
- func (sm *Manager) Previous()
- func (sm *Manager) Render() string
- func (sm *Manager) Reset()
- func (sm *Manager) SetMaxWidth(width int)
- func (sm *Manager) SetSuggestions(suggestions []string)
- func (sm *Manager) ShouldShow(input string) bool
- func (sm *Manager) UpdateFilter(input string)
- type PathManager
- type PathSuggestionsLoadedMsg
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FilterFunc ¶
FilterFunc defines how suggestions are filtered based on input
var CaseSensitiveContainsFilter FilterFunc = func(suggestion, input string) bool { return strings.Contains(suggestion, input) }
CaseSensitiveContainsFilter filters suggestions that contain the input (case-sensitive)
var CaseSensitiveStartsWithFilter FilterFunc = func(suggestion, input string) bool { return strings.HasPrefix(suggestion, input) }
CaseSensitiveStartsWithFilter filters suggestions that start with the input (case-sensitive)
var ContainsFilter FilterFunc = func(suggestion, input string) bool { return strings.Contains(strings.ToLower(suggestion), strings.ToLower(input)) }
ContainsFilter filters suggestions that contain the input anywhere (case-insensitive)
var ExactFilter FilterFunc = func(suggestion, input string) bool { return strings.EqualFold(suggestion, input) }
ExactFilter filters suggestions that exactly match the input (case-insensitive)
var StartsWithFilter FilterFunc = func(suggestion, input string) bool { return strings.HasPrefix(strings.ToLower(suggestion), strings.ToLower(input)) }
StartsWithFilter filters suggestions that start with the input (case-insensitive) This is the default filter used by NewManager when nil is passed
var WordBoundaryFilter FilterFunc = func(suggestion, input string) bool { separators := []string{" ", "-", "_", "."} words := []string{suggestion} for _, sep := range separators { var newWords []string for _, word := range words { newWords = append(newWords, strings.Split(word, sep)...) } words = newWords } inputLower := strings.ToLower(input) for _, word := range words { if strings.HasPrefix(strings.ToLower(word), inputLower) { return true } } return false }
WordBoundaryFilter filters suggestions where any word starts with the input Useful for multi-word suggestions like "terminal-bash" matching "bash"
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager handles autocomplete suggestions with navigation and rendering
func NewManager ¶
func NewManager(suggestions []string, maxVisible int, filterFunc FilterFunc) *Manager
NewManager creates a new suggestion manager (shows suggestions on empty input by default)
func NewManagerWithOptions ¶
func NewManagerWithOptions(suggestions []string, maxVisible int, filterFunc FilterFunc, showOnEmpty bool) *Manager
NewManagerWithOptions creates a new suggestion manager with custom options
func (*Manager) ApplySelected ¶
ApplySelected applies the selected suggestion to the given textinput
func (*Manager) GetSelected ¶
GetSelected returns the currently selected suggestion
func (*Manager) GetVisible ¶
GetVisible returns the suggestions that should be displayed (limited by maxVisible)
func (*Manager) Previous ¶
func (sm *Manager) Previous()
Previous moves to the previous suggestion (circular)
func (*Manager) SetMaxWidth ¶
SetMaxWidth sets the container width used when rendering suggestions, so the box can shrink to fit narrow terminals. A value <= 0 keeps the style default.
func (*Manager) SetSuggestions ¶
SetSuggestions updates all available suggestions
func (*Manager) ShouldShow ¶
ShouldShow determines if suggestions should be shown based on current state and input
func (*Manager) UpdateFilter ¶
UpdateFilter filters suggestions based on input and resets selection only if input changed
type PathManager ¶
type PathManager struct {
*Manager
// contains filtered or unexported fields
}
PathManager handles autocomplete suggestions for filesystem paths with dynamic directory scanning
func NewPathManager ¶
func NewPathManager(maxVisible int) *PathManager
NewPathManager creates a new path suggestion manager with filesystem-aware autocomplete
func (*PathManager) ApplyScannedSuggestions ¶
func (pm *PathManager) ApplyScannedSuggestions(msg PathSuggestionsLoadedMsg)
ApplyScannedSuggestions stores suggestions produced by an asynchronous directory scan and re-filters them against the current input.
func (*PathManager) ApplySelected ¶
func (pm *PathManager) ApplySelected(input *textinput.Model) tea.Cmd
ApplySelected applies the selected path suggestion to the textinput with path-specific handling. It returns the command produced by refreshing suggestions for the newly applied context.
func (*PathManager) UpdateFilter ¶
func (pm *PathManager) UpdateFilter(inputText string) tea.Cmd
UpdateFilter overrides the base UpdateFilter to provide dynamic path suggestions. When the directory context changes it returns a command that scans the filesystem off the UI goroutine; the previously known suggestions stay visible until the scan reports back via PathSuggestionsLoadedMsg. Returns nil when no rescan is needed.
type PathSuggestionsLoadedMsg ¶
type PathSuggestionsLoadedMsg struct {
Suggestions []string
}
PathSuggestionsLoadedMsg carries directory suggestions produced by an asynchronous filesystem scan, to be applied on the UI goroutine.