simplemailclient 0.2.3

A simple terminal mail client (SMTP send, IMAP fetch) with a TUI.
use std::env;
use std::fs;
use std::process::Command;
use anyhow::Result;

/// Open nano (or $EDITOR) on a temp file; return the file contents after the
/// user saves and exits.  Returns None if the user saves an empty file.
pub fn compose() -> Result<Option<String>> {
    let tmp_path = env::temp_dir().join(format!("mail-compose-{}.txt", std::process::id()));

    // Pre-populate with a blank file so nano has something to open
    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() {
        // Editor exited non-zero → treat as abort
        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))
    }
}