Module timer

Module timer 

Source
Expand description

Timer component for Bubble Tea applications.

Package timer provides a simple timeout component for Bubble Tea applications. It closely matches the Go bubbles timer component API for 1-1 compatibility.

§Basic Usage

use bubbletea_widgets::timer::{new, new_with_interval};
use std::time::Duration;

// Create a timer with default 1 second interval
let timer = new(Duration::from_secs(30));

// Create a timer with custom interval
let timer = new_with_interval(Duration::from_secs(60), Duration::from_millis(100));

§bubbletea-rs Integration

use bubbletea_rs::{Model as BubbleTeaModel, Msg, Cmd};
use bubbletea_widgets::timer::{new, Model, TickMsg, StartStopMsg, TimeoutMsg};
use std::time::Duration;

struct MyApp {
    timer: Model,
}

impl BubbleTeaModel for MyApp {
    fn init() -> (Self, Option<Cmd>) {
        let timer = new(Duration::from_secs(10));
        let cmd = timer.init();
        (Self { timer }, Some(cmd))
    }

    fn update(&mut self, msg: Msg) -> Option<Cmd> {
        // Handle timeout
        if let Some(timeout) = msg.downcast_ref::<TimeoutMsg>() {
            if timeout.id == self.timer.id() {
                // Timer finished!
            }
        }
         
        // Forward timer messages
        self.timer.update(msg)
    }

    fn view(&self) -> String {
        format!("Time remaining: {}", self.timer.view())
    }
}

§Start/Stop Control

use bubbletea_widgets::timer::new;
use std::time::Duration;

let timer = new(Duration::from_secs(30));

// These return commands that send StartStopMsg
let start_cmd = timer.start();   // Resume timer
let stop_cmd = timer.stop();     // Pause timer  
let toggle_cmd = timer.toggle(); // Toggle running state

Structs§

Model
High-precision countdown timer component for Bubble Tea applications.
StartStopMsg
Message used to start and stop timer instances.
TickMsg
Message sent on every timer tick to update the countdown.
TimeoutMsg
Message sent when a timer reaches zero and expires.

Functions§

new
Creates a new timer with the specified timeout and default 1-second interval.
new_with_interval
Creates a new timer with custom timeout and tick interval.