picker

package
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package picker is a small reusable arrow-key list selector built on bubbletea. It renders an inline (non-alt-screen) menu — a highlighted cursor you move with ↑/↓ (or j/k), Enter to confirm, Esc/q/Ctrl-C to cancel — and returns the chosen index. The frame collapses to "" on exit so the caller's own confirmation/log line is what stays in the scrollback.

The picker is domain-agnostic: callers describe each row with an Item and map any per-row status color onto a Tone, so the picker never has to know about a caller's status vocabulary. Callers own the non-TTY fallback and the single-item shortcut; Select always goes interactive.

Index

Constants

This section is empty.

Variables

View Source
var ErrCancelled = errors.New("picker cancelled")

ErrCancelled is returned when the user backs out (Esc, q, Ctrl-C/SIGTERM, EOF) or when there are no items to choose from. Callers translate it to their own clean-exit sentinel.

Functions

func Select

func Select(ctx context.Context, cfg Config) (int, error)

Select runs the interactive picker and returns the chosen index into cfg.Items. It returns ErrCancelled if the user backs out (or cfg.Items is empty), and a wrapped error if the bubbletea program itself fails. The caller is responsible for the non-TTY fallback and the single-item shortcut — Select always goes interactive. ctx cancellation yields ErrCancelled.

Types

type Config

type Config struct {
	// Prompt heads the list, e.g. "Select an image".
	Prompt string
	// Note is an optional dim block rendered between the prompt and the rows,
	// e.g. a key/value summary of what's being chosen. May span multiple lines;
	// each line is rendered as-is (indent it yourself).
	Note  string
	Items []Item
	// Cursor is the index highlighted on open (the press-Enter choice).
	// Out-of-range values fall back to 0.
	Cursor int
	// DefaultIdx marks one row with a "(default)" badge (-1 = none).
	DefaultIdx int
	// Filter enables type-to-narrow substring filtering. When off, letter keys
	// drive j/k navigation and q quits.
	Filter bool
	// In is the key source (os.Stdin); Out is where the menu renders (stderr).
	In  io.Reader
	Out io.Writer
}

Config controls a single Select call.

type Item

type Item struct {
	Title  string // primary text (e.g. an image name or a session title)
	Desc   string // optional dim secondary text shown after the title
	Status string // optional right-side status word ("" = none)
	Tone   Tone   // color hint for Status
	// Divider marks a non-selectable separator row — a group header. The cursor
	// skips over it, Enter can't choose it, and it renders as a dim bold header
	// rather than a normal row. Only Title is used; the other fields are ignored.
	Divider bool
}

Item is one row. Most items are selectable; set Divider to make a non-selectable group header instead.

type Tone

type Tone int

Tone is a semantic color hint for an item's status text. It decouples the picker from any domain's status words: the caller maps its own statuses to a Tone, and the picker maps Tone → the shared style bundle.

const (
	ToneNone    Tone = iota // no status column
	ToneSuccess             // green
	ToneWarn                // amber
	ToneDanger              // red
	ToneDim                 // grey / de-emphasized
)

Jump to

Keyboard shortcuts

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