Skip to content

Commit

Permalink
[WSL] Try wslview first instead of second
Browse files Browse the repository at this point in the history
It's more likely to do the right thing vs the `xdg-open` fallback.
  • Loading branch information
Seeker14491 committed Jun 12, 2021
1 parent e16875d commit bd30011
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
4 changes: 2 additions & 2 deletions opener/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ use std::{env, io};
///
/// - On Windows the `ShellExecuteW` Windows API function is used.
/// - On Mac the system `open` command is used.
/// - On Windows Subsystem for Linux (WSL), the system `xdg-open` will be used if available,
/// otherwise the system `wslview` from [`wslu`] is used.
/// - On Windows Subsystem for Linux (WSL), the system `wslview` from [`wslu`] is used if available,
/// otherwise the system `xdg-open` is used, if available.
/// - On non-WSL Linux and other platforms,
/// the system `xdg-open` script is used if available, otherwise an `xdg-open` script embedded in
/// this library is used.
Expand Down
16 changes: 11 additions & 5 deletions opener/src/linux_and_more.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,32 @@ pub(crate) fn open(path: &OsStr) -> Result<(), OpenError> {
}

fn wsl_open(path: &OsStr) -> Result<(), OpenError> {
let system_xdg_open = Command::new("xdg-open")
let transformed_path = crate::wsl_to_windows_path(path);
let transformed_path = transformed_path.as_deref();
let path = match transformed_path {
None => path,
Some(x) => x,
};
let wslview = Command::new("wslview")
.arg(path)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::piped())
.spawn();

if let Ok(mut child) = system_xdg_open {
return crate::wait_child(&mut child, "xdg-open (system)".into());
if let Ok(mut child) = wslview {
return crate::wait_child(&mut child, "wslview".into());
}

let mut wslview = Command::new("wslview")
let mut system_xdg_open = Command::new("xdg-open")
.arg(path)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::piped())
.spawn()
.map_err(OpenError::Io)?;

crate::wait_child(&mut wslview, "wslview".into())
crate::wait_child(&mut system_xdg_open, "xdg-open (system)".into())
}

fn non_wsl_open(path: &OsStr) -> Result<(), OpenError> {
Expand Down

0 comments on commit bd30011

Please sign in to comment.