Skip to content

Commit

Permalink
fix: identify editor via --version instead of inspecting cmd path
Browse files Browse the repository at this point in the history
e.g. run `nano --version` and inspect the output

closes: #186
  • Loading branch information
altsem committed Jun 3, 2024
1 parent 5510a16 commit 43acd75
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions src/ops/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,25 @@ fn editor(file: &Path, maybe_line: Option<u32>) -> Option<Action> {

fn parse_editor_command(editor: &str, file: &str, maybe_line: Option<u32>) -> Command {
let args = &editor.split_whitespace().collect::<Vec<_>>();

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);

Check warning on line 79 in src/ops/show.rs

View check run for this annotation

Codecov / codecov/patch

src/ops/show.rs#L69-L79

Added lines #L69 - L79 were not covered by tests

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")

Check warning on line 87 in src/ops/show.rs

View check run for this annotation

Codecov / codecov/patch

src/ops/show.rs#L85-L87

Added lines #L85 - L87 were not covered by tests
{
cmd.args([&format!("+{}", line), file]);
} else {
Expand All @@ -86,18 +95,3 @@ fn parse_editor_command(editor: &str, file: &str, maybe_line: Option<u32>) -> 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::<Vec<_>>(),
&["-f", "+42", "README.md"]
);
}
}

0 comments on commit 43acd75

Please sign in to comment.