Skip to content

Commit

Permalink
feat: add custom delimiters to select printers (#545)
Browse files Browse the repository at this point in the history
  • Loading branch information
darkliquid committed Aug 1, 2023
1 parent 19261c5 commit 3466040
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 40 deletions.
39 changes: 23 additions & 16 deletions interactive_confirm_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,30 @@ import (
"atomicgo.dev/cursor"
"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"

"github.com/pterm/pterm/internal"
)

var (
// DefaultInteractiveConfirm is the default InteractiveConfirm printer.
// Pressing "y" will return true, "n" will return false.
// Pressing enter without typing "y" or "n" will return the configured default value (by default set to "no").
DefaultInteractiveConfirm = InteractiveConfirmPrinter{
DefaultValue: false,
DefaultText: "Please confirm",
TextStyle: &ThemeDefault.PrimaryStyle,
ConfirmText: "Yes",
ConfirmStyle: &ThemeDefault.SuccessMessageStyle,
RejectText: "No",
RejectStyle: &ThemeDefault.ErrorMessageStyle,
SuffixStyle: &ThemeDefault.SecondaryStyle,
}
)
// DefaultInteractiveConfirm is the default InteractiveConfirm printer.
// Pressing "y" will return true, "n" will return false.
// Pressing enter without typing "y" or "n" will return the configured default value (by default set to "no").
var DefaultInteractiveConfirm = InteractiveConfirmPrinter{
DefaultValue: false,
DefaultText: "Please confirm",
TextStyle: &ThemeDefault.PrimaryStyle,
ConfirmText: "Yes",
ConfirmStyle: &ThemeDefault.SuccessMessageStyle,
RejectText: "No",
RejectStyle: &ThemeDefault.ErrorMessageStyle,
SuffixStyle: &ThemeDefault.SecondaryStyle,
Delimiter: ": ",
}

// InteractiveConfirmPrinter is a printer for interactive confirm prompts.
type InteractiveConfirmPrinter struct {
DefaultValue bool
DefaultText string
Delimiter string
TextStyle *Style
ConfirmText string
ConfirmStyle *Style
Expand Down Expand Up @@ -93,6 +94,12 @@ func (p InteractiveConfirmPrinter) WithOnInterruptFunc(exitFunc func()) *Interac
return &p
}

// WithDelimiter sets the delimiter between the message and the input.
func (p InteractiveConfirmPrinter) WithDelimiter(delimiter string) *InteractiveConfirmPrinter {
p.Delimiter = delimiter
return &p
}

// Show shows the confirm prompt.
//
// Example:
Expand All @@ -111,7 +118,7 @@ func (p InteractiveConfirmPrinter) Show(text ...string) (bool, error) {
text = []string{p.DefaultText}
}

p.TextStyle.Print(text[0] + " " + p.getSuffix() + ": ")
p.TextStyle.Print(text[0] + " " + p.getSuffix() + p.Delimiter)
y, n := p.getShortHandles()

var interrupted bool
Expand Down
5 changes: 5 additions & 0 deletions interactive_confirm_printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ func TestInteractiveConfirmPrinter_WithDefaultText(t *testing.T) {
testza.AssertEqual(t, p.DefaultText, "default")
}

func TestInteractiveConfirmPrinter_WithDelimiter(t *testing.T) {
p := pterm.DefaultInteractiveConfirm.WithDelimiter(">>")
testza.AssertEqual(t, p.Delimiter, ">>")
}

func TestInteractiveConfirmPrinter_WithRejectStyle(t *testing.T) {
style := pterm.NewStyle(pterm.FgRed)
p := pterm.DefaultInteractiveConfirm.WithRejectStyle(style)
Expand Down
34 changes: 20 additions & 14 deletions interactive_continue_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ import (
"github.com/pterm/pterm/internal"
)

var (
// DefaultInteractiveContinue is the default InteractiveContinue printer.
// Pressing "y" will return yes, "n" will return no, "a" returns all and "s" returns stop.
// Pressing enter without typing any letter will return the configured default value (by default set to "yes", the fisrt option).
DefaultInteractiveContinue = InteractiveContinuePrinter{
DefaultValueIndex: 0,
DefaultText: "Do you want to continue",
TextStyle: &ThemeDefault.PrimaryStyle,
Options: []string{"yes", "no", "all", "cancel"},
OptionsStyle: &ThemeDefault.SuccessMessageStyle,
SuffixStyle: &ThemeDefault.SecondaryStyle,
}
)
// DefaultInteractiveContinue is the default InteractiveContinue printer.
// Pressing "y" will return yes, "n" will return no, "a" returns all and "s" returns stop.
// Pressing enter without typing any letter will return the configured default value (by default set to "yes", the fisrt option).
var DefaultInteractiveContinue = InteractiveContinuePrinter{
DefaultValueIndex: 0,
DefaultText: "Do you want to continue",
TextStyle: &ThemeDefault.PrimaryStyle,
Options: []string{"yes", "no", "all", "cancel"},
OptionsStyle: &ThemeDefault.SuccessMessageStyle,
SuffixStyle: &ThemeDefault.SecondaryStyle,
Delimiter: ": ",
}

// InteractiveContinuePrinter is a printer for interactive continue prompts.
type InteractiveContinuePrinter struct {
DefaultValueIndex int
DefaultText string
Delimiter string
TextStyle *Style
Options []string
OptionsStyle *Style
Expand Down Expand Up @@ -108,6 +108,12 @@ func (p InteractiveContinuePrinter) WithSuffixStyle(style *Style) *InteractiveCo
return &p
}

// WithDelimiter sets the delimiter between the message and the input.
func (p InteractiveContinuePrinter) WithDelimiter(delimiter string) *InteractiveContinuePrinter {
p.Delimiter = delimiter
return &p
}

// Show shows the continue prompt.
//
// Example:
Expand All @@ -121,7 +127,7 @@ func (p InteractiveContinuePrinter) Show(text ...string) (string, error) {
text = []string{p.DefaultText}
}

p.TextStyle.Print(text[0] + " " + p.getSuffix() + ": ")
p.TextStyle.Print(text[0] + " " + p.getSuffix() + p.Delimiter)

err := keyboard.Listen(func(keyInfo keys.Key) (stop bool, err error) {
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions interactive_continue_printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ func TestInteractiveContinuePrinter_WithDefaultText(t *testing.T) {
testza.AssertEqual(t, p.DefaultText, "default")
}

func TestInteractiveContinuePrinter_WithDelimiter(t *testing.T) {
p := pterm.DefaultInteractiveContinue.WithDelimiter(">>")
testza.AssertEqual(t, p.Delimiter, ">>")
}


func TestInteractiveContinuePrinter_CustomAnswers(t *testing.T) {
p := pterm.DefaultInteractiveContinue.WithOptions([]string{"next", "stop", "continue"})
tests := []struct {
Expand Down
26 changes: 16 additions & 10 deletions interactive_textinput_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import (
"github.com/pterm/pterm/internal"
)

var (
// DefaultInteractiveTextInput is the default InteractiveTextInput printer.
DefaultInteractiveTextInput = InteractiveTextInputPrinter{
DefaultText: "Input text",
TextStyle: &ThemeDefault.PrimaryStyle,
Mask: "",
}
)
// DefaultInteractiveTextInput is the default InteractiveTextInput printer.
var DefaultInteractiveTextInput = InteractiveTextInputPrinter{
DefaultText: "Input text",
Delimiter: ": ",
TextStyle: &ThemeDefault.PrimaryStyle,
Mask: "",
}

// InteractiveTextInputPrinter is a printer for interactive select menus.
type InteractiveTextInputPrinter struct {
TextStyle *Style
DefaultText string
Delimiter string
MultiLine bool
Mask string
OnInterruptFunc func()
Expand Down Expand Up @@ -63,6 +63,12 @@ func (p InteractiveTextInputPrinter) WithOnInterruptFunc(exitFunc func()) *Inter
return &p
}

// WithDelimiter sets the delimiter between the message and the input.
func (p InteractiveTextInputPrinter) WithDelimiter(delimiter string) *InteractiveTextInputPrinter {
p.Delimiter = delimiter
return &p
}

// Show shows the interactive select menu and returns the selected entry.
func (p InteractiveTextInputPrinter) Show(text ...string) (string, error) {
// should be the first defer statement to make sure it is executed last
Expand All @@ -77,9 +83,9 @@ func (p InteractiveTextInputPrinter) Show(text ...string) (string, error) {
}

if p.MultiLine {
areaText = p.TextStyle.Sprintfln("%s %s :", text[0], ThemeDefault.SecondaryStyle.Sprint("[Press tab to submit]"))
areaText = p.TextStyle.Sprintfln("%s %s %s", text[0], ThemeDefault.SecondaryStyle.Sprint("[Press tab to submit]"), p.Delimiter)
} else {
areaText = p.TextStyle.Sprintf("%s: ", text[0])
areaText = p.TextStyle.Sprintf("%s%s", text[0], p.Delimiter)
}
p.text = areaText
area, err := DefaultArea.Start(areaText)
Expand Down
5 changes: 5 additions & 0 deletions interactive_textinput_printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ func TestInteractiveTextInputPrinter_WithDefaultText(t *testing.T) {
testza.AssertEqual(t, p.DefaultText, "default")
}

func TestInteractiveTextInputPrinter_WithDelimiter(t *testing.T) {
p := pterm.DefaultInteractiveTextInput.WithDelimiter(">>")
testza.AssertEqual(t, p.Delimiter, ">>")
}

func TestInteractiveTextInputPrinter_WithMultiLine_true(t *testing.T) {
p := pterm.DefaultInteractiveTextInput.WithMultiLine()
testza.AssertTrue(t, p.MultiLine)
Expand Down

0 comments on commit 3466040

Please sign in to comment.