Refactor commandline processing

This reduces the amount of code duplication, and similar flow
is used for both wsl and non-wsl code paths.
macos-click-through
Fred Sundvik 3 years ago
parent 22cb5bc05f
commit 7d19cd5ebe

@ -29,8 +29,12 @@ fn set_windows_creation_flags(cmd: &mut Command) {
cmd.creation_flags(0x0800_0000); // CREATE_NO_WINDOW cmd.creation_flags(0x0800_0000); // CREATE_NO_WINDOW
} }
#[cfg(windows)] fn build_nvim_cmd_with_args(bin: &str) -> Command {
fn platform_build_nvim_cmd(bin: &str) -> Option<Command> { let mut args = Vec::<String>::new();
args.push("--embed".to_string());
args.extend(SETTINGS.get::<CmdLineSettings>().neovim_args);
#[cfg(windows)]
if SETTINGS.get::<CmdLineSettings>().wsl { if SETTINGS.get::<CmdLineSettings>().wsl {
let mut cmd = Command::new("wsl"); let mut cmd = Command::new("wsl");
cmd.args(&[ cmd.args(&[
@ -38,62 +42,63 @@ fn platform_build_nvim_cmd(bin: &str) -> Option<Command> {
"-c", "-c",
"let \\$PATH=system(\"\\$SHELL -lic 'echo \\$PATH' 2>/dev/null\")", "let \\$PATH=system(\"\\$SHELL -lic 'echo \\$PATH' 2>/dev/null\")",
]); ]);
Some(cmd) cmd.args(args);
} else if Path::new(&bin).exists() { return cmd;
Some(Command::new(bin))
} else {
None
} }
let mut cmd = Command::new(bin);
cmd.args(args);
cmd
} }
#[cfg(unix)] fn platform_exists(bin: &str) -> bool {
fn platform_build_nvim_cmd(bin: &str) -> Option<Command> { #[cfg(windows)]
if Path::new(&bin).exists() { if SETTINGS.get::<CmdLineSettings>().wsl {
Some(Command::new(bin)) if let Ok(output) = std::process::Command::new("wsl")
.args(&["$SHELL", "-lic"])
.arg(format!("exists -x {}", bin))
.output()
{
return output.status.success();
} else { } else {
None error!("wsl exists failed");
std::process::exit(1);
} }
}
Path::new(&bin).exists()
} }
fn build_nvim_cmd() -> Command { fn platform_which(bin: &str) -> Option<String> {
if let Some(path) = SETTINGS.get::<CmdLineSettings>().neovim_bin {
if let Some(cmd) = platform_build_nvim_cmd(&path) {
return cmd;
} else {
warn!("NEOVIM_BIN is invalid falling back to first bin in PATH");
}
}
#[cfg(windows)] #[cfg(windows)]
if SETTINGS.get::<CmdLineSettings>().wsl { if SETTINGS.get::<CmdLineSettings>().wsl {
if let Ok(output) = std::process::Command::new("wsl") if let Ok(output) = std::process::Command::new("wsl")
.args(&["$SHELL", "-lic", "which nvim"]) .args(&["$SHELL", "-lic"])
.arg(format!("which {}", bin))
.output() .output()
{ {
if output.status.success() { if output.status.success() {
let path = String::from_utf8(output.stdout).unwrap(); return Some(String::from_utf8(output.stdout).unwrap());
let mut cmd = Command::new("wsl");
cmd.args(&[
path.trim(),
"-c",
"let \\$PATH=system(\"\\$SHELL -lic 'echo \\$PATH' 2>/dev/null\")",
]);
return cmd;
} else { } else {
error!("nvim not found in WSL path"); return None;
std::process::exit(1);
} }
} else {
error!("wsl which nvim failed");
std::process::exit(1);
} }
} }
if let Ok(path) = which::which("nvim") { if let Ok(path) = which::which(bin) {
if let Some(cmd) = platform_build_nvim_cmd(path.to_str().unwrap()) { path.into_os_string().into_string().ok()
cmd
} else { } else {
error!("nvim does not have proper permissions!"); None
std::process::exit(1); }
}
fn build_nvim_cmd() -> Command {
if let Some(path) = SETTINGS.get::<CmdLineSettings>().neovim_bin {
if platform_exists(&path) {
return build_nvim_cmd_with_args(&path);
} else {
warn!("NEOVIM_BIN is invalid falling back to first bin in PATH");
} }
}
if let Some(path) = platform_which("nvim") {
build_nvim_cmd_with_args(&path)
} else { } else {
error!("nvim not found!"); error!("nvim not found!");
std::process::exit(1); std::process::exit(1);
@ -123,9 +128,6 @@ pub fn build_neovide_command(channel: u64, num_args: u64, command: &str, event:
pub fn create_nvim_command() -> Command { pub fn create_nvim_command() -> Command {
let mut cmd = build_nvim_cmd(); let mut cmd = build_nvim_cmd();
cmd.arg("--embed")
.args(SETTINGS.get::<CmdLineSettings>().neovim_args.iter());
info!("Starting neovim with: {:?}", cmd); info!("Starting neovim with: {:?}", cmd);
#[cfg(not(debug_assertions))] #[cfg(not(debug_assertions))]

Loading…
Cancel
Save