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 ¶
- type ChangedMsg
- type KeyMap
- type Model
- func (m Model) Bindings() []keymap.Binding
- func (m *Model) Blur()
- func (m Model) Dragging() bool
- func (m *Model) Focus() tui.Cmd
- func (m Model) Focused() bool
- func (m Model) LayoutNode() layout.Node
- func (m Model) Linearize() string
- func (m Model) SetTheme(t theme.Theme) Model
- func (m *Model) SetValue(v float64)
- func (m Model) Tokens() theme.Tokens
- func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)
- func (m Model) Value() float64
- func (m Model) View() string
- func (m Model) WithTokens(tok theme.Tokens) Model
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 ¶
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 ¶
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) Focus ¶
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) LayoutNode ¶
LayoutNode adapts the slider to a layout.Node of its own width on one row. The Model is not changed.
func (Model) Linearize ¶
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 ¶
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 ¶
SetValue sets the value, held inside the range and rounded to a Step, without a key press: no ChangedMsg is delivered.
func (Model) 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 ¶
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) View ¶
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.