Module stopwatch

Source
Expand description

High-precision stopwatch component for measuring elapsed time in Bubble Tea applications.

This module provides a fully-featured stopwatch that can be started, stopped, paused, and reset through Bubble Tea’s message-driven architecture. It mirrors the functionality of Go’s bubbles stopwatch component while providing Rust-idiomatic interfaces and memory safety guarantees.

§Features

  • Precise timing: Uses Rust’s Duration type for high-precision time measurement
  • Configurable intervals: Customizable tick frequency from nanoseconds to seconds
  • Multiple instances: Each stopwatch has a unique ID for managing multiple timers
  • Start/stop/pause/reset: Full control over stopwatch lifecycle
  • Go-compatible formatting: Duration display matches Go’s time.Duration format
  • Message-driven: Integrates seamlessly with Bubble Tea’s update cycle

§Quick Start

use bubbletea_widgets::stopwatch::{new, Model};
use bubbletea_rs::{Model as BubbleTeaModel, Msg};

// Create a stopwatch with 1-second precision
let mut stopwatch = new();

// Start timing
let start_cmd = stopwatch.start();

// In your update loop, handle the start message
// stopwatch.update(start_msg); // This would start the timer

// Check elapsed time
println!("Elapsed: {}", stopwatch.view()); // "0s" initially

§Integration with Bubble Tea

use bubbletea_widgets::stopwatch::{new, Model as StopwatchModel};
use bubbletea_rs::{Model as BubbleTeaModel, Cmd, Msg};

struct App {
    stopwatch: StopwatchModel,
}

impl BubbleTeaModel for App {
    fn init() -> (Self, Option<Cmd>) {
        let stopwatch = new();
        let start_cmd = stopwatch.start();
        (
            App { stopwatch },
            Some(start_cmd)
        )
    }

    fn update(&mut self, msg: Msg) -> Option<Cmd> {
        // Forward messages to the stopwatch
        self.stopwatch.update(msg)
    }

    fn view(&self) -> String {
        format!("Elapsed time: {}", self.stopwatch.view())
    }
}

§Custom Tick Intervals

use bubbletea_widgets::stopwatch::new_with_interval;
use std::time::Duration;

// High-precision stopwatch updating every 10ms
let precise_stopwatch = new_with_interval(Duration::from_millis(10));

// Low-precision stopwatch updating every 5 seconds
let coarse_stopwatch = new_with_interval(Duration::from_secs(5));

§Message Types

The stopwatch communicates through three message types:

  • StartStopMsg: Controls running state (start/stop/pause)
  • ResetMsg: Resets elapsed time to zero
  • TickMsg: Internal timing pulses (automatically generated)

§Performance Notes

  • Each stopwatch instance has minimal memory overhead (< 64 bytes)
  • Tick frequency directly impacts CPU usage - choose appropriately for your use case
  • Multiple stopwatches can run concurrently without interference
  • Duration formatting is optimized for common time ranges

Structs§

Model
A high-precision stopwatch for measuring elapsed time in Bubble Tea applications.
ResetMsg
Message to reset a stopwatch’s elapsed time to zero.
StartStopMsg
Message to control the running state of a stopwatch.
TickMsg
Message sent on every stopwatch tick to increment the elapsed time.

Functions§

new
Creates a new stopwatch with a default 1-second tick interval.
new_with_interval
Creates a new stopwatch with a custom tick interval.