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 std::ffi::OsStr;
use std::fs::read_dir;
use std::io;
use std::path::PathBuf;
use std::process::Command;
mod move_files;
use move_files::move_files;
@@ -27,8 +27,6 @@ enum Commands {
copy: bool,
#[arg(short, long, default_value_t = false)]
new: bool,
#[arg(short, long, default_value_t = false)]
update_mpd: bool,
},
Show {
files: Vec<PathBuf>,
@@ -46,12 +44,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.map(str::to_string);
match &args.command {
Commands::Import {
files,
copy,
new,
update_mpd,
} => {
Commands::Import { files, copy, new } => {
for file in files {
if file.is_dir() {
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))
{
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);
if let Err(e) = move_files(
path.clone(),
tag?,
&extra_files,
*copy,
file.to_path_buf(),
true,
*new,
) {
if let Err(e) =
move_files(path.clone(), tag?, &extra_files, *copy, true, *new)
{
eprintln!("Failed to import {:?}: {}", path, e);
}
}
} else {
println!("Importing file: {:?}", file);
let tag = get_metadata(file);
move_files(
file.clone(),
tag?.clone(),
&extra_files,
*copy,
file.to_path_buf(),
false,
*new,
)?;
move_files(file.clone(), tag?.clone(), &extra_files, *copy, false, *new)?;
}
}
if *update_mpd {
Command::new("mpc").arg("update").spawn()?;
}
}
Commands::Show { files } => {
+16 -22
View File
@@ -5,7 +5,7 @@ use lofty::tag::Accessor;
use lofty::tag::Tag;
use std::env::var;
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::path::PathBuf;
@@ -14,7 +14,6 @@ pub fn move_files(
tag: Tag,
extra_files: &[String],
should_copy: bool,
folder_path: PathBuf,
has_parent: bool,
add_new_playlist: bool,
) -> Result<(), Box<dyn std::error::Error>> {
@@ -61,6 +60,10 @@ pub fn move_files(
rename(&file_path, target.join(&file_name))?;
}
if add_new_playlist {
new_playlist(target.join(file_name))?;
}
for ext in extra_files {
let pattern = file_path.parent().unwrap().join(format!("*.{}", ext));
@@ -86,17 +89,7 @@ pub fn move_files(
remove_dir(file_path.parent().unwrap())?;
}
if add_new_playlist {
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)?;
write_log(file_path)?;
Ok(())
}
@@ -119,19 +112,20 @@ fn new_playlist(path: PathBuf) -> std::result::Result<(), std::io::Error> {
writeln!(new_playlist, "{}", path.display())
}
fn write_log(
file_path: PathBuf,
log_path: PathBuf,
data_dir: PathBuf,
) -> Result<(), Box<dyn std::error::Error>> {
fn write_log(file_path: PathBuf) -> Result<(), Box<dyn std::error::Error>> {
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("music-sort.log");
if !data_dir.exists() {
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!(
log_file,