#[derive(Clone, PartialEq, Eq, Debug)]
pub struct EditorLaunch {
pub argv: Vec<String>,
pub inline: bool,
}
pub fn editor_launch(file: &str, env: impl Fn(&str) -> Option<String>) -> EditorLaunch {
let editor = resolve_editor(&env);
if env("ZELLIJ").is_some() {
EditorLaunch {
argv: vec![
"zellij".into(),
"action".into(),
"new-pane".into(),
"--close-on-exit".into(),
"--".into(),
editor,
file.to_string(),
],
inline: false,
}
} else if env("TMUX").is_some() {
EditorLaunch {
argv: vec![
"tmux".into(),
"split-window".into(),
editor,
file.to_string(),
],
inline: false,
}
} else if env("KITTY_WINDOW_ID").is_some() {
EditorLaunch {
argv: vec![
"kitty".into(),
"@".into(),
"launch".into(),
"--type=window".into(),
editor,
file.to_string(),
],
inline: false,
}
} else {
EditorLaunch {
argv: vec![editor, file.to_string()],
inline: true,
}
}
}
fn resolve_editor(env: &impl Fn(&str) -> Option<String>) -> String {
env("VISUAL")
.or_else(|| env("EDITOR"))
.unwrap_or_else(|| "vi".to_string())
}
pub fn inline_editor(file: &str, env: impl Fn(&str) -> Option<String>) -> Vec<String> {
vec![resolve_editor(&env), file.to_string()]
}
#[cfg(test)]
#[path = "open_tests.rs"]
mod tests;