responder

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2023 License: MIT Imports: 10 Imported by: 3

Documentation

Overview

Package responder provides a means of prompting for values and reading from the terminal. The terminal device will be put into a raw mode so that you can read single characters. The package offers a standard help feature and allows the caller to specify default values for the prompted value. The value entered will be checked against the list of valid entries.

Example
package main

import (
	"fmt"

	"github.com/nickwells/cli.mod/cli/responder"
)

func main() {
	r := responder.NewOrPanic(
		"Question",
		map[rune]string{
			'y': "to show differences",
			'n': "to skip this file",
			'q': "to quit",
		},
		responder.SetDefault('y'),
	)

	for {
		response := r.GetResponseOrDie()
		fmt.Println()

		switch response {
		case 'y':
			fmt.Println("Yes!")
		case 'n':
			fmt.Println("No.")
		case 'q':
			break
		}
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FixedResponse added in v1.1.0

type FixedResponse struct {
	Response rune
	Err      error
}

FixedResponse always returns the given response. This is expected to be useful for testing. Note that there are no checks made of the Response and so it is possible to have a response that cannot be made from the standard Responder such as an uppercase value or a whitespace character.

func (FixedResponse) GetResponse added in v1.1.0

func (fr FixedResponse) GetResponse() (rune, error)

GetResponse returns the fixed responses

func (FixedResponse) GetResponseIndent added in v1.1.0

func (fr FixedResponse) GetResponseIndent(_, _ int) (rune, error)

GetResponseIndent returns the fixed responses

func (FixedResponse) GetResponseIndentOrDie added in v1.1.0

func (fr FixedResponse) GetResponseIndentOrDie(_, _ int) rune

GetResponseIndentOrDie returns the fixed responses. It will exit if the Err field is not nil.

func (FixedResponse) GetResponseOrDie added in v1.1.0

func (fr FixedResponse) GetResponseOrDie() rune

GetResponseOrDie returns the fixed responses. It will exit if the Err field is not nil.

type R

type R struct {
	// contains filtered or unexported fields
}

R holds the details needed to collect and validate a response

func New

func New(
	prompt string,
	responses map[rune]string,
	opts ...RespOptFunc,
) (*R, error)

New creates a responder and verifies that it is correct

func NewOrPanic

func NewOrPanic(
	prompt string,
	responses map[rune]string,
	opts ...RespOptFunc,
) *R

NewOrPanic creates a new responder and panics if there are any errors

func (R) GetResponse

func (r R) GetResponse() (response rune, err error)

GetResponse will print the prompt and read a single rune from standard input. It will check that the rune is a valid response. If it is not in the set of valid responses it will print an error message and reprompt. It will do this maxReprompts times before returning an error.

If an error is detected the response returned will be the unicode ReplacementChar.

func (R) GetResponseIndent added in v1.1.0

func (r R) GetResponseIndent(first, second int) (response rune, err error)

GetResponseIndent behaves as GetResponse but the indents are taken from the parameters rather than the responder.

func (R) GetResponseIndentOrDie added in v1.1.0

func (r R) GetResponseIndentOrDie(first, second int) rune

GetResponseIndentOrDie calls GetResponseIndent to get the response but if there is an error it will print it and exit with status 1.

func (R) GetResponseOrDie

func (r R) GetResponseOrDie() rune

GetResponseOrDie calls GetResponse to get the response but if there is an error it will print it and exit with status 1.

func (R) PrintHelp

func (r R) PrintHelp()

PrintHelp prints the help message.

func (R) PrintHelpIndent added in v1.1.0

func (r R) PrintHelpIndent(indent int)

PrintHelpIndent prints the help message.

func (R) PrintPrompt

func (r R) PrintPrompt()

PrintPrompt prints the prompt and any valid responses.

Example (NoDefault)

This example shows the text that will be printed to prompt the user to respond

package main

import (
	"github.com/nickwells/cli.mod/cli/responder"
)

func main() {
	r := responder.NewOrPanic(
		"Delete File",
		map[rune]string{
			'y': "delete the file",
			'n': "leave the file alone",
		},
	)
	r.PrintPrompt()
}
Output:

Delete File? (n/y/?):
Example (WithDefault)

This example shows the text that will be printed to prompt the user to respond. In this example a default response is given

package main

import (
	"github.com/nickwells/cli.mod/cli/responder"
)

func main() {
	r := responder.NewOrPanic(
		"Delete File",
		map[rune]string{
			'y': "delete the file",
			'n': "leave the file alone",
		},
		responder.SetDefault('y'),
	)
	r.PrintPrompt()
}
Output:

Delete File? ([y]/n/?):

func (R) PrintValidResponses

func (r R) PrintValidResponses()

PrintValidResponses prints the valid response runes separated by a slash.

A rune matching the default is shown in brackets (like so: [y]).

type RespOptFunc

type RespOptFunc func(*R) error

RespOptFunc is a function which can be passed to the New function to set optional parts of the R

func SetDefault

func SetDefault(d rune) RespOptFunc

SetDefault sets the default value for a Responder

func SetIndents

func SetIndents(indentFirst, indent int) RespOptFunc

SetIndents sets the indents for the first and subsequent lines of output

func SetMaxReprompts

func SetMaxReprompts(max int) RespOptFunc

SetMaxReprompts sets the maximum number of times that the user will be reprompted for a valid response before reporting an error. The value must be greater than 0

type Responder added in v1.1.0

type Responder interface {
	GetResponse() (rune, error)
	GetResponseOrDie() rune
	GetResponseIndent(int, int) (rune, error)
	GetResponseIndentOrDie(int, int) rune
}

Responder describes the methods offered to get a response

Jump to

Keyboard shortcuts

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