You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
829 B
30 lines
829 B
3 months ago
|
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)
|
||
|
|
||
|
}
|