use std::env;
use std::fs;
use std::process::Command;
use anyhow::Result;
pub fn compose() -> Result<Option<String>> {
let tmp_path = env::temp_dir().join(format!("mail-compose-{}.txt", std::process::id()));
fs::write(&tmp_path, "")?;
let editor = env::var("EDITOR").unwrap_or_else(|_| "nano".to_string());
let status = Command::new(&editor)
.arg(&tmp_path)
.status()?;
if !status.success() {
let _ = fs::remove_file(&tmp_path);
return Ok(None);
}
let contents = fs::read_to_string(&tmp_path)?;
let _ = fs::remove_file(&tmp_path);
if contents.trim().is_empty() {
Ok(None)
} else {
Ok(Some(contents))
}
}