use configparser::ini::Ini; use core::fmt; use crate::enums::*; use untildify; #[derive(Debug, Clone)] pub struct Monitor { pub ratio: MonitorType, pub index: i64, } impl Monitor { pub fn new(monitor_type: MonitorType, index: i64) -> Monitor { Monitor { ratio: monitor_type, index: index, } } } impl fmt::Display for Monitor { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self) } } #[derive(Debug, Clone)] pub struct Monitors { pub monitors: Vec, } impl Monitors { fn add(&mut self, monitor: Monitor) { self.monitors.push(monitor); } } impl fmt::Display for Monitors { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "[")?; for v in &self.monitors { write!(f, "{}, ", v)?; } write!(f, "]") } } #[derive(Debug, Clone)] pub struct WallpaperDir { ultrawide: String, horizontal: String, vertical: String, } impl WallpaperDir { pub fn get(&self, monitor_type: MonitorType) -> String { match monitor_type { MonitorType::Ultrawide => self.ultrawide.to_string(), MonitorType::Horizontal => self.horizontal.to_string(), MonitorType::Vertical => self.vertical.to_string(), MonitorType::Error => "error".to_string(), } } } impl fmt::Display for WallpaperDir { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!( f, "ultrawide: {ultrawide}, horizontal {horizontal}, vertical {vertical}", ultrawide = self.ultrawide, horizontal = self.horizontal, vertical = self.vertical ) } } #[derive(Debug, Clone)] pub struct Config { pub x_server: String, pub wallpaper_engine: WallpaperHandler, pub wallpaper_dir: WallpaperDir, pub monitors: Monitors, } impl Config { pub fn from_config(file_path: String) -> Config { let mut config = Ini::new(); let map = config.load(file_path).unwrap(); // Get the wallpaper engine and parse it let wallpaper_engine = WallpaperHandler::new(config.get("general", "wallpaper_engine").unwrap()); // // Get the wallpaper dirs and parse them let ultrawide_dir = untildify::untildify(config.get("general", "ultra_wall_dir").unwrap().as_ref()); let horizontal_dir = untildify::untildify(config.get("general", "hor_wall_dir").unwrap().as_ref()); let vert_wall_dir = untildify::untildify(config.get("general", "vert_wall_dir").unwrap().as_ref()); let wallpaper_dir = WallpaperDir { ultrawide: ultrawide_dir, horizontal: horizontal_dir, vertical: vert_wall_dir, }; // // Get the Monitors and parse them let mut monitors = Monitors { monitors: Vec::new(), }; for mon in map.get("monitors").iter() { for key in mon.keys() { let monitor = mon.get(key).unwrap().as_ref().unwrap(); // Add monitor type to Monitors vector monitors.add(Monitor::new( MonitorType::new(monitor.to_string()), key.parse::() .expect("Failed to parse int in motitor config"), )); } } // Config { x_server: config.get("general", "x_server").unwrap(), wallpaper_engine, wallpaper_dir, monitors, } } } impl fmt::Display for Config { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Mycelium Config: X Server: {xserv}, Wallpaper Engine: {wallpaper_engine}, Wallpaper Directories: {wallpaper_dirs}, Monitors: {monitors}", xserv = self.x_server, wallpaper_engine = self.wallpaper_engine, wallpaper_dirs = self.wallpaper_dir, monitors = self.monitors) } }