selection

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2021 License: MIT Imports: 12 Imported by: 56

Documentation

Overview

Package selection implements a selection prompt that allows users to to select one of the pre-defined choices. It also offers customizable appreance and key map as well as optional support for pagination, filtering.

Index

Constants

View Source
const (
	// DefaultTemplate defines the default appearance of the selection and can
	// be copied as a starting point for a custom template.
	DefaultTemplate = `` /* 511-byte string literal not displayed */

	// DefaultConfirmationTemplate defines the default appearance with which the
	// finale result of the selection is presented.
	DefaultConfirmationTemplate = `
	{{- Bold (print .Prompt " " (Foreground "32"  .FinalChoice.String) "\n") -}}
	`

	// DefaultFilterPlaceholder is printed by default when no filter text was
	// entered yet.
	DefaultFilterPlaceholder = "Type to filter choices"
)

Variables

This section is empty.

Functions

func FilterContainsCaseInsensitive

func FilterContainsCaseInsensitive(filter string, choice *Choice) bool

FilterContainsCaseInsensitive returns true if the string representation of the choice contains the filter string without regard for capitalization.

func FilterContainsCaseSensitive

func FilterContainsCaseSensitive(filter string, choice *Choice) bool

FilterContainsCaseSensitive returns true if the string representation of the choice contains the filter string respecting capitalization.

Types

type Choice

type Choice struct {
	Index  int
	String string
	Value  interface{}
}

Choice represents a single choice. This type used as an input for the selection prompt, for filtering and as a result value. The index is populated by the prompt itself and is exported to be accessed when filtering.

func Choices

func Choices(sliceChoices interface{}) []*Choice

Choices converts a slice of anything to a slice of choices. Choices panics if the input is not a slice.

func NewChoice

func NewChoice(item interface{}) *Choice

NewChoice creates a new choice for a given input and chooses a suitable string representation. The index is left at 0 to be populated by the selection prompt later on.

type KeyMap

type KeyMap struct {
	Down        []string
	Up          []string
	Select      []string
	Abort       []string
	ClearFilter []string
	ScrollDown  []string
	ScrollUp    []string
}

KeyMap defines the keys that trigger certain actions.

func NewDefaultKeyMap

func NewDefaultKeyMap() *KeyMap

NewDefaultKeyMap returns a KeyMap with sensible default key mappings that can also be used as a starting point for customization.

type Model

type Model struct {
	*Selection

	// Err holds errors that may occur during the execution of
	// the selection prompt.
	Err error
	// contains filtered or unexported fields
}

Model implements the bubbletea.Model for a selection prompt.

func NewModel

func NewModel(selection *Selection) *Model

NewModel returns a new selection prompt model for the provided choices.

func (*Model) Choice

func (m *Model) Choice() (*Choice, error)

Choice returns the choice that is currently selected or the final choice after the prompt has concluded.

func (*Model) Init

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

Init initializes the selection prompt model.

func (*Model) Update

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

Update updates the model based on the received message.

func (*Model) View

func (m *Model) View() string

View renders the selection prompt.

type Selection

type Selection struct {
	// Choices represent all selectable choices of the selection. Slices of
	// arbitrary types can be converted to a slice of choices using the helpers
	// StringChoices, StringerChoices and SliceChoices.
	Choices []*Choice

	// Prompt holds the the prompt text or question that is printed above the
	// choices in the default template (if not empty).
	Prompt string

	// Filter is a function that decides whether a given choice should be
	// displayed based on the text entered by the user into the filter input
	// field. If Filter is nil, filtering will be disabled. By default the
	// filter FilterContainsCaseInsensitive is used.
	Filter func(filterText string, choice *Choice) bool

	// FilterPlaceholder holds the text that is displayed in the filter input
	// field when no text was entered by the user yet. If empty, the
	// DefaultFilterPlaceholder is used. If Filter is nil, filtering is disabled
	// and FilterPlaceholder does nothing.
	FilterPlaceholder string

	// PageSize is the number of choices that are displayed at once. If PageSize
	// is smaller than the number of choices, pagination is enabled. If PageSize
	// is 0, pagenation is always disabled.
	PageSize int

	// Template holds the display template. A custom template can be used to
	// completely customize the appearance of the selection prompt. If empty,
	// the DefaultTemplate is used. The following variables and functions are
	// available:
	//
	//  * Prompt string: The configured prompt
	//  * IsFiltered bool: Whether or not filtering is enabled
	//  * FilterInput string: The view of the filter input model
	//  * Choices []*Choice: The choices on the current page
	//  * NChoices int: The number of choices on the current page,
	//  * SelectedIndex int: The index that is currently selected
	//  * PageSize int: The configured page size
	//  * IsPaged bool: Whether pagination is currently active
	//  * AllChoices []*Choice: All configured choices
	//  * NAllChoices int: The number of configured choices
	//  * IsScrollDownHintPosition(idx int) bool: Returns whether
	//    the scroll down hint shoud be displayed at the given index
	//  * IsScrollUpHintPosition(idx int) bool: Returns whether the
	//    scroll up hint shoud be displayed at the given index)
	//  * termenv TemplateFuncs (see https://github.com/muesli/termenv)
	//  * The functions specified in ExtendedTemplateScope
	Template string

	// ConfirmationTemplate is rendered as soon as a choice has been selected.
	// It is intended to permanently indicate the result of the prompt when the
	// selection itself has disappeared. This template is only rendered in the
	// Run() method and NOT when the selection prompt is used as a model. The
	// following variables and functions are available:
	//
	//  * FinalChoice: The choice that was selected by the user
	//  * Prompt string: The configured prompt
	//  * AllChoices []*Choice: All configured choices
	//  * NAllChoices int: The number of configured choices
	//  * termenv TemplateFuncs (see https://github.com/muesli/termenv)
	//  * The functions specified in ExtendedTemplateScope
	ConfirmationTemplate string

	// ExtendedTemplateScope can be used to add additional functions to the
	// evaluation scope of the templates.
	ExtendedTemplateScope template.FuncMap

	// Styles of the filter input field. These will be applied as inline styles.
	//
	// For an introduction to styling with Lip Gloss see:
	// https://github.com/charmbracelet/lipgloss
	FilterInputTextStyle        lipgloss.Style
	FilterInputBackgroundStyle  lipgloss.Style
	FilterInputPlaceholderStyle lipgloss.Style
	FilterInputCursorStyle      lipgloss.Style

	// KeyMap determines with which keys the selection prompt is controlled. By
	// default, DefaultKeyMap is used.
	KeyMap *KeyMap
}

Selection represents a configurable selection prompt.

func New

func New(choices []*Choice) *Selection

New creates a new selection prompt.

func (*Selection) RunPrompt

func (s *Selection) RunPrompt() (*Choice, error)

RunPrompt executes the selection prompt.

Jump to

Keyboard shortcuts

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