Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions ui/color_ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ func (ui *ColorUI) PrintTable(table Table) {
ui.parent.PrintTable(table)
}

func (ui *ColorUI) AskForText(label string) (string, error) {
return ui.parent.AskForText(label)
func (ui *ColorUI) AskForText(opts TextOpts) (string, error) {
return ui.parent.AskForText(opts)
}

func (ui *ColorUI) AskForChoice(label string, options []string) (int, error) {
return ui.parent.AskForChoice(label, options)
func (ui *ColorUI) AskForChoice(opts ChoiceOpts) (int, error) {
return ui.parent.AskForChoice(opts)
}

func (ui *ColorUI) AskForPassword(label string) (string, error) {
Expand Down
8 changes: 4 additions & 4 deletions ui/conf_ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ func (ui *ConfUI) PrintTable(table Table) {
ui.parent.PrintTable(table)
}

func (ui *ConfUI) AskForText(label string) (string, error) {
return ui.parent.AskForText(label)
func (ui *ConfUI) AskForText(opts TextOpts) (string, error) {
return ui.parent.AskForText(opts)
}

func (ui *ConfUI) AskForChoice(label string, options []string) (int, error) {
return ui.parent.AskForChoice(label, options)
func (ui *ConfUI) AskForChoice(opts ChoiceOpts) (int, error) {
return ui.parent.AskForChoice(opts)
}

func (ui *ConfUI) AskForPassword(label string) (string, error) {
Expand Down
11 changes: 6 additions & 5 deletions ui/fakes/fake_ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"sync"

types "github.com/cppforlife/go-cli-ui/ui"
. "github.com/cppforlife/go-cli-ui/ui/table"
)

Expand Down Expand Up @@ -93,24 +94,24 @@ func (ui *FakeUI) PrintTable(table Table) {
ui.Tables = append(ui.Tables, table)
}

func (ui *FakeUI) AskForText(label string) (string, error) {
func (ui *FakeUI) AskForText(opts types.TextOpts) (string, error) {
ui.mutex.Lock()
defer ui.mutex.Unlock()

ui.AskedTextLabels = append(ui.AskedTextLabels, label)
ui.AskedTextLabels = append(ui.AskedTextLabels, opts.Label)
answer := ui.AskedText[0]
ui.AskedText = ui.AskedText[1:]
return answer.Text, answer.Error
}

func (ui *FakeUI) AskForChoice(label string, options []string) (int, error) {
func (ui *FakeUI) AskForChoice(opts types.ChoiceOpts) (int, error) {
ui.mutex.Lock()
defer ui.mutex.Unlock()

ui.AskedChoiceCalled = true

ui.AskedChoiceLabel = label
ui.AskedChoiceOptions = options
ui.AskedChoiceLabel = opts.Label
ui.AskedChoiceOptions = opts.Choices

chosen := ui.AskedChoiceChosens[0]
ui.AskedChoiceChosens = ui.AskedChoiceChosens[1:]
Expand Down
8 changes: 4 additions & 4 deletions ui/indenting_ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ func (ui *IndentingUI) PrintTable(table Table) {
ui.parent.PrintTable(table)
}

func (ui *IndentingUI) AskForText(label string) (string, error) {
return ui.parent.AskForText(label)
func (ui *IndentingUI) AskForText(opts TextOpts) (string, error) {
return ui.parent.AskForText(opts)
}

func (ui *IndentingUI) AskForChoice(label string, options []string) (int, error) {
return ui.parent.AskForChoice(label, options)
func (ui *IndentingUI) AskForChoice(opts ChoiceOpts) (int, error) {
return ui.parent.AskForChoice(opts)
}

func (ui *IndentingUI) AskForPassword(label string) (string, error) {
Expand Down
4 changes: 2 additions & 2 deletions ui/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type UI interface {

PrintTable(Table)

AskForText(label string) (string, error)
AskForChoice(label string, options []string) (int, error)
AskForText(opts TextOpts) (string, error)
AskForChoice(opts ChoiceOpts) (int, error)
AskForPassword(label string) (string, error)

// AskForConfirmation returns error if user doesnt want to continue
Expand Down
4 changes: 2 additions & 2 deletions ui/json_ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ func (ui *JSONUI) PrintTable(table Table) {
ui.uiResp.Tables = append(ui.uiResp.Tables, resp)
}

func (ui *JSONUI) AskForText(_ string) (string, error) {
func (ui *JSONUI) AskForText(_ TextOpts) (string, error) {
panic("Cannot ask for input in JSON UI")
}

func (ui *JSONUI) AskForChoice(_ string, _ []string) (int, error) {
func (ui *JSONUI) AskForChoice(_ ChoiceOpts) (int, error) {
panic("Cannot ask for a choice in JSON UI")
}

Expand Down
4 changes: 2 additions & 2 deletions ui/json_ui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func TestJSONUI(t *testing.T) {
parentUI := &fakeui.FakeUI{}
ui := NewJSONUI(parentUI, NewRecordingLogger())

assert.Panics(t, func() { ui.AskForText("") })
assert.Panics(t, func() { ui.AskForText(TextOpts{}) })
})
})

Expand All @@ -328,7 +328,7 @@ func TestJSONUI(t *testing.T) {
parentUI := &fakeui.FakeUI{}
ui := NewJSONUI(parentUI, NewRecordingLogger())

assert.Panics(t, func() { ui.AskForChoice("", nil) })
assert.Panics(t, func() { ui.AskForChoice(ChoiceOpts{}) })
})
})

Expand Down
4 changes: 2 additions & 2 deletions ui/non_interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ func (ui *NonInteractiveUI) PrintTable(table Table) {
ui.parent.PrintTable(table)
}

func (ui *NonInteractiveUI) AskForText(label string) (string, error) {
func (ui *NonInteractiveUI) AskForText(opts TextOpts) (string, error) {
panic("Cannot ask for input in non-interactive UI")
}

func (ui *NonInteractiveUI) AskForChoice(label string, options []string) (int, error) {
func (ui *NonInteractiveUI) AskForChoice(opts ChoiceOpts) (int, error) {
panic("Cannot ask for a choice in non-interactive UI")
}

Expand Down
4 changes: 2 additions & 2 deletions ui/non_interactive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestNonInteractiveUI(t *testing.T) {
parentUI := &fakeui.FakeUI{}
ui := NewNonInteractiveUI(parentUI)

assert.Panics(t, func() { ui.AskForText("") })
assert.Panics(t, func() { ui.AskForText(TextOpts{}) })
})
})

Expand All @@ -109,7 +109,7 @@ func TestNonInteractiveUI(t *testing.T) {
parentUI := &fakeui.FakeUI{}
ui := NewNonInteractiveUI(parentUI)

assert.Panics(t, func() { ui.AskForChoice("", nil) })
assert.Panics(t, func() { ui.AskForChoice(ChoiceOpts{}) })
})
})

Expand Down
8 changes: 4 additions & 4 deletions ui/non_tty_ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ func (ui *NonTTYUI) PrintTable(table Table) {
ui.parent.PrintTable(table)
}

func (ui *NonTTYUI) AskForText(label string) (string, error) {
return ui.parent.AskForText(label)
func (ui *NonTTYUI) AskForText(opts TextOpts) (string, error) {
return ui.parent.AskForText(opts)
}

func (ui *NonTTYUI) AskForChoice(label string, options []string) (int, error) {
return ui.parent.AskForChoice(label, options)
func (ui *NonTTYUI) AskForChoice(opts ChoiceOpts) (int, error) {
return ui.parent.AskForChoice(opts)
}

func (ui *NonTTYUI) AskForPassword(label string) (string, error) {
Expand Down
8 changes: 4 additions & 4 deletions ui/padding_ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ func (ui *PaddingUI) PrintTable(table Table) {
ui.parent.PrintTable(table)
}

func (ui *PaddingUI) AskForText(label string) (string, error) {
func (ui *PaddingUI) AskForText(opts TextOpts) (string, error) {
ui.padBefore(paddingUIModeAskText)
return ui.parent.AskForText(label)
return ui.parent.AskForText(opts)
}

func (ui *PaddingUI) AskForChoice(label string, options []string) (int, error) {
func (ui *PaddingUI) AskForChoice(opts ChoiceOpts) (int, error) {
ui.padBefore(paddingUIModeAuto)
return ui.parent.AskForChoice(label, options)
return ui.parent.AskForChoice(opts)
}

func (ui *PaddingUI) AskForPassword(label string) (string, error) {
Expand Down
14 changes: 14 additions & 0 deletions ui/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ui

// TextOpts Asking for text options
type TextOpts struct {
Label string
DefaultValue string
}

// ChoiceOpts asking for choice options
type ChoiceOpts struct {
Label string
DefaultValue string
Choices []string
}
27 changes: 19 additions & 8 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,27 +96,38 @@ func (ui *WriterUI) PrintTable(table Table) {
}
}

func (ui *WriterUI) AskForText(label string) (string, error) {
var text string
func (ui *WriterUI) AskForText(opts TextOpts) (string, error) {
text := opts.DefaultValue

err := interact.NewInteraction(label).Resolve(&text)
err := interact.NewInteraction(opts.Label).Resolve(&text)
if err != nil {
return "", fmt.Errorf("Asking for text: %s", err)
}

return text, nil
}

func (ui *WriterUI) AskForChoice(label string, options []string) (int, error) {
var choices []interact.Choice
func (ui *WriterUI) AskForChoice(opts ChoiceOpts) (int, error) {
var (
choices []interact.Choice
defaultMatchingWithChoices bool
chosen int
)

for i, opt := range options {
for i, opt := range opts.Choices {
if opt == opts.DefaultValue {
chosen = i
defaultMatchingWithChoices = true
}
choices = append(choices, interact.Choice{Display: opt, Value: i})
}

var chosen int
if !defaultMatchingWithChoices && opts.DefaultValue != "" {
return 0, fmt.Errorf("Default value: %s should match with one of the choices: %s",
opts.DefaultValue, opts.Choices)
}

err := interact.NewInteraction(label, choices...).Resolve(&chosen)
err := interact.NewInteraction(opts.Label, choices...).Resolve(&chosen)
if err != nil {
return 0, fmt.Errorf("Asking for choice: %s", err)
}
Expand Down