Skip to content

Commit

Permalink
Auto merge of #5888 - Seeker14491:opener, r=alexcrichton
Browse files Browse the repository at this point in the history
Handle opening browser with `opener` crate

Fixes #5701

A note about behavior on Linux:
When looking for a browser to open the docs with, Cargo currently tries the `$BROWSER` environment variable, `xdg-open`, `gnome-open`, and finally `kde-open`, in that order. With this commit, this behavior changes; the `opener` crate always uses the `xdg-open` script, which is included in the `opener` crate. The `xdg-open` script takes care of finding a suitable browser.
  • Loading branch information
bors committed Aug 15, 2018
2 parents 6a7672e + 48d0708 commit 6cc8320
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 46 deletions.
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"]),
}
}

0 comments on commit 6cc8320

Please sign in to comment.