Documentation
¶
Overview ¶
Package picker is a single-choice list widget — InkUI's "Select", renamed because 'select' is a Go keyword and can't be a package name.
Example ¶
A picker is a single-choice list: move the cursor, read it back.
package main
import (
"fmt"
"github.com/ows4444/tui"
"github.com/ows4444/tui/picker"
)
func main() {
m := picker.NewStrings("small", "medium", "large")
m, _ = m.Update(tui.Key{Type: tui.KeyDown})
m, _ = m.Update(tui.Key{Type: tui.KeyDown})
fmt.Println(m.Cursor())
}
Output: 2
Index ¶
- type Item
- type KeyMap
- type Model
- func (m Model) Bindings() []keymap.Binding
- func (m Model) Cursor() int
- func (m Model) Highlighted() Item
- func (m Model) LayoutNode() layout.Node
- func (m Model) Linearize() string
- func (m *Model) SetCursor(i int)
- 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 Model's list. Value is kept separate from Label in case a caller wants a user-facing label distinct from an underlying id/value; when they're the same, use NewStrings instead of building Items by hand.
type KeyMap ¶
type KeyMap struct {
Up keymap.Binding
Down keymap.Binding
Top keymap.Binding
Bottom keymap.Binding
Select 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.
type Model ¶
type Model struct {
Items []Item
Theme theme.Theme
// KeyMap holds the key bindings Update obeys. New and NewStrings fill it
// with DefaultKeyMap; a Model built as a literal with no bindings set
// behaves as if it held DefaultKeyMap.
KeyMap KeyMap
// Mouse, when true, makes Update handle tui.MouseEvent: the wheel moves
// the cursor by WheelStep rows and a left click on a row moves the
// cursor to it. 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 single-choice list. It renders all of its Items with no internal scrolling — for a list too long to fit on screen, compose it inside a viewport.Model instead of expecting Model to scroll itself.
func NewStrings ¶
NewStrings builds a Model whose Items each have an equal Label and Value.
func (Model) Highlighted ¶
Highlighted returns the item currently under the cursor, or the zero Item if the list is empty.
func (Model) LayoutNode ¶
LayoutNode adapts the list to a layout.Node. Measure reports its natural size (widest "> label" by one row per item). Render fits the allotted Size: items are cut to the width, and when there are more items than rows it shows a window of items containing the cursor item. The Model is not changed.
func (Model) Linearize ¶
Linearize renders the list as plain text for accessible output (see tui.Linearizer): one line per item with its label and position, and ", selected" on the cursor row, e.g. "Blue, item 2 of 3, selected". No cursor marker or styling. An empty list reads "No items".
func (*Model) SetCursor ¶
SetCursor moves the cursor to i, clamped to a valid item index (or 0 with no items).
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.ComponentPicker, ...) and then by WithTokens.
func (Model) Update ¶
Update moves the cursor on the Up/Down/Top/Bottom bindings and confirms the highlighted item on Select, returning a Cmd that delivers SelectedMsg. With Mouse on it also handles tui.MouseEvent (see Model.Mouse). Any other Msg is a no-op.
type SelectedMsg ¶
SelectedMsg is delivered (via the Cmd Update returns) when the highlighted item is confirmed with Enter.