interact: github.com/deiwin/interact Index | Examples | Files

package interact

import "github.com/deiwin/interact"

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

Code:

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, "")

}

Index

Examples

Package Files

actor.go confirm.go input.go

type Actor Uses

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

An Actor provides methods to interact with the user

func NewActor Uses

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

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

func (Actor) Confirm Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

type InputCheck func(string) error

InputCheck specifies the function signature for an input check

Package interact imports 5 packages (graph) and is imported by 4 packages. Updated 2018-01-15. Refresh now. Tools for package owners.