use crate::error::SwitcherError;
use std::path::{Path, PathBuf};
use std::process::Command;
pub fn open_in_zed(path: &Path) -> Result<(), SwitcherError> {
let binary = locate_zed_binary().ok_or_else(|| {
SwitcherError::LaunchFailed(
"could not find a `zed` binary on PATH or in standard locations".into(),
)
})?;
let status = Command::new(&binary)
.arg(path)
.status()
.map_err(|e| SwitcherError::LaunchFailed(format!("{}: {e}", binary.display())))?;
if !status.success() {
return Err(SwitcherError::LaunchFailed(format!(
"`{}` exited with status {status}",
binary.display()
)));
}
Ok(())
}
fn locate_zed_binary() -> Option<PathBuf> {
let candidates = ["zed", "zed.exe"];
if let Some(path_var) = std::env::var_os("PATH") {
for dir in std::env::split_paths(&path_var) {
for name in &candidates {
let p = dir.join(name);
if p.is_file() {
return Some(p);
}
}
}
}
#[cfg(target_os = "macos")]
{
let bundled = PathBuf::from("/Applications/Zed.app/Contents/MacOS/cli");
if bundled.is_file() {
return Some(bundled);
}
}
None
}