storyprompt

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2015 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrContinue can be returned from SelectStory function
	// to show the same dialog again.
	ErrContinue = errors.New("continue")

	// ErrReturn can be returned from SelectStory function
	// to return to the dialog one level higher.
	ErrReturn = errors.New("return")

	// ErrAbort can be returned from SelectStory function
	// to abort the dialog with panicking using prompt.ErrCanceled.
	ErrAbort = errors.New("abort")
)

Functions

func ListStories

func ListStories(stories []common.Story, w io.Writer) error

Types

type Dialog added in v0.17.0

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

Dialog represents a dialog to be used to let the user pick a single story from the list.

func NewDialog added in v0.17.0

func NewDialog() *Dialog

NewDialog creates and returns a new dialog with no options set.

func (*Dialog) NewSubdialog added in v0.17.0

func (dialog *Dialog) NewSubdialog() *Dialog

NewSubdialog can be used to create a new dialog based on the current dialog. The option list is empty again, just the dialog depth is inherited.

func (*Dialog) PushOptions added in v0.17.0

func (dialog *Dialog) PushOptions(opts ...*DialogOption)

PushOptions can be used to add options to the option chain.

The dialog will try to find the matching option based on the order the options are matching, so in case there is an option matching any input pushed as the first option, no other option body will ever be executed.

func (*Dialog) Run added in v0.17.0

func (dialog *Dialog) Run(stories []common.Story) (common.Story, error)

Run starts the dialog after the options are set. It uses the given story list to prompt the user for a story using the given options.

type DialogOption added in v0.17.0

type DialogOption struct {
	// Description is the text the user is presented when the option is active.
	// The slice represents the lines of text printed to the user.
	Description []string

	// IsActive is a function that decides whether the option is active for the dialog.
	// When the option is not active, it is not presented to the user. It is handy
	// to limit the options when e.g. there are no stories and so on.
	IsActive func(stories []common.Story, depth int) bool

	// MatchesInput is a function that decides whether SelectStory should be executed.
	// It basically checks the input and when it matches, it returns true, otherwise
	// the input is passed to the next option in the chain.
	MatchesInput func(input string, stories []common.Story) bool

	// SelectStory represents the option body, the task being to pick a single story
	// from the given story list. The function can be pretty much anything
	// as long as it matches the signature. It can run a subdialog, filter stories etc.
	SelectStory func(input string, stories []common.Story, current *Dialog) (common.Story, error)
}

DialogOption represents a single option the user has when presented with a dialog. It represents a single input choice and the associated action.

func NewFilterOption added in v0.17.0

func NewFilterOption() *DialogOption

NewFilterOption returns an option that can be used to filter stories by matching the title against the given regexp.

func NewIndexOption added in v0.17.0

func NewIndexOption() *DialogOption

NewIndexOption returns an option that can be used to choose a story by its index.

func NewReturnOrAbortOptions added in v0.17.0

func NewReturnOrAbortOptions() []*DialogOption

NewReturnOrAbortOptions returns a set of options that handle

  • press Enter -> return one level up
  • insert 'q' -> abort the dialog

Jump to

Keyboard shortcuts

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