radio

package
v0.21.0 Latest Latest
Warning

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

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

Documentation

Overview

Package radio implements an interactive radio button group widget.

Index

Constants

View Source
const (
	// DefaultSelectedRune is the default indicator rune for the selected item.
	DefaultSelectedRune = '◉'
	// DefaultUnselectedRune is the default indicator rune for an unselected item.
	DefaultUnselectedRune = '○'
	// DefaultGap is the default number of cells between radio items.
	DefaultGap = 3
	// DefaultIndicatorGap is the default number of cells between indicator and label.
	DefaultIndicatorGap = 1
)

Variables

View Source
var (
	DefaultCellOpts         = []cell.Option{cell.FgColor(cell.ColorWhite)}
	DefaultSelectedCellOpts = []cell.Option{cell.FgColor(cell.ColorCyan)}
	IndicatorSets           = struct {
		Circle  IndicatorSet
		Square  IndicatorSet
		Diamond IndicatorSet
		Target  IndicatorSet
	}{
		Circle:  IndicatorSet{Selected: "◉", Unselected: "○"},
		Square:  IndicatorSet{Selected: "■", Unselected: "□"},
		Diamond: IndicatorSet{Selected: "◆", Unselected: "◇"},
		Target:  IndicatorSet{Selected: "◎", Unselected: "·"},
	}
)

Default styling used by radio items when the caller doesn't provide item specific values.

Functions

This section is empty.

Types

type ChangeFn

type ChangeFn func(index int, label string) error

ChangeFn is called when the user selects a radio option.

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

type IndicatorSet

type IndicatorSet struct {
	Selected   string
	Unselected string
}

IndicatorSet defines the UTF-8 strings used to render selected and unselected radio indicators.

type Item

type Item struct {
	Label            string
	SelectedText     string
	UnselectedText   string
	SelectedRune     rune
	UnselectedRune   rune
	CellOpts         []cell.Option
	SelectedCellOpts []cell.Option
}

Item represents one selectable radio option.

type Option

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

Option is used to provide options.

func Gap

func Gap(cells int) Option

Gap sets the number of blank cells between radio items.

func IndicatorGap

func IndicatorGap(cells int) Option

IndicatorGap sets the number of blank cells between the indicator and label.

func Indicators

func Indicators(selected, unselected string) Option

Indicators sets the default UTF-8 strings used for radio indicators.

func OnChange

func OnChange(fn ChangeFn) Option

OnChange sets the radio widget'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 Selected

func Selected(index int) Option

Selected sets the initially selected item index.

func UseIndicatorSet

func UseIndicatorSet(set IndicatorSet) Option

UseIndicatorSet sets the default indicators from a reusable group.

type Radio

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

Radio lets the user choose exactly one item from a fixed list of options.

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

func New

func New(items []Item, opts ...Option) (*Radio, error)

New returns a new Radio for the provided items.

func (*Radio) Draw

func (r *Radio) Draw(cvs *canvas.Canvas, meta *widgetapi.Meta) error

Draw draws the Radio widget onto the canvas. Implements widgetapi.Widget.Draw.

func (*Radio) ItemText

func (r *Radio) ItemText(index int) string

ItemText returns the rendered text for the requested item.

func (*Radio) Keyboard

func (r *Radio) Keyboard(k *terminalapi.Keyboard, meta *widgetapi.EventMeta) error

Keyboard processes keyboard events for the radio group. Implements widgetapi.Widget.Keyboard.

func (*Radio) Mouse

func (r *Radio) Mouse(m *terminalapi.Mouse, meta *widgetapi.EventMeta) error

Mouse processes mouse events for the radio group. Implements widgetapi.Widget.Mouse.

func (*Radio) Options

func (r *Radio) Options() widgetapi.Options

Options implements widgetapi.Widget.Options.

func (*Radio) SelectedIndex

func (r *Radio) SelectedIndex() int

SelectedIndex returns the current selected item index.

func (*Radio) SelectedText

func (r *Radio) SelectedText() string

SelectedText returns the current selected item label.

func (*Radio) SetSelected

func (r *Radio) SetSelected(index int) error

SetSelected replaces the current selected item.

func (*Radio) Text

func (r *Radio) Text() string

Text returns the radio group's current rendered one-line text.

Directories

Path Synopsis
Binary radiodemo shows the functionality of a radio widget.
Binary radiodemo shows the functionality of a radio widget.

Jump to

Keyboard shortcuts

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