capitalize first letter of session type

wayland -> Wayland, x11 -> X11
This commit is contained in:
raf 2024-08-14 00:20:03 +03:00
parent 6cad15c330
commit b792d41fcc
Signed by: NotAShelf
GPG key ID: AF26552424E53993
2 changed files with 30 additions and 12 deletions

View file

@ -1,17 +1,35 @@
pub fn get_desktop_info() -> String {
fn capitalize_first_letter(s: &str) -> String {
if s.is_empty() {
return String::new();
}
let mut chars = s.chars();
let first_char = chars.next().unwrap().to_uppercase().to_string();
let rest: String = chars.collect();
first_char + &rest
}
// Retrieve the environment variables and handle Result types
let desktop_env = std::env::var("XDG_CURRENT_DESKTOP");
let display_backend = std::env::var("XDG_SESSION_TYPE");
let display_backend_result = std::env::var("XDG_SESSION_TYPE");
// Capitalize the first letter of the display backend value
let display_backend = capitalize_first_letter(display_backend_result.as_deref().unwrap_or(""));
// Trim "none+" from the start of desktop_env if present
// XXX: This is a workaround for NixOS modules that set XDG_CURRENT_DESKTOP to "none+foo"
// instead of just "foo"
// Use "Unknown" if desktop_env or display_backend is empty
let desktop_env = match desktop_env.as_ref() {
Err(_) => "Unknown",
Ok(s) => s.trim_start_matches("none+"),
// Use "Unknown" if desktop_env is empty or has an error
let desktop_env = match desktop_env {
Err(_) => "Unknown".to_string(),
Ok(s) => s.trim_start_matches("none+").to_string(),
};
let display_backend = display_backend.unwrap_or(String::from("Unknown"));
// Handle the case where display_backend might be empty after capitalization
let display_backend = if display_backend.is_empty() {
"Unknown".to_string()
} else {
display_backend
};
format!("{desktop_env} ({display_backend})")
}

View file

@ -22,7 +22,7 @@ fn main() -> Result<(), Report> {
kernel_version: get_system_info()?,
shell: get_shell(),
uptime: get_current()?,
window_manager: get_desktop_info(),
desktop: get_desktop_info(),
memory_usage: get_memory_usage(sysinfo()?),
storage: get_root_disk_usage()?,
colors: print_dots(),
@ -43,7 +43,7 @@ struct Fields {
kernel_version: String,
shell: String,
uptime: String,
window_manager: String,
desktop: String,
memory_usage: String,
storage: String,
colors: String,
@ -56,7 +56,7 @@ fn print_system_info(fields: &Fields) {
kernel_version,
shell,
uptime,
window_manager,
desktop,
memory_usage,
storage,
colors,
@ -69,7 +69,7 @@ fn print_system_info(fields: &Fields) {
{CYAN} {BLUE} {CYAN} {CYAN} {BLUE}Kernel{RESET} {kernel_version}
{BLUE} {BLUE}{CYAN} {CYAN} {BLUE}Shell{RESET} {shell}
{BLUE} {CYAN} {CYAN} {BLUE}Uptime{RESET} {uptime}
{BLUE} {CYAN} {CYAN} {CYAN} {BLUE}WM{RESET} {window_manager}
{BLUE} {CYAN} {CYAN} {CYAN} {BLUE}Desktop{RESET} {desktop}
{BLUE} {CYAN}{BLUE} {CYAN}󰍛 {BLUE}Memory{RESET} {memory_usage}
{BLUE} {CYAN}{BLUE} {CYAN}󱥎 {BLUE}Storage (/){RESET} {storage}
{CYAN} {BLUE} {CYAN} {BLUE}Colors{RESET} {colors}");