aboutsummaryrefslogtreecommitdiff
path: root/src/pomd.rs
diff options
context:
space:
mode:
authorLibravatar Silas Bartha <[email protected]>2024-01-04 22:49:55 -0500
committerLibravatar Silas Bartha <[email protected]>2024-01-04 22:49:55 -0500
commitad825d48308091927e57f35df0d7acfa1a7b7366 (patch)
tree8270270e4b791df802234c977bd0b34442e1c6e3 /src/pomd.rs
parent37c7a239b81102d2d1a75b9542a91468676cbd98 (diff)
Refactor + Document
Diffstat (limited to 'src/pomd.rs')
-rw-r--r--src/pomd.rs82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/pomd.rs b/src/pomd.rs
new file mode 100644
index 0000000..5a6679f
--- /dev/null
+++ b/src/pomd.rs
@@ -0,0 +1,82 @@
+use std::time::Duration;
+
+use pausable_clock::{PausableClock, PausableInstant};
+
+use notify_rust::Notification;
+
+use crate::config::PomdConfig;
+
+/// Represents the current state of the program
+pub struct Pomd {
+ pub config: PomdConfig,
+ pub duration: Duration,
+ pub iteration: u8,
+ pub on_break: bool,
+ pub clock: PausableClock,
+ pub start: PausableInstant,
+}
+
+impl Pomd {
+ /// Creates a new instance of this struct with a given configuration
+ pub fn new(config: PomdConfig) -> Self {
+ let clock = PausableClock::new(Duration::ZERO, true);
+ let start = clock.now();
+ Self {
+ config,
+ duration: Duration::from_secs_f32(config.work_duration),
+ iteration: 0,
+ on_break: false,
+ clock,
+ start,
+ }
+ }
+
+ /// Check whether sufficient time has elapsed to enter next iteration of cycle
+ pub fn update(&mut self) {
+ if self.duration < self.start.elapsed(&self.clock) {
+ if self.config.notify {
+ self.notify();
+ }
+ self.setup_next_iteration();
+ }
+ }
+
+ /// Resets state for next iteration
+ pub fn setup_next_iteration(&mut self) {
+ // Stop clock until user restarts it
+ self.clock.pause();
+
+ self.start = self.clock.now();
+ self.on_break ^= true;
+ self.duration = if self.on_break {
+ // Long break on last iteration
+ if self.iteration == self.config.num_iterations - 1 {
+ Duration::from_secs_f32(self.config.long_break_duration)
+ } else {
+ Duration::from_secs_f32(self.config.short_break_duration)
+ }
+ } else {
+ self.iteration = (self.iteration + 1) % self.config.num_iterations;
+ Duration::from_secs_f32(self.config.work_duration)
+ }
+ }
+
+ /// Displays a system notification
+ pub fn notify(&self) {
+ Notification::new()
+ .summary(
+ &(if self.on_break {
+ "Break Complete".to_string()
+ } else {
+ format!(
+ "Pomodoro Complete ({}/{})",
+ self.iteration + 1,
+ self.config.num_iterations
+ )
+ }),
+ )
+ .body("Click to dismiss")
+ .show()
+ .unwrap();
+ }
+}