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

feat(theme): added theme support for checkmarks #432

Merged
merged 6 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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