Repository: pterm/pterm
Pull request #432 (2023-01-05 04:00:33)

feat(theme): added theme support for checkmarks


Patch:
diff --git a/_examples/interactive_multiselect/custom-checkmarks/main.go b/_examples/interactive_multiselect/custom-checkmarks/main.go
index aeebaed7..d6bf4caa 100644
--- a/_examples/interactive_multiselect/custom-checkmarks/main.go
+++ b/_examples/interactive_multiselect/custom-checkmarks/main.go
@@ -19,7 +19,7 @@ func main() {
 	printer.Filter = false
 	printer.KeyConfirm = keys.Enter
 	printer.KeySelect = keys.Space
-	printer.Checkmark = pterm.Checkmark{Checked: "+", Unchecked: "-"}
+	printer.Checkmark = &pterm.Checkmark{Checked: pterm.Green("+"), Unchecked: pterm.Red("-")}
 	selectedOptions, _ := printer.Show()
 	pterm.Info.Printfln("Selected options: %s", pterm.Green(selectedOptions))
 }
diff --git a/atoms.go b/atoms.go
index b4eb0b09..bc11da24 100644
--- a/atoms.go
+++ b/atoms.go
@@ -1,5 +1,11 @@
 package pterm
 
+// Checkmark is used in the interactive multiselect printer.
+type Checkmark struct {
+	Checked   string
+	Unchecked string
+}
+
 // Bars is used to display multiple Bar.
 type Bars []Bar
 
@@ -11,12 +17,6 @@ type Bar struct {
 	LabelStyle *Style
 }
 
-// Checkmark is used in the interactive multiselect printer.
-type Checkmark struct {
-	Checked   string
-	Unchecked string
-}
-
 // WithLabel returns a new Bar with a specific option.
 func (p Bar) WithLabel(s string) *Bar {
 	p.Label = s
diff --git a/interactive_multiselect_printer.go b/interactive_multiselect_printer.go
index 5f9a63af..b55a92c8 100644
--- a/interactive_multiselect_printer.go
+++ b/interactive_multiselect_printer.go
@@ -26,10 +26,7 @@ var (
 		Filter:         true,
 		KeySelect:      keys.Enter,
 		KeyConfirm:     keys.Tab,
-		Checkmark: Checkmark{
-			Checked:   "✓",
-			Unchecked: "✗",
-		},
+		Checkmark:      &ThemeDefault.Checkmark,
 	}
 )
 
@@ -44,7 +41,7 @@ type InteractiveMultiselectPrinter struct {
 	Selector       string
 	SelectorStyle  *Style
 	Filter         bool
-	Checkmark      Checkmark
+	Checkmark      *Checkmark
 
 	selectedOption        int
 	selectedOptions       []int
@@ -102,7 +99,7 @@ func (p InteractiveMultiselectPrinter) WithKeyConfirm(keyConfirm keys.KeyCode) *
 }
 
 // WithCheckmark sets the checkmark
-func (p InteractiveMultiselectPrinter) WithCheckmark(checkmark Checkmark) *InteractiveMultiselectPrinter {
+func (p InteractiveMultiselectPrinter) WithCheckmark(checkmark *Checkmark) *InteractiveMultiselectPrinter {
 	p.Checkmark = checkmark
 	return &p
 }
@@ -357,9 +354,9 @@ func (p *InteractiveMultiselectPrinter) renderSelectMenu() string {
 		}
 		var checkmark string
 		if p.isSelected(option) {
-			checkmark = fmt.Sprintf("[%s]", Green(p.Checkmark.Checked))
+			checkmark = fmt.Sprintf("[%s]", p.Checkmark.Checked)
 		} else {
-			checkmark = fmt.Sprintf("[%s]", Red(p.Checkmark.Unchecked))
+			checkmark = fmt.Sprintf("[%s]", p.Checkmark.Unchecked)
 		}
 		if i == p.selectedOption {
 			content += Sprintf("%s %s %s\n", p.renderSelector(), checkmark, option)
diff --git a/theme.go b/theme.go
index b02ac3e2..22466ea7 100644
--- a/theme.go
+++ b/theme.go
@@ -43,6 +43,10 @@ var (
 		BarLabelStyle:           Style{FgLightCyan},
 		BarStyle:                Style{FgCyan},
 		TimerStyle:              Style{FgGray},
+		Checkmark: Checkmark{
+			Checked:   Green("✓"),
+			Unchecked: Red("✗"),
+		},
 	}
 )
 
@@ -89,6 +93,7 @@ type Theme struct {
 	BoxTextStyle            Style
 	BarLabelStyle           Style
 	BarStyle                Style
+	Checkmark               Checkmark
 }
 
 // WithPrimaryStyle returns a new theme with overridden value.


Test patch:
diff --git a/interactive_multiselect_printer_test.go b/interactive_multiselect_printer_test.go
index 8f8cc030..e34d0c8e 100644
--- a/interactive_multiselect_printer_test.go
+++ b/interactive_multiselect_printer_test.go
@@ -63,6 +63,6 @@ func TestInteractiveMultiselectPrinter_WithKeyConfirm(t *testing.T) {
 }
 
 func TestInteractiveMultiselectPrinter_WithCheckmark(t *testing.T) {
-	p := pterm.DefaultInteractiveMultiselect.WithCheckmark(pterm.Checkmark{Checked: "+", Unchecked: "-"}).WithOptions([]string{"a", "b", "c"})
-	testza.AssertEqual(t, p.Checkmark, pterm.Checkmark{Checked: "+", Unchecked: "-"})
+	p := pterm.DefaultInteractiveMultiselect.WithCheckmark(&pterm.Checkmark{Checked: "+", Unchecked: "-"}).WithOptions([]string{"a", "b", "c"})
+	testza.AssertEqual(t, p.Checkmark, &pterm.Checkmark{Checked: "+", Unchecked: "-"})
 }