feat: add function to parse editor output in edit

This commit is contained in:
2026-09-03 15:18:53 +02:00
parent f8a51ccd48
commit 0b586ecef0
2 changed files with 20 additions and 14 deletions
+18 -12
View File
@@ -1,31 +1,37 @@
use lofty::tag::Tag;
use std::collections::HashMap;
use std::env::var;
use std::io::Write;
use std::path::PathBuf;
use std::{fs::File, io::Read, process::Command};
pub fn edit(file_path: PathBuf, tag: Tag) -> std::io::Result<()> {
pub fn edit(file_path: PathBuf, mut tag: Tag) -> std::io::Result<()> {
let tmp_file = PathBuf::from("/tmp/keks-sort-metadata");
let editor = var("EDITOR").unwrap();
let mut file = File::create(&file_path)?;
let mut file = File::create(&tmp_file)?;
for item in tag.items() {
writeln!(
file,
"{:?} = {:?}",
item.key(),
item.value().text().unwrap_or("")
)?;
writeln!(file, "{:?} = {}", item.key(), item.value().text().unwrap())?;
}
Command::new(editor)
.arg(&file_path)
.arg(&tmp_file)
.status()
.expect("Something went wrong");
let mut editable = String::new();
let _ = File::open(file_path)
File::open(&tmp_file)
.expect("Could not open file")
.read_to_string(&mut editable);
.read_to_string(&mut editable)?;
println!("File content:\n{}", editable);
Ok(())
}
fn parse(input: &str) -> HashMap<String, String> {
input
.lines()
.filter_map(|line| {
let (key, value) = line.split_once('=')?;
Some((key.trim().to_string(), value.trim().to_string()))
})
.collect()
}
+2 -2
View File
@@ -72,9 +72,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
}
Commands::Edit { file } => {
let path = PathBuf::from("/tmp/keks-sort-metadata");
let tag = get_metadata(file);
edit(path, tag)?;
edit(file.to_path_buf(), tag)?;
}
}
@@ -91,5 +90,6 @@ fn get_metadata<P: AsRef<OsStr>>(file_path: P) -> lofty::tag::Tag {
Some(primary_tag) => primary_tag,
None => tagged_file.first_tag().expect("ERROR: No tags found!"),
};
tag.clone()
}