summaryrefslogtreecommitdiff
path: root/src/editor.rs
blob: 3207fb983fdee5126faa80482f75d44dc434fcdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
use anyhow::{Context, Result, ensure};
use std::{io::Write, process::Command};

pub fn open(lines: &[String]) -> Result<()> {
    let editor = std::env::var("EDITOR").unwrap_or_else(|_| "vi".into());
    let args = shell_words::split(&editor).context("Invalid quoting in EDITOR")?;
    ensure!(
        !args.is_empty(),
        "EDITOR is empty; configure an editor command"
    );
    let mut snapshot = tempfile::Builder::new()
        .prefix("roentgen-logs-")
        .suffix(".txt")
        .tempfile()?;
    for line in lines {
        writeln!(snapshot, "{}", crate::model::display_text(line))?;
    }
    snapshot.flush()?;
    let status = Command::new(&args[0])
        .args(&args[1..])
        .arg(snapshot.path())
        .status()
        .context("Could not launch EDITOR")?;
    ensure!(status.success(), "Editor exited unsuccessfully");
    Ok(())
}