From 330328d5ac6c633f117127b64b239631ca28277e Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sat, 13 Sep 2025 00:20:42 +0530 Subject: Added notifications --- src/daemon/pomo.rs | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 100 insertions(+), 2 deletions(-) (limited to 'src/daemon/pomo.rs') diff --git a/src/daemon/pomo.rs b/src/daemon/pomo.rs index d05fad8..9cabd1a 100644 --- a/src/daemon/pomo.rs +++ b/src/daemon/pomo.rs @@ -16,6 +16,7 @@ */ use bincode::{Encode, Decode}; +use notify_rust::Notification; use std::env; #[derive(Encode, Decode, PartialEq, Clone, Copy)] @@ -42,6 +43,7 @@ pub struct Pomo { pub secs_elapsed: u16, pub counter: u16, pub current_state: State, + pub notify: bool, } impl Pomo { @@ -51,6 +53,7 @@ impl Pomo { counter: 1, current_state: State::WorkIdle, + notify: get_envbool("NOTIFY", true), work_duration: get_envvar("WORK_DURATION", 25 * 60), break_duration: get_envvar("BREAK_DURATION", 5 * 60), long_break_duration: get_envvar("LONG_BREAK_DURATION", 20 * 60), @@ -78,6 +81,29 @@ impl Pomo { // start/unpause a cycle pub fn run(&mut self) { + if matches!(self.current_state, State::WorkIdle | State::BreakIdle | State::LongBreakIdle) { + if self.notify { + let _ = Notification::new() + .summary("Polydoro") + .body(match self.current_state { + State::WorkIdle => "The timer has been started. Get to work.", + State::BreakIdle => "The timer has been started. Take a break!", + State::LongBreakIdle => "The timer has been started. It's time for the much awaited long break!", + _ => "", // won't hit + }) + .show(); + } + } + + if matches!(self.current_state, State::WorkPaused | State::BreakPaused | State::LongBreakPaused) { + if self.notify { + let _ = Notification::new() + .summary("Polydoro") + .body("Unpaused the timer") + .show(); + } + } + self.current_state = match self.current_state { State::WorkIdle => State::Work, State::BreakIdle => State::Break, @@ -92,6 +118,15 @@ impl Pomo { } pub fn pause(&mut self) { + if matches!(self.current_state, State::Work | State::Break | State::LongBreak) { + if self.notify { + let _ = Notification::new() + .summary("Polydoro") + .body("Paused the timer") + .show(); + } + } + self.current_state = match self.current_state { State::Work => State::WorkPaused, State::Break => State::BreakPaused, @@ -119,6 +154,17 @@ impl Pomo { } else { State::BreakIdle }; + + if self.notify { + let _ = Notification::new() + .summary("Polydoro") + .body(if self.current_state == State::BreakIdle { + "Time to take a break!" + } else { + "Time to take a long break!" + }) + .show(); + } }, State::Break => { self.secs_elapsed = 0; @@ -126,8 +172,27 @@ impl Pomo { // because we move on to the next task after the break self.counter += 1; + + if self.notify { + let _ = Notification::new() + .summary("Polydoro") + .body(format!("Your break has ended. Get to work!").as_str()) + .show(); + } } - State::LongBreak => self.hard_reset(), + State::LongBreak => { + // just a hard reset but different notification + self.secs_elapsed = 0; + self.counter = 1; + self.current_state = State::WorkIdle; + + if self.notify { + let _ = Notification::new() + .summary("Polydoro") + .body(format!("Long break has ended. Get to work!").as_str()) + .show(); + } + }, _ => return, // paused and idle state can't be ended } } @@ -141,6 +206,13 @@ impl Pomo { State::LongBreak | State::LongBreakPaused => State::LongBreakIdle, _ => self.current_state, // if already in an idle state, don't change (probably won't hit anyways) }; + + if self.notify { + let _ = Notification::new() + .summary("Polydoro") + .body("Current timer has been reset") + .show(); + } } // reset all cycles @@ -148,6 +220,13 @@ impl Pomo { self.secs_elapsed = 0; self.counter = 1; self.current_state = State::WorkIdle; + + if self.notify { + let _ = Notification::new() + .summary("Polydoro") + .body("All timers have been reset") + .show(); + } } // conditionally do a soft or a hard reset @@ -157,8 +236,20 @@ impl Pomo { _ => self.soft_reset(), } } -} + pub fn toggle_notify(&mut self) { + self.notify = !self.notify; + + let _ = Notification::new() + .summary("Polydoro") + .body(if self.notify { + "Notifications have been turned on" + } else { + "Notifications have been turned off" + }) + .show(); + } +} fn get_envvar(key: &str, def: u16) -> u16 { env::var(key) @@ -166,3 +257,10 @@ fn get_envvar(key: &str, def: u16) -> u16 { .and_then(|v| v.parse::().ok()) .unwrap_or(def) } + +fn get_envbool(key: &str, def: bool) -> bool { + env::var(key) + .ok() + .and_then(|v| v.parse::().ok()) + .unwrap_or(def) +} -- cgit v1.2.3