Coin de Gamma
3 months ago
4 changed files with 49 additions and 9 deletions
@ -0,0 +1,29 @@
|
||||
use std::env; |
||||
use std::fs::read_to_string; |
||||
use std::process::Command; |
||||
use tempfile::NamedTempFile; |
||||
use std::io; |
||||
|
||||
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()); |
||||
|
||||
// Get the path of the temp file
|
||||
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() |
||||
.expect("Failed to open editor"); |
||||
|
||||
// Read the file content after editing
|
||||
let edited_content = read_to_string(&file_path)?; |
||||
|
||||
// Print the edited content
|
||||
Ok(edited_content) |
||||
|
||||
} |
Loading…
Reference in new issue