Skip to content

Commit

Permalink
Merge pull request #8 from klowdo/main
Browse files Browse the repository at this point in the history
feat(suggestions): option to add custom suggestion filter
  • Loading branch information
stromland authored Oct 4, 2022
2 parents 691b6f8 + a295658 commit 71338b0
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cobra-prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ type CobraPrompt struct {

// InArgsParser adds a custom parser for the command line arguments (default: strings.Fields)
InArgsParser func(args string) []string

// SuggestionFilter will be uses when filtering suggestions as typing
SuggestionFilter func(suggestions []prompt.Suggest, document *prompt.Document) []prompt.Suggest
}

// Run will automatically generate suggestions for all cobra commands and flags defined by RootCmd
Expand Down Expand Up @@ -161,5 +164,10 @@ func findSuggestions(co *CobraPrompt, d *prompt.Document) []prompt.Suggest {
if co.DynamicSuggestionsFunc != nil && annotation != "" {
suggestions = append(suggestions, co.DynamicSuggestionsFunc(annotation, d)...)
}

if co.SuggestionFilter != nil {
return co.SuggestionFilter(suggestions, d)
}

return prompt.FilterHasPrefix(suggestions, d.GetWordBeforeCursor(), true)
}

0 comments on commit 71338b0

Please sign in to comment.