use std::io;
use std::path::Path;
use std::process::{Command, ExitStatus};
pub fn launch_git_tool(program: &str, dir: &Path) -> io::Result<ExitStatus> {
Command::new(program).current_dir(dir).status()
}
pub fn open_in_editor(editor: &str, file: &Path) -> io::Result<ExitStatus> {
let mut command = Command::new(editor);
command.arg(file);
if let Some(parent) = file.parent() {
command.current_dir(parent);
}
command.status()
}
pub fn open_default_app(path: &Path) -> io::Result<ExitStatus> {
if cfg!(target_os = "windows") {
return Command::new("cmd")
.args(["/C", "start", ""])
.arg(path)
.status();
}
let program = if cfg!(target_os = "macos") {
"open"
} else {
"xdg-open"
};
Command::new(program).arg(path).status()
}
pub fn open_url(url: &str) -> io::Result<ExitStatus> {
if cfg!(target_os = "windows") {
return Command::new("cmd").args(["/C", "start", "", url]).status();
}
let program = if cfg!(target_os = "macos") {
"open"
} else {
"xdg-open"
};
Command::new(program).arg(url).status()
}
pub fn resolve_editor(configured: Option<&str>) -> String {
if let Some(editor) = configured
&& !editor.trim().is_empty()
{
return editor.to_string();
}
for var in ["VISUAL", "EDITOR"] {
if let Ok(value) = std::env::var(var)
&& !value.trim().is_empty()
{
return value;
}
}
"vi".to_string()
}