use anyhow::{Context, Result};
use std::env;
use std::path::Path;
use std::process::Command;
extern crate open;
pub fn open(p: &Path) -> Result<()> {
open::that(p).context("cannot open")?;
Ok(())
}
pub fn edit(p: &Path) -> Result<()> {
let editor = if let Ok(ed) = env::var("VISUAL") {
ed
} else if let Ok(ed) = env::var("EDITOR") {
ed
} else if cfg!(target_os = "macos") {
"vim".into()
} else if cfg!(target_os = "windows") {
"notepad".into()
} else {
"open".into()
};
if editor == "open" {
open(p)?;
} else {
Command::new(&editor)
.arg(&p)
.status()
.with_context(|| format!("Cannot find editor {:?}: ", p))?;
}
Ok(())
}