From d004461209e2afc28edc8f5a7a1e556487083567 Mon Sep 17 00:00:00 2001 From: Anas Elgarhy Date: Mon, 20 Feb 2023 23:04:38 +0200 Subject: [PATCH] Improve the artict --- src/cmus/events.rs | 142 ++++++++++++++++++++++++++++++++++++++++++++ src/cmus/query.rs | 2 +- src/lib.rs | 6 +- src/notification.rs | 102 +++++-------------------------- 4 files changed, 164 insertions(+), 88 deletions(-) diff --git a/src/cmus/events.rs b/src/cmus/events.rs index cde1535..e1b74af 100644 --- a/src/cmus/events.rs +++ b/src/cmus/events.rs @@ -1,5 +1,8 @@ use crate::cmus::player_settings::{AAAMode, PlayerSettings, Shuffle}; use crate::cmus::{Track, TrackStatus}; +use crate::notification::Action; +use crate::process_template_placeholders; +use crate::settings::Settings; #[derive(Debug, PartialEq)] pub enum CmusEvent { @@ -11,3 +14,142 @@ pub enum CmusEvent { RepeatChanged(Track, PlayerSettings), AAAMode(Track, PlayerSettings), } + +impl CmusEvent { + pub fn build_notification( + &self, + settings: &Settings, + notification: &mut notify_rust::Notification, + ) -> Action { + use CmusEvent::*; + match self { + StatusChanged(_, _) | TrackChanged(_, _) => { + let (body, body_action) = self.build_notification_body(settings); + let (summary, summary_action) = self.build_notification_summary(settings); + notification.body(&body).summary(&summary); + body_action.max(summary_action) + } + VolumeChanged(_, _) + | PositionChanged(_, _) + | ShuffleChanged(_, _) + | RepeatChanged(_, _) + | AAAMode(_, _) + if settings.show_player_notifications => + { + let (body, body_action) = self.build_notification_body(settings); + let (summary, summary_action) = self.build_notification_summary(settings); + notification.body(&body).summary(&summary); + body_action.max(summary_action) + } + _ => Action::None, + } + } + + #[inline] + fn build_notification_body(&self, settings: &Settings) -> (String, Action) { + use CmusEvent::*; + match self { + StatusChanged(track, player_settings) => ( + process_template_placeholders( + &settings.status_notification_body, + track, + player_settings, + ), + Action::Show, + ), + TrackChanged(track, player_settings) => ( + process_template_placeholders(&settings.body, track, player_settings), + Action::Show, + ), + VolumeChanged(track, player_settings) => ( + process_template_placeholders( + &settings.volume_notification_body, + track, + player_settings, + ), + Action::Show, + ), + PositionChanged(_track, _player_settings) => { + (String::new(), Action::None) // TODO: Implement this + } + ShuffleChanged(track, player_settings) => ( + process_template_placeholders( + &settings.shuffle_notification_body, + track, + player_settings, + ), + Action::Show, + ), + RepeatChanged(track, player_settings) => ( + process_template_placeholders( + &settings.repeat_notification_body, + track, + player_settings, + ), + Action::Show, + ), + AAAMode(track, player_settings) => ( + process_template_placeholders( + &settings.aaa_mode_notification_body, + track, + player_settings, + ), + Action::Show, + ), + } + } + + #[inline] + fn build_notification_summary(&self, settings: &Settings) -> (String, Action) { + use CmusEvent::*; + match self { + StatusChanged(track, player_settings) => ( + process_template_placeholders( + &settings.status_notification_summary, + track, + player_settings, + ), + Action::Show, + ), + TrackChanged(track, player_settings) => ( + process_template_placeholders(&settings.summary, track, player_settings), + Action::Show, + ), + VolumeChanged(track, player_settings) => ( + process_template_placeholders( + &settings.volume_notification_summary, + track, + player_settings, + ), + Action::Show, + ), + PositionChanged(_track, _player_settings) => { + (String::new(), Action::None) // TODO: Implement this + } + ShuffleChanged(track, player_settings) => ( + process_template_placeholders( + &settings.shuffle_notification_summary, + track, + player_settings, + ), + Action::Show, + ), + RepeatChanged(track, player_settings) => ( + process_template_placeholders( + &settings.repeat_notification_summary, + track, + player_settings, + ), + Action::Show, + ), + AAAMode(track, player_settings) => ( + process_template_placeholders( + &settings.aaa_mode_notification_summary, + track, + player_settings, + ), + Action::Show, + ), + } + } +} diff --git a/src/cmus/query.rs b/src/cmus/query.rs index ddf00aa..468f0db 100644 --- a/src/cmus/query.rs +++ b/src/cmus/query.rs @@ -51,7 +51,7 @@ impl CmusQueryResponse { if self.track_row.is_empty() || self.player_settings_row.is_empty() { #[cfg(feature = "debug")] info!("Cmus response is empty, returning empty events"); - return Ok(Vec::new()) + return Ok(Vec::new()); } let mut events = Vec::new(); diff --git a/src/lib.rs b/src/lib.rs index 93ad38f..9095113 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -209,7 +209,11 @@ fn search(search_directory: &str, matcher: ®ex::Regex) -> std::io::Result String { +pub fn process_template_placeholders( + template: &String, + track: &cmus::Track, + player_settings: &cmus::player_settings::PlayerSettings, +) -> String { #[cfg(feature = "debug")] { info!("Processing the template placeholders."); diff --git a/src/notification.rs b/src/notification.rs index 1d4b702..f02dc06 100644 --- a/src/notification.rs +++ b/src/notification.rs @@ -8,12 +8,25 @@ use crate::{process_template_placeholders, track_cover, TrackCover}; use log::{debug, info}; use notify_rust::Notification; -enum Action { +pub enum Action { Show, Update, None, } +impl Action { + pub fn max(self, other: Self) -> Self { + match (self, other) { + (Action::Show, Action::Show) => Action::Show, + (Action::Show, Action::Update) | (Action::Update, Action::Show | Action::Update) => { + Action::Update + } + (Action::None, _) => Action::None, + (_, Action::None) => Action::None, + } + } +} + pub struct NotificationsHandler { cover_set: bool, notification: Notification, @@ -46,36 +59,7 @@ impl NotificationsHandler { #[cfg(feature = "debug")] info!("event: {:?}", event); - match event { - CmusEvent::StatusChanged(track, player_settings) => { - #[cfg(feature = "debug")] - debug!("Status changed: {:?}", track.status); - action = self.build_status_notification(track, player_settings); - } - CmusEvent::TrackChanged(track, player_settings) => { - #[cfg(feature = "debug")] - debug!("Track changed: {:?}", track); - action = self.build_track_notification(track, player_settings); - } - CmusEvent::VolumeChanged(track, player_settings) - if self.settings.show_player_notifications => - { - action = self.build_volume_notification(track, player_settings); - } - /* - CmusEvent::PositionChanged(position) => todo!(), - CmusEvent::ShuffleChanged(shuffle) if settings.show_player_notifications => { - build_shuffle_notification(shuffle, settings, notification)? - } - CmusEvent::RepeatChanged(repeat) if settings.show_player_notifications => { - build_repeat_notification(repeat, settings, notification)? - } - CmusEvent::AAAMode(aaa_mode) if settings.show_player_notifications => { - build_aaa_mode_notification(aaa_mode, settings, notification)? - } - */ - _ => {} - } + action = event.build_notification(&mut self.settings, &mut self.notification); match action { Action::Show => { @@ -86,63 +70,9 @@ impl NotificationsHandler { }; } - Ok(()) } - #[inline(always)] - fn build_status_notification( - &mut self, - track: Track, - player_settings: PlayerSettings, - ) -> Action { - // Set the summary and body of the notification. - self.notification - .summary( - process_template_placeholders(&self.settings.status_notification_summary, &track) - .as_str(), - ) - .body( - process_template_placeholders(&self.settings.status_notification_body, &track) - .as_str(), - ) - .timeout(self.settings.status_notification_timeout as i32 * 1000); - Action::Show - } - - #[inline(always)] - fn build_track_notification( - &mut self, - track: Track, - player_settings: PlayerSettings, - ) -> Action { - // Set the summary and body of the notification. - self.notification - .summary(process_template_placeholders(&self.settings.summary, &track).as_str()) - .body(process_template_placeholders(&self.settings.body, &track).as_str()); - - Action::Show - } - - #[inline(always)] - fn build_volume_notification( - &mut self, - track: Track, - player_settings: PlayerSettings, - ) -> Action { - self.notification - .summary( - process_template_placeholders(&self.settings.volume_notification_summary, &track) - .as_str(), - ) - .body( - process_template_placeholders(&self.settings.volume_notification_body, &track) - .as_str(), - ); - - Action::Show - } - #[inline(always)] fn update_cover(&mut self, first_event: &CmusEvent, response: &CmusQueryResponse) { // If the track is changed, we need to update the cover. @@ -179,7 +109,7 @@ impl NotificationsHandler { self.settings.force_use_external_cover, self.settings.no_use_external_cover, ) - .set_notification_image(&mut self.notification); + .set_notification_image(&mut self.notification); // Flip the change flag self.cover_set = true; }