Documentation
¶
Overview ¶
Package menu is a nested-navigation list widget composed on top of picker.Model. Where picker.Model is a flat single-choice list, menu.Model adds drill-down into an Item's Children as a stack of picker.Model levels — one per depth currently navigated into — so Up/Down/Home/End and rendering at any given level are exactly picker.Model's behavior, and only Enter (drill in or select) and Esc (pop back out) are menu-specific.
Example ¶
Enter on an item with Children opens the next level; Enter on a leaf reports it.
package main
import (
"fmt"
"github.com/ows4444/tui"
"github.com/ows4444/tui/menu"
)
func main() {
m := menu.New([]menu.Item{
{Label: "File", Children: []menu.Item{{Label: "Open", Value: "open"}, {Label: "Quit", Value: "quit"}}},
{Label: "Help", Value: "help"},
})
enter := tui.Key{Type: tui.KeyEnter}
m, _ = m.Update(enter) // File: opens the submenu
_, cmd := m.Update(enter)
fmt.Println(cmd().(menu.SelectedMsg).Item.Value)
}
Output: open
Index ¶
- type Item
- type KeyMap
- type Model
- func (m Model) Bindings() []keymap.Binding
- 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
- type SelectedMsg
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Item ¶
Item is one entry in a Menu, optionally with nested Children. An Item with no Children is a leaf: confirming it with Enter emits SelectedMsg. An Item with Children drills into a submenu built from them instead.
type KeyMap ¶
type KeyMap struct {
Up keymap.Binding
Down keymap.Binding
Top keymap.Binding
Bottom keymap.Binding
// Select drills into an item's Children or confirms a leaf.
Select keymap.Binding
// Back pops to the parent level.
Back keymap.Binding
}
KeyMap is the set of keys Update reacts to.
func DefaultKeyMap ¶
func DefaultKeyMap() KeyMap
DefaultKeyMap returns the default bindings: up/down, home/end, enter to select, esc to go back.
type Model ¶
type Model struct {
// Raw, when true, draws item labels unchanged. By default each label is
// sanitised (ansi.Sanitize) so untrusted text cannot carry terminal
// escape sequences. Item.Value is never altered.
Raw bool
// KeyMap holds the key bindings Update obeys, at every depth. New fills
// it with DefaultKeyMap; a Model built with no bindings set behaves as
// if it held DefaultKeyMap.
KeyMap KeyMap
// Mouse, when true, makes Update handle tui.MouseEvent on the current
// level: the wheel moves the cursor by WheelStep rows and a left click
// on a row moves the cursor to it (it does not drill in). Off (the
// default) ignores the mouse.
Mouse bool
// Bounds is the screen rectangle where the app draws the widget; mouse
// events outside it are ignored. The app sets it each frame it moves.
Bounds hittest.Rect
// WheelStep is the rows the cursor moves per wheel notch; 0 means 3.
WheelStep int
// contains filtered or unexported fields
}
Model is a keyboard-navigable nested menu. It holds a stack of picker.Model levels (one per depth drilled into) plus the parallel Menu Items backing each level, so the Children of the item under any level's cursor can be looked up on Enter. The top of the stack is the level currently shown by View and driven by Update.
func New ¶
New builds a Model from items, with the root level's picker.Model built from items (mapped to picker.Item{Label, Value}) pushed as the initial and only stack entry.
func (Model) LayoutNode ¶
LayoutNode adapts the menu to a layout.Node by way of the picker that shows the level currently navigated into, so it fits the allotted Size and keeps the cursor item visible. The Model is not changed.
func (Model) Linearize ¶
Linearize renders the current menu level as plain text for accessible output (see tui.Linearizer): one line per item with its label and position, ", submenu" on items that have children, and ", selected" on the cursor row. When the menu is drilled into a submenu, a first line "Submenu: A, B" names the path of parent items entered. An empty level reads "No items".
func (Model) SetTheme ¶
SetTheme returns m with t applied to every level, including levels opened later. It makes Model a tui.ThemeSetter, so a root model can forward the Program's theme (see tui.WithTheme).
func (Model) Update ¶
Update forwards the Up/Down/Top/Bottom bindings (and, with Mouse on, mouse events) to the current level's picker.Model (#403). Select drills into an item's Children by pushing a new picker.Model level (#404), or — for a leaf — emits menu.SelectedMsg for it (#405). Esc pops the current level back to its parent, preserving the parent's cursor position, when more than the root level is present (#406); at the root it is a no-op (#407). Any other Msg is forwarded to the current level's picker.Model, matching picker.Model's own no-op behavior for a non-Key Msg.
func (Model) WithTokens ¶
WithTokens returns m with tok as its per-instance colour override, applied to every level, including levels opened later. Nil fields inherit from the theme. Theme-wide overrides registered under theme.ComponentMenu are applied by SetTheme.
type SelectedMsg ¶
type SelectedMsg struct {
Item Item
}
SelectedMsg is delivered (via the Cmd Update returns) when a leaf Item (no Children) is confirmed with Enter, mirroring picker.Model. SelectedMsg's confirm-on-Enter convention.