buttongroup

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 buttongroup is a row of buttons that take one place in the focus order: Left and Right move between them, and Enter or Space presses the one the cursor is on.

In ModeActions the buttons are plain and each press is an action. In ModeSingle and ModeMultiple they are toggle buttons and the group is a choice: one of them, or any number.

Stability: experimental. Its API may change in any minor release.

Example

A single-choice group: Right moves the cursor, Enter chooses.

package main

import (
	"fmt"

	"github.com/ows4444/tui"
	"github.com/ows4444/tui/ansi"
	"github.com/ows4444/tui/buttongroup"
)

func main() {
	align := buttongroup.New(buttongroup.ModeSingle, "Left", "Centre", "Right")
	align.Focus()
	align, _ = align.Update(tui.Key{Type: tui.KeyRight})
	align, _ = align.Update(tui.Key{Type: tui.KeyEnter})
	fmt.Println(ansi.StripANSI(align.View()))
	fmt.Println(align.On())
}
Output:
[ Left ] <●Centre > [ Right ]
[Centre]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChangedMsg

type ChangedMsg struct {
	// ID is the group's ID.
	ID string
	// Pressed is the ID of the button whose press made the change.
	Pressed string
	// On holds the IDs of the buttons that are on, left to right.
	On []string
}

ChangedMsg is delivered (via the Cmd Update returns) when a press changed which buttons of a ModeSingle or ModeMultiple group are on. It stands in place of the pressed button's own button.PressedMsg, so one press is one message. A ModeActions group delivers button.PressedMsg.

type KeyMap

type KeyMap struct {
	Prev  keymap.Binding // move to the button before
	Next  keymap.Binding // move to the button after
	First keymap.Binding // move to the first button
	Last  keymap.Binding // move to the last button
}

KeyMap names the keys that move a group's cursor.

func DefaultKeyMap

func DefaultKeyMap() KeyMap

DefaultKeyMap returns Left, Right, Home and End.

type Mode

type Mode int

Mode is what the buttons of a group are for.

const (
	// ModeActions is a row of plain buttons; a press is an action.
	ModeActions Mode = iota
	// ModeSingle is a row of toggle buttons of which at most one is on.
	ModeSingle
	// ModeMultiple is a row of toggle buttons, each on or off by itself.
	ModeMultiple
)

type Model

type Model struct {
	// ID is carried by ChangedMsg, to tell one group's change from another's.
	ID string
	// Buttons are the group's buttons, left to right. Each keeps its own
	// Label, ID, Variant, Size and Disabled; the group sets their focus,
	// their toggle state in ModeSingle, and their Mouse and Bounds.
	Buttons []button.Model
	Mode    Mode
	// Required, in ModeSingle, stops the button that is on being turned off
	// by pressing it again, so that once a choice is made one is always on.
	Required bool
	// Gap is the number of blank cells between two buttons.
	Gap int

	// KeyMap holds the keys that move the cursor. New fills it with
	// DefaultKeyMap; a Model built as a struct literal with a zero KeyMap
	// behaves as if it held DefaultKeyMap.
	KeyMap KeyMap

	// Mouse, when true, makes Update handle tui.MouseEvent: a click on a
	// button presses it and moves the cursor to it. Off (the default)
	// ignores the mouse.
	Mouse bool
	// Bounds is the screen rectangle where the app draws the group; its
	// left edge is the first button's.
	Bounds hittest.Rect
	// contains filtered or unexported fields
}

Model is a row of buttons with a cursor. The zero value is an empty ModeActions group; build one with New.

func New

func New(mode Mode, labels ...string) Model

New returns a group in the given mode with one button per label, a gap of one cell, and the cursor on the first button. Each button's ID is its label. In ModeSingle and ModeMultiple the buttons are toggle buttons.

func (Model) Bindings

func (m Model) Bindings() []keymap.Binding

Bindings returns the keys the group honours, for help text: its own, then those of the button under the cursor.

func (*Model) Blur

func (m *Model) Blur()

Blur takes keyboard focus away.

func (Model) Cursor

func (m Model) Cursor() int

Cursor returns the index of the button the cursor is on.

func (*Model) Focus

func (m *Model) Focus() tui.Cmd

Focus gives the group keyboard focus: the button under the cursor shows it, in angle brackets. It returns no Cmd; the result is there so a group can stand where any focusable widget does.

func (Model) Focused

func (m Model) Focused() bool

Focused reports whether the group has keyboard focus.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

LayoutNode adapts the group to a layout.Node of its own width on one row. The Model is not changed.

func (Model) Linearize

func (m Model) Linearize() string

Linearize renders the group as plain text for accessible output (see tui.Linearizer): one line per button, as the button reads itself, with its position, e.g. "Bold, toggle button, on, 1 of 3".

func (Model) On

func (m Model) On() []string

On returns the IDs of the buttons that are on, left to right. It is empty in ModeActions.

func (*Model) SetCursor

func (m *Model) SetCursor(i int)

SetCursor moves the cursor to button i. An index that is out of range, or names a disabled button, is ignored.

func (*Model) SetOn

func (m *Model) SetOn(ids ...string)

SetOn turns on the buttons with the given IDs and turns the others off, without a press: no message is delivered. In ModeSingle only the first of ids that names a button is turned on.

func (Model) SetTheme

func (m Model) SetTheme(t theme.Theme) Model

SetTheme returns m with t applied to every button. It makes Model a tui.ThemeSetter, so a root model can forward the Program's theme (see tui.WithTheme). The group draws nothing of its own but the gaps.

func (Model) Update

func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)

Update moves the cursor on Left, Right, Home and End, skipping disabled buttons and wrapping at the ends, and passes every other message to the button under the cursor, so Enter or Space presses it. With Mouse on, a click on a button moves the cursor to it and presses it. Only a group that has focus acts on keys.

func (Model) View

func (m Model) View() string

View renders the buttons on one row, Gap cells apart.

func (Model) Width

func (m Model) Width() int

Width is the number of cells View takes.

Jump to

Keyboard shortcuts

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