diff --git a/Cargo.lock b/Cargo.lock index d41422df44d..d5afabf500b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -847,6 +847,13 @@ name = "opaque-debug" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "opener" +version = "0.3.2" +dependencies = [ + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "openssl" version = "0.10.20" @@ -1238,6 +1245,7 @@ dependencies = [ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)", "markdown 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "opener 0.3.2", "openssl 0.10.20 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.1.6 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 0e7ac5df0a1..680db021eeb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/src/errors.rs b/src/errors.rs index 1155b1fc977..f6eccebc71c 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -25,6 +25,7 @@ error_chain! { foreign_links { Temp(temp::Error); Io(io::Error); + Open(opener::OpenError); } errors { diff --git a/src/utils/raw.rs b/src/utils/raw.rs index dde27897be3..7af3881906b 100644 --- a/src/utils/raw.rs +++ b/src/utils/raw.rs @@ -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 { - #[cfg(not(windows))] - fn inner(path: &Path) -> io::Result { - use std::process::Stdio; - - let env_browser = env::var_os("BROWSER").map(|b| env::split_paths(&b).collect::>()); - 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 { - 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; diff --git a/src/utils/utils.rs b/src/utils/utils.rs index e7edf5e48d5..59d1f327d40 100644 --- a/src/utils/utils.rs +++ b/src/utils/utils.rs @@ -355,11 +355,8 @@ pub fn read_dir(name: &'static str, path: &Path) -> Result { } 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<()> {