Skip to content

Commit

Permalink
Merge pull request #530 from alirezaarzehgar/master
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed Jul 3, 2023
2 parents 5aad478 + f3724bf commit bc4dce5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
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)
}

0 comments on commit bc4dce5

Please sign in to comment.