diff --git a/Cargo.lock b/Cargo.lock index d41422df44..af100eccb9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -847,6 +847,14 @@ name = "opaque-debug" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "opener" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "openssl" version = "0.10.20" @@ -1238,6 +1246,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.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "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)", @@ -1978,6 +1987,7 @@ dependencies = [ "checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1" "checksum num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1a23f0ed30a54abaa0c7e83b1d2d87ada7c3c23078d1d87815af3e3b6385fbba" "checksum opaque-debug 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "93f5bb2e8e8dec81642920ccff6b61f1eb94fa3020c5a325c9851ff604152409" +"checksum opener 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "998c59e83d9474c01127a96e023b7a04bb061dd286bf8bb939d31dc8d31a7448" "checksum openssl 0.10.20 (registry+https://github.com/rust-lang/crates.io-index)" = "5a0d6b781aac4ac1bd6cafe2a2f0ad8c16ae8e1dd5184822a16c50139f8838d9" "checksum openssl-probe 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de" "checksum openssl-src 111.2.1+1.1.1b (registry+https://github.com/rust-lang/crates.io-index)" = "a42e4a28c5a3da4b0df51795aee275b812164c43a690caa871bfa71bf0d52439" diff --git a/Cargo.toml b/Cargo.toml index 0e7ac5df0a..c55973e1e4 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 = "0.4.0" # 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 1155b1fc97..f6eccebc71 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 dde27897be..7af3881906 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 e7edf5e48d..bb317cf081 100644 --- a/src/utils/utils.rs +++ b/src/utils/utils.rs @@ -355,11 +355,7 @@ 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<()> {