Skip to content

Commit 5f1f80f

Browse files
committed
fix: now works within WSL if gio is installed. (#71)
`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.
2 parents c5af111 + b150494 commit 5f1f80f

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ name = "open"
1919

2020
[target.'cfg(all(unix, not(macos)))'.dependencies]
2121
pathdiff = "0.2.0"
22+
23+
[dependencies]
24+
is-wsl = "0.4.0"

src/unix.rs

+22-14
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,28 @@ use std::{
77

88
pub fn commands<T: AsRef<OsStr>>(path: T) -> Vec<Command> {
99
let path = path.as_ref();
10-
[
11-
("xdg-open", &[path] as &[_]),
12-
("gio", &[OsStr::new("open"), path]),
13-
("gnome-open", &[path]),
14-
("kde-open", &[path]),
15-
("wslview", &[&wsl_path(path)]),
16-
]
17-
.iter()
18-
.map(|(command, args)| {
19-
let mut cmd = Command::new(command);
20-
cmd.args(*args);
21-
cmd
22-
})
23-
.collect()
10+
let mut commands: Vec<(&str, Vec<&OsStr>)> = vec![];
11+
12+
let wsl_path = wsl_path(path);
13+
if is_wsl::is_wsl() {
14+
commands.push(("wslview", vec![&wsl_path]));
15+
}
16+
17+
commands.extend_from_slice(&[
18+
("xdg-open", vec![&path]),
19+
("gio", vec![OsStr::new("open"), path]),
20+
("gnome-open", vec![path]),
21+
("kde-open", vec![path]),
22+
]);
23+
24+
commands
25+
.iter()
26+
.map(|(command, args)| {
27+
let mut cmd = Command::new(command);
28+
cmd.args(args);
29+
cmd
30+
})
31+
.collect()
2432
}
2533

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

0 commit comments

Comments
 (0)