From 43acd75b9196621af0c11ee5a9436ace0e60d1cf Mon Sep 17 00:00:00 2001 From: altsem Date: Mon, 3 Jun 2024 18:08:30 +0200 Subject: [PATCH] fix: identify editor via `--version` instead of inspecting cmd path e.g. run `nano --version` and inspect the output closes: #186 --- src/ops/show.rs | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/src/ops/show.rs b/src/ops/show.rs index 0d3d41c0ab..3c1d82e849 100644 --- a/src/ops/show.rs +++ b/src/ops/show.rs @@ -66,16 +66,25 @@ fn editor(file: &Path, maybe_line: Option) -> Option { fn parse_editor_command(editor: &str, file: &str, maybe_line: Option) -> Command { let args = &editor.split_whitespace().collect::>(); + + let version = String::from_utf8( + Command::new(args[0]) + .arg("--version") + .output() + .expect("failed to get version via '--version'") + .stdout, + ) + .unwrap(); + + log::info!("editor --version: {:?}", &version); + let mut cmd = Command::new(args[0]); cmd.args(&args[1..]); - let lower = args[0].to_lowercase(); - if let Some(line) = maybe_line { - if lower.ends_with("vi") - || lower.ends_with("vim") - || lower.ends_with("nvim") - || lower.ends_with("nano") + if version.trim_start().starts_with("VIM") + || version.trim_start().starts_with("NVIM") + || version.trim_start().starts_with("GNU nano") { cmd.args([&format!("+{}", line), file]); } else { @@ -86,18 +95,3 @@ fn parse_editor_command(editor: &str, file: &str, maybe_line: Option) -> Co } cmd } - -#[cfg(test)] -mod tests { - use std::ffi::OsStr; - - #[test] - fn parse_editor_command_test() { - let cmd = super::parse_editor_command("/bin/nAnO -f", "README.md", Some(42)); - assert_eq!(cmd.get_program(), OsStr::new("/bin/nAnO")); - assert_eq!( - &cmd.get_args().collect::>(), - &["-f", "+42", "README.md"] - ); - } -}