Skip to content

Commit

Permalink
feat: add filter option for select
Browse files Browse the repository at this point in the history
- Add Filter attribute
- Add WithFilter method
- Limit fuzz search for when p.Filter is enabled
- Change content on renderSelectMenu
- Create test for WithFilter method

Signed-off-by: alireza <alirezaarzehgar82@gmail.com>
  • Loading branch information
alirezaarzehgar committed Jun 30, 2023
1 parent cb640a9 commit 8bb7ab7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
33 changes: 24 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 @@ -36,6 +37,7 @@ type InteractiveSelectPrinter struct {
MaxHeight int
Selector string
SelectorStyle *Style
Filter bool

selectedOption int
result string
Expand Down Expand Up @@ -71,6 +73,12 @@ func (p InteractiveSelectPrinter) WithMaxHeight(maxHeight int) *InteractiveSelec
return &p
}

// WithFilter sets the Filter option
func (p InteractiveSelectPrinter) WithFilter(filter bool) *InteractiveSelectPrinter {
p.Filter = filter
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 @@ -142,14 +150,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())
}

Check warning on line 162 in interactive_select_printer.go

View check run for this annotation

Codecov / codecov/patch

interactive_select_printer.go#L153-L162

Added lines #L153 - L162 were not covered by tests
case keys.Space:
p.fuzzySearchString += " "
p.selectedOption = 0
Expand Down Expand Up @@ -245,7 +255,12 @@ 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)
}

Check warning on line 263 in interactive_select_printer.go

View check run for this annotation

Codecov / codecov/patch

interactive_select_printer.go#L262-L263

Added lines #L262 - L263 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 @@ -49,3 +49,8 @@ func TestInteractiveSelectPrinter_WithMaxHeight(t *testing.T) {
p := pterm.DefaultInteractiveSelect.WithMaxHeight(1337)
testza.AssertEqual(t, p.MaxHeight, 1337)
}

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

0 comments on commit 8bb7ab7

Please sign in to comment.