suggestions

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: MIT Imports: 8 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FilterFunc

type FilterFunc func(suggestion, input string) bool

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

func (sm *Manager) ApplySelected(input *textinput.Model)

ApplySelected applies the selected suggestion to the given textinput

func (*Manager) GetSelected

func (sm *Manager) GetSelected() string

GetSelected returns the currently selected suggestion

func (*Manager) GetVisible

func (sm *Manager) GetVisible() []string

GetVisible returns the suggestions that should be displayed (limited by maxVisible)

func (*Manager) Next

func (sm *Manager) Next()

Next moves to the next suggestion (circular)

func (*Manager) Previous

func (sm *Manager) Previous()

Previous moves to the previous suggestion (circular)

func (*Manager) Render

func (sm *Manager) Render() string

Render returns the rendered suggestions as a string

func (*Manager) Reset

func (sm *Manager) Reset()

Reset resets the selected index to 0

func (*Manager) SetMaxWidth

func (sm *Manager) SetMaxWidth(width int)

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

func (sm *Manager) SetSuggestions(suggestions []string)

SetSuggestions updates all available suggestions

func (*Manager) ShouldShow

func (sm *Manager) ShouldShow(input string) bool

ShouldShow determines if suggestions should be shown based on current state and input

func (*Manager) UpdateFilter

func (sm *Manager) UpdateFilter(input string)

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.

Jump to

Keyboard shortcuts

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