Skip to content

Commit

Permalink
Honour the right syntax for $BROWSER. Closes #3. Also, include gvfs-o…
Browse files Browse the repository at this point in the history
…pen and gnome-open for #2
  • Loading branch information
amodm committed Jan 23, 2017
1 parent 8bb27c5 commit f663a7d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "webbrowser"
description = "Open URLs in web browsers available on a platform"
version = "0.2.1"
version = "0.2.2"
authors = ["Amod Malviya @amodm"]
documentation = "http://code.rootnet.in/webbrowser-rs/webbrowser"
homepage = "https://github.com/amodm/webbrowser-rs"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Currently state of platform support is:

* macos => default, as well as browsers listed under [Browser](enum.Browser.html)
* windows => default browser only
* linux => default browser only (uses $BROWSER env var, failing back to xdg-open if it is not set)
* linux => default browser only (uses $BROWSER env var, failing back to xdg-open, gvfs-open, gnome-open, whichever works first)
* android => not supported right now
* ios => not supported right now

Expand Down
42 changes: 37 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
//!
//! * macos => default, as well as browsers listed under [Browser](enum.Browser.html)
//! * windows => default browser only
//! * linux => default browser only (uses $BROWSER env var, failing back to xdg-open)
//! * linux => default browser only (uses $BROWSER env var, failing back to xdg-open, gvfs-open and
//! gnome-open, in that order)
//! * android => not supported right now
//! * ios => not supported right now
//!
Expand Down Expand Up @@ -110,19 +111,50 @@ fn open_on_windows(browser: Browser, url: &str) -> Result<Output> {
}
}

/// Deal with opening of browsers on Linux, using `xdg-open` command
/// Deal with opening of browsers on Linux - currently supports only the default browser
///
/// The mechanism of opening the default browser is as follows:
/// 1. Attempt to use $BROWSER env var if available
/// 2. Attempt to open the url via xdg-open, gvfs-open, gnome-open, respectively, whichever works
/// first
fn open_on_linux(browser: Browser, url: &str) -> Result<Output> {
match browser {
Browser::Default => Command::new(env::var("BROWSER").or::<Result<String>>(Ok("xdg-open".to_string())).unwrap())
.arg(url)
.output(),
Browser::Default => open_on_linux_using_browser_env(url)
.or_else(|_| -> Result<Output> {Command::new("xdg-open").arg(url).output()})
.or_else(|_| -> Result<Output> {Command::new("gvfs-open").arg(url).output()})
.or_else(|_| -> Result<Output> {Command::new("gnome-open").arg(url).output()}),
_ => Err(Error::new(
ErrorKind::NotFound,
"Only the default browser is supported on this platform right now"
))
}
}

/// Open on Linux using the $BROWSER env var
fn open_on_linux_using_browser_env(url: &str) -> Result<Output> {
let browsers = try!(env::var("BROWSER").map_err(|_| -> Error {Error::new(ErrorKind::NotFound, format!("BROWSER env not set"))}));
for browser in browsers.split(':') { // $BROWSER can contain ':' delimited options, each representing a potential browser command line
if !browser.is_empty() {
// each browser command can have %s to represent URL, while %c needs to be replaced
// with ':' and %% with '%'
let cmdline = browser.replace("%s", url).replace("%c", ":").replace("%%", "%");
let cmdarr: Vec<&str> = cmdline.split_whitespace().collect();
let mut cmd = Command::new(&cmdarr[0]);
if cmdarr.len() > 1 {
cmd.args(&cmdarr[1..cmdarr.len()]);
}
if !browser.contains("%s") {
// append the url as an argument only if it was not already set via %s
cmd.arg(url);
}
if let Ok(output) = cmd.output() {
return Ok(output);
}
}
}
return Err(Error::new(ErrorKind::NotFound, "No valid command in $BROWSER"));
}

#[test]
fn test_open_default() {
assert!(open("http://github.com").is_ok());
Expand Down

0 comments on commit f663a7d

Please sign in to comment.