selector

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: 6 Imported by: 0

Documentation

Index

Constants

View Source
const ResultTemplateByName = `
{{- print .Prompt " " (Final (Name .FinalChoice)) "\n" -}}
`

A helper template for displaying the result of a selection prompt when the choices are structs. The name function is called to determine the string for displaying the choice instead of splatting the struct as a string.

View Source
const ResultTemplateDefault = `
{{- print .Prompt " " (Final .FinalChoice) "\n" -}}
`

The default template for displaying the result of the selection prompt, displaying the message followed by the choice

View Source
const TemplateDefault = `` /* 858-byte string literal not displayed */

The default template for selection; shows the bolded prompt message followed on the next line by the filter prompt (if the prompt is using a filter) and then a blank line before the selection list. If the list is paginated and the user can scroll up, a "⇡" is inserted immediately before the list. If the list is paginated and the user can scroll down, a "⇣" is inserted after the last visible choice in the list. As a user moves through the list, the tentatively selected choice becomes bolded and highlighted in blue with a "»" in front of it to clarify which choice is selected.

View Source
const TemplateTable = `` /* 831-byte string literal not displayed */

The template for displaying selection choices as table entries; shows the bolded prompt message followed on the next line by the filter prompt (if the prompt is using a filter) and then a blank line before the selection table. The next line is the header row for the table. The function to write the header row is given a boolean value: true if the table is paginated and the user can scroll up, otherwise false. After the header, the choices are rendered per their specified style. If the table is paginated and the user can scroll down, a "⇣" is inserted after the table.

Variables

This section is empty.

Functions

func New

func New(message string, choices []*selection.Choice, options ...Option) *selection.Selection

Create a new selection prompt by specifying a message to help the user make a decision, a list of choices to choose from, and zero or more options to configure the prompt's behavior.

func NewModel

func NewModel(message string, choices []*selection.Choice, options ...Option) *selection.Model

This helper function immediately places the created selection prompt into a model, returning it.

func NewStringModel

func NewStringModel(message string, choices []string, options ...Option) *selection.Model

This helper function immediately places the created selection prompt generated from string choices into a model.

func NewStringSelector

func NewStringSelector(message string, choices []string, options ...Option) *selection.Selection

This helper function enables you to pass a slice of strings instead of needing to convert them into choices first.

func NewStructModel

func NewStructModel(
	message string,
	choices any,
	filter FilterFunc,
	nameFunc TemplateNameFunc,
	options ...Option,
) *selection.Model

This helper function wraps the call to NewStructSelector and returns a model directly.

func NewStructSelector

func NewStructSelector(
	message string,
	choices any,
	filter FilterFunc,
	nameFunc TemplateNameFunc,
	options ...Option,
) *selection.Selection

This helper function includes the minimum required options for a functional selection prompt where choices are structs. To use it, you need to pass the message and choices as normal. You also need to pass a filter function, to enable the prompt to filter for valid choices when the user types, and a name function, to tell the prompt what to display for each entry in the selection list.

func NewTableModel

func NewTableModel(
	message string,
	choices any,
	filter FilterFunc,
	nameFunc TemplateNameFunc,
	headerRowFunc TemplateHeaderRowFunc,
	selectedChoiceStyle ChoiceStyleFunc,
	unselectedChoiceStyle ChoiceStyleFunc,
	options ...Option,
) *selection.Model

This helper function wraps the call to NewTableSelector and returns a model directly.

func NewTableSelector

func NewTableSelector(
	message string,
	choices any,
	filter FilterFunc,
	nameFunc TemplateNameFunc,
	headerRowFunc TemplateHeaderRowFunc,
	selectedChoiceStyle ChoiceStyleFunc,
	unselectedChoiceStyle ChoiceStyleFunc,
	options ...Option,
) *selection.Selection

This helper function includes the minimum required options for a functional selection prompt displayed as a table. To use it, you need to pass the message and choices as normal. You also need to pass a filter function, name function, header row function, and choice style functions for when each row is selected/unselected. These choice style functions should return the table row for that choice, formatted appropriately.

Types

type ChoiceStyleFunc

type ChoiceStyleFunc func(choice *selection.Choice) string

A choice style function takes a selection choice as input and returns a string for displaying in the terminal. Any styling, coloring, or munging of a choice object before displaying must be done in a choice style function and passed to the prompt for the SelectedChoiceStyle or UnselectedChoiceStyle field.

func DefaultFinalChoiceStyle

func DefaultFinalChoiceStyle() ChoiceStyleFunc

This helper function returns a minimal default final choice style for the prompt, bolding and coloring blue the string representation of the final choice

func DefaultSelectedChoiceStyle

func DefaultSelectedChoiceStyle() ChoiceStyleFunc

This helper function returns a minimal default selected choice style for the prompt, bolding and coloring blue the string representation of the selected choice and placing a "»" before the choice to more clearly identify that it is selected beyond only using color/font weight.

func DefaultUnselectedChoiceStyle

func DefaultUnselectedChoiceStyle() ChoiceStyleFunc

This helper function returns a minimal default unselected choice style for the prompt, writing the string representation of the unselected choice after two spaces to ensure alignment with selected choices is not off.

type FilterFunc

type FilterFunc func(filter string, choice *selection.Choice) bool

A Filter function is a function which takes a filter string and a choice object as input and returns true if the choice matches the filter or false if it does not.

type Option

type Option func(prompt *selection.Selection)

