Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added filter option for select printer #530

Merged
merged 4 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 23 additions & 9 deletions interactive_select_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
MaxHeight: 5,
Selector: ">",
SelectorStyle: &ThemeDefault.SecondaryStyle,
Filter: true,
}
)

Expand All @@ -37,6 +38,7 @@ type InteractiveSelectPrinter struct {
Selector string
SelectorStyle *Style
OnInterruptFunc func()
Filter bool

selectedOption int
result string
Expand Down Expand Up @@ -78,6 +80,12 @@ func (p InteractiveSelectPrinter) WithOnInterruptFunc(exitFunc func()) *Interact
return &p
}

// WithFilter sets the Filter option
func (p InteractiveSelectPrinter) WithFilter(b ...bool) *InteractiveSelectPrinter {
p.Filter = internal.WithBoolean(b)
return &p
}

// Show shows the interactive select menu and returns the selected entry.
func (p *InteractiveSelectPrinter) Show(text ...string) (string, error) {
// should be the first defer statement to make sure it is executed last
Expand Down Expand Up @@ -149,14 +157,16 @@ func (p *InteractiveSelectPrinter) Show(text ...string) (string, error) {

switch key {
case keys.RuneKey:
// Fuzzy search for options
// append to fuzzy search string
p.fuzzySearchString += keyInfo.String()
p.selectedOption = 0
p.displayedOptionsStart = 0
p.displayedOptionsEnd = maxHeight
p.displayedOptions = append([]string{}, p.fuzzySearchMatches[:maxHeight]...)
area.Update(p.renderSelectMenu())
if p.Filter {
// Fuzzy search for options
// append to fuzzy search string
p.fuzzySearchString += keyInfo.String()
p.selectedOption = 0
p.displayedOptionsStart = 0
p.displayedOptionsEnd = maxHeight
p.displayedOptions = append([]string{}, p.fuzzySearchMatches[:maxHeight]...)
area.Update(p.renderSelectMenu())
}
case keys.Space:
p.fuzzySearchString += " "
p.selectedOption = 0
Expand Down Expand Up @@ -252,7 +262,11 @@ func (p *InteractiveSelectPrinter) Show(text ...string) (string, error) {

func (p *InteractiveSelectPrinter) renderSelectMenu() string {
var content string
content += Sprintf("%s %s: %s\n", p.text, p.SelectorStyle.Sprint("[type to search]"), p.fuzzySearchString)
if p.Filter {
content += Sprintf("%s %s: %s\n", p.text, p.SelectorStyle.Sprint("[type to search]"), p.fuzzySearchString)
} else {
content += Sprintf("%s:\n", p.text)
}

// find options that match fuzzy search string
rankedResults := fuzzy.RankFindFold(p.fuzzySearchString, p.Options)
Expand Down
5 changes: 5 additions & 0 deletions interactive_select_printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,8 @@ func TestInteractiveSelectPrinter_WithOnInterruptFunc(t *testing.T) {
p := pterm.DefaultInteractiveSelect.WithOnInterruptFunc(exitfunc)
testza.AssertEqual(t, reflect.ValueOf(p.OnInterruptFunc).Pointer(), reflect.ValueOf(exitfunc).Pointer())
}

func TestInteractiveSelectPrinter_WithFilter(t *testing.T) {
p := pterm.DefaultInteractiveSelect.WithFilter(false)
testza.AssertEqual(t, p.Filter, false)
}