dynamic

package
v0.0.0-...-8b058bf Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Info

type Info struct {
	// The Type represents a valid prompt and must be mappable to the Prompt enum via the EnumType() method.
	Type string
	// The Message is an arbitrary string that will be used for the prompt message.
	Message string
	// Dynamic prompts can include zero or more options which alter the behavior of the prompt.
	Options []PromptOption
}

An Info object is used to dynamically build a prompt from data, enabling the creation of prompts defined outside of the compiled code itself.

func (Info) EnumType

func (info Info) EnumType() (prompt Prompt)

The EnumType() method returns the valid prompt type that the info maps to. If the specified string does not map to any valid prompt type, it returns PromptUnknown. This method is case insensitive.

type Model

type Model struct {
	// Which prompt is active
	ActiveType Prompt
	// The model for the selection prompt
	Selection *selection.Model
	// The model for the text input prompt
	TextInput *textinput.Model
	// The model for the confirmation prompt
	Confirmation *confirmation.Model
}

The Model for a dynamic prompt is able to display confirmation, selection, and text input prompts from data without the developer knowing ahead of time which type of prompt will be needed. This allows a program to take advantage of prompts defined in data outside of the go code itself.

func New

func New(info Info) *Model

The New function takes the data for dynamic prompt info and returns a dynamic prompt model configured per the values and options defined in the Info object.

func (*Model) Init

func (model *Model) Init() tea.Cmd

When the Model is initialized, it passes straight through to appropriate submodel for the prompt.

func (*Model) Update

func (model *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd)

When the Model is updated, it passes straight through to the appropriate submodel for the prompt.

func (*Model) View

func (model *Model) View() string

When the Model is viewed, it passes straight through to the appropriate submodel for the prompt. If no prompt is active or the prompt type could not be determined, it displays an error.

type Option

type Option int

The Option enum maps an option string to a defined behavior for the dynamic prompt.

const (
	// The specified string did not map to a valid option
	OptionUnknown Option = iota
	// The option sets the default value for a confirmation prompt to yes, no, or undecided.
	ConfirmationDefault
	// The option adds the value list to the selection prompt as valid choices. Use this when the choices are a list of
	// non-complex objects, like strings or integers.
	SelectionChoiceSimple
	// The option adds the value list to the selection prompt as valid choices. Use this when the choices are a list of
	// complex objects as maps; to use this option, each choice in the data must include a name key with a string value
	// for identifying the choice.
	SelectionChoiceComplex
)

func (Option) String

func (option Option) String() (value string)

Returns the string representation of an Option enum

type Prompt

type Prompt int

The Prompt enum declares the types of prompts that the dynamic model can handle.

const (
	// Unknown prompts go unhandled, erroring.
	PromptUnknown Prompt = iota
	// A confirmation prompt asks a user a yes/no question
	Confirmation
	// A selection prompt has the user select one choice from a list of choices
	Selection
	// A text input prompt has the user enter a string of text
	TextInput
)

func (Prompt) String

func (prompt Prompt) String() (value string)

Returns the string representation of a Prompt enum

type PromptOption

type PromptOption struct {
	// The Type must be a string that maps to a valid Option via the EnumType() method.
	Type string
	// The Value can be anything but is most commonly a string, a map[string]any, or a slice of either.
	Value any
}

Prompt Options must declare a type and a value. They are used for changing the behavior of a dynamic prompt.

func (PromptOption) ConfirmationOptions

func (option PromptOption) ConfirmationOptions() (options []confirmer.Option)

The ConfirmationOptions method introspects on a PromptOption to return a slice of options to use when creating a confirmation prompt.

If the prompt option is for a confirmation default, it sets the prompt's default value to Yes (if the value in the data is true or "yes"), No (if the value in the data is false or "no"), or Undecided (if the value is anything else).

func (PromptOption) EnumType

func (option PromptOption) EnumType() (modifier Option)

The EnumType method maps the string specified in the data for a dynamic prompt's options to an Option enum. If the specified string cannot be mapped, it returns OptionUnknown and is ignored.

func (PromptOption) SelectionOptions

func (option PromptOption) SelectionOptions() (options []selector.Option)

The SelectionOptions method introspects on a PromptOption to return a slice of options to use when creating a selection prompt.

If the prompt option is for a simple selection choice, it appends the value of the prompt option to the list of valid choices.

If the prompt option is for a complex selection choice, it returns options which:

1. Append the slice of choices under the "choices" key in the option's value as valid choices 2. Add a name function to the prompt's extended template funcs, returning the value of the name key (as specified in the "name" key of the option's value) from each choice when rendering the choice in the prompt 3. Add a filter function to enable users to type to filter the available choices, using the name key again 4. Specifies the alternate result template to ensure the result is displayed using the name and not a splat of the choice's values as a string.

func (PromptOption) TextInputOptions

func (option PromptOption) TextInputOptions() (options []texter.Option)

The TextInputOptions method introspects on a PromptOption to return a slice of options to use when creating a textinput prompt.

No options are yet implemented.

Jump to

Keyboard shortcuts

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