Skip to content

Commit

Permalink
fix: now works within WSL if gio is installed. (#71)
Browse files Browse the repository at this point in the history
`gio` would fail on WSL which is fixed by detecting that WSL is present
which is when `wslview` will be prioritized.

Note that the binary size inceases by ~13kb as we try to avoid running `wslview`
first and fail everywhere, so prefer runtime performance on linux over binary
size. The binary size changes only on unix.
  • Loading branch information
Byron committed Apr 16, 2023
2 parents c5af111 + b150494 commit 5f1f80f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ name = "open"

[target.'cfg(all(unix, not(macos)))'.dependencies]
pathdiff = "0.2.0"

[dependencies]
is-wsl = "0.4.0"
36 changes: 22 additions & 14 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,28 @@ use std::{

pub fn commands<T: AsRef<OsStr>>(path: T) -> Vec<Command> {
let path = path.as_ref();
[
("xdg-open", &[path] as &[_]),
("gio", &[OsStr::new("open"), path]),
("gnome-open", &[path]),
("kde-open", &[path]),
("wslview", &[&wsl_path(path)]),
]
.iter()
.map(|(command, args)| {
let mut cmd = Command::new(command);
cmd.args(*args);
cmd
})
.collect()
let mut commands: Vec<(&str, Vec<&OsStr>)> = vec![];

let wsl_path = wsl_path(path);
if is_wsl::is_wsl() {
commands.push(("wslview", vec![&wsl_path]));
}

commands.extend_from_slice(&[
("xdg-open", vec![&path]),
("gio", vec![OsStr::new("open"), path]),
("gnome-open", vec![path]),
("kde-open", vec![path]),
]);

commands
.iter()
.map(|(command, args)| {
let mut cmd = Command::new(command);
cmd.args(args);
cmd
})
.collect()
}

pub fn with_command<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> Command {
Expand Down

0 comments on commit 5f1f80f

Please sign in to comment.