Skip to content

Commit

Permalink
Replace open_browser body with opener crate to match Cargo
Browse files Browse the repository at this point in the history
  • Loading branch information
LPGhatguy committed May 2, 2019
1 parent a0bf3c9 commit 279c8f6
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 73 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ git-testament = "0.1.4"
lazy_static = "1"
libc = "0.2"
markdown = "0.2"
opener = { version = "0.3.2", path = "../opener/opener" }
# Used by `curl` or `reqwest` backend although it isn't imported
# by our rustup.
openssl = { version = "0.10", optional = true }
Expand Down
1 change: 1 addition & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ error_chain! {
foreign_links {
Temp(temp::Error);
Io(io::Error);
Open(opener::OpenError);
}

errors {
Expand Down
68 changes: 0 additions & 68 deletions src/utils/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,74 +353,6 @@ pub fn find_cmd<'a>(cmds: &[&'a str]) -> Option<&'a str> {
cmds.iter().cloned().find(|&s| has_cmd(s))
}

pub fn open_browser(path: &Path) -> io::Result<bool> {
#[cfg(not(windows))]
fn inner(path: &Path) -> io::Result<bool> {
use std::process::Stdio;

let env_browser = env::var_os("BROWSER").map(|b| env::split_paths(&b).collect::<Vec<_>>());
let env_commands: Vec<&str> = env_browser
.as_ref()
.map(|cmds| cmds.iter().by_ref().filter_map(|b| b.to_str()).collect())
.unwrap_or_default();

let commands = [
"xdg-open",
"open",
"firefox",
"chromium",
"sensible-browser",
];
if let Some(cmd) = find_cmd(&env_commands).or_else(|| find_cmd(&commands)) {
Command::new(cmd)
.arg(path)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.map(|_| true)
} else {
Ok(false)
}
}
#[cfg(windows)]
fn inner(path: &Path) -> io::Result<bool> {
use std::ptr;
use winapi::ctypes;
use winapi::shared::minwindef::HINSTANCE;
use winapi::shared::ntdef::LPCWSTR;
use winapi::shared::windef::HWND;

// FIXME: When winapi has this function, use their version
extern "system" {
pub fn ShellExecuteW(
hwnd: HWND,
lpOperation: LPCWSTR,
lpFile: LPCWSTR,
lpParameters: LPCWSTR,
lpDirectory: LPCWSTR,
nShowCmd: ctypes::c_int,
) -> HINSTANCE;
}
const SW_SHOW: ctypes::c_int = 5;

let path = windows::to_u16s(path)?;
let operation = windows::to_u16s("open")?;
let result = unsafe {
ShellExecuteW(
ptr::null_mut(),
operation.as_ptr(),
path.as_ptr(),
ptr::null(),
ptr::null(),
SW_SHOW,
)
};
Ok(result as usize > 32)
}
inner(path)
}

#[cfg(windows)]
pub mod windows {
use std::ffi::OsStr;
Expand Down
7 changes: 2 additions & 5 deletions src/utils/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,8 @@ pub fn read_dir(name: &'static str, path: &Path) -> Result<fs::ReadDir> {
}

pub fn open_browser(path: &Path) -> Result<()> {
match raw::open_browser(path) {
Ok(true) => Ok(()),
Ok(false) => Err("no browser installed".into()),
Err(e) => Err(e).chain_err(|| "could not open browser"),
}
opener::open(path)
.chain_err(|| "couldn't open browser")
}

pub fn set_permissions(path: &Path, perms: fs::Permissions) -> Result<()> {
Expand Down

0 comments on commit 279c8f6

Please sign in to comment.