Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate browser opening logic to match Cargo #1830

Merged
merged 1 commit into from
May 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 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 = "0.4.0"
# 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
6 changes: 1 addition & 5 deletions src/utils/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,7 @@ 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