|
|
@ -2,14 +2,18 @@ use std::env; |
|
|
|
use std::fs::read_to_string; |
|
|
|
use std::fs::read_to_string; |
|
|
|
use std::process::Command; |
|
|
|
use std::process::Command; |
|
|
|
use tempfile::NamedTempFile; |
|
|
|
use tempfile::NamedTempFile; |
|
|
|
use std::io; |
|
|
|
use std::io::{self, Write}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const DEFAULT_EDITOR: &str = "nano"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn open_to_edit() -> io::Result<String> { |
|
|
|
pub fn open_to_edit() -> io::Result<String> { |
|
|
|
// Create a temporary file
|
|
|
|
// Create a temporary file
|
|
|
|
let temp_file = NamedTempFile::new()?; |
|
|
|
let temp_file = NamedTempFile::new()?; |
|
|
|
// Get the user's preferred editor from the $EDITOR environment variable
|
|
|
|
// Get the user's preferred editor from the $EDITOR environment variable
|
|
|
|
// Default is 'nano'
|
|
|
|
// 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
|
|
|
|
// Get the path of the temp file
|
|
|
|
let file_path = temp_file.path().to_str().unwrap().to_string(); |
|
|
|
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
|
|
|
|
// Open the file in the external editor
|
|
|
|
Command::new(editor) |
|
|
|
Command::new(editor) |
|
|
|
.arg(&file_path) |
|
|
|
.arg(&file_path) |
|
|
|
.status() |
|
|
|
.status()?; |
|
|
|
.expect("Failed to open editor"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Read the file content after editing
|
|
|
|
// Read the file content after editing
|
|
|
|
let edited_content = read_to_string(&file_path)?; |
|
|
|
let edited_content = read_to_string(&file_path)?; |
|
|
|
|
|
|
|
|
|
|
|
// Print the edited content
|
|
|
|
// Print the edited content
|
|
|
|
Ok(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(()) |
|
|
|
} |
|
|
|
} |
|
|
|