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

Handle opening browser with opener crate #5888

Merged
merged 1 commit into from
Aug 15, 2018
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ libc = "0.2"
log = "0.4"
libgit2-sys = "0.7.5"
num_cpus = "1.0"
opener = "0.2.0"
rustfix = "0.4.2"
same-file = "1"
semver = { version = "0.9.0", features = ["serde"] }
Expand Down
1 change: 1 addition & 0 deletions src/cargo/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ extern crate libgit2_sys;
#[macro_use]
extern crate log;
extern crate num_cpus;
extern crate opener;
extern crate rustfix;
extern crate same_file;
extern crate semver;
Expand Down
53 changes: 7 additions & 46 deletions src/cargo/ops/cargo_doc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::process::Command;

use failure::Fail;
use opener;

use core::Workspace;
use ops;
Expand Down Expand Up @@ -97,55 +99,14 @@ pub fn doc(ws: &Workspace, options: &DocOptions) -> CargoResult<()> {
if fs::metadata(&path).is_ok() {
let mut shell = options.compile_opts.config.shell();
shell.status("Opening", path.display())?;
match open_docs(&path) {
Ok(m) => shell.status("Launching", m)?,
Err(e) => {
shell.warn("warning: could not determine a browser to open docs with, tried:")?;
for method in e {
shell.warn(format!("\t{}", method))?;
}
if let Err(e) = opener::open(&path) {
shell.warn(format!("Couldn't open docs: {}", e))?;
for cause in (&e as &Fail).iter_chain() {
shell.warn(format!("Caused by:\n {}", cause))?;
}
}
}
}

Ok(())
}

#[cfg(not(any(target_os = "windows", target_os = "macos")))]
fn open_docs(path: &Path) -> Result<&'static str, Vec<&'static str>> {
use std::env;
let mut methods = Vec::new();
// trying $BROWSER
if let Ok(name) = env::var("BROWSER") {
match Command::new(name).arg(path).status() {
Ok(_) => return Ok("$BROWSER"),
Err(_) => methods.push("$BROWSER"),
}
}

for m in ["xdg-open", "gnome-open", "kde-open"].iter() {
match Command::new(m).arg(path).status() {
Ok(_) => return Ok(m),
Err(_) => methods.push(m),
}
}

Err(methods)
}

#[cfg(target_os = "windows")]
fn open_docs(path: &Path) -> Result<&'static str, Vec<&'static str>> {
match Command::new("cmd").arg("/C").arg(path).status() {
Ok(_) => Ok("cmd /C"),
Err(_) => Err(vec!["cmd /C"]),
}
}

#[cfg(target_os = "macos")]
fn open_docs(path: &Path) -> Result<&'static str, Vec<&'static str>> {
match Command::new("open").arg(path).status() {
Ok(_) => Ok("open"),
Err(_) => Err(vec!["open"]),
}
}