diff --git a/src/config.rs b/src/config.rs index 8caa90d..2f5a062 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,8 +4,6 @@ use std::collections::HashMap; use std::io::Write; use std::path::{Path, PathBuf}; -//use anyhow::Result; - /// Represents a [`PodcastConfig`] value that is either enabled, disabled, or we defer to the /// global config. #[derive(Clone, Copy, Debug, Default)] diff --git a/src/main.rs b/src/main.rs index 9399ec0..844bf86 100644 --- a/src/main.rs +++ b/src/main.rs @@ -66,7 +66,6 @@ async fn main() { return; } - eprintln!("Checking for new episodes..."); let mp = (!args.quiet).then_some(MultiProgress::new()); let podcasts = { @@ -78,17 +77,21 @@ async fn main() { }; // Longest podcast name is used for formatting. - let Some(longest_name) = podcasts + let longest_name = match podcasts .iter() .map(|podcast| podcast.name().chars().count()) .max() - else { - eprintln!("no podcasts configured"); - std::process::exit(1); + { + Some(len) => len, + None => { + eprintln!("no podcasts configured"); + std::process::exit(1); + } }; let mut futures = vec![]; + eprintln!("Checking for new episodes..."); for podcast in podcasts { let future = tokio::task::spawn(async move { podcast.sync(longest_name).await }); futures.push(future); diff --git a/src/podcast.rs b/src/podcast.rs index f9eee97..5f03613 100644 --- a/src/podcast.rs +++ b/src/podcast.rs @@ -409,12 +409,6 @@ impl Podcast { } } - fn finish_with_msg(&self, msg: String) { - if let Some(pb) = &self.progress_bar { - pb.finish_with_message(msg); - } - } - pub async fn download_episode<'a>(&self, episode: Episode<'a>) -> DownloadedEpisode<'a> { let partial_path = { let file_name = format!("{}.partial", episode.guid); @@ -512,25 +506,33 @@ impl Podcast { &self, episode: &DownloadedEpisode, ) -> Option> { - if let Some(script_path) = self.config.download_hook.clone() { - let path = episode.path.clone(); - let handle = tokio::task::spawn_blocking(move || { - let _ = std::process::Command::new(script_path).arg(path).output(); - }); + let script_path = self.config.download_hook.clone()?; + let path = episode.path.clone(); - return Some(handle); - } + let handle = tokio::task::spawn_blocking(move || { + let _ = std::process::Command::new(script_path).arg(path).output(); + }); - None + Some(handle) + } + + fn mark_complete(&self) { + if let Some(pb) = &self.progress_bar { + self.set_template("{msg}"); + let msg = format!("✅ {}", &self.name); + pb.finish_with_message(msg); + } } pub async fn sync(&self, longest_podcast_name: usize) -> Vec { self.set_download_style(); + let episodes = self.pending_episodes(); let episode_qty = episodes.len(); let mut downloaded = vec![]; let mut hook_handles = vec![]; + for (index, episode) in episodes.into_iter().enumerate() { self.show_download_info(&episode, index, longest_podcast_name, episode_qty); let mut downloaded_episode = self.download_episode(episode).await; @@ -545,10 +547,7 @@ impl Podcast { futures::future::join_all(hook_handles).await; } - self.set_template("{msg}"); - let msg = format!("✅ {}", &self.name); - self.finish_with_msg(msg); - + self.mark_complete(); downloaded.into_iter().map(|episode| episode.path).collect() } }