Documentation
¶
Overview ¶
Package rating is a score out of a small maximum, shown as a row of filled and empty marks: Left and Right lower and raise it, a digit sets it, and with the mouse on a click on a mark sets it to that mark.
Stability: experimental. Its API may change in any minor release.
Example ¶
A digit sets the score.
package main
import (
"fmt"
"github.com/ows4444/tui"
"github.com/ows4444/tui/ansi"
"github.com/ows4444/tui/rating"
)
func main() {
stars := rating.New(5)
stars.ShowValue = true
stars.Focus()
stars, _ = stars.Update(tui.Key{Type: tui.KeyRunes, Text: "4"})
fmt.Println(ansi.StripANSI(stars.View()))
}
Output: <●●●●○> 4/5
Index ¶
- type ChangedMsg
- type KeyMap
- type Model
- func (m Model) Bindings() []keymap.Binding
- func (m *Model) Blur()
- 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 int)
- func (m Model) Tokens() theme.Tokens
- func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)
- func (m Model) Value() int
- 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 ¶
ChangedMsg is delivered (via the Cmd Update returns) when the score changes.
type KeyMap ¶
type KeyMap struct {
Dec keymap.Binding // lower the score by one
Inc keymap.Binding // raise the score by one
Clear keymap.Binding // set the score to 0
Full keymap.Binding // set the score to Max
}
KeyMap names the keys of each action of a Model. A digit key always sets the score to that digit and is not in the KeyMap.
func DefaultKeyMap ¶
func DefaultKeyMap() KeyMap
DefaultKeyMap returns Left and Right, Home or Backspace, and End.
type Model ¶
type Model struct {
// ID is carried by ChangedMsg, to tell one rating's change from
// another's.
ID string
// Max is the highest score, and the number of marks drawn. A Max under 1
// is read as 5.
Max int
// ReadOnly shows the score and takes no input; it is drawn as an
// enabled rating is.
ReadOnly bool
// Disabled takes no input and is drawn dimmed.
Disabled bool
// ShowValue draws the score after the marks, as "3/5".
ShowValue 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
// a mark sets the score to it, and a press on the mark the score is
// already at clears the score. Off (the default) ignores the mouse.
Mouse bool
// Bounds is the screen rectangle where the app draws the rating. The
// marks start one cell in from its left edge.
Bounds hittest.Rect
// contains filtered or unexported fields
}
Model is one rating. The zero value is not ready to use; build one with New.
func (Model) Bindings ¶
Bindings returns the actions the rating honours, with descriptions, for help text. A read-only or disabled rating has none.
func (*Model) Focus ¶
Focus gives the rating keyboard focus. It returns no Cmd; the result is there so a rating can stand where any focusable widget does.
func (Model) LayoutNode ¶
LayoutNode adapts the rating to a layout.Node of its own width on one row. The Model is not changed.
func (Model) Linearize ¶
Linearize renders the rating as plain text for accessible output (see tui.Linearizer): "rating, 3 of 5", with ", read only" or ", unavailable".
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 score, held between 0 and Max, 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.ComponentRating, ...) and then by WithTokens.
func (Model) Update ¶
Update lowers and raises the score on Left and Right, clears it on Home or Backspace, fills it on End, and sets it to a digit that is typed, when the rating has focus. With Mouse on, a left press on a mark sets the score to it; on the mark the score is at, it clears the score. A read-only or disabled rating ignores both.
func (Model) View ¶
View renders Max marks on one row, filled up to the score and empty after it, between two cells that hold angle brackets when the rating has focus and blanks when it does not, so focus reads without colour and does not change the width. A disabled rating is dimmed and held in parentheses. ShowValue adds the score, as "3/5", after a space.