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: add custom delimiters to select printers #545

Merged
merged 2 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion interactive_confirm_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ var (
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
darkliquid marked this conversation as resolved.
Show resolved Hide resolved
ConfirmText string
ConfirmStyle *Style
Expand Down Expand Up @@ -93,6 +95,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 +119,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
27 changes: 17 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,13 @@ 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 +84,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