Skip to content

Commit f663a7d

Browse files
committed
Honour the right syntax for $BROWSER. Closes #3. Also, include gvfs-open and gnome-open for #2
1 parent 8bb27c5 commit f663a7d

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "webbrowser"
33
description = "Open URLs in web browsers available on a platform"
4-
version = "0.2.1"
4+
version = "0.2.2"
55
authors = ["Amod Malviya @amodm"]
66
documentation = "http://code.rootnet.in/webbrowser-rs/webbrowser"
77
homepage = "https://github.com/amodm/webbrowser-rs"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Currently state of platform support is:
2222

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

src/lib.rs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
//!
1717
//! * macos => default, as well as browsers listed under [Browser](enum.Browser.html)
1818
//! * windows => default browser only
19-
//! * linux => default browser only (uses $BROWSER env var, failing back to xdg-open)
19+
//! * linux => default browser only (uses $BROWSER env var, failing back to xdg-open, gvfs-open and
20+
//! gnome-open, in that order)
2021
//! * android => not supported right now
2122
//! * ios => not supported right now
2223
//!
@@ -110,19 +111,50 @@ fn open_on_windows(browser: Browser, url: &str) -> Result<Output> {
110111
}
111112
}
112113

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

133+
/// Open on Linux using the $BROWSER env var
134+
fn open_on_linux_using_browser_env(url: &str) -> Result<Output> {
135+
let browsers = try!(env::var("BROWSER").map_err(|_| -> Error {Error::new(ErrorKind::NotFound, format!("BROWSER env not set"))}));
136+
for browser in browsers.split(':') { // $BROWSER can contain ':' delimited options, each representing a potential browser command line
137+
if !browser.is_empty() {
138+
// each browser command can have %s to represent URL, while %c needs to be replaced
139+
// with ':' and %% with '%'
140+
let cmdline = browser.replace("%s", url).replace("%c", ":").replace("%%", "%");
141+
let cmdarr: Vec<&str> = cmdline.split_whitespace().collect();
142+
let mut cmd = Command::new(&cmdarr[0]);
143+
if cmdarr.len() > 1 {
144+
cmd.args(&cmdarr[1..cmdarr.len()]);
145+
}
146+
if !browser.contains("%s") {
147+
// append the url as an argument only if it was not already set via %s
148+
cmd.arg(url);
149+
}
150+
if let Ok(output) = cmd.output() {
151+
return Ok(output);
152+
}
153+
}
154+
}
155+
return Err(Error::new(ErrorKind::NotFound, "No valid command in $BROWSER"));
156+
}
157+
126158
#[test]
127159
fn test_open_default() {
128160
assert!(open("http://github.com").is_ok());

0 commit comments

Comments
 (0)