Skip to content

Commit

Permalink
Make desktop_env() return an Option
Browse files Browse the repository at this point in the history
  • Loading branch information
AldaronLau committed Nov 19, 2024
1 parent 396957b commit e55a2af
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 36 deletions.
13 changes: 11 additions & 2 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,20 @@ pub fn distro() -> Result<String> {
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<DesktopEnv> {
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)
}

Expand Down
2 changes: 1 addition & 1 deletion src/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub(crate) trait Target: Sized {
/// Return the OS distribution's name.
fn distro(self) -> Result<String>;
/// Return the desktop environment.
fn desktop_env(self) -> DesktopEnv;
fn desktop_env(self) -> Option<DesktopEnv>;
/// Return the target platform.
fn platform(self) -> Platform;
/// Return the computer's CPU architecture.
Expand Down
4 changes: 2 additions & 2 deletions src/os/daku.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DesktopEnv> {
None
}

#[inline(always)]
Expand Down
4 changes: 2 additions & 2 deletions src/os/redox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ impl Target for Os {
}

#[inline(always)]
fn desktop_env(self) -> DesktopEnv {
DesktopEnv::Orbital
fn desktop_env(self) -> Option<DesktopEnv> {
Some(DesktopEnv::Orbital)
}

#[inline(always)]
Expand Down
4 changes: 2 additions & 2 deletions src/os/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DesktopEnv> {
None
}

#[inline(always)]
Expand Down
19 changes: 2 additions & 17 deletions src/os/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,11 +589,10 @@ impl Target for Os {
}
}

fn desktop_env(self) -> DesktopEnv {
fn desktop_env(self) -> Option<DesktopEnv> {
#[cfg(target_os = "macos")]
let env = "Aqua";

// FIXME: use `let else`
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
Expand All @@ -603,21 +602,7 @@ 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") {
DesktopEnv::Aqua
Expand Down
9 changes: 3 additions & 6 deletions src/os/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DesktopEnv> {
env::var_os("DESKTOP_SESSION")
.map(|env| DesktopEnv::Unknown(env.to_string_lossy().to_string()))
}

#[inline(always)]
Expand Down
4 changes: 2 additions & 2 deletions src/os/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ impl Target for Os {
}

#[inline(always)]
fn desktop_env(self) -> DesktopEnv {
DesktopEnv::WebBrowser
fn desktop_env(self) -> Option<DesktopEnv> {
Some(DesktopEnv::WebBrowser)
}

fn platform(self) -> Platform {
Expand Down
4 changes: 2 additions & 2 deletions src/os/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,8 @@ impl Target for Os {
}

#[inline(always)]
fn desktop_env(self) -> DesktopEnv {
DesktopEnv::Windows
fn desktop_env(self) -> Option<DesktopEnv> {
Some(DesktopEnv::Windows)
}

#[inline(always)]
Expand Down

0 comments on commit e55a2af

Please sign in to comment.