Refactor track_cover function:Refactor track_cover function to support custom cover path templates

This commit is contained in:
Anas Elgarhy 2023-02-23 13:40:17 +02:00
parent 1b85605917
commit 5420ec481d
No known key found for this signature in database
GPG key ID: 0501802A1D496528
3 changed files with 31 additions and 11 deletions

View file

@ -153,26 +153,39 @@ impl TrackCover {
/// If the track has an embedded cover, and `force_use_external_cover` is `true`, the function will search for an external cover.
#[inline]
pub fn track_cover(
track: &Track,
mut path: String,
track_name: &str,
max_depth: u8,
force_use_external_cover: bool,
no_use_external_cover: bool,
) -> TrackCover {
if !force_use_external_cover {
#[cfg(feature = "debug")]
info!("Trying to get the embedded cover of \"{track_path}\".", track_path = track.path);
if let Ok(Some(cover)) = get_embedded_art(&track.path) {
info!("Trying to get the embedded cover of \"{path}\".");
if let Ok(Some(cover)) = get_embedded_art(&path) {
return TrackCover::Embedded(cover);
}
}
if !no_use_external_cover {
let (Ok(regx), path) = (match path.split("/").last() {
Some(last_pat) if last_pat.contains("r#") => {
(regex::Regex::new(&*last_pat.replace("r#", "")),
// Remove the last part of the path
path.remove(path.len() - last_pat.len() - 1).to_string())
}
_ => (regex::Regex::new(&format!(r"({track_name}).*\.(jpg|jpeg|png|gif)$")), path),
}) else {
#[cfg(feature = "debug")]
info!("Could not get the cover.");
return TrackCover::None;
};
#[cfg(feature = "debug")]
info!("Trying to get the external cover of \"{track_path}\".", track_path = track.path);
info!("Trying to get the external cover of \"{path}\".");
if let Ok(Some(cover)) = search_for(
&track.path,
&path,
max_depth,
&regex::Regex::new(&format!(r"(cover|{track_name}).*\.(jpg|jpeg|png|gif)$", track_name = track.get_name())).unwrap(),
&regx,
) {
#[cfg(feature = "debug")]
info!("Found the external cover \"{cover}\".");
@ -186,7 +199,7 @@ pub fn track_cover(
}
#[cfg(feature = "debug")]
info!("Could not get the cover of \"{track_path}\".", track_path = track.path);
info!("Could not get the cover.");
TrackCover::None
}
@ -213,7 +226,7 @@ fn search(search_directory: &str, matcher: &regex::Regex) -> std::io::Result<Opt
#[inline(always)]
pub fn process_template_placeholders(
template: String,
track: &cmus::Track,
track: &Track,
player_settings: &cmus::player_settings::PlayerSettings,
) -> String {
let res = track.process(template);

View file

@ -1,7 +1,7 @@
use crate::cmus::events::CmusEvent;
use crate::cmus::player_settings::PlayerSettings;
use crate::cmus::query::CmusQueryResponse;
use crate::cmus::{Track, TrackStatus};
use crate::cmus::{TemplateProcessor, Track, TrackStatus};
use crate::settings::Settings;
use crate::{process_template_placeholders, settings, track_cover, TrackCover};
#[cfg(feature = "debug")]
@ -101,9 +101,16 @@ impl NotificationsHandler {
fn set_cover(&mut self, track: &Track) {
// Reset the notification
self.setup_the_notification();
let path = match &self.settings.cover_path_template {
Some(template) => {
track.process(template.clone())
}
None => track.path.clone(),
};
// Get the track cover and set it to notification
let track_cover = track_cover(
track,
path,
track.get_name(),
self.settings.depth(),
self.settings.force_use_external_cover,
self.settings.no_use_external_cover,

View file

@ -62,7 +62,7 @@ pub struct Settings {
///
/// If you not specify the full path, the cover will be started from the track's directory.
#[arg(short = 'w', long = "cover-path", default_value = None)]
cover_path_template: Option<String>,
pub cover_path_template: Option<String>,
#[cfg(feature = "lyrics")]
/// The lyrics file path, if not given, the lyrics will be searched in the track's directory
/// for a text file with the name "lyrics", or with the same name as the track.