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 1 commit
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 @@
MaxHeight: 5,
Selector: ">",
SelectorStyle: &ThemeDefault.SecondaryStyle,
Filter: true,
}
)

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

selectedOption int
result string
Expand Down Expand Up @@ -78,6 +80,12 @@
return &p
}

// WithFilter sets the Filter option
func (p InteractiveSelectPrinter) WithFilter(filter bool) *InteractiveSelectPrinter {
p.Filter = filter
return &p
}
alirezaarzehgar marked this conversation as resolved.
Show resolved Hide resolved

// 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 @@

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())
}

Check warning on line 169 in interactive_select_printer.go

View check run for this annotation

Codecov / codecov/patch

interactive_select_printer.go#L160-L169

Added lines #L160 - L169 were not covered by tests
case keys.Space:
p.fuzzySearchString += " "
p.selectedOption = 0
Expand Down Expand Up @@ -252,7 +262,11 @@

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)
}

Check warning on line 269 in interactive_select_printer.go

View check run for this annotation

Codecov / codecov/patch

interactive_select_printer.go#L268-L269

Added lines #L268 - L269 were not covered by tests

// 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)
}