microfetch/src/desktop.rs
2025-12-12 22:49:20 +01:00

47 lines
1.3 KiB
Rust

use std::ffi::CStr;
#[inline]
#[cold]
const fn unknown() -> &'static str { "Unknown" }
#[must_use]
#[cfg_attr(feature = "hotpath", hotpath::measure)]
pub fn get_desktop_info() -> String {
// Retrieve the environment variables and handle Result types
let desktop_str = unsafe {
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_else(|_| unknown());
s.strip_prefix("none+").unwrap_or(s)
}
};
let backend_str = unsafe {
let ptr = libc::getenv(c"XDG_SESSION_TYPE".as_ptr());
if ptr.is_null() {
unknown()
} else {
let s = CStr::from_ptr(ptr).to_str().unwrap_or_else(|_| unknown());
if s.is_empty() { unknown() } else { s }
}
};
// Pre-calculate capacity: desktop_len + " (" + backend_len + ")"
// Capitalize first char needs temporary allocation only if backend exists
let mut result =
String::with_capacity(desktop_str.len() + backend_str.len() + 3);
result.push_str(desktop_str);
result.push(' ');
result.push('(');
// Capitalize first character of backend
if let Some(first_char) = backend_str.chars().next() {
result.push(first_char.to_ascii_uppercase());
result.push_str(&backend_str[first_char.len_utf8()..]);
}
result.push(')');
result
}