feat: handle multiple files in edit (WIP)
This commit is contained in:
+48
-22
@@ -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<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,
|
||||
"{} = {}",
|
||||
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(())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user