Use winsplit to split editor commands on windows

This commit is contained in:
Lutetium-Vanadium 2022-09-01 17:54:30 +05:30
parent 4228c51459
commit dc98a89e3e
2 changed files with 23 additions and 7 deletions

View File

@ -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

View File

@ -33,20 +33,31 @@ impl<'a> Default for Editor<'a> {
}
}
#[cfg(unix)]
fn split(command: &str) -> Option<Vec<String>> {
shell_words::split(command).ok()
}
#[cfg(windows)]
fn split(command: &str) -> Option<Vec<String>> {
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
}