Skip to content

Commit 165b933

Browse files
committed
Avoid blocking
1 parent 5c1e99a commit 165b933

File tree

3 files changed

+20
-30
lines changed

3 files changed

+20
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1212
system `xdg-open`, if available.
1313
### Changed
1414
- On Linux (non-WSL), the system `xdg-open` is now used if present. Otherwise, the bundled version is used, as before.
15+
- Avoid blocking the thread on Linux and WSL.
1516
- The command name in the `OpenError::ExitStatus` variant is now returned as a `Cow<'static, str>` instead of a
1617
`&'static str`.
1718
### Removed

opener/src/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ use std::borrow::Cow;
4444
use std::error::Error;
4545
use std::ffi::{OsStr, OsString};
4646
use std::fmt::{self, Display, Formatter};
47-
use std::io::Read;
48-
use std::process::{Child, Command, ExitStatus, Stdio};
47+
use std::process::{Command, ExitStatus, Stdio};
4948
use std::{env, io};
5049

5150
/// Opens a file or link with the system default program.
@@ -95,15 +94,15 @@ where
9594
}
9695
};
9796

98-
let mut browser_child = Command::new(&browser_var)
97+
Command::new(&browser_var)
9998
.arg(path)
10099
.stdin(Stdio::null())
101100
.stdout(Stdio::null())
102101
.stderr(Stdio::piped())
103102
.spawn()
104103
.map_err(OpenError::Io)?;
105104

106-
wait_child(&mut browser_child, browser_var.into())
105+
Ok(())
107106
} else {
108107
sys::open(path)
109108
}
@@ -204,7 +203,13 @@ fn wsl_to_windows_path(_path: &OsStr) -> Option<OsString> {
204203
unreachable!()
205204
}
206205

207-
fn wait_child(child: &mut Child, cmd_name: Cow<'static, str>) -> Result<(), OpenError> {
206+
#[cfg(not(target_os = "windows"))]
207+
fn wait_child(
208+
child: &mut std::process::Child,
209+
cmd_name: Cow<'static, str>,
210+
) -> Result<(), OpenError> {
211+
use std::io::Read;
212+
208213
let exit_status = child.wait().map_err(OpenError::Io)?;
209214
if exit_status.success() {
210215
Ok(())

opener/src/linux_and_more.rs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,17 @@ fn wsl_open(path: &OsStr) -> Result<(), OpenError> {
2020
return crate::wait_child(&mut child, "wslview".into());
2121
}
2222

23-
let mut system_xdg_open = open_with_system_xdg_open(path).map_err(OpenError::Io)?;
24-
crate::wait_child(&mut system_xdg_open, "xdg-open (system)".into())
23+
open_with_system_xdg_open(path).map_err(OpenError::Io)?;
24+
25+
Ok(())
2526
}
2627

2728
fn non_wsl_open(path: &OsStr) -> Result<(), OpenError> {
28-
let system_xdg_open = open_with_system_xdg_open(path);
29-
30-
let system_xdg_open_used;
31-
let mut xdg_open;
32-
match system_xdg_open {
33-
Ok(child) => {
34-
system_xdg_open_used = true;
35-
xdg_open = child;
36-
}
37-
Err(_) => {
38-
system_xdg_open_used = false;
39-
xdg_open = open_with_internal_xdg_open(path)?;
40-
}
41-
};
42-
43-
let cmd_name = if system_xdg_open_used {
44-
"xdg-open (system)"
45-
} else {
46-
"xdg-open (internal)"
47-
};
29+
if open_with_system_xdg_open(path).is_err() {
30+
open_with_internal_xdg_open(path)?;
31+
}
4832

49-
crate::wait_child(&mut xdg_open, cmd_name.into())
33+
Ok(())
5034
}
5135

5236
fn open_with_wslview(path: &OsStr) -> io::Result<Child> {
@@ -70,7 +54,7 @@ fn open_with_system_xdg_open(path: &OsStr) -> io::Result<Child> {
7054
.arg(path)
7155
.stdin(Stdio::null())
7256
.stdout(Stdio::null())
73-
.stderr(Stdio::piped())
57+
.stderr(Stdio::null())
7458
.spawn()
7559
}
7660

@@ -80,7 +64,7 @@ fn open_with_internal_xdg_open(path: &OsStr) -> Result<Child, OpenError> {
8064
.arg(path)
8165
.stdin(Stdio::piped())
8266
.stdout(Stdio::null())
83-
.stderr(Stdio::piped())
67+
.stderr(Stdio::null())
8468
.spawn()
8569
.map_err(OpenError::Io)?;
8670

0 commit comments

Comments
 (0)