Skip to content

Commit

Permalink
feat: add flag to disable filter/search for interactive printer
Browse files Browse the repository at this point in the history
  • Loading branch information
adombeck authored and jochil committed Sep 22, 2022
1 parent 0da799e commit 853f3e2
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions interactive_multiselect_printer.go
Expand Up @@ -22,6 +22,7 @@ var (
MaxHeight: 5,
Selector: ">",
SelectorStyle: &ThemeDefault.SecondaryStyle,
Filter: true,
}
)

Expand All @@ -35,6 +36,7 @@ type InteractiveMultiselectPrinter struct {
MaxHeight int
Selector string
SelectorStyle *Style
Filter bool

selectedOption int
selectedOptions []int
Expand Down Expand Up @@ -70,6 +72,12 @@ func (p InteractiveMultiselectPrinter) WithMaxHeight(maxHeight int) *Interactive
return &p
}

// WithFilter sets the Filter option
func (p InteractiveMultiselectPrinter) WithFilter(filter bool) *InteractiveMultiselectPrinter {
p.Filter = filter
return &p
}

// Show shows the interactive multiselect menu and returns the selected entry.
func (p *InteractiveMultiselectPrinter) Show(text ...string) ([]string, error) {
// should be the first defer statement to make sure it is executed last
Expand Down Expand Up @@ -126,13 +134,15 @@ func (p *InteractiveMultiselectPrinter) 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]...)
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.Tab:
if len(p.fuzzySearchMatches) == 0 {
Expand Down Expand Up @@ -323,7 +333,11 @@ func (p *InteractiveMultiselectPrinter) renderSelectMenu() string {
}
}

content += ThemeDefault.SecondaryStyle.Sprintfln("enter: %s | tab: %s | left: %s | right: %s | type to %s", Bold.Sprint("select"), Bold.Sprint("confirm"), Bold.Sprint("none"), Bold.Sprint("all"), Bold.Sprint("filter"))
if p.Filter {
content += ThemeDefault.SecondaryStyle.Sprintfln("enter: %s | tab: %s | left: %s | right: %s | type to %s", Bold.Sprint("select"), Bold.Sprint("confirm"), Bold.Sprint("none"), Bold.Sprint("all"), Bold.Sprint("filter"))
} else {
content += ThemeDefault.SecondaryStyle.Sprintfln("enter: %s | tab: %s | left: %s | right: %s", Bold.Sprint("select"), Bold.Sprint("confirm"), Bold.Sprint("none"), Bold.Sprint("all"))
}

return content
}
Expand Down

0 comments on commit 853f3e2

Please sign in to comment.