interact

package module
v0.0.0-...-720596b Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2015 License: MIT Imports: 5 Imported by: 9

README

Interact

A Golang utility belt for interacting with the user over a CLI

Build Status Coverage GoDoc

Example interaction

Code like this:

actor := interact.NewActor(os.Stdin, os.Stdout)

message := "Please enter something that's not empty"
notEmpty, err := actor.Prompt(message, checkNotEmpty)
if err != nil {
  log.Fatal(err)
}
message = "Please enter a positive number"
n1, err := actor.PromptAndRetry(message, checkNotEmpty, checkIsAPositiveNumber)
if err != nil {
  log.Fatal(err)
}
message = "Please enter another positive number"
n2, err := actor.PromptOptionalAndRetry(message, "7", checkNotEmpty, checkIsAPositiveNumber)
if err != nil {
  log.Fatal(err)
}
fmt.Printf("Thanks! (%s, %s, %s)\n", notEmpty, n1, n2)

Can create an interaction like this:

asciicast

For a more comprehensive example see the example test.

Documentation

Overview

Package interact is a utility belt for interacting with the user over a CLI

Example
package main

import (
	"bytes"
	"errors"
	"fmt"
	"log"
	"os"
	"strconv"

	"github.com/deiwin/interact"
)

// First let's declare some simple input validators
var (
	checkNotEmpty = func(input string) error {
		// note that the inputs provided to these checks are already trimmed
		if input == "" {
			return errors.New("Input should not be empty!")
		}
		return nil
	}
	checkIsAPositiveNumber = func(input string) error {
		if n, err := strconv.Atoi(input); err != nil {
			return err
		} else if n < 0 {
			return errors.New("The number can not be negative!")
		}
		return nil
	}
)

func main() {
	var userInput bytes.Buffer
	var b = NewTestBuffer(&userInput, os.Stdout)
	// Normally you would initiate the actor with os.Stdin and os.Stdout, but to
	// make this example test work nice we need to use something different
	actor := interact.NewActor(b, b)

	// A simple prompt for non-empty input
	userInput.WriteString("hello\n") // To keep the test simple we have to provide user inputs up front
	if result, err := actor.Prompt("Please enter something that's not empty", checkNotEmpty); err != nil {
		log.Fatal(err)
	} else if result != "hello" {
		log.Fatalf("Expected 'hello', got '%s'", result)
	}

	// A more complex example with the user retrying
	userInput.WriteString("-2\ny\n5\n")
	if result, err := actor.PromptAndRetry("Please enter a positive number", checkNotEmpty, checkIsAPositiveNumber); err != nil {
		log.Fatal(err)
	} else if result != "5" {
		log.Fatalf("Expected '5', got '%s'", result)
	}

	// An example with the user retrying and then opting to use the default value
	userInput.WriteString("-2\ny\n\n")
	if result, err := actor.PromptOptionalAndRetry("Please enter another positive number", "7", checkNotEmpty, checkIsAPositiveNumber); err != nil {
		log.Fatal(err)
	} else if result != "7" {
		log.Fatalf("Expected '7', got '%s'", result)
	}

	// This will force the last user input to be printed as well
	fmt.Fprint(b, "")

}
Output:

Please enter something that's not empty: hello
Please enter a positive number: -2
The number can not be negative!
Do you want to try again? [y/N]: y
Please enter a positive number: 5
Please enter another positive number: (7) -2
The number can not be negative!
Do you want to try again? [y/N]: y
Please enter another positive number: (7)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Actor

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

An Actor provides methods to interact with the user

func NewActor

func NewActor(rd io.Reader, w io.Writer) Actor

NewActor creates a new Actor instance with the specified io.Reader

func (Actor) Confirm

func (a Actor) Confirm(message string, def ConfirmDefault) (confirmed bool, err error)

Confirm provides the message to the user and asks yes or no. If the user doesn't select either of the possible answers they will be prompted to answer again until they do

func (Actor) Prompt

func (a Actor) Prompt(message string, checks ...InputCheck) (string, error)

Prompt asks the user for input and performs the list of added checks on the provided input. If any of the checks fail, the error will be returned.

func (Actor) PromptAndRetry

func (a Actor) PromptAndRetry(message string, checks ...InputCheck) (string, error)

PromptAndRetry asks the user for input and performs the list of added checks on the provided input. If any of the checks fail to pass the error will be displayed to the user and they will then be asked if they want to try again. If the user does not want to retry the program will return an error.

func (Actor) PromptOptional

func (a Actor) PromptOptional(message, defaultOption string, checks ...InputCheck) (string, error)

PromptOptional works exactly like Prompt, but also has a default option which will be used instead if the user simply presses enter.

func (Actor) PromptOptionalAndRetry

func (a Actor) PromptOptionalAndRetry(message, defaultOption string, checks ...InputCheck) (string, error)

PromptOptionalAndRetry works exactly like GetInputAndRetry, but also has a default option which will be used instead if the user simply presses enter.

type ConfirmDefault

type ConfirmDefault int

ConfirmDefault specifies what an empty user input defaults to

const (
	ConfirmDefaultToYes ConfirmDefault = iota
	ConfirmDefaultToNo
	ConfirmNoDefault
)

Possible options for what an empty user input defaults to

type InputCheck

type InputCheck func(string) error

InputCheck specifies the function signature for an input check

Jump to

Keyboard shortcuts

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