Options are functions which modify a Selection prompt. They provide a semantic way to both discover configuration options for a Selection prompt and to pass them dynamically as needed.

func WithAdditionalChoices

func WithAdditionalChoices(choices ...any) Option

This option allows you to append one or more choices to those already included in the prompt.

func WithChoices

func WithChoices(choices any) Option

This option allows you to specify the choices to pass to the new selection prompt without having to convert them to Choice objects yourself.

func WithColorProfile

func WithColorProfile(profile termenv.Profile) Option

This option allows you to override how colors are rendered. By default, the underlying prompt queries the terminal.

func WithExtendedTemplateFuncs

func WithExtendedTemplateFuncs(funcMap template.FuncMap) Option

This option allows you to pass additional functions for the templates used in the prompt. Specify one or more with their name as the key in a map. This option will add the function if it is not already registered or overwrite an extended function if it already exists for the prompt. For more information, see the docs for Selection: https://pkg.go.dev/github.com/erikgeiser/promptkit/selection#Selection.ExtendedTemplateFuncs and for template.FuncMap: https://pkg.go.dev/text/template#FuncMap

func WithFilter

func WithFilter(filter FilterFunc) Option

This option enables you to pass your own custom filter function to the prompt, overriding the default, which compares the filter string to the string representation of the choice.

func WithFilterInputBackgroundStyle

func WithFilterInputBackgroundStyle(style lipgloss.Style) Option

This option allows you to override the default style for the background of the filter's input text. Pass a lipgloss style and it will be applied inline to to the background.

func WithFilterInputCursorStyle

func WithFilterInputCursorStyle(style lipgloss.Style) Option

This option allows you to override the default style for the cursor when inputting text for the prompt's filter. Pass a lipgloss style and it will be applied inline to to the cursor.

func WithFilterInputPlaceholderStyle

func WithFilterInputPlaceholderStyle(style lipgloss.Style) Option

This option allows you to override the default style for the filter's placeholder text. Pass a lipgloss style and it will be applied inline to to the input text.

func WithFilterInputTextStyle

func WithFilterInputTextStyle(style lipgloss.Style) Option

This option allows you to override the default style for the filter's input text. Pass a lipgloss style and it will be applied inline to to the input text.

func WithFilterPlaceholder

func WithFilterPlaceholder(placeholder string) Option

This option enables you to override the default filter placeholder, which reads "Type to filter choices."

func WithFilterPrompt

func WithFilterPrompt(message string) Option

This option enables you to override the default filter prompt, which reads "Filter:"

func WithFinalChoiceStyle

func WithFinalChoiceStyle(styleFunction ChoiceStyleFunc) Option

This option allows you to override the default choice style that is called to determine how to render the user's choice after they have selected one and ended the prompt.

func WithKeyMap

func WithKeyMap(keymap selection.KeyMap) Option

This option allows you to override the default key map for the prompt. Specify the key presses (or combinations) you want to trigger an action from as strings for their specified action. Note that this option entirely replaces the existing key map; it is best practice to create the default key map, modify it, and then pass the modified map to this option instead of creating the key map inline.

For example:

keymap := confirmation.NewDefaultKeyMap() // start with default map
keymap.Abort := []string{"esc"} // replace ctrl+c with escape to abort
selector.New(
  "How many apples do you have?",
  "selection.Choices([]int{0, 1, 2, 3, 5}),
  "selector.WithKeyMap(keymap),
)

func WithPageSize

func WithPageSize(size int) Option

This option allows you to specify how many choices to display at once. If the page size is smaller than the number of choices or is zero, pagination is disabled.

func WithResultTemplate

func WithResultTemplate(template string) Option

This option overrides the default template the prompt uses to display results to the terminal. For more information, see the docs for Selection: https://pkg.go.dev/github.com/erikgeiser/promptkit/selection#Selection.ResultTemplate

func WithSelectedChoiceStyle

func WithSelectedChoiceStyle(styleFunction ChoiceStyleFunc) Option

This option allows you to override the default choice style that is called to determine how to render a choice when the user has tentatively selected it but not hit enter yet.

func WithTemplate

func WithTemplate(template string) Option

This option overrides the default template the prompt uses to display to the terminal. For more information, see the docs for Selection: https://pkg.go.dev/github.com/erikgeiser/promptkit/selection#Selection.Template

func WithUnselectedChoiceStyle

func WithUnselectedChoiceStyle(styleFunction ChoiceStyleFunc) Option

This option allows you to override the default choice style that is called to determine how to render all the choices a user is not tentatively selecting at that time.

func WithWrapMode

func WithWrapMode(mode promptkit.WrapMode) Option

This option allows you to override the default wrap mode for the prompt (promptkit.WordWrap). The default mode wraps the input at width, wrapping on last white space before the word which runs over the width so that words are not cut in the middle. The other built-in modes are HardWrap, which wraps at the specified width regardless of the text, and nil which disables wrapping. You can also supply your own wrap mode by specifying a function which takes an input string and width in and returns the wrapped string.

type TemplateHeaderRowFunc

type TemplateHeaderRowFunc func(canScrollUp bool) string

A template header row function is used to write the header row when using the table view for selection. It takes a boolean value for whether or not the user currently has the option to scroll up to prior choices in the table.

type TemplateNameFunc

type TemplateNameFunc func(choice *selection.Choice) string

A template name function is used to tell the prompt what the name of a choice is when choosing between structs.

Jump to

Keyboard shortcuts

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