tardy

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2016 License: BSD-3-Clause Imports: 5 Imported by: 0

README

Tardy is an easy to use, but highly configurable, CLI / terminal prompt library for Go.

Go Version Build Status Coverage GoDoc

 

Features

What can tardy do? Lots!

  • Simple, straightforward prompt mechanism
  • Per-prompt return values or catch-all so you can check all prompts at the end
  • Built in Prompt types for common use-cases
    • Open-ended string values
    • Yes / No values
    • Pick from list of possible values
  • Retry values that don't meet criteria or are not supplied (if not optional)
  • Optionality and default prompt values
  • Case sensitive (or insensitive) matching
  • Extensible Prompt struct so you're not constrained when you need a custom input type with the following features:
    • Prompt validation function
    • Value conversion function (from string to whatever you want)

Installing

To install, run:

go get -u github.com/goposse/tardy

You can then import tardy using:

import github.com/goposse/tardy

Getting started

This is a super simple overview. If you want to really understand Tardy, we suggest you check out the Tardy Guide.

Anyhow, let's run through a super simple example.

p := tardy.NewPrompter()
p.Prompt(tardy.SimplePrompt("What is your name?", tardy.Required, ""))
fmt.Println("Your name is:", p.IndexedValues[0])

When run, you will see a prompt. After entering a value you should see something like the following:

What is your name?:  John Smith
Your name is: John Smith

FAQ

Why should I use this and not ____?

Why not?

We designed this library because nothing else fit our needs. If it fits yours, cool. If not, we won't hold it against you if you use something else.

Give it a try and if you like it, let us know! Either way, we love feedback.

Has it been tested in production? Can I use it in production?

The code here has been written based on Posse's experiences with clients of all sizes. It has been production tested. That said, code is always evolving. We plan to keep on using it in production but we also plan to keep on improving it. If you find a bug, let us know!

Who the f*ck is Posse?

We're the best friggin mobile shop in NYC that's who. Hey, but we're biased. Our stuff is at http://goposse.com. Go check it out.

Outro

Credits

Tardy is sponsored, owned and maintained by Posse Productions LLC. Follow us on Twitter @goposse. Feel free to reach out with suggestions, ideas or to say hey.

Security

If you believe you have identified a serious security vulnerability or issue with Tardy, please report it as soon as possible to apps@goposse.com. Please refrain from posting it to the public issue tracker so that we have a chance to address it and notify everyone accordingly.

License

Tardy is released under a modified MIT license. See LICENSE for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Optionality

type Optionality int

Optionality - whether thing is optional or not optional (for better visibility)

const (
	// Required - is not optional
	Required Optionality = 1 << iota

	// NotRequired - is optional
	NotRequired
)

type Prompt

type Prompt struct {
	// Message - the prompt message
	Message string

	// ValueHint - textual hint to the user describing what they should enter
	ValueHint string

	// SecureEntry - whether or not what the user is typing should be visible
	SecureEntry bool

	// DefaultValue - the value that will be returned if no value is entered and the
	// entry is not required
	DefaultValue interface{}

	// Required - whether or not the entry is required
	Required Optionality

	// RetryIfNoMatch - should we re-ask for a value if no match / value was entered
	RetryIfNoMatch bool

	// FailIfNoMatch - should the entry fail if it doesn't pass validation?
	FailIfNoMatch bool

	// CaseSensitiveMatch - should we do a case sensitive check of acceptable values
	CaseSensitiveMatch bool

	// ValueConverter - logic to do conversion of the string entry to your preferred
	// output type
	ValueConverter PromptValueConverter

	// ValidationFunc - logic to validate the entry
	ValidationFunc PromptValueValidator
}

Prompt - an individual request to get back information via a prompt

func SimplePrompt

func SimplePrompt(message string, required Optionality, defaultValue string) Prompt

SimplePrompt - a simple prompt to get a value with no restrictions. allows for a default.

func SimpleSecurePrompt

func SimpleSecurePrompt(message string, required Optionality, defaultValue string) Prompt

SimpleSecurePrompt - a simple prompt to get a value with no restrictions. entry is capture without local echo (typing not visible). allows for a default.

func SingleValuePrompt

func SingleValuePrompt(message string, hint string, values []string, required Optionality, defaultValue string) Prompt

SingleValuePrompt - prompts for an answer from within a subset of valid answers. allows for a default.

func YesNoPrompt

func YesNoPrompt(message string, hint string, required Optionality, defaultValue bool) Prompt

YesNoPrompt - prompts for a yes or no answer. the final value will be a boolean. allows for a default.

type PromptValueConverter

type PromptValueConverter func(*Prompt, string) interface{}

PromptValueConverter - converts a provided value to a final value type

type PromptValueValidator

type PromptValueValidator func(*Prompt, string) (string, Validity)

PromptValueValidator - checks to see if the value provided matches the criteria

type Prompter

type Prompter struct {
	Reader *bufio.Reader

	// Values - map of all completed values keyed on the Prompt's Message value
	Values map[string]interface{}

	// IndexedValues - all completed values based on the order they were entered
	IndexedValues []interface{}

	// TrimSpace - should we trim leading and trailing spaces?
	TrimSpace bool

	// PromptSuffix - should we add a suffix to the end of the prompt message for
	// all messages
	PromptSuffix string
}

Prompter - the primary prompt controller

func NewPrompter

func NewPrompter() Prompter

NewPrompter - creates a new prompter instance

func (*Prompter) ClearValues

func (pmt *Prompter) ClearValues()

ClearValues - clear any currently tracked values

func (*Prompter) Do

func (pmt *Prompter) Do(prompts ...Prompt) []map[string]interface{}

Do - add a series of prompts in one go. will return an array containing a map of the value and validity values for each prompt (i.e.: { "value" : "..", "validity" : ".." }).

func (*Prompter) Prompt

func (pmt *Prompter) Prompt(prompt Prompt) (interface{}, Validity)

Prompt - prompt for a single entry and return the provided value and validity status

type Validity

type Validity int

Validity - whether thing is valid (for better visibility)

const (
	// IsNotValid - The value does not match the prompt criteria
	IsNotValid Validity = 1 << iota

	// IsValid - The value matched the prompt criteria
	IsValid
)

Jump to

Keyboard shortcuts

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