Skip to content
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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

prnvbn
Copy link

@prnvbn prnvbn commented Jan 26, 2024

Description

  • Add OptionsRenderer interface
    • this is used by interactive multiselect printer to render the selected options
    • this interface is used to allow the user to overwrite how the selected options are rendered using the WithCustomOptionsRenderer receiver
  • Add WithCustomOptionsRenderer receiver for InteractiveMultiselectPrinter (explained above)
  • Add WithShowSelectedOptions receiver for InteractiveMultiselectPrinter. This adds an option renderer that renders the selected options as comma separated values

Scope

What is affected by this pull request?

  • Bug Fix
  • New Feature
  • Documentation
  • Other

Related Issue

Fixes #621

To-Do Checklist

  • I tested my changes
  • I have commented every method that I created/changed
  • I updated the examples to fit with my changes
  • I have added tests for my newly created methods

Copy link

codecov bot commented Jan 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (02ac513) 81.66% compared to head (f37afee) 81.27%.
Report is 9 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

@@ -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"))
Copy link
Author

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 🙃

@MarvinJWendt
Copy link
Member

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

Copy link
Member

@MarvinJWendt MarvinJWendt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As stated above

@MarvinJWendt MarvinJWendt added the waiting for response Waiting for author to respond label Feb 12, 2024
@devnyxie
Copy link

devnyxie commented Mar 7, 2024

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.

@prnvbn
Copy link
Author

prnvbn commented Mar 8, 2024

ahh sorry, didnt realised, I had done that 😅 . Thanks @devnyxie :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for response Waiting for author to respond
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update InteractiveMultiselectPrinter to optionally show selected options
3 participants