picker

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

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
}

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 New

func New(items ...Item) Model

New builds a Model from items, with the cursor on the first one.

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

func (m Model) Highlighted() Item

Highlighted returns the item currently under the cursor, or the zero Item if the list is empty.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

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

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

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) 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.ComponentPicker, ...) 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 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.

func (Model) View

func (m Model) View() string

View renders the item list with the highlighted item marked by a cursor.

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.

type SelectedMsg

type SelectedMsg struct {
	Index int
	Item  Item
}

SelectedMsg is delivered (via the Cmd Update returns) when the highlighted item is confirmed with Enter.

Jump to

Keyboard shortcuts

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