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 0eccf6f
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 50 deletions.
4 changes: 3 additions & 1 deletion examples/whoami-demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ fn main() {
);
println!(
"Device's Desktop Env. whoami::desktop_env(): {}",
whoami::desktop_env(),
whoami::desktop_env()
.map(|e| e.to_string())
.unwrap_or_else(|| "<unknown>".to_string()),
);
println!(
"Device's CPU Arch whoami::arch(): {}",
Expand Down
16 changes: 13 additions & 3 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ffi::OsString;
use std::{env, ffi::OsString};

use crate::{
conversions,
Expand Down Expand Up @@ -104,11 +104,21 @@ 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
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
//! );
//! println!(
//! "Device's Desktop Env. whoami::desktop_env(): {}",
//! whoami::desktop_env(),
//! whoami::desktop_env()
//! .map(|e| e.to_string())
//! .unwrap_or_else(|| "<unknown>".to_string()),
//! );
//! println!(
//! "Device's CPU Arch whoami::arch(): {}",
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
45 changes: 17 additions & 28 deletions src/os/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
target_os = "hurd",
))]
use std::env;
#[cfg(target_os = "macos")]
use std::{
ffi::OsStr,
os::{
raw::{c_long, c_uchar},
unix::ffi::OsStrExt,
},
ptr::null_mut,
};
use std::{
ffi::{c_void, CStr, OsString},
fs,
Expand All @@ -19,14 +28,6 @@ use std::{
},
slice,
};
#[cfg(target_os = "macos")]
use std::{
os::{
raw::{c_long, c_uchar},
unix::ffi::OsStrExt,
},
ptr::null_mut,
};

use crate::{
os::{Os, Target},
Expand Down Expand Up @@ -589,11 +590,10 @@ impl Target for Os {
}
}

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

// FIXME: use `let else`
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
Expand All @@ -603,23 +603,12 @@ 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")?;

if env.eq_ignore_ascii_case("AQUA") {
// convert `OsStr` to `Cow`
let env = env.to_string_lossy();

Some(if env.eq_ignore_ascii_case("AQUA") {
DesktopEnv::Aqua
} else if env.eq_ignore_ascii_case("GNOME") {
DesktopEnv::Gnome
Expand All @@ -636,7 +625,7 @@ impl Target for Os {
// TODO: Other Linux Desktop Environments
} else {
DesktopEnv::Unknown(env.to_string())
}
})
}

#[inline(always)]
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 0eccf6f

Please sign in to comment.