toggle

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 toggle is an on/off switch with a label, the Switch of web component libraries ("switch" is a Go keyword and cannot name a package). Space or Enter flips it when it has focus, Left turns it off and Right turns it on, and with the mouse on so does a click.

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

Example

Right turns a focused switch on.

package main

import (
	"fmt"

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

func main() {
	overwrite := toggle.New("Overwrite")
	overwrite.Focus()
	overwrite, _ = overwrite.Update(tui.Key{Type: tui.KeyRight})
	fmt.Println(ansi.StripANSI(overwrite.View()))
	fmt.Println(overwrite.On())
}
Output:
<  ●> Overwrite
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 switch's ID.
	ID string
	// On is the switch's state after the change.
	On bool
}

ChangedMsg is delivered (via the Cmd Update returns) when a key or a click turns the switch on or off.

type KeyMap

type KeyMap struct {
	Flip keymap.Binding // turn the switch on if it is off, and off if it is on
	Off  keymap.Binding // turn the switch off
	On   keymap.Binding // turn the switch on
}

KeyMap names the keys of each action of a Model.

func DefaultKeyMap

func DefaultKeyMap() KeyMap

DefaultKeyMap returns Space or Enter, Left and Right.

type Model

type Model struct {
	// ID is carried by ChangedMsg, to tell one switch's change from
	// another's.
	ID string
	// Label is the text after the switch. An empty Label draws the switch
	// alone.
	Label string
	// Disabled stops the switch being changed; it is drawn faint.
	Disabled bool
	Theme    theme.Theme

	// KeyMap holds the keys for each action. 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 switch or its label flips it. Off (the default) ignores the mouse.
	Mouse bool
	// Bounds is the screen rectangle where the app draws the switch.
	Bounds hittest.Rect
	// contains filtered or unexported fields
}

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

func New

func New(label string) Model

New returns a switch labelled label, turned off.

func (Model) Bindings

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

Bindings returns the actions the switch honours, with descriptions, for help text. A disabled one has none.

func (*Model) Blur

func (m *Model) Blur()

Blur takes keyboard focus away.

func (*Model) Focus

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

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

func (Model) Focused

func (m Model) Focused() bool

Focused reports whether the switch 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 switch as plain text for accessible output (see tui.Linearizer): "Overwrite, switch, on", with ", unavailable" when disabled.

func (Model) On

func (m Model) On() bool

On reports whether the switch is on.

func (*Model) SetOn

func (m *Model) SetOn(on bool)

SetOn turns the switch on or off without a key press: no ChangedMsg is delivered.

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.ComponentToggle, ...) and then by WithTokens.

func (Model) Update

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

Update flips the switch on Space or Enter, turns it off on Left and on on Right, when it has focus, and, with Mouse on, flips it on a left press inside Bounds. A disabled switch ignores both.

func (Model) View

func (m Model) View() string

View renders a track with a knob at its left end when off, "(○ )", and at its right end, filled, when on, "( ●)", followed by the label, so the state reads without colour in two ways. A switch that has focus is drawn with angle brackets, "< ●>", 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