From 70d547cbb117c1cebe74c7578a3ba7f5b3e0a43a Mon Sep 17 00:00:00 2001 From: Henrik Friedrichsen Date: Sat, 30 Mar 2019 10:42:41 +0100 Subject: [PATCH 1/2] add support for the *BSD family --- src/lib.rs | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 49eb569..d763c17 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,7 @@ //! //! * 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, gvfs-open and +//! * linux or *bsd => 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 @@ -235,13 +235,18 @@ fn open_browser_internal(browser: Browser, url: &str) -> Result { } } -/// Deal with opening of browsers on Linux - currently supports only the default browser +/// Deal with opening of browsers on Linux and *BSD - 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 -#[cfg(target_os = "linux")] +#[cfg(any( + target_os = "linux", + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd" +))] #[inline] fn open_browser_internal(browser: Browser, url: &str) -> Result { match browser { @@ -255,9 +260,12 @@ fn open_browser_internal(browser: Browser, url: &str) -> Result { )), } } - -/// Open on Linux using the $BROWSER env var -#[cfg(target_os = "linux")] +#[cfg(any( + target_os = "linux", + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd" +))] fn open_on_linux_using_browser_env(url: &str) -> Result { let browsers = ::std::env::var("BROWSER") .map_err(|_| -> Error { Error::new(ErrorKind::NotFound, "BROWSER env not set") })?; @@ -290,8 +298,15 @@ fn open_on_linux_using_browser_env(url: &str) -> Result { )) } -#[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))] -compile_error!("Only Windows, Mac OS and Linux are currently supported"); +#[cfg(not(any( + target_os = "windows", + target_os = "macos", + target_os = "linux", + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd" +)))] +compile_error!("Only Windows, Mac OS, Linux and *BSD are currently supported"); #[test] fn test_open_default() { From b9a95cdc5415ccb38c8b067fa4cbf51f88dca2d2 Mon Sep 17 00:00:00 2001 From: Henrik Friedrichsen Date: Sun, 31 Mar 2019 14:40:24 +0200 Subject: [PATCH 2/2] rename previously linux-only function to include BSDs --- README.md | 2 +- src/lib.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b6e8458..cb1fe48 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,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, gvfs-open, gnome-open, whichever works first) +* linux/*bsd => 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 diff --git a/src/lib.rs b/src/lib.rs index d763c17..23d7f79 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -250,7 +250,7 @@ fn open_browser_internal(browser: Browser, url: &str) -> Result { #[inline] fn open_browser_internal(browser: Browser, url: &str) -> Result { match browser { - Browser::Default => open_on_linux_using_browser_env(url) + Browser::Default => open_on_unix_using_browser_env(url) .or_else(|_| -> Result { Command::new("xdg-open").arg(url).output() }) .or_else(|_| -> Result { Command::new("gvfs-open").arg(url).output() }) .or_else(|_| -> Result { Command::new("gnome-open").arg(url).output() }), @@ -266,7 +266,7 @@ fn open_browser_internal(browser: Browser, url: &str) -> Result { target_os = "netbsd", target_os = "openbsd" ))] -fn open_on_linux_using_browser_env(url: &str) -> Result { +fn open_on_unix_using_browser_env(url: &str) -> Result { let browsers = ::std::env::var("BROWSER") .map_err(|_| -> Error { Error::new(ErrorKind::NotFound, "BROWSER env not set") })?; for browser in browsers.split(':') {