use std::path::Path;
use std::process::Command;
use anyhow::{bail, Context, Result};
pub fn open(path: &Path) -> Result<()> {
let editor = std::env::var("VISUAL")
.ok()
.filter(|s| !s.trim().is_empty())
.or_else(|| {
std::env::var("EDITOR")
.ok()
.filter(|s| !s.trim().is_empty())
})
.unwrap_or_else(|| default_editor().to_string());
let mut parts = editor.split_whitespace();
let program = parts.next().expect("editor string is never empty");
let status = Command::new(program)
.args(parts)
.arg(path)
.status()
.with_context(|| format!("launching editor `{program}`"))?;
if !status.success() {
bail!("editor `{program}` exited with {status}");
}
Ok(())
}
#[cfg(windows)]
fn default_editor() -> &'static str {
"notepad"
}
#[cfg(not(windows))]
fn default_editor() -> &'static str {
"vi"
}