slider

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: 9 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 slider is a value chosen from a range by moving a thumb along a track: Left and Right move it one step, Page Up and Page Down a larger one, Home and End to either end. With the mouse on, a press or a drag on the track puts the thumb under the pointer.

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

Example

Page Up raises the value by ten steps.

package main

import (
	"fmt"

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

func main() {
	volume := slider.New(0, 100)
	volume.Width = 11
	volume.ShowValue = true
	volume.Focus()
	for i := 0; i < 4; i++ {
		volume, _ = volume.Update(tui.Key{Type: tui.KeyPgUp})
	}
	fmt.Println(ansi.StripANSI(volume.View()))
}
Output:
<████●░░░░░░> 40

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 slider's ID.
	ID string
	// Value is the new value.
	Value float64
}

ChangedMsg is delivered (via the Cmd Update returns) when the value changes.

type KeyMap

type KeyMap struct {
	Dec    keymap.Binding // lower the value by Step
	Inc    keymap.Binding // raise the value by Step
	DecBig keymap.Binding // lower the value by BigStep
	IncBig keymap.Binding // raise the value by BigStep
	ToMin  keymap.Binding // set the value to Min
	ToMax  keymap.Binding // set the value to Max
}

KeyMap names the keys of each action of a Model.

func DefaultKeyMap

func DefaultKeyMap() KeyMap

DefaultKeyMap returns the arrow keys, Page Up and Page Down, Home and End.

type Model

type Model struct {
	// ID is carried by ChangedMsg, to tell one slider's change from
	// another's.
	ID string
	// Min and Max are the ends of the range. A Max below Min is read as Min.
	Min, Max float64
	// Step is how far Left and Right move the value, and what the value is
	// rounded to. A Step of 0 or less is read as 1.
	Step float64
	// BigStep is how far Page Up and Page Down move the value. A BigStep of
	// 0 or less is read as ten Steps.
	BigStep float64
	// Width is the length of the track in cells. A Width under 2 is read as
	// 2.
	Width int
	// ShowValue draws the value after the track.
	ShowValue bool
	// Format turns the value into the text ShowValue draws and Linearize
	// reads. Nil prints the shortest decimal that is exact.
	Format func(float64) string
	// Disabled stops the value being changed; the slider is drawn dimmed.
	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 track moves the thumb there, and it follows the pointer until the
	// button comes up. Off (the default) ignores the mouse.
	Mouse bool
	// Bounds is the screen rectangle where the app draws the slider. The
	// track starts one cell in from its left edge.
	Bounds hittest.Rect
	// contains filtered or unexported fields
}

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

func New

func New(min, max float64) Model

New returns a slider over min to max with a Step of 1, a track of 20 cells, and its value at min.

func (Model) Bindings

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

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

func (*Model) Blur

func (m *Model) Blur()

Blur takes keyboard focus away, and lets go of a drag in progress.

func (Model) Dragging

func (m Model) Dragging() bool

Dragging reports whether the pointer is holding the thumb.

func (*Model) Focus

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

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

func (Model) Focused

func (m Model) Focused() bool

Focused reports whether the slider has keyboard focus.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

LayoutNode adapts the slider 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 slider as plain text for accessible output (see tui.Linearizer): "slider, 40, from 0 to 100", with ", unavailable" when disabled.

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) SetValue

func (m *Model) SetValue(v float64)

SetValue sets the value, held inside the range and rounded to a Step, without a key press: no ChangedMsg is delivered.

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

func (Model) Update

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

Update changes the value on the arrow keys, Page Up and Page Down, Home and End when the slider has focus, and, with Mouse on, on a press or a drag along the track. A disabled slider ignores both.

func (Model) Value

func (m Model) Value() float64

Value returns the slider's value.

func (Model) View

func (m Model) View() string

View renders the track on one row: the part below the value filled, the thumb, and the rest empty, between two cells that hold angle brackets when the slider has focus and blanks when it does not, so focus reads without colour and does not change the width. A disabled slider is dimmed and held in parentheses. ShowValue adds the value after a space.

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