Documentation ¶
Overview ¶
Package prompter provides various methods for prompting the user with questions for input.
Index ¶
- type FileReader
- type FileWriter
- type Prompter
- func (p *Prompter) Confirm(prompt string, defaultValue bool) (bool, error)
- func (p *Prompter) Input(prompt, defaultValue string) (string, error)
- func (p *Prompter) MultiSelect(prompt string, defaultValues, options []string) ([]int, error)
- func (p *Prompter) Password(prompt string) (string, error)
- func (p *Prompter) Select(prompt, defaultValue string, options []string) (int, error)
- type PrompterMock
- func (m *PrompterMock) Confirm(prompt string, defaultValue bool) (bool, error)
- func (m *PrompterMock) Input(prompt, defaultValue string) (string, error)
- func (m *PrompterMock) MultiSelect(prompt string, defaultValues, options []string) ([]int, error)
- func (m *PrompterMock) Password(prompt string) (string, error)
- func (m *PrompterMock) RegisterConfirm(prompt string, stub func(_ string, _ bool) (bool, error))
- func (m *PrompterMock) RegisterInput(prompt string, stub func(_, _ string) (string, error))
- func (m *PrompterMock) RegisterMultiSelect(prompt string, d, opts []string, ...)
- func (m *PrompterMock) RegisterPassword(prompt string, stub func(string) (string, error))
- func (m *PrompterMock) RegisterSelect(prompt string, opts []string, stub func(_, _ string, _ []string) (int, error))
- func (m *PrompterMock) Select(prompt, defaultValue string, options []string) (int, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FileReader ¶
FileReader provides a minimal readable interface for stdin.
type FileWriter ¶
FileWriter provides a minimal writable interface for stdout and stderr.
type Prompter ¶
type Prompter struct {
// contains filtered or unexported fields
}
Prompter provides methods for prompting the user.
Example ¶
term := term.FromEnv() in, ok := term.In().(*os.File) if !ok { log.Fatal("error casting to file") } out, ok := term.Out().(*os.File) if !ok { log.Fatal("error casting to file") } errOut, ok := term.ErrOut().(*os.File) if !ok { log.Fatal("error casting to file") } prompter := New(in, out, errOut) response, err := prompter.Confirm("Shall we play a game", true) if err != nil { log.Fatal(err) } fmt.Println(response)
Output:
func New ¶
func New(stdin FileReader, stdout FileWriter, stderr FileWriter) *Prompter
New instantiates a new Prompter.
func (*Prompter) MultiSelect ¶
MultiSelect prompts the user to select multiple options from a list of options.
type PrompterMock ¶
type PrompterMock struct {
// contains filtered or unexported fields
}
PrompterMock provides stubbed out methods for prompting the user for use in tests. PrompterMock has a superset of the methods on Prompter so they both can satisfy the same interface.
A basic example of how PrompterMock can be used:
type ConfirmPrompter interface { Confirm(string, bool) (bool, error) } func PlayGame(prompter ConfirmPrompter) (int, error) { confirm, err := prompter.Confirm("Shall we play a game", true) if err != nil { return 0, err } if confirm { return 1, nil } return 2, nil } func TestPlayGame(t *testing.T) { expectedOutcome := 1 mock := NewMock(t) mock.RegisterConfirm("Shall we play a game", func(prompt string, defaultValue bool) (bool, error) { return true, nil }) outcome, err := PlayGame(mock) if err != nil { t.Fatalf("unexpected error: %v", err) } if outcome != expectedOutcome { t.Errorf("expected %q, got %q", expectedOutcome, outcome) } }
func (*PrompterMock) Confirm ¶
func (m *PrompterMock) Confirm(prompt string, defaultValue bool) (bool, error)
Confirm prompts the user to confirm a yes/no question.
func (*PrompterMock) Input ¶
func (m *PrompterMock) Input(prompt, defaultValue string) (string, error)
Input prompts the user to input a single-line string.
func (*PrompterMock) MultiSelect ¶
func (m *PrompterMock) MultiSelect(prompt string, defaultValues, options []string) ([]int, error)
MultiSelect prompts the user to select multiple options from a list of options.
func (*PrompterMock) Password ¶
func (m *PrompterMock) Password(prompt string) (string, error)
Password prompts the user to input a single-line string without echoing the input.
func (*PrompterMock) RegisterConfirm ¶
RegisterConfirm records that a Confirm prompt should be called.
func (*PrompterMock) RegisterInput ¶
func (m *PrompterMock) RegisterInput(prompt string, stub func(_, _ string) (string, error))
RegisterInput records that an Input prompt should be called.
func (*PrompterMock) RegisterMultiSelect ¶
func (m *PrompterMock) RegisterMultiSelect(prompt string, d, opts []string, stub func(_ string, _, _ []string) ([]int, error))
RegisterMultiSelect records that a MultiSelect prompt should be called.
func (*PrompterMock) RegisterPassword ¶
func (m *PrompterMock) RegisterPassword(prompt string, stub func(string) (string, error))
RegisterPassword records that a Password prompt should be called.
func (*PrompterMock) RegisterSelect ¶
func (m *PrompterMock) RegisterSelect(prompt string, opts []string, stub func(_, _ string, _ []string) (int, error))
RegisterSelect records that a Select prompt should be called.