Help us shape the future of Go! Take the 2026 Go Developer Survey

confirmation

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: MIT Imports: 10 Imported by: 58

Documentation

Overview

Package confirmation implements prompt for a binary confirmation such as a yes/no question. It also offers customizable appreance and a customizable key map.

Index

Constants

View Source
const (
	// DefaultTemplate defines the default appearance of the text input and can
	// be copied as a starting point for a custom template.
	DefaultTemplate = TemplateArrow

	// DefaultResultTemplate defines the default appearance with which the
	// finale result of the prompt is presented.
	DefaultResultTemplate = ResultTemplateArrow
)
View Source
const ResultTemplateArrow = `` /* 135-byte string literal not displayed */

ResultTemplateArrow is the ResultTemplate that matches TemplateArrow.

View Source
const ResultTemplateYN = `` /* 173-byte string literal not displayed */

ResultTemplateYN is the ResultTemplate that matches TemplateYN.

View Source
const TemplateArrow = `` /* 197-byte string literal not displayed */

TemplateArrow is a template where the current choice is indicated by an arrow.

View Source
const TemplateYN = `` /* 190-byte string literal not displayed */

TemplateYN is a classic template with ja [yn] indicator where the current value is capitalized and bold.

Variables

View Source
var (

	// Yes is a possible value of the confirmation prompt that corresponds to
	// true.
	Yes = Value(&yes)
	// No is a possible value of the confirmation prompt that corresponds to
	// false.
	No = Value(&no)
	// Undecided is a possible value of the confirmation prompt that is used
	// when neither Yes nor No are selected.
	Undecided = Value(nil)
)

Functions

This section is empty.

Types

type Confirmation

type Confirmation struct {
	// Prompt holds the question.
	Prompt string

	// DefaultValue decides if a value should already be selected at startup. By
	// default it is Undecided but it can be set to Yes (corresponds to true)
	// and No (corresponds to false).
	DefaultValue Value

	// Template holds the display template. A custom template can be used to
	// completely customize the appearance of the text input. If empty, the
	// DefaultTemplate is used. The following variables and functions are
	// available:
	//
	//  * Prompt string: The configured prompt.
	//  * YesSelected bool: Whether or not Yes is the currently selected
	//    value.
	//  * NoSelected bool: Whether or not No is the currently selected value.
	//  * Undecided bool: Whether or not Undecided is the currently selected
	//    value.
	//  * DefaultYes bool: Whether or not Yes is confiured as default value.
	//  * DefaultNo bool: Whether or not No is confiured as default value.
	//  * DefaultUndecided bool: Whether or not Undecided is confiured as
	//    default value.
	//  * TerminalWidth int: The width of the terminal.
	//  * KeyMap *KeyMap: The configured key map.
	//  * promptkit.UtilFuncMap: Handy helper functions.
	//  * termenv TemplateFuncs (see https://github.com/muesli/termenv).
	//  * The functions specified in ExtendedTemplateFuncs.
	Template string

	// ResultTemplate is rendered as soon as a input has been confirmed.
	// It is intended to permanently indicate the result of the prompt when the
	// input itself has disappeared. This template is only rendered in the Run()
	// method and NOT when the text input is used as a model. The following
	// variables and functions are available:
	//
	//  * FinalValue bool: The final value of the confirmation.
	//  * FinalValue string: The final value's string representation ("true"
	//    or "false").
	//  * Prompt string: The configured prompt.
	//  * DefaultYes bool: Whether or not Yes is confiured as default value.
	//  * DefaultNo bool: Whether or not No is confiured as default value.
	//  * DefaultUndecided bool: Whether or not Undecided is confiured as
	//    default value.
	//  * TerminalWidth int: The width of the terminal.
	//  * promptkit.UtilFuncMap: Handy helper functions.
	//  * termenv TemplateFuncs (see https://github.com/muesli/termenv).
	//  * The functions specified in ExtendedTemplateFuncs.
	ResultTemplate string

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

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

	// WrapMode decides which way the prompt view is wrapped if it does not fit
	// the terminal. It can be a WrapMode provided by promptkit or a custom
	// function. By default it is promptkit.WordWrap. It can also be nil which
	// disables wrapping and likely causes output glitches.
	WrapMode promptkit.WrapMode

	// Output is the output writer, by default os.Stdout is used.
	Output io.Writer
	// Input is the input reader, by default, os.Stdin is used.
	Input io.Reader

	// ColorProfile determines how colors are rendered. By default, the terminal
	// is queried.
	ColorProfile termenv.Profile
}

Confirmation represents a configurable confirmation prompt.

func New

func New(prompt string, defaultValue Value) *Confirmation

New creates a new text input. If the default value is nil it is equivalent to Undecided. See the Confirmation properties for more documentation.

func (*Confirmation) RunPrompt

func (c *Confirmation) RunPrompt() (bool, error)

RunPrompt executes the confirmation prompt.

type KeyMap

type KeyMap struct {
	Yes       []string
	No        []string
	SelectYes []string
	SelectNo  []string
	Toggle    []string
	Submit    []string
	Abort     []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 {
	*Confirmation

	// Err holds errors that may occur during the execution of
	// the confirmation prompt.
	Err error

	// MaxWidth limits the width of the view using the Confirmation's WrapMode.
	MaxWidth int
	// contains filtered or unexported fields
}

Model implements the bubbletea.Model for a confirmation prompt.

func NewModel

func NewModel(confirmation *Confirmation) *Model

NewModel returns a new model based on the provided confirmation prompt.

func (*Model) Init

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

Init initializes the confirmation 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) Value

func (m *Model) Value() (bool, error)

Value returns the current value and error.

func (*Model) View

func (m *Model) View() tea.View

View renders the confirmation prompt.

type Value

type Value *bool

Value is the value of the confirmation prompt which can be Undecided, Yes or No.

func NewValue

func NewValue(v bool) Value

NewValue creates a Value from a bool.

Jump to

Keyboard shortcuts

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