From b4941442cd438a15c7b4da7355875a9fc3e6728a Mon Sep 17 00:00:00 2001 From: KeksNino Date: Wed, 28 Oct 2026 16:42:44 +0100 Subject: [PATCH] feat: add new -n flag to write imported file path to m3u file --- src/main.rs | 7 +++++-- src/move_files.rs | 30 +++++++++++++++++++++++------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 27588dc..2036ea7 100755 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,8 @@ enum Commands { files: Vec, #[arg(short, long, default_value_t = false)] copy: bool, + #[arg(short, long, default_value_t = false)] + new: bool, }, Show { files: Vec, @@ -41,7 +43,7 @@ fn main() -> Result<(), Box> { .map(str::to_string); match &args.command { - Commands::Import { files, copy } => { + Commands::Import { files, copy, new } => { for file in files { if file.is_dir() { println!("Importing folder: {:?}", file); @@ -59,7 +61,7 @@ fn main() -> Result<(), Box> { let tag = get_metadata(&path); if let Err(e) = - move_files(path.clone(), tag?, extra_files.clone(), *copy, true) + move_files(path.clone(), tag?, extra_files.clone(), *copy, true, *new) { eprintln!("Failed to import {:?}: {}", path, e); } @@ -73,6 +75,7 @@ fn main() -> Result<(), Box> { extra_files.clone(), *copy, false, + *new, )?; } } diff --git a/src/move_files.rs b/src/move_files.rs index 61d7779..010980a 100644 --- a/src/move_files.rs +++ b/src/move_files.rs @@ -5,7 +5,7 @@ use lofty::tag::Accessor; use lofty::tag::Tag; use std::env::var; use std::fs::OpenOptions; -use std::fs::{copy, create_dir, create_dir_all, remove_dir, rename, File}; +use std::fs::{copy, create_dir, create_dir_all, remove_dir, rename}; use std::io::Write; use std::path::PathBuf; @@ -15,6 +15,7 @@ pub fn move_files( extra_files: [String; 9], should_copy: bool, has_parent: bool, + add_new_playlist: bool, ) -> Result<(), Box> { let target_dir = PathBuf::from("/home/user/Music/Tracks"); @@ -54,9 +55,13 @@ pub fn move_files( }; if should_copy { - copy(&file_path, target.join(file_name))?; + copy(&file_path, target.join(&file_name))?; } else { - rename(&file_path, target.join(file_name))?; + rename(&file_path, target.join(&file_name))?; + } + + if add_new_playlist { + new_playlist(target.join(file_name))?; } for ext in &extra_files { @@ -98,6 +103,16 @@ fn fix_filename(name: &str) -> String { .collect() } +fn new_playlist(path: PathBuf) -> std::result::Result<(), std::io::Error> { + let new_playlist_path = PathBuf::from("/home/user/Music/Playlists/active - new.m3u"); + let mut new_playlist = OpenOptions::new() + .append(true) + .open(new_playlist_path) + .unwrap(); + println!("{:?}", path); + writeln!(new_playlist, "{}", path.display()) +} + fn write_log(file_path: PathBuf) -> Result<(), Box> { let home_dir: PathBuf = PathBuf::from(var("HOME").expect("HOME is not set")); let data_dir = home_dir.join(".local").join("share").join("music-sort"); @@ -106,11 +121,12 @@ fn write_log(file_path: PathBuf) -> Result<(), Box> { if !data_dir.exists() { create_dir(&data_dir)?; } - if !log_file_path.exists() { - File::create(&log_file_path)?; - } - let mut log_file = OpenOptions::new().append(true).open(log_file_path).unwrap(); + let mut log_file = OpenOptions::new() + .append(true) + .create(true) + .open(log_file_path) + .unwrap(); if let Err(e) = writeln!( log_file,