checkbox

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: 6 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 checkbox is one box that is checked or not, with a label: Space checks and unchecks it when it has focus, and with the mouse on so does a click. For a list of them with one cursor, see multiselect.

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

Example

Space checks a focused checkbox.

package main

import (
	"fmt"

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

func main() {
	keep := checkbox.New("Keep metadata")
	keep.Focus()
	keep, _ = keep.Update(tui.Key{Type: tui.KeySpace})
	fmt.Println(ansi.StripANSI(keep.View()))
	fmt.Println(keep.Checked())
}
Output:
<x> Keep metadata
true

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 checkbox's ID.
	ID string
	// Checked is the box's state after the change.
	Checked bool
}

ChangedMsg is delivered (via the Cmd Update returns) when a key or a click checks or unchecks the box.

type KeyMap

type KeyMap struct {
	Toggle keymap.Binding // check or uncheck the box
}

KeyMap names the key of a Model's one action.

func DefaultKeyMap

func DefaultKeyMap() KeyMap

DefaultKeyMap returns Space. Enter is left out, since in a form it moves to the next field.

type Model

type Model struct {
	// ID is carried by ChangedMsg, to tell one checkbox's change from
	// another's.
	ID string
	// Label is the text after the box. An empty Label draws the box alone.
	Label string
	// Indeterminate draws the box as "[-]": neither checked nor unchecked,
	// as the box over a list some of whose items are checked. The next press
	// checks it and clears Indeterminate.
	Indeterminate bool
	// Disabled stops the box being changed; it is drawn faint.
	Disabled bool
	Theme    theme.Theme

	// KeyMap holds the key that checks and unchecks the box. 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 left press on
	// the box or its label checks or unchecks it. Off (the default) ignores
	// the mouse.
	Mouse bool
	// Bounds is the screen rectangle where the app draws the checkbox.
	Bounds hittest.Rect
	// contains filtered or unexported fields
}

Model is one checkbox. The zero value is not ready to use; build one with New.

func New

func New(label string) Model

New returns an unchecked checkbox labelled label.

func (Model) Bindings

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

Bindings returns the action the checkbox honours, for help text. A disabled one has none.

func (*Model) Blur

func (m *Model) Blur()

Blur takes keyboard focus away.

func (Model) Checked

func (m Model) Checked() bool

Checked reports whether the box is checked. An indeterminate box is not.

func (*Model) Focus

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

Focus gives the checkbox keyboard focus. It returns no Cmd; the result is there so a checkbox can stand where any focusable widget does.

func (Model) Focused

func (m Model) Focused() bool

Focused reports whether the checkbox has keyboard focus.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

LayoutNode adapts the widget 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 checkbox as plain text for accessible output (see tui.Linearizer): "Keep metadata, checkbox, checked", with "not checked" or "partly checked", and ", unavailable" when disabled.

func (*Model) SetChecked

func (m *Model) SetChecked(checked bool)

SetChecked checks or unchecks the box without a key press: no ChangedMsg is delivered. It clears Indeterminate.

func (Model) SetTheme

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

SetTheme returns m with t applied. It makes Model a tui.ThemeSetter, so a root model can forward the Program's theme (see tui.WithTheme).

func (Model) Tokens

func (m Model) Tokens() theme.Tokens

Tokens returns the colour tokens the widget renders with: its theme's roles, overridden by any theme.WithTokens(theme.ComponentCheckbox, ...) and then by WithTokens.

func (Model) Update

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

Update checks or unchecks the box on Space when it has focus, and, with Mouse on, on a left press inside Bounds. A disabled checkbox ignores both.

func (Model) View

func (m Model) View() string

View renders "[x] Label", "[ ] Label" or, when Indeterminate, "[-] Label", so the state reads without colour. A checkbox that has focus is drawn with angle brackets, "<x> Label", in the focus style; a disabled one is faint.

func (Model) Width

func (m Model) Width() int

Width is the number of cells View takes.

func (Model) WithTokens

func (m Model) WithTokens(tok theme.Tokens) Model

WithTokens returns m with tok as its per-instance colour override. Nil fields inherit from the theme, so only the roles tok names change; the theme and every other widget are untouched. A second call replaces the first.

Jump to

Keyboard shortcuts

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