diff --git a/src/api.rs b/src/api.rs index eeed4d8..dd05072 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,4 +1,4 @@ -use std::ffi::OsString; +use std::{env, ffi::OsString}; use crate::{ conversions, @@ -104,11 +104,21 @@ pub fn distro() -> Result { Target::distro(Os) } -/// Get the desktop environment. +/// Get the desktop environment (if any). /// /// Example: "gnome" or "windows" +/// +/// Returns `None` if a desktop environment is not available (for example in a +/// TTY or over SSH) #[inline(always)] -pub fn desktop_env() -> DesktopEnv { +pub fn desktop_env() -> Option { + if env::var_os("SSH_CLIENT").is_some() + || env::var_os("SSH_TTY").is_some() + || env::var_os("SSH_CONNECTION").is_some() + { + return None; + } + Target::desktop_env(Os) } diff --git a/src/os.rs b/src/os.rs index af3695d..bec975e 100644 --- a/src/os.rs +++ b/src/os.rs @@ -72,7 +72,7 @@ pub(crate) trait Target: Sized { /// Return the OS distribution's name. fn distro(self) -> Result; /// Return the desktop environment. - fn desktop_env(self) -> DesktopEnv; + fn desktop_env(self) -> Option; /// Return the target platform. fn platform(self) -> Platform; /// Return the computer's CPU architecture. diff --git a/src/os/daku.rs b/src/os/daku.rs index 826b0ba..3e93560 100644 --- a/src/os/daku.rs +++ b/src/os/daku.rs @@ -42,8 +42,8 @@ impl Target for Os { } #[inline(always)] - fn desktop_env(self) -> DesktopEnv { - DesktopEnv::Unknown("Unknown Daku".to_string()) + fn desktop_env(self) -> Option { + None } #[inline(always)] diff --git a/src/os/redox.rs b/src/os/redox.rs index 2e7994a..0d92901 100644 --- a/src/os/redox.rs +++ b/src/os/redox.rs @@ -130,8 +130,8 @@ impl Target for Os { } #[inline(always)] - fn desktop_env(self) -> DesktopEnv { - DesktopEnv::Orbital + fn desktop_env(self) -> Option { + Some(DesktopEnv::Orbital) } #[inline(always)] diff --git a/src/os/target.rs b/src/os/target.rs index 7822257..8ac1f12 100644 --- a/src/os/target.rs +++ b/src/os/target.rs @@ -44,8 +44,8 @@ impl Target for Os { } #[inline(always)] - fn desktop_env(self) -> DesktopEnv { - DesktopEnv::Unknown("Unknown".to_string()) + fn desktop_env(self) -> Option { + None } #[inline(always)] diff --git a/src/os/unix.rs b/src/os/unix.rs index 5755bdf..c37341c 100644 --- a/src/os/unix.rs +++ b/src/os/unix.rs @@ -589,11 +589,10 @@ impl Target for Os { } } - fn desktop_env(self) -> DesktopEnv { + fn desktop_env(self) -> Option { #[cfg(target_os = "macos")] let env = "Aqua"; - // FIXME: use `let else` #[cfg(any( target_os = "linux", target_os = "dragonfly", @@ -603,23 +602,9 @@ impl Target for Os { target_os = "illumos", target_os = "hurd", ))] - let env = env::var_os("DESKTOP_SESSION"); - #[cfg(any( - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd", - target_os = "illumos", - target_os = "hurd", - ))] - let env = if let Some(ref env) = env { - env.to_string_lossy() - } else { - return DesktopEnv::Unknown("Unknown".to_string()); - }; + let env = env::var_os("DESKTOP_SESSION")?.to_string_lossy(); - if env.eq_ignore_ascii_case("AQUA") { + Some(if env.eq_ignore_ascii_case("AQUA") { DesktopEnv::Aqua } else if env.eq_ignore_ascii_case("GNOME") { DesktopEnv::Gnome @@ -636,7 +621,7 @@ impl Target for Os { // TODO: Other Linux Desktop Environments } else { DesktopEnv::Unknown(env.to_string()) - } + }) } #[inline(always)] diff --git a/src/os/wasi.rs b/src/os/wasi.rs index a26f108..6c65d58 100644 --- a/src/os/wasi.rs +++ b/src/os/wasi.rs @@ -45,12 +45,9 @@ impl Target for Os { } #[inline(always)] - fn desktop_env(self) -> DesktopEnv { - if let Some(ref env) = env::var_os("DESKTOP_SESSION") { - DesktopEnv::Unknown(env.to_string_lossy().to_string()) - } else { - DesktopEnv::Unknown("Unknown WASI".to_string()) - } + fn desktop_env(self) -> Option { + env::var_os("DESKTOP_SESSION") + .map(|env| DesktopEnv::Unknown(env.to_string_lossy().to_string())) } #[inline(always)] diff --git a/src/os/web.rs b/src/os/web.rs index d92d430..57f7ac0 100644 --- a/src/os/web.rs +++ b/src/os/web.rs @@ -160,8 +160,8 @@ impl Target for Os { } #[inline(always)] - fn desktop_env(self) -> DesktopEnv { - DesktopEnv::WebBrowser + fn desktop_env(self) -> Option { + Some(DesktopEnv::WebBrowser) } fn platform(self) -> Platform { diff --git a/src/os/windows.rs b/src/os/windows.rs index 1412a4b..8c25237 100644 --- a/src/os/windows.rs +++ b/src/os/windows.rs @@ -385,8 +385,8 @@ impl Target for Os { } #[inline(always)] - fn desktop_env(self) -> DesktopEnv { - DesktopEnv::Windows + fn desktop_env(self) -> Option { + Some(DesktopEnv::Windows) } #[inline(always)]