Improve the artict
This commit is contained in:
parent
9a5a93450a
commit
d004461209
4 changed files with 164 additions and 88 deletions
|
@ -1,5 +1,8 @@
|
||||||
use crate::cmus::player_settings::{AAAMode, PlayerSettings, Shuffle};
|
use crate::cmus::player_settings::{AAAMode, PlayerSettings, Shuffle};
|
||||||
use crate::cmus::{Track, TrackStatus};
|
use crate::cmus::{Track, TrackStatus};
|
||||||
|
use crate::notification::Action;
|
||||||
|
use crate::process_template_placeholders;
|
||||||
|
use crate::settings::Settings;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum CmusEvent {
|
pub enum CmusEvent {
|
||||||
|
@ -11,3 +14,142 @@ pub enum CmusEvent {
|
||||||
RepeatChanged(Track, PlayerSettings),
|
RepeatChanged(Track, PlayerSettings),
|
||||||
AAAMode(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,
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -51,7 +51,7 @@ impl CmusQueryResponse {
|
||||||
if self.track_row.is_empty() || self.player_settings_row.is_empty() {
|
if self.track_row.is_empty() || self.player_settings_row.is_empty() {
|
||||||
#[cfg(feature = "debug")]
|
#[cfg(feature = "debug")]
|
||||||
info!("Cmus response is empty, returning empty events");
|
info!("Cmus response is empty, returning empty events");
|
||||||
return Ok(Vec::new())
|
return Ok(Vec::new());
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut events = Vec::new();
|
let mut events = Vec::new();
|
||||||
|
|
|
@ -209,7 +209,11 @@ fn search(search_directory: &str, matcher: ®ex::Regex) -> std::io::Result<Opt
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Replace all the placeholders in the template with their matching value.
|
/// Replace all the placeholders in the template with their matching value.
|
||||||
pub fn process_template_placeholders(template: &String, track: &cmus::Track) -> String {
|
pub fn process_template_placeholders(
|
||||||
|
template: &String,
|
||||||
|
track: &cmus::Track,
|
||||||
|
player_settings: &cmus::player_settings::PlayerSettings,
|
||||||
|
) -> String {
|
||||||
#[cfg(feature = "debug")]
|
#[cfg(feature = "debug")]
|
||||||
{
|
{
|
||||||
info!("Processing the template placeholders.");
|
info!("Processing the template placeholders.");
|
||||||
|
|
|
@ -8,12 +8,25 @@ use crate::{process_template_placeholders, track_cover, TrackCover};
|
||||||
use log::{debug, info};
|
use log::{debug, info};
|
||||||
use notify_rust::Notification;
|
use notify_rust::Notification;
|
||||||
|
|
||||||
enum Action {
|
pub enum Action {
|
||||||
Show,
|
Show,
|
||||||
Update,
|
Update,
|
||||||
None,
|
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 {
|
pub struct NotificationsHandler {
|
||||||
cover_set: bool,
|
cover_set: bool,
|
||||||
notification: Notification,
|
notification: Notification,
|
||||||
|
@ -46,36 +59,7 @@ impl NotificationsHandler {
|
||||||
#[cfg(feature = "debug")]
|
#[cfg(feature = "debug")]
|
||||||
info!("event: {:?}", event);
|
info!("event: {:?}", event);
|
||||||
|
|
||||||
match event {
|
action = event.build_notification(&mut self.settings, &mut self.notification);
|
||||||
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)?
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
match action {
|
match action {
|
||||||
Action::Show => {
|
Action::Show => {
|
||||||
|
@ -86,63 +70,9 @@ impl NotificationsHandler {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Ok(())
|
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)]
|
#[inline(always)]
|
||||||
fn update_cover(&mut self, first_event: &CmusEvent, response: &CmusQueryResponse) {
|
fn update_cover(&mut self, first_event: &CmusEvent, response: &CmusQueryResponse) {
|
||||||
// If the track is changed, we need to update the cover.
|
// If the track is changed, we need to update the cover.
|
||||||
|
|
Loading…
Reference in a new issue