userio

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2024 License: GPL-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package userio provides an interface for user interaction prompts, and a bubbletea implementation of such an interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func UpdateTreeSelection

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

UpdateTreeSelection processes incoming messages and updates the state of the BubbleTeaSeedablesPrompter in tree selection mode.

Types

type BubbleTeaSeedablesPrompter

type BubbleTeaSeedablesPrompter struct {

	// Logger is the logger to use for logging messages.
	Logger logger.ILogger

	// Message is the message to display to the user.
	Message string

	// TreeChoices is the tree of choices to present to the user.
	TreeChoices []PromptTreeElement

	// Breadcrump is the breadcrumb to display to the user.
	Breadcrump []string

	// FullySelectedTreeChar is the character to display when a tree element is fully selected.
	FullySelectedTreeChar string

	// SemiSelectedTreeChar is the character to display when a tree element is partially selected.
	SemiSelectedTreeChar string

	// UnselectedTreeChar is the character to display when a tree element is not selected.
	UnselectedTreeChar string

	// ElementsCount is the number of elements in the tree.
	ElementsCount int

	// IsTreeChoices is true if the prompt is a tree of choices.
	IsTreeChoices bool

	// IsConfirmation is true if the prompt is a yes/no confirmation.
	IsConfirmation bool

	// ConfirmationResult is the result of the confirmation prompt.
	ConfirmationResult bool

	// UserInputText is the text that the user has input.
	UserInputText string
	// contains filtered or unexported fields
}

BubbleTeaSeedablesPrompter is a Prompter that uses the BubbleTea library to present interactive prompts to the user.

It implements the Prompter interface, and can be used to create prompts for the user to select multiple values from a tree.

func (*BubbleTeaSeedablesPrompter) Confirmation

func (m *BubbleTeaSeedablesPrompter) Confirmation(msg string) (result bool, err error)

Confirmation prompts the user with a yes/no question and returns the result.

It sets the text of the prompt to the given message and then runs the BubbleTea program. When the program finishes, it returns the result of the confirmation prompt.

func (*BubbleTeaSeedablesPrompter) ConfirmationView

func (m *BubbleTeaSeedablesPrompter) ConfirmationView() string

ConfirmationView renders a confirmation prompt to a string.

func (*BubbleTeaSeedablesPrompter) Init

Init is the BubbleTea initialization function. It's called when the program starts. In this case, there's no I/O to be done, so it simply returns `nil`.

func (*BubbleTeaSeedablesPrompter) MultiChooseTree

func (m *BubbleTeaSeedablesPrompter) MultiChooseTree(msg string, choices []PromptTreeElement) (selected []PromptTreeElement, err error)

MultiChooseTree presents a tree of choices to the user and returns the selected elements.

It sets the text of the prompt to the given message and then runs the BubbleTea program. When the program finishes, it returns the selected elements.

func (*BubbleTeaSeedablesPrompter) PropagateCounterIndexToTree

func (m *BubbleTeaSeedablesPrompter) PropagateCounterIndexToTree(total int, tree []PromptTreeElement) (int, []PromptTreeElement)

PropagateCounterIndexToTree takes a slice of PromptTreeElements and returns the total count of elements in the tree and the same slice with the CounterIndex field of each element set to the correct value. The CounterIndex of each element is the index of the element in the flattened tree.

This function is used to set the CounterIndex of each element in the tree so that BubbleTea can use it to determine the index of the selected element.

func (*BubbleTeaSeedablesPrompter) TreeSelectionView

func (m *BubbleTeaSeedablesPrompter) TreeSelectionView(level int, message string, breadcrump []string, choices []PromptTreeElement, finishMessage string) string

TreeSelectionView renders a tree selection prompt to a string.

func (*BubbleTeaSeedablesPrompter) Update

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

Update processes incoming messages and updates the state of the BubbleTeaSeedablesPrompter. It handles key presses for quitting the program and delegates to specific update functions based on the current mode (tree selection or confirmation).

func (*BubbleTeaSeedablesPrompter) UpdateConfirmation

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

UpdateConfirmation processes incoming messages and updates the state of the BubbleTeaSeedablesPrompter in confirmation mode.

func (*BubbleTeaSeedablesPrompter) View

View renders the BubbleTeaSeedablesPrompter to a string.

type PromptTreeElement

type PromptTreeElement struct {
	Title        string
	Selected     bool
	Level        int
	CounterIndex int
	SubElements  []PromptTreeElement
}

PromptTreeElement represents an element in the tree of choices.

Title is the title of the element. Selected is true if the element is selected. Level is the level of the element in the tree (0 is the root, 1 is a child of the root, etc). CounterIndex is the index of the element in the tree for use in the BubbleTea prompt. SubElements are the sub-elements of the element.

func ToggleSelectedTreeElement

func ToggleSelectedTreeElement(targetindex int, tree []PromptTreeElement) (result []PromptTreeElement, found bool)

ToggleSelectedTreeElement takes a target index and a slice of PromptTreeElements and toggles the `Selected` field of the element at the target index and all of its children. If the target index is not found in the tree, it does nothing.

It returns the modified tree and a boolean indicating whether the target index was found.

type Prompter

type Prompter interface {
	// MultiChooseTree presents a tree of choices to the user and returns the selected elements.
	MultiChooseTree(msg string, choices []PromptTreeElement) (selected []PromptTreeElement, err error)

	// Confirmation prompts the user with a yes/no question and returns the result.
	Confirmation(msg string) (bool, error)
}

Prompter defines an interface for user interaction prompts.

Jump to

Keyboard shortcuts

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