picker

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package picker is the searchable list behind the command palette and every other picker of the UI (functions, sheets, named ranges, macros, themes, files to import): a search field on top, results below with matched characters highlighted, in the manner of fzf. It knows the UI only through Host.

Index

Constants

View Source
const FirstRow = 3

FirstRow is the box's first result row, under the top border, the search field and a separator.

View Source
const ID = "picker"

ID identifies the picker's box in mouse events.

Variables

This section is empty.

Functions

This section is empty.

Types

type Host

type Host interface {
	Theme() *theme.Theme
	Size() (width, height int)
	// Line is the shared edit line the search is typed in.
	Line() *lineedit.Line
	// Close closes the open overlay.
	Close()
	// RecordAnswer tells a macro recording the answer to the question of
	// the command that opened the picker, or that it was cancelled.
	RecordAnswer(answer string, cancelled bool)
}

Host is what a picker needs of the UI.

type Item

type Item struct {
	Title  string // e.g. "Save as"
	Name   int    // bytes of Title that are searched, e.g. just "SUM" of "SUM(value, ...)"
	Detail string // a dimmed second column, also searched, e.g. the menu path
	Key    string // shortcut, shown as a key chip
	Desc   string // what it does, for the status line
	Off    bool   // unavailable right now
	Pick   func() tea.Cmd
	// Swatch, when set, is drawn before the title as it is, styled: a
	// sample of a color or a line.
	Swatch string
}

Item is one row of a picker.

type Match

type Match struct {
	Item              *Item
	InTitle, InDetail []int
}

Match is an item that matches the search, with the matched byte offsets in its title and detail.

type Picker

type Picker struct {
	overlay.List

	// Action is what Enter does, for the key hints ("run" by default).
	Action string
	Items  []Item

	// Narrow, when set, sees the search first and can narrow the items by
	// it, as "dark" does the themes: it returns the rest of the search,
	// matched against the kept items' titles only, and which items to
	// keep, or a nil keep to search every item as usual.
	Narrow func(query string) (rest string, keep func(*Item) bool)

	// Enter, when set, gets the first look at Enter with the search text,
	// e.g. to take a typed path; it reports whether it handled it.
	Enter func(query string) (tea.Cmd, bool)

	// Answers marks a picker that is the question of a command that
	// changes the workbook: a recording keeps the title picked as the
	// command's answer, and scripts answer it with a title.
	Answers bool

	// At, when set, is where the box's top-left corner goes, as a
	// dropdown opens under its cell; the box stays on screen. Pickers
	// open centered under the menu bar otherwise.
	At *[2]int
	// contains filtered or unexported fields
}

Picker is a searchable list in a box: a search field on top, results below with matched characters highlighted, and the highlighted result's description on the status line. The search is edited in the host's edit line with the usual line-editing keys.

func New

func New(h Host, title, placeholder string, maxW int, items []Item) *Picker

New returns a picker of items titled title, at most maxW columns wide inside its frame, with the edit line cleared for the search.

func (*Picker) Answer

func (p *Picker) Answer(title string) (tea.Cmd, bool)

Answer picks the item titled title (in any case), as a script answers the picker's question, and reports whether there was one.

func (*Picker) Changed

func (p *Picker) Changed()

Changed filters the items by the search. Best matches come first; ties keep menu order.

func (*Picker) Close

func (p *Picker) Close()

Close closes the picker without picking, which cancels the question it asks.

func (*Picker) Cursor

func (p *Picker) Cursor() (int, int)

func (*Picker) Indicator

func (p *Picker) Indicator() string

func (*Picker) Key

func (p *Picker) Key(k tea.KeyPressMsg) tea.Cmd

func (*Picker) Layout

func (p *Picker) Layout() []overlay.Box

func (*Picker) Mouse

func (p *Picker) Mouse(e overlay.MouseEvent) tea.Cmd

func (*Picker) Selected

func (p *Picker) Selected() *Item

Selected is the highlighted item, or nil when nothing matches.

func (*Picker) Shown

func (p *Picker) Shown() []Match

Shown are the items that match the search, best first.

func (*Picker) Status

func (p *Picker) Status() (string, string)

func (*Picker) Title

func (p *Picker) Title() string

Title is the picker's title, on its frame.

Jump to

Keyboard shortcuts

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