diff --git a/src/editor.rs b/src/editor.rs index a5ed247..3d3acf3 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -2,14 +2,18 @@ use std::env; use std::fs::read_to_string; use std::process::Command; use tempfile::NamedTempFile; -use std::io; +use std::io::{self, Write}; + + +const DEFAULT_EDITOR: &str = "nano"; + pub fn open_to_edit() -> io::Result { // Create a temporary file let temp_file = NamedTempFile::new()?; // Get the user's preferred editor from the $EDITOR environment variable // Default is 'nano' - let editor = env::var("EDITOR").unwrap_or_else(|_| "nano".to_string()); + let editor = env::var("EDITOR").unwrap_or_else(|_| DEFAULT_EDITOR.to_string()); // Get the path of the temp file let file_path = temp_file.path().to_str().unwrap().to_string(); @@ -17,13 +21,29 @@ pub fn open_to_edit() -> io::Result { // Open the file in the external editor Command::new(editor) .arg(&file_path) - .status() - .expect("Failed to open editor"); + .status()?; // Read the file content after editing let edited_content = read_to_string(&file_path)?; // Print the edited content Ok(edited_content) +} +pub fn open_to_show(content: &String) -> io::Result<()> { + let mut temp_file = NamedTempFile::new()?; + + // Write the content to the file + write!(temp_file, "{}", content)?; + + // Default is 'nano' + let editor = env::var("EDITOR").unwrap_or_else(|_| DEFAULT_EDITOR.to_string()); + + let file_path = temp_file.path().to_str().unwrap().to_string(); + + // Open the file in the external editor + Command::new(editor) + .arg(&file_path) + .status()?; + Ok(()) } diff --git a/src/main.rs b/src/main.rs index a5ba8f1..cc9ddc5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -140,8 +140,7 @@ fn show(id: &String) -> io::Result<()> { } let item = st.get(id); - println!("content:"); - println!("{}", item.content); + editor::open_to_show(&item.content); Ok(()) }