menu

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item struct {
	Label    string
	Value    string
	Children []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

func New(items []Item) Model

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) Bindings

func (m Model) Bindings() []keymap.Binding

Bindings returns the active bindings, for help text.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

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

func (m Model) Linearize() string

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

func (m Model) SetTheme(t theme.Theme) Model

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) Tokens

func (m Model) Tokens() theme.Tokens

Tokens returns the instance's colour overrides as set by WithTokens.

func (Model) Update

func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)

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) View

func (m Model) View() string

View delegates to the current (top-of-stack) level's picker.Model.View.

func (Model) WithTokens

func (m Model) WithTokens(tok theme.Tokens) Model

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.

Jump to

Keyboard shortcuts

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