input

package
v1.9.0-test2 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2025 License: LGPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInputCanceled = eris.New("input canceled")
	ErrInvalidInput  = eris.New("invalid input")
)

Functions

This section is empty.

Types

type MockService

type MockService struct {
	mock.Mock
}

MockService provides a mock implementation of ServiceInterface for testing.

func (*MockService) Confirm

func (m *MockService) Confirm(ctx context.Context, prompt, defaultValue string) (bool, error)

Confirm mocks the Confirm method.

func (*MockService) Prompt

func (m *MockService) Prompt(ctx context.Context, prompt, defaultValue string) (string, error)

Prompt mocks the Prompt method.

func (*MockService) Select

func (m *MockService) Select(
	ctx context.Context,
	title, prompt string,
	options []string,
	defaultIndex int,
) (int, error)

Select mocks the Select method.

func (*MockService) SelectString

func (m *MockService) SelectString(
	ctx context.Context,
	title, prompt string,
	options []string,
	defaultValue string,
) (string, error)

SelectString mocks the SelectString method.

type Service

type Service struct {
	Input  io.Reader // Allows injection of different input sources for testing
	Output io.Writer // Allows injection of different output destinations for testing
}

Service implements the input interface using standard input/output.

func NewService

func NewService() Service

NewService creates a new input service with standard stdin/stdout.

func NewTestService

func NewTestService(input io.Reader, output io.Writer) Service

NewTestService creates a new input service for testing with custom input/output.

func (*Service) Confirm

func (s *Service) Confirm(ctx context.Context, prompt, defaultValue string) (bool, error)

Confirm asks for Y/n confirmation with default.

func (*Service) Prompt

func (s *Service) Prompt(ctx context.Context, prompt, defaultValue string) (string, error)

Prompt displays a prompt and returns user input.

func (*Service) Select

func (s *Service) Select(ctx context.Context, title, prompt string, options []string, defaultIndex int) (int, error)

Select allows user to select from multiple options by number.

func (*Service) SelectString

func (s *Service) SelectString(
	ctx context.Context,
	title, prompt string,
	options []string,
	defaultValue string,
) (string, error)

SelectString allows user to select from multiple options, returns the selected string.

type ServiceInterface

type ServiceInterface interface {
	// Prompt displays a prompt and returns user input
	Prompt(ctx context.Context, prompt, defaultValue string) (string, error)

	// Confirm asks for Y/n confirmation with default
	Confirm(ctx context.Context, prompt, defaultValue string) (bool, error)

	// Select allows user to select from multiple options by number
	Select(ctx context.Context, title, prompt string, options []string, defaultIndex int) (int, error)

	// SelectString allows user to select from multiple options, returns the selected string
	SelectString(ctx context.Context, title, prompt string, options []string, defaultValue string) (string, error)
}

ServiceInterface defines methods for handling user input.

type TestInputService

type TestInputService struct {
	Responses       []string // Predefined responses in order
	ResponseIndex   int      // Current index in responses
	ConfirmResponse bool     // Default confirm response
}

TestInputService provides a simple test implementation with predefined responses.

func NewTestInputService

func NewTestInputService(responses []string) *TestInputService

NewTestInputService creates a new test service with predefined responses.

func (*TestInputService) Confirm

func (t *TestInputService) Confirm(_ context.Context, _, _ string) (bool, error)

Confirm returns the predefined confirm response.

func (*TestInputService) Prompt

func (t *TestInputService) Prompt(_ context.Context, _, defaultValue string) (string, error)

Prompt returns the next predefined response.

func (*TestInputService) Select

func (t *TestInputService) Select(_ context.Context, _ string, options []string, defaultIndex int) (int, error)

Select returns the first option by default.

func (*TestInputService) SelectString

func (t *TestInputService) SelectString(
	_ context.Context,
	_ string,
	options []string,
	defaultValue string,
) (string, error)

SelectString returns the default value or first option.

Jump to

Keyboard shortcuts

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