Browse Source

basic show

fix-typo
Coin de Gamma 3 months ago
parent
commit
02ab45c9c7
  1. 28
      src/editor.rs
  2. 3
      src/main.rs

28
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<String> {
// 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<String> {
// 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(())
}

3
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(())
}

Loading…
Cancel
Save