Skip to content

Commit

Permalink
Merge pull request #432 from pterm/feat/checkmark/theme-support
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed Jan 5, 2023
2 parents e73ea68 + a16c45e commit 681e947
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
Expand Up @@ -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))
}
12 changes: 6 additions & 6 deletions 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

Expand All @@ -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
Expand Down
13 changes: 5 additions & 8 deletions interactive_multiselect_printer.go
Expand Up @@ -26,10 +26,7 @@ var (
Filter: true,
KeySelect: keys.Enter,
KeyConfirm: keys.Tab,
Checkmark: Checkmark{
Checked: "✓",
Unchecked: "✗",
},
Checkmark: &ThemeDefault.Checkmark,
}
)

Expand All @@ -44,7 +41,7 @@ type InteractiveMultiselectPrinter struct {
Selector string
SelectorStyle *Style
Filter bool
Checkmark Checkmark
Checkmark *Checkmark

selectedOption int
selectedOptions []int
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions interactive_multiselect_printer_test.go
Expand Up @@ -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: "-"})
}
5 changes: 5 additions & 0 deletions theme.go
Expand Up @@ -43,6 +43,10 @@ var (
BarLabelStyle: Style{FgLightCyan},
BarStyle: Style{FgCyan},
TimerStyle: Style{FgGray},
Checkmark: Checkmark{
Checked: Green("✓"),
Unchecked: Red("✗"),
},
}
)

Expand Down Expand Up @@ -89,6 +93,7 @@ type Theme struct {
BoxTextStyle Style
BarLabelStyle Style
BarStyle Style
Checkmark Checkmark
}

// WithPrimaryStyle returns a new theme with overridden value.
Expand Down

0 comments on commit 681e947

Please sign in to comment.