Repository: pterm/pterm
Pull request #530 (2023-06-30 22:42:42)

feat: add filter option for `select`
### Description
We have Filter option for multiselect printer but this feature is not available for select printer.
This PR implemented filter option for select printer.

### Scope
> What is affected by this pull request?

- [ ] Bug Fix
- [x] New Feature
- [ ] Documentation
- [ ] Other

### To-Do Checklist
- [x] I tested my changes
- [x] I have commented every method that I created/changed
- [ ] I updated the examples to fit with my changes
- [x] I have added tests for my newly created methods


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/pterm/pterm/pull/530?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pterm) Report
> Merging [#530](https://app.codecov.io/gh/pterm/pterm/pull/530?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pterm) (8bb7ab7) into [master](https://app.codecov.io/gh/pterm/pterm/commit/156868c4b1b73fd09fd100d79d7e7be3cb46278e?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pterm) (156868c) will **decrease** coverage by `0.11%`.
> The diff coverage is `n/a`.

```diff
@@            Coverage Diff             @@
##           master     #530      +/-   ##
==========================================
- Coverage   82.28%   82.17%   -0.11%     
==========================================
  Files          30       30              
  Lines        3482     3490       +8     
==========================================
+ Hits         2865     2868       +3     
- Misses        560      564       +4     
- Partials       57       58       +1     
```


| [Impacted Files](https://app.codecov.io/gh/pterm/pterm/pull/530?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pterm) | Coverage Δ | |
|---|---|---|
| [interactive\_select\_printer.go](https://app.codecov.io/gh/pterm/pterm/pull/530?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pterm#diff-aW50ZXJhY3RpdmVfc2VsZWN0X3ByaW50ZXIuZ28=) | `52.63% <ø> (-0.61%)` | :arrow_down: |


MarvinJWendt: Hi @alirezaarzehgar, thanks for the PR! Could you please merge `master` into this PR, and solve the conflicts? This should also fix the failed check, as we switched something in our CI System that was faulty.

Patch:
diff --git a/interactive_select_printer.go b/interactive_select_printer.go
index 22ac4efa..2dbab662 100644
--- a/interactive_select_printer.go
+++ b/interactive_select_printer.go
@@ -23,6 +23,7 @@ var (
 		MaxHeight:     5,
 		Selector:      ">",
 		SelectorStyle: &ThemeDefault.SecondaryStyle,
+		Filter:        true,
 	}
 )
 
@@ -37,6 +38,7 @@ type InteractiveSelectPrinter struct {
 	Selector        string
 	SelectorStyle   *Style
 	OnInterruptFunc func()
+	Filter          bool
 
 	selectedOption        int
 	result                string
@@ -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
@@ -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
@@ -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)


Test patch:
diff --git a/interactive_select_printer_test.go b/interactive_select_printer_test.go
index d4af8b6e..a6b555d8 100644
--- a/interactive_select_printer_test.go
+++ b/interactive_select_printer_test.go
@@ -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)
+}