dropdown

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package dropdown implements an interactive dropdown selection widget.

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultTextColor         = cell.ColorWhite
	DefaultFocusedTextColor  = cell.ColorCyan
	DefaultSelectedTextColor = cell.ColorWhite
	DefaultSelectedFillColor = cell.ColorNumber(60)
	DefaultBorderColor       = cell.ColorCyan
	GlyphProfiles            = struct {
		Classic GlyphProfile
		Minimal GlyphProfile
	}{
		Classic: GlyphProfile{
			ClosedArrow:      '▼',
			OpenArrow:        '▲',
			SelectedPrefix:   ">",
			UnselectedPrefix: " ",
			Border: BorderRunes{
				TopLeft:     '┌',
				TopRight:    '┐',
				BottomLeft:  '└',
				BottomRight: '┘',
				Horizontal:  '─',
				Vertical:    '│',
			},
		},
		Minimal: GlyphProfile{
			ClosedArrow:      '▾',
			OpenArrow:        '▴',
			SelectedPrefix:   "›",
			UnselectedPrefix: " ",
			Border: BorderRunes{
				TopLeft:     '╭',
				TopRight:    '╮',
				BottomLeft:  '╰',
				BottomRight: '╯',
				Horizontal:  '─',
				Vertical:    '│',
			},
		},
	}
)

Default colors used by the dropdown widget.

Functions

func IntRange

func IntRange(start, end, step int, format string) []string

IntRange returns a slice of decimal strings covering the requested range.

The format defaults to "%d" when empty, making it easy to create dropdown items such as "1" through "12" or zero-padded variants like "%02d".

Types

type BorderRunes

type BorderRunes struct {
	TopLeft     rune
	TopRight    rune
	BottomLeft  rune
	BottomRight rune
	Horizontal  rune
	Vertical    rune
}

BorderRunes defines the UTF-8 runes used to draw the open dropdown box.

type Dropdown struct {
	// contains filtered or unexported fields
}

Dropdown allows users to select one value from a fixed list of items.

When focused, the widget can be opened with Enter, Space, or the arrow keys. Once open, Up and Down move the active row, Enter commits the selection, and Esc closes the list.

Implements widgetapi.Widget. This object is thread-safe.

func New

func New(items []string, opts ...Option) (*Dropdown, error)

New returns a new Dropdown for the provided item labels.

func (d *Dropdown) CanvasSize(maxHeight int) image.Point

CanvasSize returns the size needed to draw the dropdown in its current state within the provided height budget.

When closed, the dropdown only needs a single row for the trigger. When open, the returned height includes the trigger, visible rows, and border.

func (d *Dropdown) Close()

Close collapses the dropdown list.

func (d *Dropdown) Draw(cvs *canvas.Canvas, meta *widgetapi.Meta) error

Draw draws the Dropdown onto the canvas. Implements widgetapi.Widget.Draw.

func (d *Dropdown) Keyboard(k *terminalapi.Keyboard, meta *widgetapi.EventMeta) error

Keyboard processes keyboard events for the dropdown. Implements widgetapi.Widget.Keyboard.

func (d *Dropdown) Mouse(m *terminalapi.Mouse, meta *widgetapi.EventMeta) error

Mouse processes mouse events for the dropdown. Implements widgetapi.Widget.Mouse.

func (d *Dropdown) Open()

Open expands the dropdown list.

func (d *Dropdown) Options() widgetapi.Options

Options implements widgetapi.Widget.Options.

func (d *Dropdown) SelectedIndex() int

SelectedIndex returns the currently selected item index.

func (d *Dropdown) SelectedText() string

SelectedText returns the currently selected item label.

func (d *Dropdown) SetItems(items []string) error

SetItems replaces the dropdown item list.

If the current selection no longer fits into the updated item set, the selection resets to the first item.

func (d *Dropdown) SetSelected(index int) error

SetSelected replaces the current selection programmatically.

func (d *Dropdown) TriggerText() string

TriggerText returns the dropdown's current trigger line text.

func (d *Dropdown) TriggerTextFor(label string) string

TriggerTextFor returns the trigger text for the provided label using the dropdown's configured closed-arrow glyphs.

type GlyphProfile

type GlyphProfile struct {
	ClosedArrow      rune
	OpenArrow        rune
	SelectedPrefix   string
	UnselectedPrefix string
	Border           BorderRunes
}

GlyphProfile groups reusable dropdown glyph choices.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option is used to provide options.

func Arrows

func Arrows(closed, open rune) Option

Arrows sets the UTF-8 runes used while the dropdown is closed and open.

func BorderCellOpts

func BorderCellOpts(cellOpts ...cell.Option) Option

BorderCellOpts sets the styling used for the open dropdown box border.

func BorderGlyphs

func BorderGlyphs(runes BorderRunes) Option

BorderGlyphs sets the runes used for the open dropdown box border.

func CellOpts

func CellOpts(cellOpts ...cell.Option) Option

CellOpts sets the default cell styling used for the trigger and unselected list rows.

func FocusedCellOpts

func FocusedCellOpts(cellOpts ...cell.Option) Option

FocusedCellOpts sets the styling used for the closed trigger while the widget's container is focused.

func GlyphSet

func GlyphSet(set GlyphProfile) Option

GlyphSet sets the dropdown glyphs from a reusable group.

func OnSelect

func OnSelect(fn SelectFn) Option

OnSelect sets the dropdown's selection hook.

This is the widget's canonical callback surface. Callers that need delayed or asynchronous work should build that from this hook so the widget keeps a single stable event path.

func RowPrefixes

func RowPrefixes(selected, unselected string) Option

RowPrefixes sets the leading text used for selected and unselected rows.

func Selected

func Selected(index int) Option

Selected sets the initially selected item index.

func SelectedCellOpts

func SelectedCellOpts(cellOpts ...cell.Option) Option

SelectedCellOpts sets the styling used for the active row while the list is open.

func Width

func Width(cells int) Option

Width sets the dropdown width in terminal cells.

Width controls both the closed trigger and the open list box.

type SelectFn

type SelectFn func(index int, label string) error

SelectFn is called when the user commits a new dropdown selection.

The callback must be thread-safe because it is triggered from the keyboard and mouse event handling paths, which run in separate goroutines.

Directories

Path Synopsis
Binary dropdowndemo shows the functionality of a dropdown widget.
Binary dropdowndemo shows the functionality of a dropdown widget.

Jump to

Keyboard shortcuts

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