Add cross-platform notification support

This commit is contained in:
2020-08-30 22:15:51 -05:00
parent 6954e3dfd8
commit 4a1633da1f
4 changed files with 453 additions and 7 deletions

View File

@@ -1,7 +1,7 @@
use domasi::Pomodoro;
fn main() {
let mut timer: Pomodoro = Default::default();
let mut domasi: Pomodoro = Default::default();
timer.start();
domasi.start();
}

View File

@@ -1,3 +1,4 @@
use std::error::Error;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::io::{self, Write};
use std::thread;
@@ -11,11 +12,12 @@ pub struct Pomodoro {
paused: PausedState,
}
static POLLING_RATE: Duration = Duration::from_millis(300);
static POMODORO_CYCLES: u64 = 5;
static WORK_TIME: u64 = 2;
static SBREAK_TIME: u64 = 2;
static LBREAK_TIME: u64 = 2;
static POLLING_RATE: Duration = Duration::from_millis(300);
impl Pomodoro {
pub fn new() -> Self {
Self::default()
@@ -30,6 +32,60 @@ impl Pomodoro {
}
}
fn notify_complete(&self) -> Result<(), Box<dyn Error>> {
let mut toast = notify_rust::Notification::new();
toast.summary("Pomodoro cycle complete!");
toast.body("domasi will now exit.");
toast.show()?;
Ok(())
}
fn notify(&self) -> Result<(), Box<dyn Error>> {
fn pmdr_cycle_count(i: u64) -> String {
let i = i / 4;
match i {
1 => "After this, you will have completed 1 cycle.".to_string(),
_ => format!("After this, you will have completed {} cycles.", i),
}
}
fn work_sesh_num(i: u64) -> &'static str {
match i % 4 {
0 => "Time for your 1st work session.",
1 => "Time for your 2nd work session.",
2 => "Time for your 3rd work session.",
3 => "Time for your 4th work session.",
_ => unreachable!(),
}
}
let mut toast = notify_rust::Notification::new();
match self.state {
State::Inactive => {}
State::Work => {
toast
.summary(work_sesh_num(self.count))
.body("Remember to stay focused.")
.show()?;
}
State::ShortBreak => {
toast
.summary("Time for a quick break.")
.body("Sit back and relax.")
.show()?;
}
State::LongBreak => {
toast
.summary("Enjoy your long break!")
.body(&pmdr_cycle_count(self.count))
.show()?;
}
};
Ok(())
}
fn poll(&mut self) -> Status {
let status = self.check();
@@ -59,10 +115,13 @@ impl Pomodoro {
State::Work | State::ShortBreak | State::LongBreak => Some(Instant::now()),
};
let _ = self.notify();
self.display_time();
}
Status::Complete => {
println!("\rPomodoro Cycle Complete!");
println!("\rPomodoro cycle complete!");
let _ = self.notify_complete();
}
Status::Inactive => {}
};
@@ -81,6 +140,10 @@ impl Pomodoro {
fn print(text: &str) -> io::Result<()> {
let out = io::stdout();
let mut handle = out.lock();
// Empty String so that we can clear line before writing
// from the mostleft side of the terminal again.
handle.write_all(b"\r ")?;
handle.write_all(text.as_bytes())?;
handle.flush()?;
Ok(())
@@ -118,10 +181,10 @@ impl Pomodoro {
}
}
None => {
if self.count == 0 {
Status::NextState
} else {
if self.count / 4 == POMODORO_CYCLES {
Status::Complete
} else {
Status::NextState
}
}
}