From b0f8486209e05d65c5c399a5587b45180efe156c Mon Sep 17 00:00:00 2001 From: KeksNino Date: Fri, 4 Sep 2026 19:11:21 +0200 Subject: [PATCH] feat: handle multiple files in edit (WIP) --- src/edit.rs | 70 ++++++++++++++++++++++++++++++++++++----------------- src/main.rs | 8 +++--- 2 files changed, 52 insertions(+), 26 deletions(-) diff --git a/src/edit.rs b/src/edit.rs index a33573a..fb86712 100644 --- a/src/edit.rs +++ b/src/edit.rs @@ -1,30 +1,54 @@ use lofty::config::WriteOptions; -use lofty::tag::TagItem; use lofty::tag::TagType::VorbisComments; +use lofty::tag::{Accessor, TagItem}; use lofty::tag::{ItemKey, ItemValue}; use lofty::tag::{Tag, TagExt}; use std::collections::HashMap; use std::env::var; use std::io::Write; use std::path::PathBuf; -use std::{fs::File, io::Read, process::Command}; +use std::{fs::File, fs::read_dir, io::Read, process::Command}; -pub fn edit(file_path: PathBuf, mut tag: Tag) -> std::io::Result<()> { +pub fn edit(file_path: PathBuf, extra_files: [String; 8]) -> std::io::Result<()> { let tmp_file = PathBuf::from("/tmp/keks-sort-metadata"); let editor = var("EDITOR").unwrap(); let mut file = File::create(&tmp_file)?; - writeln!( - file.try_clone()?, - "------------ {:?} ------------", - file_path - )?; - for item in tag.items() { - writeln!( - file, - "{} = {}", - item.key().map_key(VorbisComments).unwrap(), - item.value().text().unwrap() - )?; + let mut tags: Vec = Vec::new(); + let mut path: PathBuf = file_path.clone(); + + if file_path.is_dir() { + for music_file in read_dir(&file_path)? { + path = music_file?.path(); + println!("{:?}", path); + + if path + .extension() + .and_then(|ext| ext.to_str()) + .is_some_and(|ext| extra_files.iter().any(|x| x == ext)) + { + continue; + } + + let tag = crate::get_metadata(&path); + tags.push(tag); + } + } else { + let tag = &crate::get_metadata(&file_path); + tags.push(tag.clone()); + } + + for tag in &tags { + println!("{:?}", tag.title()); + writeln!(file.try_clone()?, "------------ {:?} ------------", path)?; + for item in tag.items() { + writeln!( + file, + "{} = {}", + item.key().map_key(VorbisComments).unwrap(), + item.value().text().unwrap() + )?; + } + writeln!(file.try_clone()?)?; // writes empty line } Command::new(editor) @@ -38,14 +62,16 @@ pub fn edit(file_path: PathBuf, mut tag: Tag) -> std::io::Result<()> { .read_to_string(&mut editable)?; let parsed = parse(&editable); - for (key, value) in parsed { - let item_key = - ItemKey::from_key(VorbisComments, &key).expect("ERROR: Failed to get item key"); - let item = TagItem::new(item_key, ItemValue::Text(value)); - tag.insert(item.clone()); + for mut tag in tags { + for (key, value) in &parsed { + let item_key = + ItemKey::from_key(VorbisComments, key).expect("ERROR: Failed to get item key"); + let item = TagItem::new(item_key, ItemValue::Text(value.to_string())); + tag.insert(item.clone()); + } + tag.save_to_path(&file_path, WriteOptions::default()) + .expect("ERROR: Failed to write tag"); } - tag.save_to_path(file_path, WriteOptions::default()) - .expect("ERROR: Failed to write tag"); Ok(()) } diff --git a/src/main.rs b/src/main.rs index b556559..bbb32fb 100755 --- a/src/main.rs +++ b/src/main.rs @@ -78,16 +78,16 @@ fn main() -> Result<(), Box> { println!("{:?}", item) } } - Commands::Edit { file } => { - let tag = get_metadata(file); - edit(file.to_path_buf(), tag)?; + + Commands::Edit { files } => { + edit(files.to_path_buf(), extra_files)?; } } Ok(()) } -fn get_metadata>(file_path: P) -> lofty::tag::Tag { +pub fn get_metadata>(file_path: P) -> lofty::tag::Tag { let tagged_file = Probe::open(file_path.as_ref()) .expect("ERROR: Bad path provided!") .read()