From b53689584a0e4efbcec091229eff0c47f6a38b35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Sat, 29 Jul 2023 17:14:41 +0200 Subject: [PATCH] refactor: factor out notification sending --- src/daemon.rs | 32 +++++++++----------------------- src/main.rs | 9 +++++---- src/notification.rs | 8 ++++++++ src/timer.rs | 7 ++----- 4 files changed, 24 insertions(+), 32 deletions(-) create mode 100644 src/notification.rs diff --git a/src/daemon.rs b/src/daemon.rs index 9a08f99..7b15c35 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -1,7 +1,7 @@ +use crate::notification::send_notifictation; use crate::pomodoro::Pomodoro; pub use crate::timer::Timer; use anyhow::Context; -use notify_rust::Notification; use serde::{Deserialize, Serialize}; use std::fmt::{Display, Formatter}; use std::{ @@ -100,21 +100,14 @@ impl Daemon { } if self.notify { - match Notification::new() - .summary("󰀠 Timers") - .body( - format!( - "Started timer {} for {}", - &name, - humantime::format_duration(duration) - ) - .as_str(), + send_notifictation( + format!( + "Started timer {} for {}", + &name, + humantime::format_duration(duration) ) - .show() - { - Ok(_) => println!("Sent notification sucessfully."), - Err(_) => println!("Failed to send notification."), - }; + .as_str(), + ); } let timer = Timer::new(name, duration); @@ -135,14 +128,7 @@ impl Daemon { long_pause, pauses_till_long, } => { - match Notification::new() - .summary("󰀠 Timers") - .body("Started pomodoro.") - .show() - { - Ok(_) => println!("Sent notification sucessfully."), - Err(_) => println!("Failed to send notification."), - }; + send_notifictation("Started pomodoro."); self.pomodoro = Some(Pomodoro::new(work, pause, long_pause, pauses_till_long)); Ok(Answer::Ok) } diff --git a/src/main.rs b/src/main.rs index 4af8bb2..b7cb0ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,8 @@ -pub mod cli; -pub mod daemon; -pub mod pomodoro; -pub mod timer; +mod cli; +mod daemon; +mod pomodoro; +mod timer; +mod notification; use crate::cli::{send_command, Cli, Command as CliCommand}; use crate::daemon::{Command as DaemonCommand, Daemon}; diff --git a/src/notification.rs b/src/notification.rs new file mode 100644 index 0000000..28be7e5 --- /dev/null +++ b/src/notification.rs @@ -0,0 +1,8 @@ +use notify_rust::Notification; + +pub fn send_notifictation(msg: &str) { + match Notification::new().summary("󰀠 Timers").body(msg).show() { + Ok(_) => println!("Sent notification sucessfully."), + Err(_) => println!("Failed to send notification."), + }; +} diff --git a/src/timer.rs b/src/timer.rs index 8e4a953..44c929c 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -1,4 +1,4 @@ -use notify_rust::Notification; +use crate::notification::send_notifictation; use serde::{Deserialize, Serialize}; use std::{ fmt::{Display, Formatter}, @@ -74,10 +74,7 @@ impl Timer { let msg = format!("Timer {} has expired!", self.name); println!("{}", &msg); if notify { - match Notification::new().summary("󰀠 Timers").body(&msg).show() { - Ok(_) => println!("Sent notification sucessfully."), - Err(_) => println!("Failed to send notification."), - } + send_notifictation(msg.as_str()); } } }