clockview

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: 6 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 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 Mode

type Mode int

Mode selects what a Model tracks and renders.

const (
	// ModeClock renders the current wall-clock time.
	ModeClock Mode = iota
	// ModeStopwatch counts elapsed time up from zero.
	ModeStopwatch
	// ModeTimer counts remaining time down from Duration to zero.
	ModeTimer
)

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

func New(mode Mode) Model

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

func (m Model) LayoutNode() layout.Node

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

func (m Model) Linearize() string

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

func (m Model) Running() bool

Running reports whether the animation is currently running.

func (*Model) Start

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

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.

func (Model) Update

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

Update advances the model on each of its own ticks; a no-op for any other Msg, another Model's tick included, or while stopped. In ModeTimer, once elapsed reaches Duration it clamps, sets Running() false and stops rescheduling — further ticks are no-ops.

func (Model) View

func (m Model) View() string

View renders the model per its Mode: ModeStopwatch renders elapsed time counting up, ModeTimer renders remaining time (Duration - elapsed) counting down, both formatted as HH:MM:SS, and ModeClock renders m.Now().Format(m.Layout).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL