Refactor track_cover
function:Refactor track_cover
function to support custom cover path templates
This commit is contained in:
parent
1b85605917
commit
5420ec481d
3 changed files with 31 additions and 11 deletions
29
src/lib.rs
29
src/lib.rs
|
@ -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.
|
/// If the track has an embedded cover, and `force_use_external_cover` is `true`, the function will search for an external cover.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn track_cover(
|
pub fn track_cover(
|
||||||
track: &Track,
|
mut path: String,
|
||||||
|
track_name: &str,
|
||||||
max_depth: u8,
|
max_depth: u8,
|
||||||
force_use_external_cover: bool,
|
force_use_external_cover: bool,
|
||||||
no_use_external_cover: bool,
|
no_use_external_cover: bool,
|
||||||
) -> TrackCover {
|
) -> TrackCover {
|
||||||
if !force_use_external_cover {
|
if !force_use_external_cover {
|
||||||
#[cfg(feature = "debug")]
|
#[cfg(feature = "debug")]
|
||||||
info!("Trying to get the embedded cover of \"{track_path}\".", track_path = track.path);
|
info!("Trying to get the embedded cover of \"{path}\".");
|
||||||
if let Ok(Some(cover)) = get_embedded_art(&track.path) {
|
if let Ok(Some(cover)) = get_embedded_art(&path) {
|
||||||
return TrackCover::Embedded(cover);
|
return TrackCover::Embedded(cover);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !no_use_external_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")]
|
#[cfg(feature = "debug")]
|
||||||
info!("Trying to get the external cover of \"{track_path}\".", track_path = track.path);
|
info!("Could not get the cover.");
|
||||||
|
return TrackCover::None;
|
||||||
|
};
|
||||||
|
#[cfg(feature = "debug")]
|
||||||
|
info!("Trying to get the external cover of \"{path}\".");
|
||||||
if let Ok(Some(cover)) = search_for(
|
if let Ok(Some(cover)) = search_for(
|
||||||
&track.path,
|
&path,
|
||||||
max_depth,
|
max_depth,
|
||||||
®ex::Regex::new(&format!(r"(cover|{track_name}).*\.(jpg|jpeg|png|gif)$", track_name = track.get_name())).unwrap(),
|
®x,
|
||||||
) {
|
) {
|
||||||
#[cfg(feature = "debug")]
|
#[cfg(feature = "debug")]
|
||||||
info!("Found the external cover \"{cover}\".");
|
info!("Found the external cover \"{cover}\".");
|
||||||
|
@ -186,7 +199,7 @@ pub fn track_cover(
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "debug")]
|
#[cfg(feature = "debug")]
|
||||||
info!("Could not get the cover of \"{track_path}\".", track_path = track.path);
|
info!("Could not get the cover.");
|
||||||
|
|
||||||
TrackCover::None
|
TrackCover::None
|
||||||
}
|
}
|
||||||
|
@ -213,7 +226,7 @@ fn search(search_directory: &str, matcher: ®ex::Regex) -> std::io::Result<Opt
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn process_template_placeholders(
|
pub fn process_template_placeholders(
|
||||||
template: String,
|
template: String,
|
||||||
track: &cmus::Track,
|
track: &Track,
|
||||||
player_settings: &cmus::player_settings::PlayerSettings,
|
player_settings: &cmus::player_settings::PlayerSettings,
|
||||||
) -> String {
|
) -> String {
|
||||||
let res = track.process(template);
|
let res = track.process(template);
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::cmus::events::CmusEvent;
|
use crate::cmus::events::CmusEvent;
|
||||||
use crate::cmus::player_settings::PlayerSettings;
|
use crate::cmus::player_settings::PlayerSettings;
|
||||||
use crate::cmus::query::CmusQueryResponse;
|
use crate::cmus::query::CmusQueryResponse;
|
||||||
use crate::cmus::{Track, TrackStatus};
|
use crate::cmus::{TemplateProcessor, Track, TrackStatus};
|
||||||
use crate::settings::Settings;
|
use crate::settings::Settings;
|
||||||
use crate::{process_template_placeholders, settings, track_cover, TrackCover};
|
use crate::{process_template_placeholders, settings, track_cover, TrackCover};
|
||||||
#[cfg(feature = "debug")]
|
#[cfg(feature = "debug")]
|
||||||
|
@ -101,9 +101,16 @@ impl NotificationsHandler {
|
||||||
fn set_cover(&mut self, track: &Track) {
|
fn set_cover(&mut self, track: &Track) {
|
||||||
// Reset the notification
|
// Reset the notification
|
||||||
self.setup_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
|
// Get the track cover and set it to notification
|
||||||
let track_cover = track_cover(
|
let track_cover = track_cover(
|
||||||
track,
|
path,
|
||||||
|
track.get_name(),
|
||||||
self.settings.depth(),
|
self.settings.depth(),
|
||||||
self.settings.force_use_external_cover,
|
self.settings.force_use_external_cover,
|
||||||
self.settings.no_use_external_cover,
|
self.settings.no_use_external_cover,
|
||||||
|
|
|
@ -62,7 +62,7 @@ pub struct Settings {
|
||||||
///
|
///
|
||||||
/// If you not specify the full path, the cover will be started from the track's directory.
|
/// 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)]
|
#[arg(short = 'w', long = "cover-path", default_value = None)]
|
||||||
cover_path_template: Option<String>,
|
pub cover_path_template: Option<String>,
|
||||||
#[cfg(feature = "lyrics")]
|
#[cfg(feature = "lyrics")]
|
||||||
/// The lyrics file path, if not given, the lyrics will be searched in the track's directory
|
/// 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.
|
/// for a text file with the name "lyrics", or with the same name as the track.
|
||||||
|
|
Loading…
Reference in a new issue