use crate::ENVIRON;
use serde::{Deserialize, Serialize};
use std::{fmt, str::FromStr};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Desktop {
Gnome,
Xfce,
Hyprland,
Niri,
#[serde(other)]
Openbox,
}
impl FromStr for Desktop {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s.to_lowercase();
if s.contains("gnome") {
Ok(Desktop::Gnome)
} else if s.contains("xfce") {
Ok(Desktop::Xfce)
} else if s.contains("hyprland") {
Ok(Desktop::Hyprland)
} else if s.contains("niri") {
Ok(Desktop::Niri)
} else {
Ok(Desktop::Openbox)
}
}
}
impl fmt::Display for Desktop {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let s = match self {
Desktop::Gnome => "gnome",
Desktop::Xfce => "xfce",
Desktop::Hyprland => "hyprland",
Desktop::Niri => "niri",
Desktop::Openbox => "openbox",
};
write!(f, "{s}")
}
}
impl Desktop {
pub fn detect() -> Self {
Self::from_str(&ENVIRON.desktop).unwrap_or(Desktop::Openbox)
}
}