multiselect

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

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 Item

type Item struct {
	Label string
	Value string
}

Item is one entry in a Model's list.

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 New

func New(items ...Item) Model

New builds a Model from items, all initially unselected.

func NewStrings

func NewStrings(items ...string) Model

NewStrings builds a Model whose Items each have an equal Label and Value.

func (Model) Bindings

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

Bindings returns the active bindings, for help text.

func (Model) Cursor

func (m Model) Cursor() int

Cursor returns the index of the item currently under the cursor.

func (Model) IsSelected

func (m Model) IsSelected(i int) bool

IsSelected reports whether item i is checked.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

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

func (m Model) Linearize() string

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

func (m Model) SelectedIndexes() []int

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

func (m Model) SelectedItems() []Item

SelectedItems returns the checked items, in list order.

func (*Model) SetCursor

func (m *Model) SetCursor(i int)

SetCursor moves the cursor to i, clamped to a valid item index (or 0 with no items).

func (Model) SetTheme

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

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

func (m *Model) Toggle(i int)

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

func (m Model) Tokens() theme.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

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

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.

func (Model) View

func (m Model) View() string

View renders the item list with a cursor marker and a checkbox per item.

func (Model) WithTokens

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

WithTokens returns m with tok as its per-instance colour override. Nil fields inherit from the theme, so only the roles tok names change; the theme and every other widget are untouched. A second call replaces the first.

Jump to

Keyboard shortcuts

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