Skip to content

Commit

Permalink
Small optimizations and stylistic improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mandx authored and Byron committed Jul 11, 2018
1 parent dd9dde6 commit 88ddb6f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
32 changes: 19 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,41 @@
//! }
//! # }
//! ```
use std::ffi::OsStr;
use std::io;
use std::process::{Command, ExitStatus};
use std::ffi::OsStr;

#[cfg(not(any(target_os = "windows", target_os = "macos")))]
pub fn that<T: AsRef<OsStr> + Sized>(path: T) -> io::Result<ExitStatus> {
let mut last_err: io::Result<ExitStatus> = Err(io::Error::from_raw_os_error(0));
let path_ref = path.as_ref();
let mut last_err: io::Error = io::Error::from_raw_os_error(0);
for program in &["xdg-open", "gnome-open", "kde-open"] {
match Command::new(program).arg(path.as_ref()).spawn() {
match Command::new(program).arg(path_ref).spawn() {
Ok(mut child) => return child.wait(),
Err(err) => {
last_err = Err(err);
last_err = err;
continue;
}
}
}
last_err
Err(last_err)
}

#[cfg(target_os = "windows")]
pub fn that<T: AsRef<OsStr> + Sized>(path: T) -> io::Result<ExitStatus> {
let mut cmd = Command::new("cmd");
cmd.arg("/C").arg("start").arg("");
if let Some(s) = path.as_ref().to_str() {
cmd.arg(s.replace("&", "^&"));
} else {
cmd.arg(path.as_ref());
}
cmd.spawn()?.wait()
Command::new("cmd")
.arg("/C")
.arg("start")
.arg("")
.arg({
let path_ref = path.as_ref();
match path_ref.to_str() {
Some(s) => s.replace("&", "^&"),
None => path_ref,
}
})
.spawn()?
.wait()
}

#[cfg(target_os = "macos")]
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate open;

use std::io::{stderr, Write};
use std::env;
use std::io::{stderr, Write};
use std::process;

fn main() {
Expand Down

0 comments on commit 88ddb6f

Please sign in to comment.