-
-
Notifications
You must be signed in to change notification settings - Fork 194
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
Update InteractiveMultiselectPrinter
to optionally show selected options
#625
base: master
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #625 +/- ##
==========================================
- Coverage 81.66% 81.27% -0.39%
==========================================
Files 33 34 +1
Lines 4139 4160 +21
==========================================
+ Hits 3380 3381 +1
- Misses 695 714 +19
- Partials 64 65 +1 ☔ View full report in Codecov by Sentry. |
@@ -379,10 +391,20 @@ func (p *InteractiveMultiselectPrinter) renderSelectMenu() string { | |||
|
|||
help := fmt.Sprintf("%s: %s | %s: %s | left: %s | right: %s", p.KeySelect, Bold.Sprint("select"), p.KeyConfirm, Bold.Sprint("confirm"), Bold.Sprint("none"), Bold.Sprint("all")) | |||
if p.Filter { | |||
help += fmt.Sprintf("| type to %s", Bold.Sprint("filter")) | |||
help += fmt.Sprintf(" | type to %s", Bold.Sprint("filter")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there was sipposed to be a leading space here 🙃
Hi, you have disabled that Maintainers can edit the PR branch. I did some refactorings, but as I cannot push them to your branch, you would have to apply them manually: From 9914e41ff990939e09cef3505312d11c5f1c0984 Mon Sep 17 00:00:00 2001
From: MarvinJWendt <git@marvinjwendt.com>
Date: Sun, 4 Feb 2024 19:08:19 +0100
Subject: [PATCH] refactored logic
---
.../show-selected-options/main.go | 5 --
interactive_multiselect_printer.go | 46 ++++++++-----------
options_renderer.go | 25 ----------
3 files changed, 20 insertions(+), 56 deletions(-)
delete mode 100644 options_renderer.go
diff --git a/_examples/interactive_multiselect/show-selected-options/main.go b/_examples/interactive_multiselect/show-selected-options/main.go
index f4541b37..6ccf0722 100644
--- a/_examples/interactive_multiselect/show-selected-options/main.go
+++ b/_examples/interactive_multiselect/show-selected-options/main.go
@@ -15,11 +15,6 @@ func main() {
options = append(options, fmt.Sprintf("Option %d", i))
}
- // Add 5 more options to the slice, indicating the availability of fuzzy searching.
- for i := 0; i < 5; i++ {
- options = append(options, fmt.Sprintf("You can use fuzzy searching (%d)", i))
- }
-
// Use PTerm's interactive multiselect to present the options to the user and capture their selections.
// The Show() method displays the options and waits for user input.
selectedOptions, _ := pterm.DefaultInteractiveMultiselect.
diff --git a/interactive_multiselect_printer.go b/interactive_multiselect_printer.go
index 8febccff..2ad85880 100644
--- a/interactive_multiselect_printer.go
+++ b/interactive_multiselect_printer.go
@@ -1,15 +1,14 @@
package pterm
import (
- "fmt"
- "sort"
-
"atomicgo.dev/cursor"
"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"
+ "fmt"
"github.com/lithammer/fuzzysearch/fuzzy"
-
"github.com/pterm/pterm/internal"
+ "sort"
+ "strings"
)
var (
@@ -32,18 +31,18 @@ var (
// InteractiveMultiselectPrinter is a printer for interactive multiselect menus.
type InteractiveMultiselectPrinter struct {
- DefaultText string
- TextStyle *Style
- Options []string
- OptionStyle *Style
- DefaultOptions []string
- MaxHeight int
- Selector string
- SelectorStyle *Style
- Filter bool
- Checkmark *Checkmark
- OnInterruptFunc func()
- OptionsRenderer OptionsRenderer
+ DefaultText string
+ TextStyle *Style
+ Options []string
+ OptionStyle *Style
+ DefaultOptions []string
+ MaxHeight int
+ Selector string
+ SelectorStyle *Style
+ Filter bool
+ Checkmark *Checkmark
+ OnInterruptFunc func()
+ ShowSelectedOptions bool
selectedOption int
selectedOptions []int
@@ -111,20 +110,15 @@ func (p InteractiveMultiselectPrinter) WithCheckmark(checkmark *Checkmark) *Inte
return &p
}
-// OnInterrupt sets the function to execute on exit of the input reader
+// WithOnInterruptFunc sets the function to execute on exit of the input reader
func (p InteractiveMultiselectPrinter) WithOnInterruptFunc(exitFunc func()) *InteractiveMultiselectPrinter {
p.OnInterruptFunc = exitFunc
return &p
}
// WithShowSelectedOptions shows the selected options at the bottom if the menu
-func (p InteractiveMultiselectPrinter) WithShowSelectedOptions(b bool) *InteractiveMultiselectPrinter {
- return p.WithCustomOptionsRenderer(NewInterpolatedOptions(", "))
-}
-
-// WithCustomOptionsRenderer uses the provided OptionsRenderer to render the selected options
-func (p InteractiveMultiselectPrinter) WithCustomOptionsRenderer(r OptionsRenderer) *InteractiveMultiselectPrinter {
- p.OptionsRenderer = r
+func (p InteractiveMultiselectPrinter) WithShowSelectedOptions(b ...bool) *InteractiveMultiselectPrinter {
+ p.ShowSelectedOptions = internal.WithBoolean(b)
return &p
}
@@ -396,13 +390,13 @@ func (p *InteractiveMultiselectPrinter) renderSelectMenu() string {
content += ThemeDefault.SecondaryStyle.Sprintfln(help)
// Optionally, add selected options to the menu
- if p.OptionsRenderer != nil && len(p.selectedOptions) > 0 {
+ if p.ShowSelectedOptions && len(p.selectedOptions) > 0 {
selected := make([]string, len(p.selectedOptions))
for i, optIdx := range p.selectedOptions {
selected[i] = p.Options[optIdx]
}
- content += p.OptionsRenderer.RenderOptions(selected)
+ content += ThemeDefault.SecondaryStyle.Sprint("Selected: ") + Green(strings.Join(selected, Gray(", "))) + "\n"
}
return content
diff --git a/options_renderer.go b/options_renderer.go
deleted file mode 100644
index 73510385..00000000
--- a/options_renderer.go
+++ /dev/null
@@ -1,25 +0,0 @@
-package pterm
-
-import "strings"
-
-type OptionsRenderer interface {
- RenderOptions(options []string) string
-}
-
-type InterpolatedOptionsRenderer struct {
- separator string
-}
-
-// RenderOptions implements OptionsRenderer.
-func (r InterpolatedOptionsRenderer) RenderOptions(options []string) string {
- content := ThemeDefault.SecondaryStyle.Sprint("you have selected: ")
- content += ThemeDefault.SecondaryStyle.Add(*Italic.ToStyle()).
- Sprintln(strings.Join(options, r.separator))
- return content
-}
-
-func NewInterpolatedOptions(separator string) OptionsRenderer {
- return &InterpolatedOptionsRenderer{
- separator: separator,
- }
-}
--
2.34.1.windows.1
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As stated above
Hey @MarvinJWendt! Since we had no answer for a month, I've decided to transfer all his changes along with your refactorings to this PR. |
ahh sorry, didnt realised, I had done that 😅 . Thanks @devnyxie :) |
Description
OptionsRenderer
interfaceWithCustomOptionsRenderer
receiverWithCustomOptionsRenderer
receiver forInteractiveMultiselectPrinter
(explained above)WithShowSelectedOptions
receiver forInteractiveMultiselectPrinter
. This adds an option renderer that renders the selected options as comma separated valuesScope
Related Issue
Fixes #621
To-Do Checklist