Skip to content

Commit

Permalink
Avoid blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
Seeker14491 committed Jun 12, 2021
1 parent 5c1e99a commit 165b933
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
system `xdg-open`, if available.
### Changed
- On Linux (non-WSL), the system `xdg-open` is now used if present. Otherwise, the bundled version is used, as before.
- Avoid blocking the thread on Linux and WSL.
- The command name in the `OpenError::ExitStatus` variant is now returned as a `Cow<'static, str>` instead of a
`&'static str`.
### Removed
Expand Down
15 changes: 10 additions & 5 deletions opener/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ use std::borrow::Cow;
use std::error::Error;
use std::ffi::{OsStr, OsString};
use std::fmt::{self, Display, Formatter};
use std::io::Read;
use std::process::{Child, Command, ExitStatus, Stdio};
use std::process::{Command, ExitStatus, Stdio};
use std::{env, io};

/// Opens a file or link with the system default program.
Expand Down Expand Up @@ -95,15 +94,15 @@ where
}
};

let mut browser_child = Command::new(&browser_var)
Command::new(&browser_var)
.arg(path)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::piped())
.spawn()
.map_err(OpenError::Io)?;

wait_child(&mut browser_child, browser_var.into())
Ok(())
} else {
sys::open(path)
}
Expand Down Expand Up @@ -204,7 +203,13 @@ fn wsl_to_windows_path(_path: &OsStr) -> Option<OsString> {
unreachable!()
}

fn wait_child(child: &mut Child, cmd_name: Cow<'static, str>) -> Result<(), OpenError> {
#[cfg(not(target_os = "windows"))]
fn wait_child(
child: &mut std::process::Child,
cmd_name: Cow<'static, str>,
) -> Result<(), OpenError> {
use std::io::Read;

let exit_status = child.wait().map_err(OpenError::Io)?;
if exit_status.success() {
Ok(())
Expand Down
34 changes: 9 additions & 25 deletions opener/src/linux_and_more.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,17 @@ fn wsl_open(path: &OsStr) -> Result<(), OpenError> {
return crate::wait_child(&mut child, "wslview".into());
}

let mut system_xdg_open = open_with_system_xdg_open(path).map_err(OpenError::Io)?;
crate::wait_child(&mut system_xdg_open, "xdg-open (system)".into())
open_with_system_xdg_open(path).map_err(OpenError::Io)?;

Ok(())
}

fn non_wsl_open(path: &OsStr) -> Result<(), OpenError> {
let system_xdg_open = open_with_system_xdg_open(path);

let system_xdg_open_used;
let mut xdg_open;
match system_xdg_open {
Ok(child) => {
system_xdg_open_used = true;
xdg_open = child;
}
Err(_) => {
system_xdg_open_used = false;
xdg_open = open_with_internal_xdg_open(path)?;
}
};

let cmd_name = if system_xdg_open_used {
"xdg-open (system)"
} else {
"xdg-open (internal)"
};
if open_with_system_xdg_open(path).is_err() {
open_with_internal_xdg_open(path)?;
}

crate::wait_child(&mut xdg_open, cmd_name.into())
Ok(())
}

fn open_with_wslview(path: &OsStr) -> io::Result<Child> {
Expand All @@ -70,7 +54,7 @@ fn open_with_system_xdg_open(path: &OsStr) -> io::Result<Child> {
.arg(path)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::piped())
.stderr(Stdio::null())
.spawn()
}

Expand All @@ -80,7 +64,7 @@ fn open_with_internal_xdg_open(path: &OsStr) -> Result<Child, OpenError> {
.arg(path)
.stdin(Stdio::piped())
.stdout(Stdio::null())
.stderr(Stdio::piped())
.stderr(Stdio::null())
.spawn()
.map_err(OpenError::Io)?;

Expand Down

0 comments on commit 165b933

Please sign in to comment.