Documentation
¶
Overview ¶
Package clockview is a wall-clock / stopwatch / countdown-timer widget. All three share the same motion-driven animation mechanism (spinner.Model's self-rescheduling tick pattern), and stopwatch/timer share the same elapsed-time mechanism — only View and the tick's effect on elapsed differ per Mode, so this is one Model type with a Mode field rather than three separate types.
Example ¶
A stopwatch starts at zero and runs only after Start, which returns the Cmd that schedules its first tick.
package main
import (
"fmt"
"github.com/ows4444/tui/ansi"
"github.com/ows4444/tui/clockview"
)
func main() {
m := clockview.New(clockview.ModeStopwatch)
fmt.Println(m.Running(), ansi.StripANSI(m.View()))
_ = m.Start()
fmt.Println(m.Running())
m.Stop()
fmt.Println(m.Running())
}
Output: false 00:00:00 true false
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Model ¶
type Model struct {
Mode Mode
// Duration is the countdown total in ModeTimer; ignored otherwise.
Duration time.Duration
// Interval is the tick rate.
Interval time.Duration
// Layout is the time.Format layout used in ModeClock.
Layout string
// Now returns the current time; injectable for testability. Defaults
// to time.Now in New.
Now func() time.Time
// contains filtered or unexported fields
}
Model animates a clock, stopwatch or timer depending on Mode. It uses the same self-rescheduling motion tick pattern spinner.Model does: each tick's Cmd only reschedules itself while still running, so Stop needs no explicit cancellation.
func New ¶
New returns an idle Model in the given Mode with sane defaults: a 1s Interval, a "15:04:05" clock Layout and time.Now for Now. Set Duration (ModeTimer) or Layout (ModeClock) on the returned Model as needed.
func (Model) LayoutNode ¶
LayoutNode adapts the reading to a layout.Node: it measures and renders whatever View shows (for a clock, read afresh each time), cut to the allotted Size. The Model is not changed.
func (Model) Linearize ¶
Linearize renders the clock as plain text for accessible output (see tui.Linearizer): its kind and reading, and for a stopwatch or timer whether it is running, e.g. "Timer, 00:09 remaining, running". The reading is the same text View shows.
func (*Model) Start ¶
Start begins the animation; return the Cmd it produces from your own Init or Update so it actually runs. Call it on the model your program keeps: in an Init with a value receiver it would run on a copy, and the kept model would ignore the ticks. Start where the model is built and have Init return that Cmd.
func (*Model) Stop ¶
func (m *Model) Stop()
Stop ends the animation without resetting elapsed. A tick already on its way is dropped even if Start is called again before it arrives.