1 Commits

Author SHA1 Message Date
KeksNino 0a79ece1bb feat: WIP dialog when copying non music files 2026-11-22 01:56:05 +01:00
2 changed files with 45 additions and 52 deletions
+29 -30
View File
@@ -3,8 +3,8 @@ use lofty::file::TaggedFileExt;
use lofty::probe::Probe; use lofty::probe::Probe;
use std::ffi::OsStr; use std::ffi::OsStr;
use std::fs::read_dir; use std::fs::read_dir;
use std::io;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::Command;
mod move_files; mod move_files;
use move_files::move_files; use move_files::move_files;
@@ -27,8 +27,6 @@ enum Commands {
copy: bool, copy: bool,
#[arg(short, long, default_value_t = false)] #[arg(short, long, default_value_t = false)]
new: bool, new: bool,
#[arg(short, long, default_value_t = false)]
update_mpd: bool,
}, },
Show { Show {
files: Vec<PathBuf>, files: Vec<PathBuf>,
@@ -46,12 +44,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.map(str::to_string); .map(str::to_string);
match &args.command { match &args.command {
Commands::Import { Commands::Import { files, copy, new } => {
files,
copy,
new,
update_mpd,
} => {
for file in files { for file in files {
if file.is_dir() { if file.is_dir() {
println!("Importing folder: {:?}", file); println!("Importing folder: {:?}", file);
@@ -64,39 +57,45 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.is_some_and(|ext| extra_files.iter().any(|x| x == ext)) .is_some_and(|ext| extra_files.iter().any(|x| x == ext))
{ {
continue; continue;
} else {
let mut input = String::new();
eprintln!("Found files that aren't music files: {}", path.display());
eprintln!(" [1] Copy all files");
eprintln!(" [2] Skip non-music files");
eprintln!(" [3] Cancel import");
eprint!("> ");
io::stdin()
.read_line(&mut input)
.expect("failed to read input");
let mut other_files: Vec<PathBuf>;
match input.trim() {
"1" => {
other_files.extend(&path);
}
"2" => {}
_ => {
println!("Invalid option.");
}
}
} }
let tag = get_metadata(&path); let tag = get_metadata(&path);
if let Err(e) = move_files( if let Err(e) =
path.clone(), move_files(path.clone(), tag?, &extra_files, *copy, true, *new)
tag?, {
&extra_files,
*copy,
file.to_path_buf(),
true,
*new,
) {
eprintln!("Failed to import {:?}: {}", path, e); eprintln!("Failed to import {:?}: {}", path, e);
} }
} }
} else { } else {
println!("Importing file: {:?}", file); println!("Importing file: {:?}", file);
let tag = get_metadata(file); let tag = get_metadata(file);
move_files( move_files(file.clone(), tag?.clone(), &extra_files, *copy, false, *new)?;
file.clone(),
tag?.clone(),
&extra_files,
*copy,
file.to_path_buf(),
false,
*new,
)?;
} }
} }
if *update_mpd {
Command::new("mpc").arg("update").spawn()?;
}
} }
Commands::Show { files } => { Commands::Show { files } => {
+16 -22
View File
@@ -5,7 +5,7 @@ use lofty::tag::Accessor;
use lofty::tag::Tag; use lofty::tag::Tag;
use std::env::var; use std::env::var;
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::fs::{File, copy, create_dir, create_dir_all, remove_dir, rename}; use std::fs::{copy, create_dir, create_dir_all, remove_dir, rename};
use std::io::Write; use std::io::Write;
use std::path::PathBuf; use std::path::PathBuf;
@@ -14,7 +14,6 @@ pub fn move_files(
tag: Tag, tag: Tag,
extra_files: &[String], extra_files: &[String],
should_copy: bool, should_copy: bool,
folder_path: PathBuf,
has_parent: bool, has_parent: bool,
add_new_playlist: bool, add_new_playlist: bool,
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn std::error::Error>> {
@@ -61,6 +60,10 @@ pub fn move_files(
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 { for ext in extra_files {
let pattern = file_path.parent().unwrap().join(format!("*.{}", ext)); let pattern = file_path.parent().unwrap().join(format!("*.{}", ext));
@@ -86,17 +89,7 @@ pub fn move_files(
remove_dir(file_path.parent().unwrap())?; remove_dir(file_path.parent().unwrap())?;
} }
if add_new_playlist { write_log(file_path)?;
new_playlist(target.join(file_name))?;
}
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");
let log_file_path = data_dir.join("verbose.log");
write_log(file_path.clone(), log_file_path, data_dir.clone())?;
let log_file_path = data_dir.join("music-sort.log");
write_log(folder_path, log_file_path, data_dir)?;
Ok(()) Ok(())
} }
@@ -119,19 +112,20 @@ fn new_playlist(path: PathBuf) -> std::result::Result<(), std::io::Error> {
writeln!(new_playlist, "{}", path.display()) writeln!(new_playlist, "{}", path.display())
} }
fn write_log( fn write_log(file_path: PathBuf) -> Result<(), Box<dyn std::error::Error>> {
file_path: PathBuf, let home_dir: PathBuf = PathBuf::from(var("HOME").expect("HOME is not set"));
log_path: PathBuf, let data_dir = home_dir.join(".local").join("share").join("music-sort");
data_dir: PathBuf, let log_file_path = data_dir.join("music-sort.log");
) -> Result<(), Box<dyn std::error::Error>> {
if !data_dir.exists() { if !data_dir.exists() {
create_dir(&data_dir)?; create_dir(&data_dir)?;
} }
if !log_path.exists() {
File::create(&log_path)?;
}
let mut log_file = OpenOptions::new().append(true).open(log_path).unwrap(); let mut log_file = OpenOptions::new()
.append(true)
.create(true)
.open(log_file_path)
.unwrap();
if let Err(e) = writeln!( if let Err(e) = writeln!(
log_file, log_file,