mirror of
https://github.com/NotAShelf/microfetch.git
synced 2026-04-12 12:57:41 +00:00
desktop: replace libc::getenv with env::var_os
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: Ie94268934435b70bfbcd81e4676600206a6a6964
This commit is contained in:
parent
90ba703506
commit
72de00771f
1 changed files with 14 additions and 19 deletions
|
|
@ -1,27 +1,22 @@
|
||||||
use std::{ffi::CStr, fmt::Write};
|
use std::{ffi::OsStr, fmt::Write};
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
#[cfg_attr(feature = "hotpath", hotpath::measure)]
|
#[cfg_attr(feature = "hotpath", hotpath::measure)]
|
||||||
pub fn get_desktop_info() -> String {
|
pub fn get_desktop_info() -> String {
|
||||||
// Retrieve the environment variables and handle Result types
|
let desktop_os = std::env::var_os("XDG_CURRENT_DESKTOP");
|
||||||
let desktop_str = unsafe {
|
let session_os = std::env::var_os("XDG_SESSION_TYPE");
|
||||||
let ptr = libc::getenv(c"XDG_CURRENT_DESKTOP".as_ptr());
|
|
||||||
if ptr.is_null() {
|
|
||||||
"Unknown"
|
|
||||||
} else {
|
|
||||||
let s = CStr::from_ptr(ptr).to_str().unwrap_or("Unknown");
|
|
||||||
s.strip_prefix("none+").unwrap_or(s)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let backend_str = unsafe {
|
let desktop_raw = desktop_os
|
||||||
let ptr = libc::getenv(c"XDG_SESSION_TYPE".as_ptr());
|
.as_deref()
|
||||||
if ptr.is_null() {
|
.and_then(OsStr::to_str)
|
||||||
"Unknown"
|
.unwrap_or("Unknown");
|
||||||
} else {
|
let desktop_str = desktop_raw.strip_prefix("none+").unwrap_or(desktop_raw);
|
||||||
let s = CStr::from_ptr(ptr).to_str().unwrap_or("Unknown");
|
|
||||||
if s.is_empty() { "Unknown" } else { s }
|
let session_raw = session_os.as_deref().and_then(OsStr::to_str).unwrap_or("");
|
||||||
}
|
let backend_str = if session_raw.is_empty() {
|
||||||
|
"Unknown"
|
||||||
|
} else {
|
||||||
|
session_raw
|
||||||
};
|
};
|
||||||
|
|
||||||
// Pre-calculate capacity: desktop_len + " (" + backend_len + ")"
|
// Pre-calculate capacity: desktop_len + " (" + backend_len + ")"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue