prompter

package
v2.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2024 License: MIT Imports: 7 Imported by: 4

Documentation

Overview

Package prompter provides various methods for prompting the user with questions for input.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileReader

type FileReader interface {
	io.Reader
	Fd() uintptr
}

FileReader provides a minimal readable interface for stdin.

type FileWriter

type FileWriter interface {
	io.Writer
	Fd() uintptr
}

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) Confirm

func (p *Prompter) Confirm(prompt string, defaultValue bool) (bool, error)

Confirm prompts the user to confirm a yes/no question.

func (*Prompter) Input

func (p *Prompter) Input(prompt, defaultValue string) (string, error)

Input prompts the user to input a single-line string.

func (*Prompter) MultiSelect

func (p *Prompter) MultiSelect(prompt string, defaultValues, options []string) ([]int, error)

MultiSelect prompts the user to select multiple options from a list of options.

func (*Prompter) Password

func (p *Prompter) Password(prompt string) (string, error)

Password prompts the user to input a single-line string without echoing the input.

func (*Prompter) Select

func (p *Prompter) Select(prompt, defaultValue string, options []string) (int, error)

Select prompts the user to select an option 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 NewMock

func NewMock(t *testing.T) *PrompterMock

NewMock instantiates a new PrompterMock.

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

func (m *PrompterMock) RegisterConfirm(prompt string, stub func(_ string, _ bool) (bool, error))

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.

func (*PrompterMock) Select

func (m *PrompterMock) Select(prompt, defaultValue string, options []string) (int, error)

Select prompts the user to select an option from a list of options.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL