From dc98a89e3e4d84aec461556ef919bba507667cce Mon Sep 17 00:00:00 2001 From: Lutetium-Vanadium Date: Thu, 1 Sep 2022 17:54:30 +0530 Subject: [PATCH] Use winsplit to split editor commands on windows --- Cargo.toml | 7 ++++++- src/question/editor.rs | 23 +++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3afec92..d2c6a77 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,10 +25,15 @@ ui = { package = "requestty-ui", path = "./requestty-ui", version = "=0.4.1" } macro = { package = "requestty-macro", path = "./requestty-macro", optional = true, version = "=0.4.1" } tempfile = "3" -shell-words = "1" smallvec = { version = "1.8", optional = true } +[target.'cfg(unix)'.dependencies] +shell-words = "1.1" + +[target.'cfg(windows)'.dependencies] +winsplit = "0.1" + [dev-dependencies] trybuild = { version = "1.0.42", features = ["diff"] } # remove color printing since it messes with the snapshot's colours diff --git a/src/question/editor.rs b/src/question/editor.rs index 6d8cd7c..77dd426 100644 --- a/src/question/editor.rs +++ b/src/question/editor.rs @@ -33,20 +33,31 @@ impl<'a> Default for Editor<'a> { } } +#[cfg(unix)] +fn split(command: &str) -> Option> { + shell_words::split(command).ok() +} +#[cfg(windows)] +fn split(command: &str) -> Option> { + Some(winsplit::split(command)) +} + fn get_editor() -> Command { - let editor_command = env::var_os("VISUAL") + let editor_args = env::var_os("VISUAL") .or_else(|| env::var_os("EDITOR")) + .map(|editor_command| editor_command.to_str().map(split)) + .flatten() + .flatten() .unwrap_or_else(|| { if cfg!(windows) { - "notepad".into() + vec!["notepad".into()] } else { - "vim".into() + vec!["vim".into()] } }); - let parts = shell_words::split(editor_command.to_str().unwrap()).unwrap(); - let mut command = Command::new(&parts[0]); - command.args(&parts[1..]); + let mut command = Command::new(&editor_args[0]); + command.args(&editor_args[1..]); command }