From d29b1655e1e927f075fb12ca08e0593acd5a10d1 Mon Sep 17 00:00:00 2001 From: Fergus Argyll Date: Mon, 8 Sep 2025 23:33:50 +0300 Subject: [PATCH] Feat: added command line flag to truncate filenames --- src/config.rs | 4 ++++ src/downloads.rs | 9 ++++++++- src/main.rs | 11 ++++++++++- src/main_controller.rs | 1 + 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/config.rs b/src/config.rs index 39b0260..080cea7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -57,6 +57,7 @@ pub struct Config pub colors: AppColors, pub filename_prefix: String, pub filename_suffix: String, + pub short_filename: bool, } /// A temporary struct used to deserialize data from the TOML configuration @@ -73,6 +74,7 @@ struct ConfigFromToml colors: Option, filename_prefix: Option, filename_suffix: Option, + short_filename: Option, } /// A temporary struct used to deserialize keybinding data from the TOML @@ -202,6 +204,7 @@ impl Config colors: Some(colors), filename_prefix: None, filename_suffix: None, + short_filename: None, } } }; @@ -315,6 +318,7 @@ fn config_with_defaults(config_toml: ConfigFromToml) -> Result colors: colors, filename_prefix: filename_prefix, filename_suffix: filename_suffix, + short_filename: config_toml.short_filename.unwrap_or(false), }); } diff --git a/src/downloads.rs b/src/downloads.rs index 80597b1..3c43426 100644 --- a/src/downloads.rs +++ b/src/downloads.rs @@ -43,6 +43,7 @@ pub fn download_list( max_retries: usize, filename_prefix: &str, filename_suffix: &str, + short_filename: bool, threadpool: &Threadpool, tx_to_main: Sender, ) { @@ -54,7 +55,7 @@ pub fn download_list( let prefix = filename_prefix.to_owned(); let suffix = filename_suffix.to_owned(); threadpool.execute(move || { - let result = download_file(ep, dest2, max_retries, prefix, suffix); + let result = download_file(ep, dest2, max_retries, prefix, suffix, short_filename); tx.send(Message::Dl(result)) .expect("Thread messaging error"); }); @@ -69,6 +70,7 @@ fn download_file( mut max_retries: usize, filename_prefix: String, filename_suffix: String, + short_filename: bool, ) -> DownloadMsg { let agent_builder = ureq::builder() @@ -133,6 +135,11 @@ fn download_file( pubdate.format(&filename_suffix) ); } + + // If short_filename is enabled, truncate to 140 characters to stay under Windows path limits + if short_filename && file_name.len() > 140 { + file_name.truncate(140); + } let mut file_path = dest; file_path.push(format!("{file_name}.{ext}")); diff --git a/src/main.rs b/src/main.rs index 701f9e4..7c8f443 100644 --- a/src/main.rs +++ b/src/main.rs @@ -79,6 +79,14 @@ fn main() -> Result<()> "Sets a custom config file location. Can also be set with environment variable." ) ) + .arg(Arg::new("short-filename") + .short('s') + .long("short-filename") + .global(true) + .help( + "Truncates filenames to 140 characters to stay under Windows path length limits." + ) + ) .subcommand(Command::new("sync") .about("Syncs all podcasts in database") .arg(Arg::new("quiet") @@ -132,7 +140,8 @@ fn main() -> Result<()> ); process::exit(1); }); - let config = Config::new(&config_path)?; + let mut config = Config::new(&config_path)?; + config.short_filename = args.is_present("short-filename"); let mut db_path = config_path; if !db_path.pop() diff --git a/src/main_controller.rs b/src/main_controller.rs index 98e7b82..c8ca15d 100644 --- a/src/main_controller.rs +++ b/src/main_controller.rs @@ -667,6 +667,7 @@ impl MainController self.config.max_retries, &self.config.filename_prefix, &self.config.filename_suffix, + self.config.short_filename, &self.threadpool, self.tx_to_main.clone(), );