From cd9ec1d9b1300f10e44a64aa97e78711359e47f7 Mon Sep 17 00:00:00 2001 From: KeksNino Date: Sun, 30 Aug 2026 21:28:11 +0200 Subject: [PATCH] feat: add new subcommand to open default editor with metadata --- src/edit.rs | 31 +++++++++++++++++++++++++++++++ src/main.rs | 11 +++++++++++ 2 files changed, 42 insertions(+) create mode 100644 src/edit.rs diff --git a/src/edit.rs b/src/edit.rs new file mode 100644 index 0000000..c21fa93 --- /dev/null +++ b/src/edit.rs @@ -0,0 +1,31 @@ +use lofty::tag::Tag; +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<()> { + let editor = var("EDITOR").unwrap(); + let mut file = File::create(&file_path)?; + for item in tag.items() { + writeln!( + file, + "{:?} = {:?}", + item.key(), + item.value().text().unwrap_or("") + )?; + } + + Command::new(editor) + .arg(&file_path) + .status() + .expect("Something went wrong"); + + let mut editable = String::new(); + let _ = File::open(file_path) + .expect("Could not open file") + .read_to_string(&mut editable); + + println!("File content:\n{}", editable); + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index 66e7678..5d08310 100755 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,9 @@ use std::path::PathBuf; mod move_files; use move_files::move_files; +mod edit; +use edit::edit; + #[derive(Parser)] #[command(version, about, long_about = None)] struct Args { @@ -25,6 +28,9 @@ enum Commands { Show { file: PathBuf, }, + Edit { + file: PathBuf, + }, } fn main() -> Result<(), Box> { @@ -61,6 +67,11 @@ fn main() -> Result<(), Box> { println!("{:?}", item) } } + Commands::Edit { file } => { + let path = PathBuf::from("/tmp/keks-sort-metadata"); + let tag = get_metadata(file); + edit(path, tag); + } } Ok(())