Documentation
¶
Overview ¶
Package confirm is a yes/no prompt widget — InkUI's "Confirm".
Example ¶
Pressing y confirms yes whatever is highlighted; the answer arrives as a ConfirmedMsg from the returned Cmd.
package main
import (
"fmt"
"github.com/ows4444/tui"
"github.com/ows4444/tui/confirm"
)
func main() {
m := confirm.New("Delete the file?")
_, cmd := m.Update(tui.Key{Type: tui.KeyRunes, Text: "y"})
fmt.Printf("%+v\n", cmd())
}
Output: {Yes:true}
Index ¶
- type ConfirmedMsg
- type KeyMap
- type Model
- func (m Model) Bindings() []keymap.Binding
- func (m Model) Highlighted() bool
- func (m Model) LayoutNode() layout.Node
- func (m Model) Linearize() string
- func (m Model) SetTheme(t theme.Theme) Model
- func (m Model) Tokens() theme.Tokens
- func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)
- func (m Model) View() string
- func (m Model) WithTokens(tok theme.Tokens) Model
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ConfirmedMsg ¶
type ConfirmedMsg struct {
Yes bool
}
ConfirmedMsg is delivered (via the Cmd Update returns) once the prompt is answered, by Enter or by a direct 'y'/'n' keypress.
type KeyMap ¶
type KeyMap struct {
Toggle keymap.Binding // move the highlight to the other option
Accept keymap.Binding // confirm the highlighted option
Yes keymap.Binding // confirm Yes directly
No keymap.Binding // confirm No directly
}
KeyMap names the keys of each action of a Model.
func DefaultKeyMap ¶
func DefaultKeyMap() KeyMap
DefaultKeyMap returns the keys a Model used before KeyMap existed.
type Model ¶
type Model struct {
Prompt string
YesLabel string
NoLabel string
Theme theme.Theme
// KeyMap holds the keys for each action. New fills it with
// DefaultKeyMap; a Model built as a struct literal with a zero KeyMap
// behaves as if it held DefaultKeyMap.
KeyMap KeyMap
// Mouse, when true, makes Update handle tui.MouseEvent: a left click on the
// Yes or No label answers the prompt as that key would. Off (the default)
// ignores the mouse.
Mouse bool
// Bounds is the screen rectangle where the app draws the prompt (its first
// row is the prompt's first line); clicks outside it are ignored.
Bounds hittest.Rect
// contains filtered or unexported fields
}
Model is a two-option Yes/No prompt: Left/Right/Tab move which option is highlighted, Enter confirms the highlighted one, and 'y'/'n' confirm directly regardless of what's highlighted.
func New ¶
New returns a Model with "Yes"/"No" labels and Yes highlighted, the common default for a prompt where doing nothing is the safe choice.
func (Model) Bindings ¶
Bindings returns the actions the prompt currently honours, with descriptions, for help text.
func (Model) Highlighted ¶
Highlighted reports which option the cursor is currently on — not which was chosen; that only happens via ConfirmedMsg.
func (Model) LayoutNode ¶
LayoutNode adapts the prompt to a layout.Node. When the prompt and both options fit on one row it is View exactly; when narrower, the prompt word-wraps to the allotted width and the options sit on their own row beneath it. The Model is not changed.
func (Model) Linearize ¶
Linearize renders the prompt as plain text for accessible output (see tui.Linearizer): the prompt line, then one line per option with its position, ", selected" on the highlighted one, e.g. "Yes, option 1 of 2, selected". No brackets or reverse-video.
func (Model) SetTheme ¶
SetTheme returns m with t applied. It makes Model a tui.ThemeSetter, so a root model can forward the Program's theme (see tui.WithTheme).
func (Model) Tokens ¶
Tokens returns the colour tokens the widget renders with: its theme's roles, overridden by any theme.WithTokens(theme.ComponentConfirm, ...) and then by WithTokens.
func (Model) Update ¶
Update moves the highlight on Left/Right/Tab, and confirms on Enter (the highlighted option) or a direct 'y'/'n' keypress, returning a Cmd that delivers ConfirmedMsg. With Mouse on, a left click on the Yes or No label confirms it the same way.