feat: allow notifications to be configurable
This commit is contained in:
+37
-7
@@ -50,6 +50,7 @@ struct Termato {
|
|||||||
duration_in_secs: u32,
|
duration_in_secs: u32,
|
||||||
time_remaining_in_sec: u32,
|
time_remaining_in_sec: u32,
|
||||||
show_help: bool,
|
show_help: bool,
|
||||||
|
enable_notifications: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Termato {
|
impl Termato {
|
||||||
@@ -62,9 +63,15 @@ impl Termato {
|
|||||||
duration_in_secs: work_mins * SECONDS_PER_MIN,
|
duration_in_secs: work_mins * SECONDS_PER_MIN,
|
||||||
time_remaining_in_sec: work_mins * SECONDS_PER_MIN,
|
time_remaining_in_sec: work_mins * SECONDS_PER_MIN,
|
||||||
show_help: false,
|
show_help: false,
|
||||||
|
enable_notifications: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn with_notifications(mut self, enable: bool) -> Self {
|
||||||
|
self.enable_notifications = enable;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
fn toggle_running(&mut self) {
|
fn toggle_running(&mut self) {
|
||||||
self.is_running = !self.is_running;
|
self.is_running = !self.is_running;
|
||||||
}
|
}
|
||||||
@@ -86,9 +93,11 @@ impl Termato {
|
|||||||
self.time_remaining_in_sec -= 1;
|
self.time_remaining_in_sec -= 1;
|
||||||
|
|
||||||
if self.time_remaining_in_sec == 0 {
|
if self.time_remaining_in_sec == 0 {
|
||||||
match self.mode {
|
if self.enable_notifications {
|
||||||
TimerMode::Work => send_desktop_notification("Work Done", "Time to take a break."),
|
match self.mode {
|
||||||
TimerMode::Break => send_desktop_notification("Break Over", "Time to focus."),
|
TimerMode::Work => send_desktop_notification("Work Done", "Time to take a break."),
|
||||||
|
TimerMode::Break => send_desktop_notification("Break Over", "Time to focus."),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.toggle_mode();
|
self.toggle_mode();
|
||||||
@@ -130,7 +139,13 @@ fn main() -> Result<(), io::Error> {
|
|||||||
let args: Vec<String> = std::env::args().collect();
|
let args: Vec<String> = std::env::args().collect();
|
||||||
|
|
||||||
if args.iter().any(|arg| arg == "-h" || arg == "--help") {
|
if args.iter().any(|arg| arg == "-h" || arg == "--help") {
|
||||||
println!("Usage: termato [work_minutes] [break_minutes]");
|
println!("Usage: termato [options] [work_minutes] [break_minutes]");
|
||||||
|
println!();
|
||||||
|
println!("Options:");
|
||||||
|
println!(" -n, --notify Enable desktop notifications");
|
||||||
|
println!(" -h, --help Print this help message");
|
||||||
|
println!(" -v, --version Print the version number");
|
||||||
|
println!();
|
||||||
println!("Defaults: work_minutes = 25, break_minutes = 5");
|
println!("Defaults: work_minutes = 25, break_minutes = 5");
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
@@ -146,9 +161,15 @@ fn main() -> Result<(), io::Error> {
|
|||||||
let mut terminal = Terminal::new(backend)?;
|
let mut terminal = Terminal::new(backend)?;
|
||||||
let font = Toilet::smblock().unwrap();
|
let font = Toilet::smblock().unwrap();
|
||||||
|
|
||||||
let work_mins = args.get(1).and_then(|s| s.parse().ok()).unwrap_or(25);
|
let enable_notifications = args.iter().any(|arg| arg == "-n" || arg == "--notify");
|
||||||
let break_mins = args.get(2).and_then(|s| s.parse().ok()).unwrap_or(5);
|
let positional_args: Vec<&String> = args.iter()
|
||||||
let mut termato = Termato::new(work_mins, break_mins);
|
.skip(1)
|
||||||
|
.filter(|arg| *arg != "-n" && *arg != "--notify")
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let work_mins = positional_args.get(0).and_then(|s| s.parse().ok()).unwrap_or(25);
|
||||||
|
let break_mins = positional_args.get(1).and_then(|s| s.parse().ok()).unwrap_or(5);
|
||||||
|
let mut termato = Termato::new(work_mins, break_mins).with_notifications(enable_notifications);
|
||||||
|
|
||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
@@ -406,4 +427,13 @@ mod tests {
|
|||||||
|
|
||||||
assert!(!termato.show_help);
|
assert!(!termato.show_help);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn termato_notifications_when_called_it_should_verify_opt_in_behavior() {
|
||||||
|
let result = Termato::new(25, 5);
|
||||||
|
assert!(!result.enable_notifications);
|
||||||
|
|
||||||
|
let result = result.with_notifications(true);
|
||||||
|
assert!(result.enable_notifications);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user