Crate zentime_rs_timer

Crate zentime_rs_timer 

Source
Expand description

Pomodoro/Productivity timer that can transition between various states ([Paused]/[Running]), tracks intervals and can be configured.

§Example

use std::sync::mpsc::{self, RecvTimeoutError};
use std::sync::mpsc::{Receiver, Sender};
use std::thread;
use std::rc::Rc;
use std::time::Duration;
use zentime_rs_timer::config::PomodoroTimerConfig;
use zentime_rs_timer::pomodoro_timer_action::PomodoroTimerAction;
use zentime_rs_timer::pomodoro_timer::{ PomodoroTimer, TimerKind, ViewState };

    let (terminal_input_sender, terminal_input_receiver): (Sender<PomodoroTimerAction>, Receiver<PomodoroTimerAction>) =
        mpsc::channel();
    let (view_sender, view_receiver): (Sender<ViewState>, Receiver<ViewState>) =
        mpsc::channel();

    let config = PomodoroTimerConfig::default();

    // Run timer in its own thread so it does not block the current one
    thread::spawn(move || {
        let timer = PomodoroTimer::new(
            config,
            Rc::new(move |state, msg, _| {
                println!("{} {}", state.round, msg.unwrap());
            }),
            Rc::new(move |view_state| -> Option<PomodoroTimerAction> {
                view_sender.send(view_state).unwrap();

                let input = terminal_input_receiver.recv_timeout(Duration::from_millis(100));

                match input {
                    Ok(action) => Some(action),
                    Err(RecvTimeoutError::Disconnected) => std::process::exit(0),
                    _ => None,
                }
            }),
        );

        timer.init();
    });

    let action_jh = thread::spawn(move || {
        // Start the timer
        terminal_input_sender.send(PomodoroTimerAction::PlayPause).unwrap();

        // Render current timer state three seconds in a row
        for _ in 0..3 {
            thread::sleep(Duration::from_millis(100));
            if let Ok(state) = view_receiver.recv() {
                println!("{}", state.time)
            }
        }

    });

    action_jh.join().unwrap();

Re-exports§

pub use timer::Timer;
pub use timer_action::TimerAction;

Modules§

config
Configuration of a [Timer]
pomodoro_timer
Pomodoro timer implementation. When instantiated this runs instances of [Timer] internally and allows the transitioning between various states like [Interval], [ShortBreak] or [LongBreak].
pomodoro_timer_action
Action enum that can be passed to the timer on each tick to interact with it
timer
Timer implementation The timer hast the ability to toggle playback and end. It will call a given on_tick closure on every tick update and an on_timer_end-closure, when it’s done (either by receiving a TimerAction::End) or when the internal timer is down to 0
timer_action
Action enum that can be passed to the timer on each tick to interact with it
util
Small helper fns