rating

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: 8 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 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

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 rating's ID.
	ID string
	// Value is the new score.
	Value int
}

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 New

func New(max int) Model

New returns a rating out of max with a score of 0.

func (Model) Bindings

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

Bindings returns the actions the rating honours, with descriptions, for help text. A read-only or disabled rating 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 rating keyboard focus. It returns no Cmd; the result is there so a rating can stand where any focusable widget does.

func (Model) Focused

func (m Model) Focused() bool

Focused reports whether the rating has keyboard focus.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

LayoutNode adapts the rating 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 rating as plain text for accessible output (see tui.Linearizer): "rating, 3 of 5", with ", read only" or ", unavailable".

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

SetValue sets the score, held between 0 and Max, 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.ComponentRating, ...) and then by WithTokens.

func (Model) Update

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

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

func (m Model) Value() int

Value returns the score, from 0 to Max.

func (Model) View

func (m Model) View() string

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.

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