Documentation
¶
Overview ¶
Package multiselect is a multi-choice list widget — InkUI's "MultiSelect": a cursor moves between items, Space toggles the item under it, and Enter confirms the whole set of checked items.
Example ¶
A multiselect keeps a set of chosen items; Toggle flips one by index.
package main
import (
"fmt"
"github.com/ows4444/tui/multiselect"
)
func main() {
m := multiselect.NewStrings("go", "rust", "zig")
m.Toggle(0)
m.Toggle(2)
fmt.Println(m.SelectedIndexes())
}
Output: [0 2]
Index ¶
- type ConfirmedMsg
- type Item
- type KeyMap
- type Model
- func (m Model) Bindings() []keymap.Binding
- func (m Model) Cursor() int
- func (m Model) IsSelected(i int) bool
- func (m Model) LayoutNode() layout.Node
- func (m Model) Linearize() string
- func (m Model) SelectedIndexes() []int
- func (m Model) SelectedItems() []Item
- func (m *Model) SetCursor(i int)
- func (m Model) SetTheme(t theme.Theme) Model
- func (m *Model) Toggle(i int)
- 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 {
Items []Item
}
ConfirmedMsg is delivered (via the Cmd Update returns) when the current selection is confirmed with Enter.
type KeyMap ¶
type KeyMap struct {
Up keymap.Binding
Down keymap.Binding
Top keymap.Binding
Bottom keymap.Binding
Toggle keymap.Binding
Confirm 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, space to toggle, enter to confirm.
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 (it does not toggle). 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 multi-choice list. Like picker.Model, it renders all of its Items with no internal scrolling.
func NewStrings ¶
NewStrings builds a Model whose Items each have an equal Label and Value.
func (Model) IsSelected ¶
IsSelected reports whether item i is checked.
func (Model) LayoutNode ¶
LayoutNode adapts the list to a layout.Node. Measure reports its natural size (widest "> [x] 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 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, "checked" or "not checked", its position, and ", current" on the cursor row, e.g. "Milk, checked, item 1 of 3, current". No checkbox glyphs or styling. An empty list reads "No items".
func (Model) SelectedIndexes ¶
SelectedIndexes returns the checked indexes in ascending order — map iteration order is randomized in Go, so this sorts explicitly rather than leaving callers to discover that the hard way.
func (Model) SelectedItems ¶
SelectedItems returns the checked items, in list order.
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) Toggle ¶
Toggle flips whether item i is selected. It always replaces m.selected with a fresh map rather than mutating the existing one in place: Model is used with value semantics (Update returns a new Model each call, the same convention textinput/viewport/picker follow), but a map field's header copies by value while its underlying table does not — mutating it in place would silently corrupt any other Model value that still shares that same map, e.g. a caller that kept the pre-Update value around. Copy-on-write here keeps every Model value's selection independent, the same guarantee the rest of this codebase already gets for free from immutable types.
func (Model) Tokens ¶
Tokens returns the colour tokens the widget renders with: its theme's roles, overridden by any theme.WithTokens(theme.ComponentMultiSelect, ...) and then by WithTokens.
func (Model) Update ¶
Update moves the cursor on the Up/Down/Top/Bottom bindings, toggles the item under it on Toggle, and confirms the selection on Confirm, returning a Cmd that delivers ConfirmedMsg. With Mouse on it also handles tui.MouseEvent (see Model.Mouse). Any other Msg is a no-op.