feat: handle multiple files in edit (WIP)

This commit is contained in:
2026-09-04 19:11:21 +02:00
parent e1e56f7e27
commit b0f8486209
2 changed files with 52 additions and 26 deletions
+38 -12
View File
@@ -1,23 +1,45 @@
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
)?;
let mut tags: Vec<Tag> = 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,
@@ -26,6 +48,8 @@ pub fn edit(file_path: PathBuf, mut tag: Tag) -> std::io::Result<()> {
item.value().text().unwrap()
)?;
}
writeln!(file.try_clone()?)?; // writes empty line
}
Command::new(editor)
.arg(&tmp_file)
@@ -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 {
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));
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())
tag.save_to_path(&file_path, WriteOptions::default())
.expect("ERROR: Failed to write tag");
}
Ok(())
}
+4 -4
View File
@@ -78,16 +78,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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<P: AsRef<OsStr>>(file_path: P) -> lofty::tag::Tag {
pub fn get_metadata<P: AsRef<OsStr>>(file_path: P) -> lofty::tag::Tag {
let tagged_file = Probe::open(file_path.as_ref())
.expect("ERROR: Bad path provided!")
.read()