Skip to content

Commit

Permalink
Open docs with opener crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Seeker14491 committed Aug 19, 2018
1 parent 22b6cdb commit 1aad3c8
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 79 deletions.
57 changes: 52 additions & 5 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions src/rustup-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ license = "MIT OR Apache-2.0"
[dependencies]
download = { path = "../download" }
error-chain = "0.12"
failure = "0.1.2"
libc = "0.2.0"
opener = "0.3.0"
rand = "0.5.2"
remove_dir_all = "0.5.1"
scopeguard = "0.3.0"
Expand Down
2 changes: 2 additions & 0 deletions src/rustup-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
extern crate download;
#[macro_use]
extern crate error_chain;
extern crate failure;
extern crate opener;
extern crate rand;
extern crate scopeguard;
extern crate semver;
Expand Down
69 changes: 0 additions & 69 deletions src/rustup-utils/src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,75 +356,6 @@ pub fn find_cmd<'a>(cmds: &[&'a str]) -> Option<&'a str> {
cmds.into_iter().map(|&s| s).filter(|&s| has_cmd(s)).next()
}

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

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

let commands = [
"xdg-open",
"open",
"firefox",
"chromium",
"sensible-browser",
];
if let Some(cmd) = find_cmd(&env_commands).or(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 winapi::ctypes;
use winapi::shared::windef::HWND;
use winapi::shared::ntdef::LPCWSTR;
use winapi::shared::minwindef::HINSTANCE;
use std::ptr;

// 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 winapi::um::{combaseapi, shlobj, shtypes};
Expand Down
9 changes: 4 additions & 5 deletions src/rustup-utils/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::env;
use std::sync::atomic::{AtomicBool, Ordering, ATOMIC_BOOL_INIT};
use sha2::Sha256;
use notifications::Notification;
use opener;
use raw;
#[cfg(windows)]
use winapi::shared::minwindef::DWORD;
Expand Down Expand Up @@ -347,11 +348,9 @@ 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"),
}
use failure::ResultExt;

opener::open(path).compat().chain_err(|| "could not open browser")
}

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

0 comments on commit 1aad3c8

Please sign in to comment.