From c5474f17010e1035f7b7d49560d51eca4b71e12f Mon Sep 17 00:00:00 2001 From: aaron-jack-manning Date: Sun, 21 Aug 2022 12:10:31 +1000 Subject: [PATCH] option to edit info only in separate file --- src/edit.rs | 57 ++++++++++++++++++++++++++++++++++++++++++----------- src/main.rs | 11 +++++++++-- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/src/edit.rs b/src/edit.rs index 6761bce..54cc1bd 100644 --- a/src/edit.rs +++ b/src/edit.rs @@ -7,7 +7,49 @@ use crate::tasks; use crate::error; use crate::tasks::Id; -pub fn edit_raw(id : Id, vault_folder : path::PathBuf) -> Result<(), error::Error> { +pub fn open_editor(path : &path::Path, editor : &str) -> Result { + let mut command = process::Command::new(editor); + + command + .args(vec![&path]); + + let mut child = command.spawn()?; + + child.wait().map_err(|err| error::Error::from(err)) +} + +pub fn edit_info(id : Id, vault_folder : path::PathBuf, editor : &str) -> Result<(), error::Error> { + let mut task = tasks::Task::load(id, vault_folder.clone(), false)?; + + let temp_path = vault_folder.join("temp.md"); + + fs::write(&temp_path, &task.data.info.unwrap_or(String::new()).as_bytes())?; + + let status = open_editor(&temp_path, editor)?; + + if !status.success() { + match status.code() { + Some(code) => Err(error::Error::Generic(format!("Process responded with a non-zero status code: {}", code))), + None => Err(error::Error::Generic(String::from("Process was interrupted by signal"))), + } + } + else { + let file_contents = fs::read_to_string(&temp_path)?; + + task.data.info = if file_contents.is_empty() { + None + } + else { + Some(file_contents) + }; + + task.save()?; + + Ok(()) + } +} + +pub fn edit_raw(id : Id, vault_folder : path::PathBuf, editor : &str) -> Result<(), error::Error> { let mut task = tasks::Task::load(id, vault_folder.clone(), false)?; @@ -15,17 +57,8 @@ pub fn edit_raw(id : Id, vault_folder : path::PathBuf) -> Result<(), error::Erro fs::copy(task.path(), &temp_path)?; - // This will be a matter of configuration later on. - let mut command = process::Command::new("nvim"); - - command - .current_dir(&vault_folder) - .args(vec![&temp_path]); - - let mut child = command.spawn()?; - - let status = child.wait()?; - + let status = open_editor(&temp_path, editor)?; + if !status.success() { match status.code() { Some(code) => Err(error::Error::Generic(format!("Process responded with a non-zero status code: {}", code))), diff --git a/src/main.rs b/src/main.rs index 28e73fd..ed95e9c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,6 +41,8 @@ enum Command { }, Edit { id : Id, + #[clap(long)] + info : bool, }, /// Delete a task completely. Delete { @@ -167,8 +169,13 @@ fn program() -> Result<(), error::Error> { let task = tasks::Task::load(id, vault_folder.clone(), true)?; task.display()?; }, - Edit { id } => { - edit::edit_raw(id, vault_folder.clone())?; + Edit { id, info } => { + if info { + edit::edit_info(id, vault_folder.clone(), "nvim")?; + } + else { + edit::edit_raw(id, vault_folder.clone(), "nvim")?; + } println!("Updated task {}", colour::id(&id.to_string())); }, Discard { id } => {